Repository navigation
Conversation
Allow afterEach extension hooks to modify result.namedScores and result.metadata and have those changes persisted to the database, file outputs, and web viewer. Closes promptfoo#7515 Co-Authored-By: Claude Opus 4.6 <[email protected]>
There was a problem hiding this comment.
👍 All Clear
I reviewed this PR for LLM security vulnerabilities, focusing on the new capability for extension hooks to modify evaluation results before persistence. The PR modifies an LLM evaluation framework where extension hooks are trusted developer-provided code (similar to Jest reporters or ESLint plugins), not untrusted external input. No LLM security vulnerabilities were identified.
Minimum severity threshold: 🟡 Medium | To re-scan after changes, comment @promptfoo-scanner
Learn more
There was a problem hiding this comment.
👍 All Clear
I reviewed this PR focusing on LLM security vulnerabilities (prompt injection, data exfiltration, secrets/PII in prompts, insecure output handling, excessive agency, and jailbreak risks). The changes modify evaluation framework infrastructure - specifically the timing and merging behavior of afterEach hooks in test result persistence. No LLM security vulnerabilities were identified.
Minimum severity threshold: 🟡 Medium | To re-scan after changes, comment @promptfoo-scanner
Learn more
|
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughWalkthroughThis PR implements feature Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…g in afterEach hook - Pass shallow copy of result to afterEach hook so in-place mutations to success/score/response don't affect the live row or desync stats - Fix chaining: pass updatedContext (not original context) to each extension so subsequent hooks see prior extensions' changes - Add integration test verifying hook namedScores/metadata flow to persisted results and prompt metrics - Add chaining input test verifying extension promptfoo#2 receives extension promptfoo#1's accumulated context Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Thanks @mldangelo! completely overlooked mutation bypass and chaining issues. Great additions! 🙏 |
Resolve conflicts: - evaluator.ts: apply afterEach persistence to refactored evaluator structure - test/evaluator.test.ts: moved integration test to test/evaluator/metadata.test.ts (test file was split into test/evaluator/ directory on main) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Extend afterEach hook persistence to also support result.response.metadata, completing the scope requested in promptfoo#7515. The shallow copy now includes response.metadata, and the merge logic in runExtensionHook handles it alongside namedScores and metadata. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Avoids coercing undefined metadata/namedScores to {} when there are
no extension hooks to run.
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Document that trackCompletedRow intentionally runs before afterEach hooks since it only reads success/failureReason/tokenUsage fields. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Verify that when multiple beforeAll extensions run, each subsequent extension receives the accumulated context from prior extensions. Covers the chaining fix that changed context→updatedContext. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add afterEach to the list of hooks that can persist changes, add a mutable properties table for afterEach (namedScores, metadata, response.metadata), and document that all merges are shallow. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add inline comment in runExtensionHook switch explaining why afterAll has no case (side-effect-only hook, no downstream consumer). Also document this in the extension hooks reference docs. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Wrap the afterEach extension hook call in try-catch so a throwing hook doesn't prevent the row from being persisted. The error is logged and the row is saved with its original data (no partial hook modifications applied). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Filter namedScores values to only allow finite numbers. Non-numeric values (strings, null, arrays, objects, NaN, Infinity) injected by extension hooks would corrupt metrics aggregation via arithmetic on non-numbers. Validation is applied both in the merge logic (evaluatorHelpers.ts) and at the assignment point (evaluator.ts) to cover both return-value and in-place mutation paths. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Extract the duplicated namedScores sanitization loop into a shared filterFiniteScores() utility in src/util/namedMetrics.ts. Both evaluator.ts and evaluatorHelpers.ts now use it, giving the filtering rule a single source of truth. Also trim comments that described WHAT the code does rather than WHY, keeping only the ordering constraint note and the afterAll omission rationale. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…dependency namedMetrics.ts imports from assertions/index.ts, which creates a cycle when evaluatorHelpers.ts imports from namedMetrics.ts. Move filterFiniteScores to a standalone util/numeric.ts with no dependencies to break the cycle. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Thank you @bishnubista! |
Summary
Closes #7515
Allow
afterEachextension hooks to persist changes toresult.namedScoresandresult.metadata— previously these mutations were silently discarded.afterEachcase inrunExtensionHookswitch to merge returnednamedScoresandmetadataafterEachhook call before DB persist (addResult) and metrics aggregation so changes flow through to database, file outputs, and web viewerAfterEachExtensionHookContextJSDoc to document mutable fieldsScope
This PR addresses the "at minimum" scope from #7515: persisting
result.namedScoresandresult.metadata. Support forresult.response.metadatais intentionally left for a follow-up if maintainers want full parity with the broader problem statement.Design decisions
namedScoresandmetadataare writable.success,score,response, and other fields are not overridable fromafterEachto prevent accidental overrides of assertion results.{...existing, ...returned}so extensions can add/override individual keys but cannot delete existing ones.beforeAll/beforeEach).Test plan
afterEachreturn value handling (6 tests):success,score, andresponsenpm run l)npm run tsc)🤖 Generated with Claude Code