Skip to content

Fixes #32600: include samples in AI context - #32608

Merged
pmbrull merged 2 commits into
mainfrom
pmbrull/san-juan
Sep 5, 2026
Merged

pmbrull merged 2 commits into
mainfrom
pmbrull/san-juan

Conversation

@pmbrull

@pmbrull pmbrull commented Sep 4, 2026

Copy link
Copy Markdown
Member

Describe your changes:

Fixes #32600

Adds stored table samples to the structured per-entity AI context so SQL agents can inspect representative value shapes without first querying the warehouse.

The endpoint returns at most 10 rows, reuses the existing ViewSampleData authorization, and applies the same PII masking as GET /tables/{id}/sampleData. If the caller cannot view samples—or stored data cannot be loaded—the rest of /context still succeeds without sample rows.

Companion consumer: open-metadata/ai-platform#982.

Type of change:

  • Improvement

High-level design:

  • Adds the optional typed sampleData field to assetContext.table in aiContext.json and regenerates dependent TypeScript models.
  • Loads samples through TableRepository.getSampleData(..., canViewPii) after the same ViewSampleData and PII authorization checks used by TableResource.
  • Defensively copies the payload and caps it at 10 rows before adding it to the context response.
  • No migration is needed: this is an optional additive response field backed by the existing table sample-data extension; no persisted format changes.
  • The default Markdown rendering is unchanged. The AI consumer reads the structured ?format=json representation.

Tests:

Use cases covered

  • An authorized caller receives columns and only the first 10 of 12 stored rows.
  • A caller with explicit deny ViewSampleData still receives the context response, without sample rows.
  • The bounded copy is independent from the repository payload.

Unit tests

  • Updated AIContextBuilderTest; 35 tests passed.

Backend integration tests

  • Updated AIContextRestIT; 7 tests passed against the HTTP endpoint.

Ingestion integration tests

  • Not applicable (no ingestion changes).

Playwright (UI) tests

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

Manual testing performed

  • mvn -pl openmetadata-spec -am install -DskipTests
  • mvn test -pl openmetadata-service -Dtest=AIContextBuilderTest -DfailIfNoTests=false
  • mvn test -pl openmetadata-integration-tests -Dtest=AIContextRestIT -DfailIfNoTests=false
  • mvn spotless:check

UI screen recording / screenshots:

Not applicable; there are no UI behavior changes.

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.
  • For UI changes: not applicable; generated types only.
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

Improvement

  • I have added tests around the new logic.
  • For connector/ingestion changes: not applicable.

Limit structured table context to 10 permission-filtered rows so SQL agents can inspect value shape without a warehouse discovery query.
@pmbrull
pmbrull requested a review from a team as a code owner September 4, 2026 12:53
Copilot AI lite review requested due to automatic review settings September 4, 2026 12:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added Ingestion safe to test Add this label to run secure Github workflows on PRs labels Sep 4, 2026
@pmbrull pmbrull mentioned this pull request Sep 4, 2026
14 tasks done
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Jest test Coverage

UI tests summary

Lines Statements Branches Functions
Coverage: 69%
69.75% (91894/131743) 54.4% (57037/104829) 55.75% (17368/31148)

@github-actions

github-actions Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

✅ Playwright Results — workflow succeeded

Validated commit 2d07cc413acd4647e398ae5492a75863e94c03f7 in Playwright run 33884198021, attempt 1.

✅ 764 passed · ❌ 0 failed · 🟡 1 flaky · ⏭️ 0 skipped · 🧰 0 lifecycle flaky

Performance

Blocking targets: ✅ met · Optimization targets: 🟡 in progress

Shard-job maxima below are not the full workflow wall time; the linked run includes build, fixture, planning, and reporting.

🕒 Full workflow signal wall (to summary) 48m 31s

⏱️ Max setup 4m 53s · max shard execution 15m 16s · max shard-job elapsed before upload 19m 28s · reporting 5s

🌐 236.82 requests/attempt · 2.51 app boots/UI scenario · 25.44% common-shard skew

Optimization targets still in progress:

  • Common shard skew was 25.44% (convergence target: at most 15%).
  • Browser traffic was 236.82 requests per attempt (convergence target: fewer than 200).
  • Application boot ratio was 2.51 per UI scenario (1973 boots / 787 scenarios; convergence target: at most 1).
Shard Passed Failed Flaky Skipped Lifecycle failed Lifecycle flaky
✅ Shard chromium-01 101 0 0 0 0 0
✅ Shard chromium-02 116 0 0 0 0 0
🟡 Shard chromium-03 135 0 1 0 0 0
✅ Shard chromium-04 121 0 0 0 0 0
✅ Shard chromium-05 133 0 0 0 0 0
✅ Shard data-asset-rules-01 65 0 0 0 0 0
✅ Shard domain-isolation-01 16 0 0 0 0 0
✅ Shard global-state-01 34 0 0 0 0 0
✅ Shard ingestion-01 1 0 0 0 0 0
✅ Shard reindex-01 2 0 0 0 0 0
✅ Shard search-01 11 0 0 0 0 0
✅ Shard search-rbac-01 29 0 0 0 0 0
🟡 1 flaky test(s) (passed on retry)
  • Pages/Entity.spec.ts › User should be denied access to edit description when deny policy rule is applied on an entity (shard chromium-03, 1 retry)

📦 Download artifacts

How to debug locally
# Download playwright-test-results-<shard> artifact and unzip
npx playwright show-trace path/to/trace.zip    # view trace

The AI context sample-data check wrapped an already-loaded table in a
pre-resolved ResourceContext. That constructor never runs resolveEntity(),
so owners and domains stayed absent (TABLE_FIELDS does not request them)
and every owner- or domain-conditioned rule misfired in both directions:
an owner-conditioned Deny failed open, and authorizePII() saw null owners
so a table's own owner had their sample data masked.

Resolve by id instead, which loads authorizationFields(), matching what
TableResource's GET /tables/{id}/sampleData already does.

The existing IT used an unconditioned Deny, which passes whether or not
owners are loaded. Add an owner-conditioned Deny case that first asserts
the real endpoint returns 403, then asserts the AI context endpoint
agrees. Verified: it returns 10 sample rows before the fix, none after.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings September 4, 2026 14:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@gitar-bot

gitar-bot Bot commented Sep 4, 2026

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

Adds table samples to the AI context endpoint so SQL agents can inspect representative data shapes without querying the warehouse. The stored samples are limited to 10 rows, reuse existing ViewSampleData authorization and PII masking, and gracefully omit from the response if the caller lacks permission or data cannot be loaded. Sample-data authorization now correctly loads table metadata with owners/domains resolved.

✅ 1 resolved
✅ Security: Sample-data authz uses table loaded without owners/domains

📄 openmetadata-service/src/main/java/org/openmetadata/service/aicontext/AIContextBuilder.java:882-894
resolveSampleData builds a pre-resolved ResourceContext<>(Entity.TABLE, table, repository) from a table fetched with TABLE_FIELDS (columns,tableConstraints,joins,tablePartition,tags,testSuite,dataModel), which omits owners and domains. Per ResourceContext's own docs (lines 295-319), the pre-resolved constructor never runs resolveEntity(), so getOwners() returns null and any owner/domain-based policy condition in the VIEW_SAMPLE_DATA authorize call misfires in both directions (a Deny can fail open, an owner-Allow can fail closed). It also makes authorizePII(securityContext, resourceContext.getOwners()) pass null owners, so isOwner returns false and a legitimate table owner has their sample-data PII masked even though GET /tables/{id}/sampleData would show it unmasked. Fix by resolving through the id-based constructor (which loads authorizationFields()), or load owners/domains onto the table before wrapping it. TableResource's real endpoint uses getResourceContextById(id) for exactly this reason.

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

@sonarqubecloud

sonarqubecloud Bot commented Sep 4, 2026

Copy link
Copy Markdown

@sonarqubecloud

sonarqubecloud Bot commented Sep 4, 2026

Copy link
Copy Markdown

@pmbrull
pmbrull merged commit 8298911 into main Sep 5, 2026
141 checks passed
@pmbrull
pmbrull deleted the pmbrull/san-juan branch September 5, 2026 09:23
harshach pushed a commit that referenced this pull request Sep 8, 2026
* feat(api): add samples to AI context

Limit structured table context to 10 permission-filtered rows so SQL agents can inspect value shape without a warehouse discovery query.

* fix(api): resolve sample-data authz with owners loaded

The AI context sample-data check wrapped an already-loaded table in a
pre-resolved ResourceContext. That constructor never runs resolveEntity(),
so owners and domains stayed absent (TABLE_FIELDS does not request them)
and every owner- or domain-conditioned rule misfired in both directions:
an owner-conditioned Deny failed open, and authorizePII() saw null owners
so a table's own owner had their sample data masked.

Resolve by id instead, which loads authorizationFields(), matching what
TableResource's GET /tables/{id}/sampleData already does.

The existing IT used an unconditioned Deny, which passes whether or not
owners are loaded. Add an owner-conditioned Deny case that first asserts
the real endpoint returns 403, then asserts the AI context endpoint
agrees. Verified: it returns 10 sample rows before the fix, none after.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
(cherry picked from commit 8298911)
pmbrull added a commit that referenced this pull request Sep 10, 2026
* feat(data-insights): capture completeness evidence and extend run lifecycle

* feat(search): report embedding token usage instead of discarding it (#32552)

* feat(search): report embedding token usage instead of discarding it

* fix(search): report the text the provider received, not the caller's input

(cherry picked from commit 2209bd7)

* Restrict cross-user follower changes (#32322)

* fix(security): restrict cross-user follower changes

* fix(security): validate follower user IDs

* fix(cache): bypass read-bundle cache for uncached entities

buildReadBundle used the read-bundle cache for every non-deleted read,
ignoring UNCACHED_ENTITY_TYPES (user, task, workflow*, bot, domain, dataProduct).
For those types a mutation followed by an immediate re-read — e.g. the follower
add/delete then fetch in this PR's DataProductResourceIT — could serve a stale
cached bundle. Without this, the follower authorization tests fail under a
cache-enabled deployment (postgres-elasticsearch-redis / postgres-opensearch
integration lanes) with `expected <false> but was <true>` after a delete.

Gate the read-bundle cache on isCacheableEntityType(entityType) so uncached
types take the already-supported bundleCache == null path and always reflect the
current entity_relationship state. Required for the follower tests to pass on the
Redis/OpenSearch lanes (documents why this caching change is part of the security
fix, per reviewer request).

* test(followers): rename put_-prefixed delete-follower tests to delete_ prefix

The two round-trip tests exercise the DELETE follower endpoint (add is setup),
so name them with the delete_ verb prefix to match the method-name convention
(addresses the Copilot review comment).

---------

Co-authored-by: sonika-shah <[email protected]>
(cherry picked from commit 2fb1cb5)

* Fixes #32600: include samples in AI context (#32608)

* feat(api): add samples to AI context

Limit structured table context to 10 permission-filtered rows so SQL agents can inspect value shape without a warehouse discovery query.

* fix(api): resolve sample-data authz with owners loaded

The AI context sample-data check wrapped an already-loaded table in a
pre-resolved ResourceContext. That constructor never runs resolveEntity(),
so owners and domains stayed absent (TABLE_FIELDS does not request them)
and every owner- or domain-conditioned rule misfired in both directions:
an owner-conditioned Deny failed open, and authorizePII() saw null owners
so a table's own owner had their sample data masked.

Resolve by id instead, which loads authorizationFields(), matching what
TableResource's GET /tables/{id}/sampleData already does.

The existing IT used an unconditioned Deny, which passes whether or not
owners are loaded. Add an owner-conditioned Deny case that first asserts
the real endpoint returns 403, then asserts the AI context endpoint
agrees. Verified: it returns 10 sample rows before the fix, none after.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
(cherry picked from commit 8298911)

* fix(search): resolve DI indexes with the cluster data stream prefix

* fix(ui): keep autocomplete responses with their own requests

---------

Co-authored-by: Sriharsha Chintalapani <[email protected]>
Co-authored-by: Vishnu Jain <[email protected]>
Co-authored-by: Sriharsha Chintalapani <[email protected]>
Co-authored-by: sonika-shah <[email protected]>
Co-authored-by: Pere Miquel Brull <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
timothybrush pushed a commit to timothybrush/OpenMetadata that referenced this pull request Sep 10, 2026
…en-metadata#33126)

open-metadata#32608 added `assetContext.table.sampleData` to the AI context but only
the structured representation surfaced it. Markdown is the default for
both `GET /{entity}/{id}/context` and the MCP `get_entity` tool, so the
stored samples were reachable only via `?format=json` — the gap this
closes.

Renders a `# Sample Data` table under the SCHEMA section, immediately
reusing the payload the builder already authorized against
`ViewSampleData` and PII-masked. Values are flattened to one line, pipe
escaped, padded/truncated to the declared column width, and capped at
120 chars so a blob column cannot crowd out the rest of the context. A
caveat line marks the rows as a sample so an agent does not count or
aggregate over them.

The persona context path is unaffected: `AIContextBuilder.applySearchFields`
indexes `buildTableContext(table)` without sample data, so the cached,
cross-user persona document has nothing to render.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include permission-filtered table samples in AI context

2 participants