Repository navigation
fix(messages): output guardrails on /v1/messages, streaming + non-streaming (#448) #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bb920c5
fix(messages): run output guardrails on non-streaming /v1/messages
jarvis9443 d14e71f
fix(messages): output guardrails on /v1/messages (non-streaming + str…
jarvis9443 b4e71e9
test(messages): assert exact 422 content-filter envelope on /v1/messages
jarvis9443 7a53192
fix(messages): scan streamed tool-call output in /v1/messages guardrails
jarvis9443 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { createHash } from "node:crypto"; | ||
| import { afterAll, beforeAll, describe, expect, test } from "vitest"; | ||
| import { | ||
| AdminClient, | ||
| EtcdClient, | ||
| spawnApp, | ||
| startOpenAiUpstream, | ||
| waitConfigPropagation, | ||
| type OpenAiUpstream, | ||
| type SpawnedApp, | ||
| } from "../harness/index.js"; | ||
|
|
||
| // E2E: non-streaming /v1/messages runs OUTPUT guardrails (#448 #22). | ||
| // Pre-fix the /v1/messages response was returned without any output | ||
| // check. Here a cross-provider /v1/messages request (Anthropic protocol → | ||
| // OpenAI bridge) gets the mock's canned "mock reply"; an output guardrail | ||
| // blocking the literal "reply" must reject the response rather than | ||
| // return it. | ||
|
|
||
| const CALLER = "sk-msgout-gr-caller"; | ||
| const HASH = createHash("sha256").update(CALLER).digest("hex"); | ||
|
|
||
| describe("/v1/messages output guardrail (#448)", () => { | ||
| let app: SpawnedApp | undefined; | ||
| let upstream: OpenAiUpstream | undefined; | ||
| let admin: AdminClient | undefined; | ||
| let etcdReachable = false; | ||
|
|
||
| beforeAll(async () => { | ||
| etcdReachable = await new EtcdClient().ping(); | ||
| if (!etcdReachable) return; | ||
| upstream = await startOpenAiUpstream(); | ||
| app = await spawnApp(); | ||
| admin = new AdminClient(app.adminUrl, app.adminKey); | ||
| const pk = await admin.createProviderKey({ | ||
| display_name: "msgout-gr-pk", | ||
| secret: "sk-mock", | ||
| api_base: `${upstream.baseUrl}/v1`, | ||
| }); | ||
| // OpenAI-provider model → /v1/messages takes the cross-provider path | ||
| // (Anthropic protocol translated to the OpenAI bridge). | ||
| await admin.createModel({ | ||
| display_name: "msgout-gr", | ||
| provider: "openai", | ||
| model_name: "gpt-4o-mini", | ||
| provider_key_id: pk.id, | ||
| }); | ||
| await admin.createApiKey({ key_hash: HASH, allowed_models: ["msgout-gr"] }); | ||
| await admin.json("POST", "/admin/v1/guardrails", { | ||
| name: "msgout-gr-output-keyword", | ||
| enabled: true, | ||
| hook_point: "output", | ||
| kind: "keyword", | ||
| patterns: [{ kind: "literal", value: "reply" }], | ||
| }); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await app?.exit(); | ||
| await upstream?.close(); | ||
| }); | ||
|
|
||
| const messages = (content: string) => | ||
| fetch(`${app!.proxyUrl}/v1/messages`, { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json", "x-api-key": CALLER }, | ||
| body: JSON.stringify({ | ||
| model: "msgout-gr", | ||
| max_tokens: 64, | ||
| messages: [{ role: "user", content }], | ||
| }), | ||
| }); | ||
|
|
||
| test("a forbidden response is blocked on /v1/messages (non-streaming)", async (ctx) => { | ||
| if (!etcdReachable || !app || !upstream) { | ||
| ctx.skip(); | ||
| return; | ||
| } | ||
| // The mock always replies "mock reply", so once the output guardrail | ||
| // is live every /v1/messages call should be blocked. | ||
| await waitConfigPropagation(async () => (await messages("ready-probe")).status >= 400); | ||
|
|
||
| const res = await messages("anything at all"); | ||
| expect(res.status, "output guardrail must block the forbidden reply").toBeGreaterThanOrEqual(400); | ||
| const body = await res.text(); | ||
| expect(body).toContain("content"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.