Skip to content

Commit 7fc9c8e

Browse files
Kasper Jungeclaude
authored andcommitted
refactor: inline _render_context into resolve_contexts to simplify data flow
The private _render_context() helper was used exactly once and added an unnecessary level of indirection. Inlining it makes the rendering-to- resolution flow visible in a single function. Tests now exercise rendering through the public API instead of importing a private symbol. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9491b02 commit 7fc9c8e

2 files changed

Lines changed: 29 additions & 31 deletions

File tree

src/ralphify/contexts.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,13 @@ def run_all_contexts(contexts: list[Context], project_root: Path) -> list[Contex
9595
return [run_context(ctx, project_root) for ctx in contexts]
9696

9797

98-
def _render_context(result: ContextResult) -> str:
99-
"""Render a single context result into text for prompt injection."""
100-
parts = []
101-
102-
if result.context.static_content:
103-
parts.append(result.context.static_content)
104-
105-
output = truncate_output(result.output)
106-
107-
if output.strip():
108-
parts.append(output.strip())
109-
110-
return "\n".join(parts)
111-
112-
11398
def resolve_contexts(prompt: str, results: list[ContextResult]) -> str:
11499
"""Replace context placeholders in a prompt string.
115100
101+
Each context result is rendered by combining its static content (if any)
102+
with its truncated command output, then injected into the prompt via
103+
:func:`resolve_placeholders`.
104+
116105
Callers are responsible for passing only the results they want
117106
resolved (the engine pre-filters via ``_discover_enabled_primitives``).
118107
@@ -122,7 +111,13 @@ def resolve_contexts(prompt: str, results: list[ContextResult]) -> str:
122111
"""
123112
available: dict[str, str] = {}
124113
for r in results:
125-
rendered = _render_context(r)
114+
parts = []
115+
if r.context.static_content:
116+
parts.append(r.context.static_content)
117+
output = truncate_output(r.output)
118+
if output.strip():
119+
parts.append(output.strip())
120+
rendered = "\n".join(parts)
126121
if rendered:
127122
available[r.context.name] = rendered
128123

tests/test_contexts.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
resolve_contexts,
1111
run_context,
1212
run_all_contexts,
13-
_render_context,
1413
)
1514

1615
_MOCK_SUBPROCESS = "ralphify._runner.subprocess.run"
@@ -260,41 +259,45 @@ def test_runs_all_contexts(self, mock_run):
260259
assert mock_run.call_count == 2
261260

262261

263-
class TestRenderContext:
264-
def _make_result(self, static_content="", output="", success=True):
262+
class TestContextRendering:
263+
"""Test context rendering (static content + command output) via resolve_contexts."""
264+
265+
def _make_result(self, name="test", static_content="", output="", success=True):
265266
ctx = Context(
266-
name="test",
267+
name=name,
267268
path=Path("/fake"),
268269
command="echo",
269270
static_content=static_content,
270271
)
271272
return ContextResult(context=ctx, output=output, success=success)
272273

274+
def _render_via_resolve(self, static_content="", output=""):
275+
"""Render a context through resolve_contexts with a named placeholder."""
276+
result = self._make_result(static_content=static_content, output=output)
277+
return resolve_contexts("{{ contexts.test }}", [result])
278+
273279
def test_static_and_output(self):
274-
result = self._make_result(static_content="Header:", output="data\n")
275-
rendered = _render_context(result)
280+
rendered = self._render_via_resolve(static_content="Header:", output="data\n")
276281
assert "Header:" in rendered
277282
assert "data" in rendered
278283

279284
def test_static_only(self):
280-
result = self._make_result(static_content="Just static.", output="")
281-
rendered = _render_context(result)
285+
rendered = self._render_via_resolve(static_content="Just static.", output="")
282286
assert rendered == "Just static."
283287

284288
def test_output_only(self):
285-
result = self._make_result(static_content="", output="dynamic output\n")
286-
rendered = _render_context(result)
289+
rendered = self._render_via_resolve(static_content="", output="dynamic output\n")
287290
assert rendered == "dynamic output"
288291

289-
def test_empty_context(self):
292+
def test_empty_context_not_injected(self):
290293
result = self._make_result(static_content="", output="")
291-
rendered = _render_context(result)
292-
assert rendered == ""
294+
prompt = "Base prompt."
295+
rendered = resolve_contexts(prompt, [result])
296+
assert rendered == prompt
293297

294298
def test_output_truncation(self):
295299
long_output = "x" * (MAX_OUTPUT_LEN + 1000)
296-
result = self._make_result(output=long_output)
297-
rendered = _render_context(result)
300+
rendered = self._render_via_resolve(output=long_output)
298301
assert "truncated" in rendered
299302
assert len(rendered) < len(long_output)
300303

0 commit comments

Comments
 (0)