Skip to content

feat(guardrails): Aliyun AI Guardrails kind (MultiModalGuard) with suggestion verdicts and mask write-back#799

Open
jarvis9443 wants to merge 4 commits into
mainfrom
feat/1070-aliyun-ai-guardrail
Open

feat(guardrails): Aliyun AI Guardrails kind (MultiModalGuard) with suggestion verdicts and mask write-back#799
jarvis9443 wants to merge 4 commits into
mainfrom
feat/1070-aliyun-ai-guardrail

Conversation

@jarvis9443

Copy link
Copy Markdown
Contributor

Adds guardrail kind aliyun_ai_guardrail, integrating Aliyun's AI Guardrails product (AI 安全护栏, action MultiModalGuard). This is a different Aliyun product from the existing aliyun_text_moderation (TextModerationPlus / Content Moderation): separately activated (commodity lvwang_guardrail_public_cn), separately billed, policy-configured in its own console — and its calls appear in that console's records, which TextModerationPlus calls never do. That console visibility is the core ask of the issue.

What it does

  • Same endpoint + signing, new action. MultiModalGuard on green-cip.<region>.aliyuncs.com (verified live: the endpoint routes both actions), RPC v1 signature shared with the TextModerationPlus dispatcher. Config carries region/endpoint/access_key_id/access_key_secret/timeout_ms plus service_level: pro|basicpro (default) calls query_security_check_pro / response_security_check_pro, basic the non-Pro codes.
  • Suggestion-driven verdicts. The block decision follows the returned Data.Suggestion, which Aliyun computes from the console-configured policies — no local risk threshold. block → 422; pass/watch → release; full per-dimension detail (Detail[].Type/Level/Suggestion, including sensitiveData's S0–S3 vocabulary) goes to logs/telemetry as opaque strings, so an Aliyun vocabulary or dimension extension can't break a request.
  • mask honored via segment write-back (Bedrock pattern): on the segment path (non-streaming bodies, held-back tail of a streamed response) a mask suggestion rewrites the body with Aliyun's Ext.Desensitization text — single segment reuses the verdict call's rewrite, multiple segments are re-checked per segment so masked[i] stays aligned with texts[i] (MultiModalGuard takes one content string per call). mask with no usable rewrite fails closed. On the blob path (mid-stream windows — no write-back channel) mask maps to Block so un-masked content is never released.
  • Streaming: windowed like the TMP kind; every window carries the stream-stable sessionId and chatId so Aliyun stitches one console record per response. done is not sent (optional per Aliyun docs; the trait has no end-of-stream signal).
  • Diagnostics: upstream RequestId on every path (x-acs-request-id header first, body fallback — the feat(guardrails): preserve Aliyun's upstream RequestId and correlate it to the gateway request #781 pattern); business Code 408 (commodity not activated) logs an error naming the activation fix; 4xx bodies are never echoed (only the symbolic Code), and matched content (Ext, risk words) has no deserialized path into a log.

Testing

  • Unit (wiremock): suggestion mapping (pass/block/watch), blob-path mask→Block, segment mask write-back (single + multi-segment realign + string-encoded Ext + missing-rewrite fail-closed), service-tier codes, session/chat ids, 408/4xx/5xx/timeout/malformed paths, no-content-leak log assertions, clipped-tail re-attach.
  • E2E (tests/e2e, real aisix + etcd + mock green-cip): input block before upstream, output block after upstream, watch release, mask write-back visible to the caller, streaming SSE error with stable sessionId/chatId across windows, basic-tier service codes.
  • An --ignored live smoke (live_smoke_real_endpoint) exercises the real endpoint including a sensitive-data mask probe; the Ext.Desensitization accumulation assumption (doc-derived) is flagged there for live re-verification.

LiteLLM has no Aliyun guardrail integration (only dashscope LLM providers), so there is no baseline to compare against.

CP exposure (kind enum, schema descriptor, dashboard) ships separately per the cross-plane workflow.

Fixes api7/AISIX-Cloud#1070

… MultiModalGuard)

Adds a new guardrail kind for Aliyun's AI Guardrails product (AI 安全护栏)
— a different Aliyun product from the existing aliyun_text_moderation
(TextModerationPlus / Content Moderation): separately activated (commodity
lvwang_guardrail_public_cn), separately billed, policy-configured in its
own console, and — the point of the feature — its calls appear in that
console's records, which TextModerationPlus calls never do (AISIX-Cloud#1070).

- Action=MultiModalGuard on the same green-cip endpoint and RPC v1
  signature (signing helpers shared with the TextModerationPlus
  dispatcher).
- Input hook: query_security_check_pro (query_security_check at
  service_level: basic); output hook: response_security_check_pro /
  response_security_check.
- Verdict follows the returned Data.Suggestion (computed by Aliyun from
  the console-configured policy): block blocks; pass/watch/mask release.
  No local risk threshold. Full per-dimension detail
  (Type/Level/Suggestion, both level vocabularies incl. sensitiveData
  S0–S3) lands in logs; only detected labels are logged, and matched
  content (Ext) has no deserialized path to a log (#153).
- Streaming output: windowed like the TMP kind; each window carries the
  stream-stable sessionId AND chatId so Aliyun stitches one console
  record per response. done is not sent (optional per docs; the trait
  has no end-of-stream signal).
- Code 408 (commodity not activated) logs a fix-naming error, the most
  common first-run failure.
- Upstream RequestId diagnostics follow the #781 pattern
  (x-acs-request-id header first, body fallback).

Ref AISIX-Cloud#1070
Completes the aliyun_ai_guardrail verdict semantics for Suggestion=mask
(Aliyun-side desensitization policies), following the Bedrock pattern:

- Segment path (non-streaming bodies, held-back tail of a streamed
  response): moderates_segments()=true; a mask suggestion rewrites the
  body with Aliyun's Ext.Desensitization text. A single segment (the
  common case) reuses the verdict call's rewrite directly; multiple
  segments are re-checked one call per segment because MultiModalGuard
  takes one content string per call — the re-calls keep masked[i]
  aligned with texts[i] by construction. mask with no usable
  Desensitization fails closed. A clipped (>2000 chars) segment gets
  its unscanned tail re-attached on write-back.
- Blob path (check_input/check_output — the mid-stream windows): mask
  maps to Block, since there is no write-back channel and releasing
  un-masked content would defeat the operator's policy.
- Ext is parsed tolerantly (object, or a JSON document nested in a
  string — Aliyun types it inconsistently); the desensitized text
  travels outside AigDiagnostics so the diagnostics type stays
  structurally content-free (#153).
- The Desensitization accumulation assumption (last non-empty rewrite
  wins across Result[] entries) is doc-derived and flagged for live
  re-verification during #1070 QA.
- Test-side: cross-module lock for log-capturing tests — a concurrently
  dropped capture subscriber recomputes tracing's GLOBAL max-level hint
  and can fast-path away another thread's events mid-capture.

Ref AISIX-Cloud#1070
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 17 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0297d1a5-b3f5-4c36-ad52-0f9765b81521

📥 Commits

Reviewing files that changed from the base of the PR and between b3ac72c and 4b0d8e6.

📒 Files selected for processing (10)
  • crates/aisix-admin/src/openapi.rs
  • crates/aisix-core/src/models/guardrail.rs
  • crates/aisix-core/src/models/mod.rs
  • crates/aisix-guardrails/src/aliyun.rs
  • crates/aisix-guardrails/src/aliyun_ai_guardrail.rs
  • crates/aisix-guardrails/src/build.rs
  • crates/aisix-guardrails/src/lib.rs
  • crates/aisix-server/src/heartbeat.rs
  • schemas/resources/guardrail.schema.json
  • tests/e2e/src/cases/guardrail-aliyun-ai-guardrail-e2e.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/1070-aliyun-ai-guardrail

Comment @coderabbitai help to get the list of available commands.

…uardrail

# Conflicts:
#	crates/aisix-core/src/models/mod.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant