Skip to content

Conversation

@nishu-builder
Copy link
Contributor

@nishu-builder nishu-builder commented Jan 17, 2026

Summary

  • Created models/policies.py with Policy, PolicyVersion, and PolicyVersionTag SQLModel models
  • Created queries/policy_queries.py with SQLAlchemy query functions
  • Updated routes/stats_routes.py to use the new query module
  • Removed policy methods and classes from MettaRepo (PolicyRow, PolicyVersionRow, PolicyVersionWithName, PublicPolicyVersionRow, upsert_policy, get_latest_policy_version, create_policy_version, etc.)

Part of stack to migrate app_backend from raw SQL to SQLAlchemy.

@nishu-builder nishu-builder marked this pull request as ready for review January 17, 2026 19:42
Copy link
Contributor Author

nishu-builder commented Jan 17, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • add-to-merge-queue - adds this PR to the back of the merge queue
  • add-to-merge-queue-as-hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6ccbf717b9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +276 to +280
"policy_created_at": pv.policy.created_at,
"user_id": pv.policy.user_id,
"name": pv.policy.name,
"version": pv.version,
"tags": {tag.key: tag.value for tag in pv.tags} if pv.tags else {},

Choose a reason for hiding this comment

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

P2 Badge Eager-load tags before accessing them

Because @with_db closes the session when the query function returns, any PolicyVersion objects handed to callers are detached. This helper accesses pv.tags, which is lazily loaded and is not eagerly loaded in get_policy_versions/get_policy_version_by_id (they only selectinload(PolicyVersion.policy)), so calling policy_version_to_public_dict on those returned objects will raise a DetachedInstanceError or trigger per-row lazy loads before the session is gone. Consider eager-loading PolicyVersion.tags in the query functions or avoid touching pv.tags after the session context ends.

Useful? React with 👍 / 👎.

@nishu-builder nishu-builder changed the title Add SQLAlchemy policy queries module Migrate policy queries to SQLAlchemy Jan 17, 2026
@datadog-official
Copy link

datadog-official bot commented Jan 17, 2026

⚠️ Tests

Fix all issues with Cursor

⚠️ Warnings

🧪 6 Tests failed

    test_recipe_discovery_without_init from test_recipe_discovery.py (Fix with Cursor)

    test_recipe_registry_get_normalizes_paths from test_recipe_discovery.py (Fix with Cursor)

    test_recipe_short_name from test_recipe_discovery.py (Fix with Cursor)

View all

ℹ️ Info

❄️ No new flaky tests detected

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: fa7f5de | Docs | Was this helpful? Give us feedback!

session = get_db()

query = select(PolicyVersion).join(PolicyVersion.policy)
count_query = select(func.count(func.distinct(PolicyVersion.policy_id))).join(PolicyVersion.policy)
Copy link
Contributor

Choose a reason for hiding this comment

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

The count query is counting distinct policy_id values instead of counting the total number of PolicyVersion records. This will return the wrong total count when multiple versions of the same policy match the filters.

Should be:

count_query = select(func.count()).select_from(PolicyVersion).join(PolicyVersion.policy)
Suggested change
count_query = select(func.count(func.distinct(PolicyVersion.policy_id))).join(PolicyVersion.policy)
count_query = select(func.count()).select_from(PolicyVersion).join(PolicyVersion.policy)

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

@graphite-app graphite-app bot changed the base branch from nishad/unique-policy-names to graphite-base/4970 January 17, 2026 21:18
@graphite-app graphite-app bot force-pushed the graphite-base/4970 branch from 8cfe0d6 to be5f9c0 Compare January 17, 2026 21:19
@graphite-app graphite-app bot force-pushed the nishad/sqlalchemy-migration branch from 24f1d7d to 19c4a42 Compare January 17, 2026 21:19
@blacksmith-sh
Copy link

blacksmith-sh bot commented Jan 17, 2026

Found 6 test failures on Blacksmith runners:

Failures

Test View Logs
EditionsCodegenTests/third_party/protobuf/editions/golden/simple_proto3.pb.cc View Logs
EditionsCodegenTests/third_party/protobuf/editions/golden/simple_proto3.pb.cc View Logs
EditionsCodegenTests/third_party/protobuf/editions/golden/simple_proto3.pb.cc View Logs
EditionsCodegenTests/third_party/protobuf/editions/golden/simple_proto3.pb.h View Logs
EditionsCodegenTests/third_party/protobuf/editions/golden/simple_proto3.pb.h View Logs
EditionsCodegenTests/third_party/protobuf/editions/golden/simple_proto3.pb.h View Logs

Fix in Cursor

@graphite-app graphite-app bot changed the base branch from graphite-base/4970 to main January 17, 2026 21:19
@graphite-app graphite-app bot force-pushed the nishad/sqlalchemy-migration branch from 19c4a42 to afc9c59 Compare January 17, 2026 21:19
@nishu-builder nishu-builder force-pushed the nishad/sqlalchemy-migration branch from afc9c59 to 9d4cd17 Compare January 17, 2026 21:37
Comment on lines 34 to 37
s3_path: str | None = None
git_hash: str | None = None
policy_spec: dict[str, Any] | None = Field(default=None, sa_column=Column(JSONB))
attributes: dict[str, Any] | None = Field(default=None, sa_column=Column(JSONB))
Copy link
Contributor

Choose a reason for hiding this comment

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

The PolicyVersion model is missing the 'internal_id' field which is referenced in other parts of the code and appears to be required by the database schema. Add 'internal_id: int = Field(sa_column=Column(Integer, autoincrement=True))' or similar definition to the model to match the database schema requirements.

Spotted by Graphite Agent (based on CI logs)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Nishad and others added 4 commits January 18, 2026 20:46
- Add migration to change unique constraint from (user_id, name) to (name)
- Update upsert_policy to reject duplicate names with 409 Conflict
- Deduping of existing duplicates was already run in prod

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add PolicyVersionTag model and update Policy/PolicyVersion models
- Create queries/policy_queries.py with SQLAlchemy-based queries
- Includes helper functions for converting models to response dicts
- Foundation for migrating stats_routes from raw SQL to SQLAlchemy

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Replace stats_repo.upsert_policy with policy_queries.upsert_policy
- Replace stats_repo.create_policy_version with policy_queries.create_policy_version
- Replace stats_repo.get_policy_version_with_name with policy_queries.get_policy_version_with_name
- Replace stats_repo.get_public_policy_version_by_id with policy_queries.get_policy_version_by_id
- Replace stats_repo.upsert_policy_version_tags with policy_queries.upsert_policy_version_tags
- Replace stats_repo.get_policies with policy_queries.get_policies
- Replace stats_repo.get_policy_versions with policy_queries.get_policy_versions
- Replace stats_repo.get_versions_for_policy with policy_queries.get_versions_for_policy
- Replace stats_repo.get_user_policy_versions with policy_queries.get_user_policy_versions
- Move response models (PolicyRow, PublicPolicyVersionRow, PolicyVersionWithName) to stats_routes

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@nishu-builder nishu-builder force-pushed the nishad/sqlalchemy-migration branch from 9d4cd17 to fa7f5de Compare January 19, 2026 04:49
version_count: int | None = None


class EpisodeReplay(BaseModel):
Copy link
Contributor

Choose a reason for hiding this comment

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

Add an import statement to re-export PolicyVersionWithName from its new location to maintain backward compatibility: 'from metta.app_backend.routes.stats_routes import PolicyVersionWithName'

Spotted by Graphite Agent (based on CI logs)

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

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.

2 participants