fix(llmrails): load library files deterministically (2/5)#1975
fix(llmrails): load library files deterministically (2/5)#1975Pouyanpi wants to merge 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
6fec9aa to
9cad57c
Compare
59c19ea to
c0ff0c7
Compare
Greptile SummaryThis PR makes library and prompt file loading order deterministic by sorting directory and file lists during
|
| Filename | Overview |
|---|---|
| nemoguardrails/llm/prompts.py | Adds dirs.sort() and files.sort() inside the os.walk loop so prompt YAML files are loaded in a stable, platform-independent order. |
| nemoguardrails/rails/llm/llmrails.py | Adds dirs.sort() and switches to for file in sorted(files): during library .co file traversal, making bot_message insertion order deterministic across platforms. |
| tests/test_prompt_override.py | Adds a new test that verifies _load_prompts() returns files in sorted order by passing an out-of-order list through a monkeypatched os.walk stub. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[LLMRails.__init__] --> B[os.walk library_path]
B --> C[dirs.sort in-place for os.walk recursion order]
C --> D[for file in sorted files]
D --> E{file.endswith .co?}
E -- Yes --> F[parse_colang_file]
F --> G[extend config.flows]
F --> H[insert bot_messages if not already set]
E -- No --> D
I[_load_prompts] --> J[os.walk prompts_dir]
J --> K[dirs.sort + files.sort]
K --> L[for filename in files]
L --> M{.yml or .yaml?}
M -- Yes --> N[yaml.safe_load]
N --> O[extend prompts list]
M -- No --> L
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A[LLMRails.__init__] --> B[os.walk library_path]
B --> C[dirs.sort in-place for os.walk recursion order]
C --> D[for file in sorted files]
D --> E{file.endswith .co?}
E -- Yes --> F[parse_colang_file]
F --> G[extend config.flows]
F --> H[insert bot_messages if not already set]
E -- No --> D
I[_load_prompts] --> J[os.walk prompts_dir]
J --> K[dirs.sort + files.sort]
K --> L[for filename in files]
L --> M{.yml or .yaml?}
M -- Yes --> N[yaml.safe_load]
N --> O[extend prompts list]
M -- No --> L
Reviews (10): Last reviewed commit: "test(prompts): isolate prompt loading or..." | Re-trigger Greptile
c0ff0c7 to
b3c164e
Compare
7310fa7 to
6b783f2
Compare
b3c164e to
f2aa414
Compare
3d885f4 to
7000fcc
Compare
d44488d to
68db0d2
Compare
fa2ce24 to
05def3f
Compare
05def3f to
f912ae5
Compare
ab362c6 to
4aa1590
Compare
Summary
Sorts library traversal during LLMRails initialization so recorded rail tests do not depend on filesystem walk order.
Why
Recorded tests expose that library bot message insertion order can vary across platforms when files are loaded in filesystem order.
What Changed
Review Notes
This is the only runtime change in the stack.
Stack Position
Part 2 of 5.
Stack Context
This stack decomposes recorded end-to-end replay coverage into reviewable slices. The PRs should be reviewed against their parent branch in the stack.
Please review each PR against its parent branch, not directly against the root base branch, except for part 1.
stack/recorded-tests-01-harnessdevelopstack/recorded-tests-02-deterministic-library-loadstack/recorded-tests-01-harnessstack/recorded-tests-03-clientsstack/recorded-tests-02-deterministic-library-loadstack/recorded-tests-04-public-apistack/recorded-tests-03-clientsstack/recorded-tests-05-library-railsstack/recorded-tests-04-public-apiValidation