Skip to content

Fix gateway asset restoration and migration order - #3657

Merged
richturner merged 2 commits into
mainfrom
fix/gateway-restoration-and-migration
Dec 2, 2026
Merged

richturner merged 2 commits into
mainfrom
fix/gateway-restoration-and-migration

Conversation

@wborn

@wborn wborn commented Dec 2, 2026

Copy link
Copy Markdown
Member

Description

Follow up on the post-merge feedback on #3547 and fix the Flyway upgrade from 1.35.1.

  • Rename the application version migration so it runs after the migrations shipped in 1.35.1.
  • Preserve stored agent link metadata when gateway events bypass agent interception.
  • Let a gateway restore an asset it owns once its ancestors are active. Keep failed restores eligible for deletion retry.
  • Log the edge and central asset IDs when a sync save fails.
  • Start the asset integrity test fixture in setupSpec and clean up its assets.

Validation

  • Backend Spotless and git diff --check passed.
  • 26 focused unit tests and 34 integration tests passed. Two tunnel tests were skipped. GatewayTest used a temporary Keycloak port override for the isolated test stack.
  • On PostgreSQL, the renamed migration applied and validated after the last 1.35.1 migration with out-of-order execution disabled.

Checklist

  • 1. Acceptance criteria of the linked issue(s) are met
  • 2. Tests are written and all tests pass
  • 3. Changes are manually tested by you and the reviewer
  • 4. Documentation is written or updated (existing upgrade guidance applies)

- Run the application version migration after the 1.35.1 migrations
- Preserve stored agent links during gateway attribute processing
- Restore owned pending assets while enforcing ancestry checks
- Log rejected gateway asset IDs and clean up test fixtures
@wborn
wborn requested a review from richturner December 2, 2026 12:43
@wborn wborn added the Bug Error or issue in the system label Dec 2, 2026 — with ChatGPT Codex Connector

@richturner richturner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the follow-up. The pending-deletion and migration changes look good. One change requested on the agent link fix.

Load the asset read-only in AssetProcessingService instead of copying the meta in GatewayService

Copying the MetaMap in GatewayService.onAttributeEventIntercepted stops the agent link from being lost. But it leaves the root cause in place for every other handler.

The bug comes from #3547 replacing assetStorageService.find(...) with em.find(Asset.class, id, lockMode) in processAttributeEvent. The old lookup ran a native query with AvailableHints.HINT_READ_ONLY, so Hibernate never checked the entity for changes. em.find returns a managed entity, and new AttributeEvent(asset, attribute, ...) takes attribute.getMeta() by reference. So any AttributeEventInterceptor that changes the event's meta changes the stored asset, and so does any interceptor that changes the asset it gets from em.find in the same transaction. AttributeEventInterceptor is a public extension point, so extensions and custom projects are exposed to this too. The copy also has gaps:

  • It is shallow. The MetaItem instances are still shared, so a handler that changes an item's value still writes it through.
  • It only covers this one call site. Other interceptors and other mutable state, such as asset fields, are still exposed.

Loading the asset read-only restores the behavior from before #3547 for all handlers at once. It also removes the dirty check at flush:

// Read-only: interceptors may change the event or the asset in memory, but nothing is flushed.
// The value is persisted separately by the native update in updateAttributeValue.
Asset<?> asset =
    em.find(Asset.class, event.getId(), lockMode, Map.of(AvailableHints.HINT_READ_ONLY, true));
  • Lock: the row lock belongs to the transaction, not the entity, so PESSIMISTIC_WRITE still applies.
  • Coverage: the later em.find in GatewayService returns this same instance from the persistence context, so it is read-only as well.
  • Fallback: if find turns out to ignore the hint in our Hibernate version, em.unwrap(Session.class).setReadOnly(asset, true) straight after the load does the same thing.

With that in place, the MetaMap copy in GatewayService can go. The plain event.getMeta().remove(MetaItemType.AGENT_LINK) only affects the event again, as it did on main before #3547, and the merged branch for gateway and internal sources can stay.

Tests:

  • The new GatewayMappingTest case asserts attribute.hasMeta(AGENT_LINK) on the source attribute. That only holds because of the copy, so it would fail once the copy is removed. Keep the assertions that the event no longer has the link and that ownership is checked, and drop the assertion on the source attribute.
  • The real protection lives in AssetProcessingService and needs a container to test, so please add an integration test. Either works:
    • In GatewayTest: after the initial sync, have the edge client send an AttributeEvent for an agent-linked attribute. Once the value lands, re-read the central asset and assert that temp still has AGENT_LINK and that version has not changed.
    • A general guard: register an interceptor with assetProcessingService.addEventInterceptor { em, event -> event.meta.remove(AGENT_LINK); false }, as RulesEngineFiringTest does. Send an event, then assert the stored meta and version are unchanged. This covers any future handler, not just the gateway one.

Other notes

  • Migration rename: this is correct. 1.35.1 shipped V20261125_1401 and V20261130_1200, so an upgrade from 1.35.1 with outOfOrder=false would fail on V20261105_1530. One risk remains: a database that ran a main build after #3547 already has V20261105_1530 applied. Flyway validation will report it as applied but not found, and the renamed script will fail on CREATE TABLE application_version. Consider CREATE TABLE IF NOT EXISTS in the renamed script, and add a note for anyone running main snapshots about cleaning up the old flyway_schema_history row, e.g. with flyway repair.
  • Pending deletion: PendingDeletion.ALLOW_SUBJECT plus moving failedAssetDeleteIds.remove to after a successful merge looks right. The test covers a pending ancestor, an invalid replacement and a valid restore.
  • Nit: saveAssetLocally now logs and rethrows, and onGatewayEvent logs the same exception again before disconnecting. Consider attaching the IDs to the rethrown exception instead, so each failure is logged once.

- Keep gateway row locks while preventing interceptor mutations from persisting
- Persist gateway credential repairs explicitly in the attribute transaction
- Include asset IDs in sync errors and cover read-only interception
@wborn
wborn requested a review from richturner December 2, 2026 13:39

wborn commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Addressed the review feedback in 2ac3580. Attribute processing now loads assets read-only while keeping the gateway row lock and preserving explicit credential repairs. Sync failures now log the edge and central asset IDs once. Added integration coverage for interceptor changes and verified that an inbound gateway update preserves AGENT_LINK and the asset version. The migration rename stays as proposed.

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@richturner
richturner enabled auto-merge December 2, 2026 13:56
@richturner
richturner added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit 58a619b Dec 2, 2026
23 checks passed
@richturner
richturner deleted the fix/gateway-restoration-and-migration branch December 2, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Error or issue in the system

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants