Skip to content

feat: single source for the system prompt, rendered by both packages - #304

Merged
jat255 merged 2 commits into
mainfrom
jat255/fec8-shared-prompt-source
Sep 7, 2026
Merged

feat: single source for the system prompt, rendered by both packages#304
jat255 merged 2 commits into
mainfrom
jat255/fec8-shared-prompt-source

Conversation

@jat255

@jat255 jat255 commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

This PR (the first of M5) adds the system prompt to the package sharing mechanism. Python gains prompt rendering, which it did not have before.

Summary

prompts/ at the repo root holds system-prompt.md and citation-request.md, the second of which was previously a string literal in R's citation_reminder_text(). scripts/sync-shared.sh generalizes the fixture sync to a table of source and destination pairs and copies prompts/ into both packages; verify-shared-synced.yaml replaces verify-shared-fixtures.yaml and fails on a stale copy. Because the copies sit inside each package directory, editing a prompt now also triggers R-CMD-check and py-check.

The template language for the prompt is Jinja2: this is directly interpretable using jinja2 in Python, and this PR adds a small renderer in R to handle the subset of jinja the template uses. glue could not be used as the shared syntax because its conditionals are R expressions, so a Python renderer would have to evaluate R. Jinja2's conditionals are data, and a subset this small is cheap to implement in R and to pin with fixtures.

Review notes

The five expected prompts in the test fixtures are the exact text the R implementation produced before this change, so the PR is effectively behavior neutral on the R side.

Two renderers of one template is a standing divergence risk, so both implementations have a test to reject anything outside the subset instead of letting a template render in Python and fail in R.

Out of scope, as planned: Python's system_prompt_data() and the fields that need a data source. The following files are generated from the shared data and don't need a review:: pkg-r/inst/prompts/, pkg-py/src/commons/prompts/, and pkg-r/tests/testthat/fixtures/shared/.

Testing

Full R suite: 7023 pass, 0 fail. Python: 920 pass, ruff and pyrefly clean. The built wheel contains commons/prompts/.

R-side summary

@simonpcouch

commons_system_prompt() keeps its signature and returns the same text as before for every input the package can build. Under it, render_system_prompt() uses a new internal render_template() instead of glue, system_prompt_data() trades the two rendered tool-output lists for one flag per tool, and citation_reminder_text() reads its words from the generated inst/prompts/ copy.

The only reachable behaviour change is citation_trust_exception(), which names trusted tools in a fixed order rather than registration order. That differs only for a hand-passed tool list.

All of this is internal: system_prompt_data() and render_system_prompt() are reached only through commons_system_prompt(), itself called only by Commons$set_system_prompt(); both call sites of citation_reminder_text() get the same words; the two deleted *_tool_output_text() helpers had no other callers.

The expected prompts in the shared fixture came from R as it stood before this change, and R still reproduces them exactly, with the full suite passing.

@jat255 jat255 added this to the py-M5: agent, tools, and prompt milestone Sep 7, 2026
@jat255 jat255 added r Affects the R implementation py Affects the Python implementation labels Sep 7, 2026
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Preview deployed to Connect (dogfood.team.pct.posit.it): https://dogfood.team.pct.posit.it/connect/#/apps/d7a36cae-8f27-448b-a478-61b81fbe3942/draft/368618

Deployed from commit de92f9d.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Preview deployed to Connect (connect.staging.pct.posit.it): https://connect.staging.pct.posit.it/connect/#/apps/ad662e1b-5048-4acc-9ad7-f9478c92274e/draft/2677

Deployed from commit de92f9d.

@jat255
jat255 requested a review from simonpcouch September 7, 2026 03:28
@jat255
jat255 marked this pull request as ready for review September 7, 2026 03:37
@jat255
jat255 force-pushed the jat255/fec8-shared-prompt-source branch from 758a91c to 3d0fc40 Compare September 7, 2026 03:39
@jat255
jat255 changed the base branch from main to jat255/context-layer-augment September 7, 2026 03:39
@jat255 jat255 closed this Sep 7, 2026
@jat255
jat255 deleted the jat255/fec8-shared-prompt-source branch September 7, 2026 04:27
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Cleaned up 2 preview bundle(s) on https://dogfood.team.pct.posit.it: 368317, 368362

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Cleaned up 2 preview bundle(s) on https://connect.staging.pct.posit.it: 2661, 2662

@jat255
jat255 restored the jat255/fec8-shared-prompt-source branch September 7, 2026 04:28
@jat255 jat255 reopened this Sep 7, 2026
Base automatically changed from jat255/context-layer-augment to main September 7, 2026 19:42
The system prompt moves to `prompts/` at the repository root, alongside the
citation request text that was a string literal in R. `scripts/sync-shared.sh`
(the fixture sync, generalized) copies both into `pkg-r/inst/prompts/` and
`pkg-py/src/commons/prompts/`, and `verify-shared-synced.yaml` fails when a
committed copy is stale. Since the copies live inside each package directory, a
prompt edit now also triggers R-CMD-check and py-check.

A shared file can only have one syntax, so the template is Jinja2: Python
renders it with jinja2, R with a small renderer covering the subset the template
uses. Conditions are plain names, so the two compound conditions move into
`system_prompt_data()`, and the citable and non-citable output lists move out of
`citations.R` into the template behind one flag per tool. Both renderers are
pinned to the same output by `tests/shared/prompt-render.json`, whose expected
prompts are what the R implementation produced before this change.
@jat255
jat255 force-pushed the jat255/fec8-shared-prompt-source branch from 3d0fc40 to 148333a Compare September 7, 2026 19:42

@simonpcouch simonpcouch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! Warehouse tests pass.

Comment thread pkg-r/R/citations.R Outdated
Comment thread pkg-r/R/prompt.R Outdated
@jat255
jat255 merged commit 34f247f into main Sep 7, 2026
13 checks passed
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Cleaned up 3 preview bundle(s) on https://dogfood.team.pct.posit.it: 368502, 368615, 368618

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Cleaned up 3 preview bundle(s) on https://connect.staging.pct.posit.it: 2664, 2673, 2677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py Affects the Python implementation r Affects the R implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants