Skip to content

fix(store): carry opt-outs in a data move, keeping the destination's live ones - #2201

Merged
thesyncim merged 6 commits into
mainfrom
fix/ai-3489-opt-outs-move
Nov 23, 2026
Merged

thesyncim merged 6 commits into
mainfrom
fix/ai-3489-opt-outs-move

Conversation

@thesyncim

@thesyncim thesyncim commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem
opt_outs was not in dataTables, so GET /v1/data/export and GET /v1/data/changes left it out. After an app moved, a recipient who had texted STOP was no longer opted out at the destination and could be reached again.

Fix

  • opt_outs is in dataTables, so the export carries it and the change-feed trigger records later STOP and START messages.
  • importRow skips an imported live opt-out when the destination already has a live one for the same recipient and channel under another id, so the partial unique index holds and the destination's row stays.
  • An imported revoked row revokes only the row of its own id, so a replayed START never lifts an opt-out recorded at the destination. On an id conflict the held row is updated only when the imported one names the same recipient and channel, so an imported row never moves an opt-out onto another recipient or lifts it from another channel.
  • HoldingTable skips opt_outs: the import keeps a live opt-out at the destination, so an app holding only opt-outs still imports without full (POST /v1/data/import, router replicate).
  • opt_outs is in importedModels (from fix(store): validate imported rows and force what a request counts against #2181), so an imported opt-out is refused with OptOut.Validate's error, as the API refuses it.
  • The export reads opt_outs revoked rows first (order on dataTable, used by selectRows). Otherwise a second copy after a START and a new STOP could read the new live row first, skip it beside the old copy, then lift that copy. The change feed replays in sequence order and is unaffected.

Tests

  • TestAMovedRecipientIsStillOptedOut: opted out after a move.
  • TestAStopAndAStartAfterTheCopyArriveThroughTheChanges: both replay through the changes.
  • TestAnImportOverALiveOptOutLeavesIt: the destination's live row stays and the import does not fail.
  • TestAnImportedRevokedOptOutDoesNotLiftOneRecordedHere: a replayed START leaves the destination's opt-out live and the revoked copy is kept beside it; failed with the live-only condition dropped from the skip.
  • TestASecondCopyAfterAStartAndAStopKeepsTheRecipientOptedOut: source rows stored newest first, copied twice; failed with the ordering removed.
  • TestAnOptOutHereOnAnotherChannelDoesNotKeepOneOut, TestAnOptOutHereForAnotherRecipientDoesNotKeepOneOut, TestAnotherAppsOptOutHereDoesNotKeepOneOut: a destination opt-out on another channel, for another recipient or of another app does not keep the source's out; each failed with its predicate dropped from the skip.
  • TestAnImportedRowUnderTheIdOfAnOptOutHereLeavesItsRecipientOptedOut: an imported row under a destination opt-out's id, for another recipient or channel, leaves it live; failed with either match dropped from the conflict guard.
  • TestAnAppHoldingOnlyAnOptOutImportsWithoutFull (api): failed with a 409 without the HoldingTable skip.
  • TestAnImportedOptOutWithoutARecipientIsRefused: an imported opt-out with an empty recipient is refused; it was stored without the importedModels entry.

The first four fail without opt_outs in dataTables.

Fixes AI-3489

@thesyncim
thesyncim force-pushed the fix/ai-3489-opt-outs-move branch from 3386eb6 to 417783f Compare November 23, 2026 13:40
@thesyncim
thesyncim marked this pull request as ready for review November 23, 2026 13:41
@thesyncim thesyncim added data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review and removed data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review labels Nov 23, 2026
@thesyncim
thesyncim force-pushed the fix/ai-3489-opt-outs-move branch from 417783f to 99be289 Compare November 23, 2026 13:54
@thesyncim thesyncim added the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 23, 2026

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round-1 review at 99be289, merged onto main 19da294.

CI: all 13 checks pass. On the merged tree these suites pass: store DataMoveSuite (201), the opt-out tests in StoreSuite, api DataMoveSuite (36) and the cmd/router TestReplicate* suites (74).

[Question] R1.4 (watchDataChanges, datamove.go:1841): after a rollback, the change trigger stays attached to opt_outs. A source on the previous release then serves opt_outs changes. A destination on the previous release fails the whole change batch with "opt_outs" is not a table an export carries. An export from this release into a destination on the previous release fails the same way. This fails closed, and every earlier dataTables addition has the same property. Should the rollback runbook drop the trigger? Found by reading the code; not probed.

Checked and sound:

  • STOP→START→STOP replayed through the changes leaves the recipient opted out, also when the changes overlap a copy that already holds them.
  • A START from the source lifts a moved copy, and leaves an opt-out recorded here alone.
  • Another app's row under the same id is left alone.
  • order is read only by selectRows, and the SQL for every other table is unchanged.
  • The trigger is attached at startup, so no migration is needed.
  • Mutations: 12 checked, 5 survived. One survivor is equivalent; the other four are in R1.1.

Verdict: request changes, for R1.1 and R1.2 (Should fix).

Comment thread acceleration/internal/store/datamove.go Resolved
Comment thread acceleration/internal/store/datamove.go Resolved
Comment thread acceleration/internal/store/datamove.go Resolved
@thesyncim thesyncim removed the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 23, 2026
@thesyncim

Copy link
Copy Markdown
Member Author

On R1.4: I'd leave the runbook alone. As you say, it fails closed. The old release refuses the whole batch, so nothing is written wrong or lost; the move just can't run across mixed releases until both sides run this one. Every earlier dataTables addition behaves the same way, and nothing has shipped yet, so there are no cross-release moves to protect. If we later want moves to work across versions, that should be one rule for every carried table, not a step for this one.

I'm fixing R1.1 to R1.3 now and will reply on each thread once they're pushed.

@thesyncim
thesyncim force-pushed the fix/ai-3489-opt-outs-move branch from 99be289 to 89c0c34 Compare November 23, 2026 14:42
@thesyncim
thesyncim requested a review from kanat November 23, 2026 14:53
@thesyncim thesyncim added the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 23, 2026

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Delta round 2, head 89c0c34 (rebased onto main f03b4a2): NO-GO until R2.1 is fixed. R1.1 to R1.3 are fixed. One new Should fix in the new API test.

Prior findings:

  • R1.1 fixed. I reran M6 (revoked rows skipped), M8 (no channel), M9 (no recipient) and M10 (no customer) on this head. Each now fails one of the new or extended tests.
  • R1.2 fixed. HoldingTable skips opt_outs. Reverting the skip fails TestAnAppHoldingOnlyAnOptOutImportsWithoutFull with the 409. See R2.1 for that test.
  • R1.3 fixed. The conflict guard keeps recipient and channel. Removing either check, applying it only to live rows, or applying it only to revoked rows fails TestAnImportedRowUnderTheIdOfAnOptOutHereLeavesItsRecipientOptedOut.
  • R1.4 stays a ticket, not blocking.

10 mutations checked, 0 survived. TestDataMoveSuite (store and api) and TestReplicate pass. gofmt and vet are clean. CI at this head shows all green.

New finding: R2.1 inline. Everything else in the delta looks right.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Delta round 3: GO at cb3f59ff0

  • R2.1 fixed. TestAnAppHoldingOnlyAnOptOutImportsWithoutFull now imports into a fresh app, so no customer_activity row makes the app count as holding data.
    • It passed 30 of 30 runs (-count=30).
    • With the opt_outs skip in HoldingTable reverted (N1), it failed 5 of 5 runs, so the test pins the skip.
  • No other change. The other five commits are identical to the reviewed ones (git range-diff: all =); only the rebase onto main moved them.
  • R1.1–R1.3 were fixed in round 2. R1.4 (dropping the trigger after a rollback) becomes a ticket and does not block.
  • CI: go, integration (api) and integration (router) were still pending when I checked. Everything else passes.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving on the reviewer's GO (findings in the review comments above).

@thesyncim
thesyncim merged commit e6fed27 into main Nov 23, 2026
13 checks passed
@thesyncim
thesyncim deleted the fix/ai-3489-opt-outs-move branch November 23, 2026 17:41
thesyncim added a commit that referenced this pull request Nov 23, 2026
…copes' policies, and forget cached rows a move wrote (#2227)

**Problem**
`deleteRow` matched a replayed delete on every primary key column, the
customer column included, so for `users`, `scheduled_tasks`,
`customer_activity` and the app's `policies` a delete replayed under
another app id matched nothing. A policy change is recorded under its
scope id whatever its scope, so the change feed listed an organization's
policy sharing the app's id, and the replay wrote it as the app's.
`ImportRow` and `ApplyChanges` dropped no cached rows, so configs,
skills, voices and policies they wrote were served stale until the cache
TTL.

**Fix**
- `deleteRow` leaves the customer column out of the key conditions; the
destination's customer condition still scopes the delete.
- `Changes` lists a `policies` change only when its key's scope is
`app`, filtered before the limit so a page stays full and `caught_up`
holds. `applyChanges` replays only those too, for changes from a source
without the filter.
- `importRow` and `deleteRow` collect the cache keys of the row as it
reads after and before the write, so a rename drops the old name too.
Only a delete from a cached table returns its row. `ImportRow` forgets
the keys after its statement, `ApplyChanges` after its transaction
commits, using the existing key functions and `forget`.
- The second expiry check in `Changes` is left for a separate change.
- Conflicts with #2221 and #2201 near the end of `importRow`; either
merge order needs only a small rebase.

**Tests**
Each failed before its fix, except `TestAnAppsPolicyChangeIsReplayed`,
which guards the scope filter.
- `TestADeletedTaskIsReplayedUnderTheIDTheAppMovedTo`: a delete replayed
under another app id removes that task and keeps its sibling.
- `TestChangesCarryNoOrganizationsPolicyOfTheAppsID`: the feed leaves
the organization's policy out and still fills the page.
- `TestAnOrganizationsPolicyIsNotReplayedOverTheAppsOfTheSameID`: an
organization's policy change leaves the app's as it was.
- `TestAPolicyChangeNamingNoScopeIsNotReplayed`: a change with no scope
in its key is not replayed.
- `TestAnAppsPolicyChangeIsReplayed`: an app's own policy change still
lands.
- `TestAnImportedConfigIsServedRightAfterTheImport`: a cached missing
config is dropped by an import.
- `TestAConfigImportedWithANumericNameIsServedRightAfterTheImport`: the
same for an id sent as a number.
- `TestARouterConfigImportedIsServedRightAfterTheImport`: the same for a
router config.
- `TestAVoiceImportedIsServedRightAfterTheImport`: the same for a voice.
- `TestAnImportedPolicyIsServedRightAfterTheImport`: the same for an app
policy.
- `TestAnImportedPolicyDecidesTheAppsRequestsRightAfterTheImport`: the
app's merged policies are dropped too.
- `TestAReplayedConfigChangeIsServedRightAfterIt`: a replayed update is
served at once.
- `TestAReplayedRouterConfigChangeIsServedRightAfterIt`: the same for a
router config by id.
- `TestAReplayedVoiceChangeIsServedRightAfterIt`: the same for a voice
by id.
- `TestAReplayedSkillChangeIsServedRightAfterIt`: the same for a skill.
- `TestAConfigRenamedByAReplayIsNoLongerServedByItsOldName`: a replayed
rename drops the old name.
- `TestAConfigDeletedByAReplayIsNoLongerServed`: a replayed delete drops
the config.
- `TestAPolicyDeletedByAReplayIsNoLongerServed`: a replayed delete drops
the app's policy.
- `TestACursorPastAnOrganizationsPolicyLeftOutIsNotExpiredByItsPruning`:
a page that leaves out an organization's policy still moves the cursor
past it, so pruning that change does not expire the follower.
- `TestAFullPageOfChangesResumesAfterItsLastChange`: a full page ends at
its last change and skips nothing.
- `TestARunWhosePageCarriesNoChangeStillMovesItsCursor`: `follow` saves
the newer cursor from an empty page.
- `TestASkillMovedByAReplayLeavesItsOldConfig`: a skill moved to another
config is no longer served from the old one.
- `TestAVoiceRenamedByAReplayIsNoLongerServedByItsOldName`: a replayed
voice rename drops the old name.

Part of AI-3495
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review docs project-info

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants