fix(agent-context): send Document fields to OSS servers; keep semantic search score Cloud-only

SH
2d ago
+289-31

Summary

Two fixes to how the agent tools in datahub-agent-context send tagged GraphQL fields to OSS servers.

  1. get_entities sent no Document fields to OSS servers. Fields that arrived after older GMS releases are tagged #[NEWER_GMS] in the .gql files. That includes every ... on Document arm in entity_details.gql. execute_graphql only turned those fields on for Cloud. OSS servers never got them, so a document came back without its title, contents, owners, tags or domain. That is true even on OSS releases that have had Document since v1.4.0.
    execute_graphql now sends tagged fields to every server and, on a schema error, retries without them. The fallback is cached per graph and query, so one query that needs the fallback no longer turns tagged fields off for every other tool on that server. This is the same logic mcp-server-datahub already uses (graphql_helpers.execute_graphql).

  2. Semantic document search asked for score, which the OSS schema doesn't have. SearchResult.score is only in the Cloud schema. It is now tagged #[CLOUD] #[NEWER_GMS], like incidentStatus in entity_details.gql. CLOUD filtering comments it out for OSS before the tagged-field check runs, so it can't make OSS queries fail and fall back. Cloud still requests it and still falls back if a server lacks it.

  3. Fallbacks are keyed on the graph object, not id(graph). CPython reuses a freed object's id(), so a new DataHubGraph could inherit an earlier graph's cached fallbacks and leave out newer fields its server supports. The cache is now a weakref.WeakKeyDictionary keyed on the graph, and its entries go away with the graph.

Fields that only exist on Cloud stay tagged #[CLOUD]. With these two changes, every .gql file in the package validates against the OSS schema in datahub-graphql-core on master. That holds for both the first query and the fallback. Before, document_semantic_search.gql failed on score.

Behaviour on other servers

  • OSS before v1.4.0 (no Document type): GetEntity now fails once on each server and falls back to the query without tagged fields. That fallback is what those servers ran before, and it is cached for the query, so later calls go straight to it.
  • Cloud: the first attempt is unchanged. A fallback is now remembered per query, not for every query on that server.
  • DISABLE_NEWER_GMS_FIELD_DETECTION=true still turns tagged fields off everywhere.

Overlap with open PRs

#21876 adds OKF fields to document_search.gql and document_semantic_search.gql. With this change, OSS servers get those fields on the first try. The only overlap is the score line in document_semantic_search.gql, which is a separate hunk. No conflict is expected.

Testing

New regression tests. Each one fails on master and passes with this change:

  • tests/unit/mcp_tools/test_newer_gms_fields.py::test_get_entities_sends_document_fields_to_oss: on an OSS graph, get_entities on a document sends a query with an active ... on Document arm, including contents and ownership. Fails on master.
  • tests/unit/mcp_tools/test_gql_schema_compat.py::test_full_query_validates_against_oss_schema[document_semantic_search.gql]: validates each .gql file with graphql-core against datahub-graphql-core/src/main/resources/**/*.graphql, as an OSS server receives it (CLOUD lines stripped). It checks the full query and the fallback. Fails on master with Cannot query field 'score' on type 'SearchResult'.
  • test_newer_gms_fields.py::test_fallback_is_cached_per_query: a fallback in one query doesn't drop tagged fields from another. Fails on master.
  • test_newer_gms_fields.py::test_fallback_is_not_shared_by_graphs_with_the_same_id: two graphs that report the same id() don't share fallbacks. Fails before the cache fix (the new graph inherits the old one's fallback).
  • test_newer_gms_fields.py::test_fallbacks_are_dropped_with_their_graph: a freed graph's fallbacks are removed from the cache.
  • Also added: OSS semantic search runs once and doesn't request score; Cloud still requests score; after a failed fallback, the next call sends the full query again.

Checks run:

  • pytest tests: 705 passed, 3 xfailed.
  • ruff check, ruff format --check and mypy pass.
  • graphql-core is added to the dev extra for the schema test.

Checklist

  • The PR conforms to DataHub's Contributing Guideline (PR title format)
  • Tests for the changes have been added
  • Docs: n/a
  • Breaking changes: n/a

šŸ¤– Opened with cclaude on behalf of @shirshanka