Skip to content

Commit 15b3427

Browse files
jpalvarezlCopilot
andcommitted
Python: Preserve MessageText expressions in interpolated strings
Traverse only original expression positions so interpolation expressions are preprocessed without reinterpreting literal text or message values. Move quoted-token handling to module scope and add focused regression coverage for the new interpolation contexts. Co-authored-by: Copilot <[email protected]>
1 parent 882ea4d commit 15b3427

2 files changed

Lines changed: 148 additions & 48 deletions

File tree

‎python/packages/declarative/agent_framework_declarative/_workflows/_declarative_base.py‎

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import re
3232
import sys
3333
import uuid
34-
from collections.abc import Mapping
34+
from collections.abc import Iterator, Mapping
3535
from dataclasses import dataclass, field
3636
from decimal import Decimal as _Decimal
3737
from enum import Enum
@@ -67,6 +67,73 @@
6767
_SAFE_PATH_SEGMENT_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_]*$")
6868

6969

70+
def _skip_powerfx_opaque_token(formula: str, start: int) -> int:
71+
"""Return the end of an ordinary quoted token or comment, or start if neither."""
72+
quote = formula[start]
73+
if quote in ('"', "'"):
74+
pos = start + 1
75+
while pos < len(formula):
76+
if formula[pos] == quote:
77+
if pos + 1 < len(formula) and formula[pos + 1] == quote:
78+
pos += 2
79+
continue
80+
return pos + 1
81+
pos += 1
82+
return pos
83+
if formula.startswith("//", start):
84+
pos = start + 2
85+
while pos < len(formula) and formula[pos] not in "\r\n":
86+
pos += 1
87+
return pos
88+
if formula.startswith("/*", start):
89+
end = formula.find("*/", start + 2)
90+
return len(formula) if end == -1 else end + 2
91+
return start
92+
93+
94+
def _iter_powerfx_expression_indices(formula: str) -> Iterator[int]:
95+
"""Yield code positions, excluding literals/comments but including interpolation expressions."""
96+
# None denotes interpolated text; integers track record braces in expression sections.
97+
scopes: list[int | None] = [0]
98+
cursor = 0
99+
while cursor < len(formula):
100+
depth = scopes[-1]
101+
char = formula[cursor]
102+
if depth is None:
103+
if formula.startswith(('""', "{{", "}}"), cursor):
104+
cursor += 2
105+
continue
106+
if char == "{":
107+
scopes.append(0)
108+
elif char == '"':
109+
scopes.pop()
110+
cursor += 1
111+
continue
112+
113+
if formula.startswith('$"', cursor):
114+
scopes.append(None)
115+
cursor += 2
116+
continue
117+
118+
token_end = _skip_powerfx_opaque_token(formula, cursor)
119+
if token_end != cursor:
120+
cursor = token_end
121+
continue
122+
123+
if char == "{":
124+
scopes[-1] = depth + 1
125+
elif char == "}":
126+
if depth > 0:
127+
scopes[-1] = depth - 1
128+
elif len(scopes) > 1:
129+
scopes.pop()
130+
cursor += 1
131+
continue
132+
133+
yield cursor
134+
cursor += 1
135+
136+
70137
@dataclass(frozen=True)
71138
class DeclarativeEnvConfig:
72139
"""Configuration that populates the PowerFx ``Env`` symbol for a workflow.
@@ -670,7 +737,8 @@ def _preprocess_custom_functions(self, formula: str, temp_writes: list[tuple[str
670737
Results are stored in temporary state variables so untrusted message text is
671738
passed to PowerFx as data rather than inserted into formula source.
672739
Temporary names avoid existing Local keys and references in the original formula.
673-
Quoted strings, quoted identifiers, and comments are left untouched.
740+
Literal text, quoted identifiers, and comments are left untouched; expression
741+
sections inside PowerFx interpolated strings are preprocessed as code.
674742
675743
Args:
676744
formula: The PowerFx formula to pre-process
@@ -683,71 +751,36 @@ def _preprocess_custom_functions(self, formula: str, temp_writes: list[tuple[str
683751
Returns:
684752
The rewritten formula.
685753
"""
686-
687-
def skip_opaque_token(start: int) -> int:
688-
"""Return the end of a quoted token or comment, or start if neither."""
689-
quote = formula[start]
690-
if quote in ('"', "'"):
691-
pos = start + 1
692-
while pos < len(formula):
693-
if formula[pos] == quote:
694-
if pos + 1 < len(formula) and formula[pos + 1] == quote:
695-
pos += 2
696-
continue
697-
return pos + 1
698-
pos += 1
699-
return pos
700-
if formula.startswith("//", start):
701-
pos = start + 2
702-
while pos < len(formula) and formula[pos] not in "\r\n":
703-
pos += 1
704-
return pos
705-
if formula.startswith("/*", start):
706-
end = formula.find("*/", start + 2)
707-
return len(formula) if end == -1 else end + 2
708-
return start
709-
710754
temp_var_counter = 0
711755
reserved_names = {name.casefold() for name in self.get_state_data().get("Local", {})}
712756
# Reserve formula references too, so previously undefined names stay undefined.
713757
folded_formula = formula.casefold()
714758
function_name = "MessageText"
759+
call_prefix = f"{function_name}("
715760
result: list[str] = []
716-
cursor = 0
717-
718-
while cursor < len(formula):
719-
token_end = skip_opaque_token(cursor)
720-
if token_end != cursor:
721-
result.append(formula[cursor:token_end])
722-
cursor = token_end
723-
continue
761+
copied_until = 0
762+
positions = _iter_powerfx_expression_indices(formula)
724763

725-
call_prefix = f"{function_name}("
764+
for cursor in positions:
726765
if not formula.startswith(call_prefix, cursor):
727-
result.append(formula[cursor])
728-
cursor += 1
729766
continue
730767

731768
paren_start = cursor + len(function_name)
732769
depth = 1
733-
pos = paren_start + 1
734-
while pos < len(formula) and depth > 0:
735-
token_end = skip_opaque_token(pos)
736-
if token_end != pos:
737-
pos = token_end
770+
for pos in positions:
771+
if pos <= paren_start:
738772
continue
739773
char = formula[pos]
740774
if char == "(":
741775
depth += 1
742776
elif char == ")":
743777
depth -= 1
744-
pos += 1
745-
746-
if depth != 0:
747-
result.append(formula[cursor:])
778+
if depth == 0:
779+
break
780+
else:
748781
break
749782

750-
inner_expr = formula[paren_start + 1 : pos - 1]
783+
inner_expr = formula[paren_start + 1 : pos]
751784
replacement = self._eval_and_replace_message_text(inner_expr)
752785
while True:
753786
temp_var_name = f"_TempMessageText{temp_var_counter}"
@@ -758,10 +791,12 @@ def skip_opaque_token(start: int) -> int:
758791
temp_var_path = f"Local.{temp_var_name}"
759792
temp_writes.append((temp_var_path, self.get(temp_var_path, default=self._MISSING)))
760793
self.set(temp_var_path, replacement)
794+
result.append(formula[copied_until:cursor])
761795
result.append(temp_var_path)
762796
logger.debug(f"Stored MessageText result ({len(replacement)} chars) in temp variable {temp_var_name}")
763-
cursor = pos
797+
copied_until = pos + 1
764798

799+
result.append(formula[copied_until:])
765800
return "".join(result)
766801

767802
def _eval_and_replace_message_text(self, inner_expr: str) -> str:

‎python/packages/declarative/tests/test_declarative_state_path_safety.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,29 @@ def replace_message_text(inner_expr: str) -> str:
324324
assert calls == ["Local.Messages"]
325325
assert state.get("Local._TempMessageText0") == "MessageText(Local.Secret)"
326326

327+
def test_interpolation_preprocesses_only_original_expression_sections(
328+
self, state: DeclarativeWorkflowState, monkeypatch: pytest.MonkeyPatch
329+
) -> None:
330+
calls: list[str] = []
331+
message_text = '$"MessageText(Local.Secret) {MessageText(Local.Secret)}"'
332+
333+
def replace_message_text(inner_expr: str) -> str:
334+
calls.append(inner_expr)
335+
return message_text
336+
337+
monkeypatch.setattr(state, "_eval_and_replace_message_text", replace_message_text)
338+
temp_writes: list[tuple[str, Any]] = []
339+
formula = '$"Literal MessageText(Local.Messages) {{MessageText(Local.Messages)}} {MessageText(Local.Messages)}"'
340+
341+
result = state._preprocess_custom_functions(formula, temp_writes)
342+
343+
assert result == (
344+
'$"Literal MessageText(Local.Messages) {{MessageText(Local.Messages)}} {Local._TempMessageText0}"'
345+
)
346+
assert calls == ["Local.Messages"]
347+
assert state.get("Local._TempMessageText0") == message_text
348+
assert temp_writes == [("Local._TempMessageText0", state._MISSING)]
349+
327350
def test_message_text_inside_string_literal_is_not_preprocessed(
328351
self,
329352
state: DeclarativeWorkflowState,
@@ -453,6 +476,48 @@ def test_message_text_argument_with_quoted_identifier(self, state: DeclarativeWo
453476
assert state.eval(f"=Upper(MessageText(Local.'{quoted_name}'))") == "HELLO"
454477
assert state.get_state_data()["Local"] == original_local
455478

479+
@pytest.mark.parametrize(
480+
("formula", "expected"),
481+
[
482+
('$"Message: {MessageText(Local.Messages)}"', "Message: hello"),
483+
(
484+
'$"Literal MessageText(ignored) ""{{{MessageText(Local.Messages)}}}"""',
485+
'Literal MessageText(ignored) "{hello}"',
486+
),
487+
(
488+
'$"{With({nested: {text: $"Inner {MessageText(Local.Messages)}"}}, nested.text)}"',
489+
"Inner hello",
490+
),
491+
('$"{MessageText(Local.Messages)}:{Upper(MessageText(Local.Other))}"', "hello:OTHER"),
492+
('$"{/* } MessageText(Local.Secret) */ MessageText(Local.Messages)}"', "hello"),
493+
(
494+
(
495+
'Upper(MessageText(If($"Literal ) {MessageText(Local.Messages)}" = "Literal ) hello", '
496+
"Local.Messages, Local.Other)))"
497+
),
498+
"HELLO",
499+
),
500+
],
501+
ids=[
502+
"basic",
503+
"literal-text-and-escapes",
504+
"nested-records-and-interpolation",
505+
"multiple-expressions",
506+
"commented-interpolation-delimiter",
507+
"interpolation-in-function-argument",
508+
],
509+
)
510+
def test_expression_sections_evaluate_with_powerfx(
511+
self, state: DeclarativeWorkflowState, formula: str, expected: str
512+
) -> None:
513+
"""Only expression sections of interpolated strings may evaluate MessageText calls."""
514+
state.set("Local.Messages", [{"text": "hello", "contents": [{"type": "text", "text": "hello"}]}])
515+
state.set("Local.Other", [{"text": "other", "contents": [{"type": "text", "text": "other"}]}])
516+
original_local = state.get_state_data()["Local"].copy()
517+
518+
assert state.eval(f"={formula}") == expected
519+
assert state.get_state_data()["Local"] == original_local
520+
456521
@pytest.mark.parametrize("name", ["MessageText(ignored)", "Archived' MessageText(ignored)"])
457522
def test_quoted_identifier_with_function_like_text(self, state: DeclarativeWorkflowState, name: str) -> None:
458523
state.set(f"Local.{name}", "hello")

0 commit comments

Comments
 (0)