Skip to content

Fixes #32260: trust signals for AI context — gate pills on Active, read-time staleness stamping - #32261

Merged
pmbrull merged 9 commits into
open-metadata:mainfrom
arnabwithab:pill-trust-gating
Sep 8, 2026
Merged

pmbrull merged 9 commits into
open-metadata:mainfrom
arnabwithab:pill-trust-gating

Conversation

@arnabwithab

@arnabwithab arnabwithab commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Describe your changes:

Fixes #32260

Knowledge pills (ContextMemory items) attached to an asset flow into AI context bundles (get_asset_context, persona documents) regardless of lifecycle state, and the bundle never tells an agent whether the knowledge it is receiving is still current. This PR lands PR1 of the three-part plan proposed in #32260:

  1. Active-only pill gating — toPillKnowledgeItem now mirrors the glossary path's approval gating: pills in Draft or Archived state with a live APPLIED_TO edge are no longer served to agents as current context. Pre-lifecycle memories (no status) remain visible, matching how isApproved treats a missing glossary review status.
  2. Read-time staleness stamping — KnowledgeItem gains additive stale + staleReasons fields, stamped by AIContextBuilder from two deterministic, pure-function signals:
    • assetUpdatedAfterKnowledge — the attached asset's updatedAt is newer than the knowledge item's last update;
    • dataQualityFailing / upstreamDataQualityFailing — the asset's (or a direct upstream's, capped at 3 edges and memoized per bundle) data-quality tests are failing.
      Stamping is informational only — items are never removed, so agents can weigh the signal instead of silently losing context.
  3. Compact Markdown trust cue — stale items render a one-line cue naming the reasons, only when stale; the cue's length is reserved inside the existing knowledge budget (fitItem) so it can never push a bundle past the caller's budget.

No new status vocabulary (uses the flag annotation, not EntityStatus), no new provenance model, no relational migration (additive fields on the KnowledgeItem JSON type; ContextMemory schema untouched). PR2 (validity fields on ContextMemory) and PR3 (lineage-triggered invalidation) proceed only after this lands, per the issue discussion.

Type of change:

  • Bug fix
  • Improvement

High-level design:

  • aiContext.json (+ generated Java/TS): KnowledgeItem.stale (boolean) and KnowledgeItem.staleReasons (string[]), both optional/absent when no signal fires — backward compatible for existing consumers.
  • AIContextBuilder: isActivePill gate in toPillKnowledgeItem; static, repository-free stampStaleness computes signals per pill; asset DQ is fetched once per bundle (shared with the observability section instead of a second fetch); upstream DQ is lazily computed once per bundle and capped at MAX_UPSTREAM_DQ_CHECKS = 3 edges.
  • AIContextMarkdown: renders _⚠ Stale — <reasons>._ before the excerpt hint; fitItem reserves STALE_CUE_RESERVE = 80 chars for stale items so cue + excerpt stay within budget.
  • Alternatives considered in [AI Context] Knowledge pills have no trust signals: Archived/Draft pills surface in AI bundles, and staleness detection is promised but never implemented #32260: a persisted status vocabulary and provenance chains were rejected for this PR — the computed-on-read annotation gives the same agent-facing signal without schema lifecycle coupling.

Tests:

Use cases covered

  • Archived/Draft pill attached to a table is excluded from get_asset_context output
  • Pill on an asset updated after the pill's last update is flagged stale with assetUpdatedAfterKnowledge
  • Pill on an asset (or upstream) with failing DQ tests is flagged with the corresponding reason
  • Fresh pill on a healthy asset carries no stale flag or reasons
  • A stale item's markdown excerpt shrinks so cue + excerpt fit the caller's knowledge budget
  • Trust cue renders in Compact Markdown exactly once, only for stale items

Unit tests

  • Added 7 tests in AIContextBuilderTest (41 total in class, all green): gating, each staleness branch, zero-failure/no-timestamp negative cases, budget reserve, markdown cue.
  • Full sweep green: 128 tests across all aicontext + ContextMemoryRepositoryTest classes, plus 83 in the search-bulk-sink/filter/serializer tests that exercise KnowledgeItem serialization (211 total).
  • Regenerated artifacts: Java POJOs (Maven), pydantic models + committed TS types (make generate, update_typescript_types).

Backend integration tests

  • Not added in this PR — the gating and stamping paths are exercised end-to-end by the existing AIContextMcpIT harness; happy to extend it with a stale-pill case if reviewers prefer it here.

Ingestion integration tests

  • Not applicable (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI behavior change; generated TS types only).

Manual testing performed

  • Schema→POJO→TS regeneration verified; unit suites above run locally against the full application test harness.

UI screen recording / screenshots:

Not applicable.

Checklist:

  • I have read the CONTRIBUTING document.
  • My PR title is Fixes <issue-number>: <short explanation>
  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.
  • I have commented on my code, particularly in hard-to-understand areas.
  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed. — additive JSON-type fields only; entity JSON column storage means no migration is required.
  • For UI changes: I attached a screen recording and/or screenshots above. — N/A
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

Greptile Summary

Adds lifecycle gating and read-time staleness signals for AI-context knowledge items while preserving backward-compatible optional schema fields.

  • Excludes Draft and Archived context-memory pills from asset context.
  • Marks pills stale based on asset updates and failing local or authorized upstream data-quality tests.
  • Adds compact Markdown trust cues with exact shared-budget accounting.
  • Fixes zero-length excerpt handling, bounds structural previews, and enforces caller-relative upstream test permissions.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextBuilder.java Implements active-pill gating, staleness computation, bounded upstream DQ checks, authorization enforcement, and corrected excerpt-budget accounting.
openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextMarkdown.java Renders stale trust cues only alongside visible knowledge content using the same string charged by the builder.
openmetadata-service/src/test/java/org/openmetadata/service/aicontext/AIContextBuilderTest.java Covers lifecycle gating, staleness signals, authorization failure handling, structural-preview bounds, and shared stale-cue budgeting.
openmetadata-spec/src/main/resources/json/schema/type/aiContext.json Adds optional stale and staleReasons fields to the shared KnowledgeItem contract.
openmetadata-ui/src/main/resources/ui/src/generated/type/aiContext.ts Updates the generated AI-context TypeScript model with the optional staleness fields.
openmetadata-ui/src/main/resources/ui/src/generated/type/personaContext.ts Updates the generated persona-context KnowledgeItem model with the optional staleness fields.

Reviews (7): Last reviewed commit: "Merge branch 'main' into pill-trust-gati..." | Re-trigger Greptile

@arnabwithab
arnabwithab requested a review from a team as a code owner August 30, 2026 10:18
@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@arnabwithab

arnabwithab commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi team, the pr-metadata-check wants issue #32260 added to the Shipping project. As an external contributor I don't have project access. could a maintainer please add it (or apply skip-pr-checks)? Happy to do it myself if my token gets the read:project scope and the project allows it. Thanks!

…upstream DQ authorization gate, log placeholder
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

…l when no caller context exists (persona materialization)
@github-actions

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@harshach harshach added the safe to test Add this label to run secure Github workflows on PRs label Aug 31, 2026
@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Jest test Coverage

UI tests summary

Lines Statements Branches Functions
Coverage: 70%
70.39% (95039/135002) 54.9% (56203/102369) 56.3% (18770/33336)

@github-actions github-actions Bot removed the safe to test Add this label to run secure Github workflows on PRs label Sep 7, 2026
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@arnabwithab

arnabwithab commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @pmbrull ! brought the branch upto date with main so that it could be merged safely! please do add the safe-to-test label :)
Also im not sure if the issue needs to be put on the shipping project, but please do let me know! it was flagged earlier, otherwise i believe it needs to skip-pr-check label

@gitar-bot

gitar-bot Bot commented Sep 7, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 3 resolved / 3 findings

Implements active-pill gating and read-time staleness stamping for AI-context knowledge items. Pills in Draft or Archived state are now excluded from agent context, and stale flags with reasons are computed from asset-update timing and upstream data-quality signals. Corrects stale-cue budget reserve sizing, upstream DQ logging, and zero-length excerpt handling. No issues found.

✅ 3 resolved
✅ Quality: STALE_CUE_RESERVE (80) is smaller than the rendered cue

📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextBuilder.java:133-137 📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextBuilder.java:260 📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextMarkdown.java:560-567
The reserve is documented and used to guarantee the stale trust cue "never pushes a bundle past the caller's budget", but the cue rendered in AIContextMarkdown.appendKnowledgeContent is ~93 chars even with no reasons ("
⚠ Stale. Weigh this against the asset's current state before relying on it for decisions.
") and grows further once staleReasons are joined in (e.g. "assetUpdatedAfterKnowledge" adds ~28 more). With STALE_CUE_RESERVE=80 the excerpt+cue can exceed the reserved share, so the invariant does not actually hold. The impact is small against the 8000-char soft budget, but the guarantee is stated explicitly and the test only checks the excerpt shrank, not that the full cue fits. Raise the reserve to cover the fixed cue text plus a reasons allowance.

✅ Bug: Upstream DQ log drops error detail (arg/placeholder mismatch)

📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextBuilder.java:539-542
The warn call has a single {} placeholder but passes two arguments: ref.getName() and e.getMessage(). SLF4J substitutes only the first arg into the one placeholder; because e.getMessage() is a String (not a Throwable), the second arg is silently discarded, so the actual failure reason is never logged. Add a second placeholder for the message, or pass the exception itself to capture the stack trace.

✅ Bug: excerpt(content, 0) throws StringIndexOutOfBoundsException

📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextBuilder.java:380-388 📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextBuilder.java:344
The reworked excerpt computes cut = limit - 1, which is -1 when limit == 0, making content.substring(0, cut) throw StringIndexOutOfBoundsException. This path is reachable: structuralPreview calls excerpt(leadParagraph(content), Math.max(0, limit - outline.length())), so a long Sections: outline (>= the incoming limit, which can be as low as MIN_EXCERPT_CHARS=120) passes 0, crashing bundle assembly for a truncated, heading-heavy knowledge item. The previous code returned an empty string safely for limit=0. Guard the negative cut, e.g. clamp limit - 1 to 0 or return early when limit <= 0.

Options

Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Hi there 👋 Thanks for your contribution!

The OpenMetadata team will review the PR shortly! Once it has been labeled as safe to test, the CI workflows
will start executing and we'll be able to make sure everything is working as expected.

Let us know if you need any help!

@pmbrull pmbrull added the safe to test Add this label to run secure Github workflows on PRs label Sep 8, 2026
@sonarqubecloud

sonarqubecloud Bot commented Sep 8, 2026

Copy link
Copy Markdown

@arnabwithab

Copy link
Copy Markdown
Contributor Author

hey @pmbrull
we'll need to add #32260 to the Shipping project with Status/Source/Priority/Domain/Release set, or slap skip-pr-checks on the PR!

@sonarqubecloud

sonarqubecloud Bot commented Sep 8, 2026

Copy link
Copy Markdown

@pmbrull
pmbrull added this pull request to the merge queue Sep 8, 2026
Merged via the queue into open-metadata:main with commit c3d6c03 Sep 8, 2026
206 of 235 checks passed
pmbrull added a commit that referenced this pull request Sep 8, 2026
…ntext-memory visibility (#32465) (#32973)

* Fixes #32260: trust signals for AI context — gate pills on Active, read-time staleness stamping (#32261)

* Fixes #32260: trust signals for AI context — gate pills on Active, read-time staleness stamping

* Fixes #32260: address review — exact cue budget sizing, upstream DQ authorization gate, log placeholder

* Fixes #32260: charge stale cue to the shared budget pool; count ellipsis inside the excerpt limit

* Fixes #32260: guard excerpt against non-positive limits (zero-allowance structural preview)

* Fixes #32260: gate upstream DQ behind VIEW_TESTS, bound structural preview to the excerpt limit

* Fixes #32260: suppress caller-relative upstream DQ signal when no caller context exists (persona materialization)

(cherry picked from commit c3d6c03)

* Apply context memory visibility rules on MCP entity reads (#32465)

* fix(mcp): apply per-entity visibility rules on direct entity reads

* test(mcp): cover the shared-pill lookup and state the rule in comments

* review: exercise the sharedWith path, cover the admin branch, correct the scope note

(cherry picked from commit d3ab545)

---------

Co-authored-by: Arnab Mandal <[email protected]>
Co-authored-by: Vishnu Jain <[email protected]>
@pmbrull

pmbrull commented Sep 8, 2026

Copy link
Copy Markdown
Member

picked to 2.0 here #32973

@arnabwithab
arnabwithab deleted the pill-trust-gating branch September 29, 2026 10:59

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

3 participants