promptfoo icon indicating copy to clipboard operation
promptfoo copied to clipboard

Code that imports promptfoo during an eval gets a separate copy of its state, so --no-cache and the config base path don't apply

Open christso opened this issue 1 month ago • 1 comments

Describe the bug

Code that imports promptfoo while promptfoo eval is running, such as a file:// or package: assertion or a custom provider, gets its own copy of Promptfoo's internal state. As a result, --no-cache doesn't apply to it: cache.isCacheEnabled() still returns true, and providers it loads with loadApiProvider keep serving cached replies. PROMPTFOO_CACHE_ENABLED=false does work, because each copy reads it when it loads.

To Reproduce

probe.mjs (point the import at your installed promptfoo):

import { cache } from 'promptfoo';
export default async () => ({
  pass: true,
  score: 1,
  reason: 'isCacheEnabled=' + cache.isCacheEnabled(),
});

probe.yaml:

prompts: ["hello"]
providers: [echo]
tests:
  - assert:
      - type: javascript
        value: file://probe.mjs
Command The assertion's reason
promptfoo eval -c probe.yaml --no-cache isCacheEnabled=true
PROMPTFOO_CACHE_ENABLED=false promptfoo eval -c probe.yaml --no-cache isCacheEnabled=false

We also saw the same split for the config's base path. Calling loadApiProvider('file://judge.mjs') from an assertion in a config under sub/ resolved judge.mjs against the working directory, not sub/, because the library's copy of cliState.basePath is empty.

Expected behavior

Code that uses the public API during an eval should see that eval's cache setting and base path. Either option would do:

  • share one instance of the stateful modules between the CLI and library entry points, or
  • give extensions a supported handle, for example a getCache and the base path in the assertion context, like the getCache that target providers already receive.

System information:

Output of promptfoo debug:

{
  "version": "0.124.0",
  "platform": {
    "os": "linux",
    "release": "6.18.40.1-microsoft-standard-WSL2",
    "arch": "x64",
    "nodeVersion": "v22.22.3"
  },
  "env": {
    "telemetryDisabled": true,
    "telemetryDebug": false
  },
  "configInfo": {
    "configExists": false,
    "configContent": null
  }
}
  • Promptfoo version: 0.124.0 (not yet checked on 0.124.1)

Additional context

Why

In the published 0.124.0 build, the CLI entry dist/src/main.js imports cache-CH1rM0Ie.js, while the library entry dist/src/index.js imports cache-CXzLv3GU.js. The bundler emitted separate copies of the stateful modules for each entry point. --no-cache calls disableCache(), and the CLI sets cliState.basePath, but only on the CLI's copy. Extension code that imports promptfoo gets the library's copy.

Workaround

Set PROMPTFOO_CACHE_ENABLED=false alongside --no-cache, and pass absolute paths to loadApiProvider.

christso avatar Oct 22 '26 03:10 christso

I'd like to take this one. My plan: make the stateful modules (the cache and cliState) process-wide singletons keyed with Symbol.for(...) on globalThis, so the CLI bundle and the library bundle share one instance no matter which copy is loaded. Then I'll add a regression test using your probe.mjs repro, for both --no-cache and the sub/ base-path case. If you'd prefer the other option (passing getCache and the base path in the assertion context), let me know and I'll do that instead.

ahmadkhancodes avatar Nov 12 '26 09:11 ahmadkhancodes