Skip to content

Commit a77c5d1

Browse files
ninthreezyclaude
andcommitted
0.8.2: leave ${{ }} text the runner does not own exactly as written
The runner renders only its own namespaces in step inputs (runner/agent, directory, machine, python, step, action, run, input/variable). Any other ${{ ... }} expression (steps.*, inputs.*, jobs.*, secrets.*, unknown names, text that does not parse) passes through untouched: never rendered, never emptied, never an error. The server resolves its namespaces before dispatch, so such text still in an input is data. A message input that quoted an older error such as ${{ steps.fetch.output.body }} used to fail the step with "'steps' is undefined" (watchdog-sense, 2026-10-29). Objects and arrays were never rendered and still pass through. Adds CHANGELOG.md. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 7d890fa commit a77c5d1

4 files changed

Lines changed: 214 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Changelog
2+
3+
Notable changes to the `datatorch` python package (CLI, SDK and the runner
4+
daemon). Versions are the tags on this repository and the releases on PyPI.
5+
6+
## 0.8.2 (2026-10-29)
7+
8+
### Added
9+
10+
- CLI: `datatorch import coco` runs the import on the server by default
11+
(upload, match images to dataset files by name, create missing labels
12+
with keypoint names and skeleton, write boxes, polygons and keypoints,
13+
then wait for the job and print its counts and warnings). `--dataset`
14+
picks the dataset when the project has more than one; the old
15+
pycocotools path stays behind `--client-side`.
16+
17+
### Fixed
18+
19+
- Runner: step inputs that merely contain `${{ ... }}` text no longer fail
20+
or get rewritten. The runner now renders only its own namespaces
21+
(`runner`/`agent`, `directory`, `machine`, `python`, `step`, `action`,
22+
`run`, `input`/`variable`); any other expression (`steps.*`, `inputs.*`,
23+
`jobs.*`, `secrets.*`, unknown names, or text that does not parse) is
24+
passed through exactly as written. Before, a `message` input
25+
that quoted an older error such as `${{ steps.fetch.output.body }}`
26+
failed the step with "'steps' is undefined" (seen on the org-root
27+
`watchdog-sense` pipeline, 2026-10-29). Objects and arrays were never
28+
rendered and still pass through untouched.
29+
30+
## 0.8.1 and earlier
31+
32+
See the git history and the GitHub releases.

‎datatorch/runner/pipelines/template.py‎

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import platform
33
import re
44
import typing
5-
from typing import Dict
6-
from jinja2 import Template
5+
import uuid
6+
from typing import Dict, List, Tuple
7+
from jinja2 import Environment, Template, meta
8+
from jinja2.exceptions import TemplateSyntaxError
79
from ..directory import runner_directory
810

911
if typing.TYPE_CHECKING:
@@ -19,6 +21,17 @@
1921
# $INPUT_<NAME> env vars instead.
2022
_INPUT_REF_IN_TEMPLATE = re.compile(r"\$\{[{%][^}]*?(?<![.\w])(?:input|variable)(?!\w)")
2123

24+
# One `${{ ... }}` expression, as written. Used to find the expressions in
25+
# a string before rendering so that the ones the runner does not own can
26+
# be kept exactly as they are (see `Variables.render`).
27+
_EXPRESSION = re.compile(r"\$\{\{.*?\}\}", re.DOTALL)
28+
29+
_environment = Environment(
30+
block_start_string="${%",
31+
variable_start_string="${{",
32+
comment_start_string="${#",
33+
)
34+
2235

2336
class InputInjectionError(Exception):
2437
"""Raised when a shell command/script interpolates ${{ input.* }} /
@@ -72,6 +85,17 @@ def _env_key(key: str) -> str:
7285
},
7386
}
7487

88+
# The namespaces the runner renders (`run` since 0.8.1: the step's place
89+
# in the record). `${{ ... }}` text rooted anywhere else (`steps.*`,
90+
# `inputs.*`, `jobs.*`, `secrets.*`, unknown names) is not a reference
91+
# the runner can resolve: the server resolves its own namespaces before
92+
# dispatch, and whatever still reads like one in a step input is DATA (a
93+
# log line, a fetched body, an older error message) that must reach the
94+
# action untouched.
95+
RUNNER_NAMESPACES = frozenset(
96+
{*global_variables.keys(), "input", "variable", "step", "action", "run"}
97+
)
98+
7599

76100
class Variables(object):
77101
"""
@@ -92,6 +116,12 @@ class Variables(object):
92116
for use inside the action's own templates (``runs.command`` etc.).
93117
``variable`` is a legacy alias of ``input``; both are scoped to the
94118
one step. The old job-wide accumulating namespace is gone.
119+
120+
Everything else is inert. A ``${{ ... }}`` whose root names are not all
121+
in ``RUNNER_NAMESPACES`` (or that does not parse) is left exactly as
122+
written, never raising and never rewritten: the runner does not author
123+
that text, so it must not interpret it. Only string inputs are rendered;
124+
objects and arrays pass through as they arrive.
95125
"""
96126

97127
def __init__(self):
@@ -140,8 +170,54 @@ def merge(self, section: str, variables: dict):
140170
self.variables[section] = {**self.variables[section], **variables}
141171

142172
def render(self, string: str):
143-
tp = self._template(string)
144-
return tp.render({**global_variables, **self.variables})
173+
"""Render the runner's own namespaces in ``string``; keep every
174+
other ``${{ ... }}`` exactly as written (see the class docstring).
175+
"""
176+
if string is None or "${" not in string:
177+
return string
178+
protected, kept = self._protect_foreign_expressions(string)
179+
try:
180+
rendered = self._template(protected).render(
181+
{**global_variables, **self.variables}
182+
)
183+
except TemplateSyntaxError:
184+
# Not a template the runner can read (an unclosed `${{`, a
185+
# stray `${%` in data): it is text, hand it back as it came.
186+
return string
187+
for token, text in kept:
188+
rendered = rendered.replace(token, text)
189+
return rendered
190+
191+
@classmethod
192+
def _protect_foreign_expressions(
193+
cls, string: str
194+
) -> Tuple[str, List[Tuple[str, str]]]:
195+
"""Swap each ``${{ ... }}`` the runner does not own for an opaque
196+
token so Jinja never sees it; the caller restores them after
197+
rendering. Returns the protected string and the (token, original
198+
text) pairs."""
199+
nonce = uuid.uuid4().hex
200+
kept: List[Tuple[str, str]] = []
201+
202+
def swap(match: "re.Match[str]") -> str:
203+
text = match.group(0)
204+
if cls._owns(text):
205+
return text
206+
token = f"\x00{nonce}:{len(kept)}\x00"
207+
kept.append((token, text))
208+
return token
209+
210+
return _EXPRESSION.sub(swap, string), kept
211+
212+
@staticmethod
213+
def _owns(expression: str) -> bool:
214+
"""True when ``expression`` (one ``${{ ... }}``) parses and every
215+
name it reads is one of the runner's namespaces."""
216+
try:
217+
ast = _environment.parse(expression)
218+
except TemplateSyntaxError:
219+
return False
220+
return meta.find_undeclared_variables(ast) <= RUNNER_NAMESPACES
145221

146222
def render_command(self, string: str):
147223
"""Render a shell command/script template.
@@ -177,12 +253,7 @@ def env_inputs(self) -> Dict[str, str]:
177253

178254
@staticmethod
179255
def _template(string: str) -> Template:
180-
return Template(
181-
string,
182-
block_start_string="${%",
183-
variable_start_string="${{",
184-
comment_start_string="${#",
185-
)
256+
return _environment.from_string(string)
186257

187258
@property
188259
def inputs(self) -> dict:

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
setup(
3535
name="datatorch",
36-
version="0.8.1",
36+
version="0.8.2",
3737
description="A CLI and library for interacting with DataTorch.",
3838
author="DataTorch",
3939
author_email="support@datatorch.io",

‎test/runner/pipelines/test_template.py‎

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,108 @@ def test_string_inputs_render_machine_local_refs(self):
3636
v.add_input("path", "${{ directory.temp }}/x")
3737
self.assertEqual(v.inputs["path"], global_variables["directory"]["temp"] + "/x")
3838

39-
def test_retired_namespaces_raise(self):
40-
# job/run/pipeline/trigger/event are server-side concepts now; the
41-
# agent context no longer carries them, so referencing one fails
42-
# the step loudly instead of silently rendering an empty string.
43-
from jinja2.exceptions import UndefinedError
39+
def test_server_namespaces_pass_through_unchanged(self):
40+
# job/pipeline/trigger/event/steps/inputs/secrets are server
41+
# concepts: the server resolves them before dispatch, so any such
42+
# text still in an input is data the runner did not author. It is
43+
# left exactly as written: not rendered, not emptied, no error.
44+
# (`run` is runner-owned since 0.8.1; see test_run_identity.)
45+
v = Variables()
46+
for template in (
47+
"${{ job.id }}",
48+
"${{ pipeline.id }}",
49+
"${{ trigger.type }}",
50+
"${{ event.userId }}",
51+
"${{ steps.fetch.output.body }}",
52+
"${{ inputs.message }}",
53+
"${{ jobs.build.output.path }}",
54+
"${{ secrets.NAME }}",
55+
"${{ nobody.knows }}",
56+
"${{ unknown }}",
57+
):
58+
self.assertEqual(v.render(template), template)
59+
60+
def test_foreign_text_passes_through_while_own_refs_render(self):
61+
v = Variables()
62+
rendered = v.render(
63+
'error: "${{ steps.fetch.output.body }}" key=${{ secrets.NAME }} '
64+
"on ${{ machine.name }} in ${{directory.temp}}"
65+
)
66+
self.assertEqual(
67+
rendered,
68+
'error: "${{ steps.fetch.output.body }}" key=${{ secrets.NAME }} '
69+
"on "
70+
+ global_variables["machine"]["name"]
71+
+ " in "
72+
+ global_variables["directory"]["temp"],
73+
)
4474

75+
def test_string_input_carrying_server_refs_is_inert(self):
76+
# The watchdog-sense case: a JSON snapshot of older failure
77+
# messages, inlined into a `message` input, quoted `${{ steps... }}`.
78+
snapshot = (
79+
'{"errors": ["Step failed: ${{ steps.fetch.output.body }}", '
80+
'"missing ${{ secrets.NAME }}"], "host": "${{ machine.name }}"}'
81+
)
4582
v = Variables()
46-
for template in ("${{ run.id }}", "${{ trigger.type }}", "${{ event.userId }}"):
47-
with self.assertRaises(UndefinedError):
48-
v.render(template)
83+
v.add_input("message", snapshot)
84+
self.assertEqual(
85+
v.inputs["message"],
86+
snapshot.replace(
87+
"${{ machine.name }}", global_variables["machine"]["name"]
88+
),
89+
)
90+
91+
def test_nested_object_and_array_inputs_pass_through(self):
92+
snapshot = {
93+
"errors": ["${{ steps.fetch.output.body }}", {"k": "${{ secrets.NAME }}"}],
94+
"host": "${{ machine.name }}",
95+
}
96+
v = Variables()
97+
v.add_input("snapshot", snapshot)
98+
v.add_input("list", ["${{ steps.a.output.b }}", "${{ job.id }}"])
99+
self.assertEqual(v.inputs["snapshot"], snapshot)
100+
self.assertEqual(v.inputs["list"], ["${{ steps.a.output.b }}", "${{ job.id }}"])
101+
102+
def test_expression_mixing_foreign_and_own_names_is_data(self):
103+
v = Variables()
104+
template = "${{ machine.name ~ steps.x.output.y }}"
105+
self.assertEqual(v.render(template), template)
106+
107+
def test_malformed_and_unclosed_expressions_never_raise(self):
108+
v = Variables()
109+
for text in (
110+
"${{ }}",
111+
"${{ not valid jinja ( }}",
112+
"${{ steps.x",
113+
"${% if steps.x %}",
114+
):
115+
self.assertEqual(v.render(text), text)
116+
117+
def test_rendering_is_idempotent_for_foreign_text(self):
118+
# Inputs are rendered twice on the way to an action (the step seeds
119+
# them, the action's type-cast pass re-adds strings); the second
120+
# pass must not change what the first one kept.
121+
v = Variables()
122+
text = "${{ steps.fetch.output.body }} on ${{ machine.name }}"
123+
once = v.render(text)
124+
self.assertEqual(v.render(once), once)
125+
126+
def test_run_namespace_still_renders_in_inputs(self):
127+
# `run` is the runner's own since 0.8.1 (set by set_step), so a
128+
# string input may read it, next to text it must not touch.
129+
v = Variables()
130+
v.set("run", {"id": "run-1", "projectId": "proj-1"})
131+
v.add_input("note", "${{ run.id }} saw ${{ steps.fetch.output.body }}")
132+
self.assertEqual(v.inputs["note"], "run-1 saw ${{ steps.fetch.output.body }}")
133+
134+
def test_own_namespace_behaviour_is_unchanged(self):
135+
v = Variables()
136+
v.add_input("total", 42)
137+
self.assertEqual(v.render("${{ input.total }}"), "42")
138+
self.assertEqual(v.render("${{ 'literal' }}"), "literal")
139+
self.assertEqual(v.render("no refs here"), "no refs here")
140+
self.assertIsNone(v.render(None))
49141

50142
def test_inputs_are_scoped_per_instance(self):
51143
a = Variables()

0 commit comments

Comments
 (0)