Skip to content

fix(messaging): sync the transactional workflow on asset url rewrites - #118965

Closed
Silthus wants to merge 4 commits into
PostHog:masterfrom
Silthus:fix/rewrite-email-asset-url-sync
Closed

Silthus wants to merge 4 commits into
PostHog:masterfrom
Silthus:fix/rewrite-email-asset-url-sync

Conversation

@Silthus

@Silthus Silthus commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

  • When an operator moves an email asset with rewrite_email_asset_url, a transactional template's sends can keep the old asset URL.
  • Sends use the workflow's copy of the template content. The command rewrote the template row and never rebuilt that copy, so the template and its workflow disagreed.
  • This bites when a run is narrowed to --targets message_templates or --id <template id>. A default run also rewrites hog_flows in place, so sends get fixed there, but the workflow gets no new version.
  • Found in the ship review of feat(messaging): upload, send and lock code-managed email templates #118924 (Silthus/posthog#498). The gap predates that PR.

Merge Danger

Warning

🟡 Merge risk: medium · two-way door (merge-risk rubric)

  • The API template update and the design patch now save through the new shared path. That path holds the template row lock and runs the workflow-editor check. A mistake there would affect every template save, and the editor check guards transactional workflows.
  • Limited by: the existing transactional email, template API and design operation tests all pass on the new path, and a revert restores the old inline blocks. The rewrite command writes workflow versions only when an operator runs it.

Changes

  • An asset URL rewrite of a transactional template now writes a new version of its workflow with the rewritten content, in the same transaction as the template save.
  • All three template content writers share one path, services/template_saves.py: the API update, the design patch, and the rewrite command. It locks the template row, checks the workflow-editor permission, checks the template can still send, saves, and syncs the workflow.
  • The rewrite command has no user, so it passes no workflow-editor check. A template that can no longer send (a marketing category, no subject or body, no sender at all) now fails its row and keeps the old URL, instead of drifting from its workflow. A workflow keeps its existing sender, as an API save does. The command already reports a failed row and exits non-zero.
  • The design patch now runs the same "can it still send" check as the update. Before, it skipped that check.
  • The rewrite now also covers a template's unpublished draft (draft_content, added in feat(workflows): draft and publish edits to transactional email templates #118664). Before, a draft saved before a rewrite kept the old URL, and publishing it brought that URL back.
  • A draft-only rewrite saves the draft alone and writes no workflow version, because sends never read the draft.
  • Mechanical: the view's inline lock-save-sync blocks, lock_transactional_workflow_id, check_transactional_update, update_template and edit_content_locked are gone. Nothing else called them.

Rebase note

#118924 moves this same orchestration into services/template_saves.py (save_template / save_locked_template), with a write intent and a content hash. This PR uses the same file, names and signatures, minus those two parameters, so whichever lands second resolves the conflict to #118924's version of the shared functions.

How did you test this code?

Test rationale: one case in TestTransactionalEmail (products/messaging/backend/tests/api/test_transactional_email.py) catches a rewrite that saves the template without syncing its workflow. The closest existing test, test_saving_a_transactional_template_writes_a_new_workflow_version, covers the API update only. The rewrite command tests in products/workflows/backend/test/test_rewrite_email_asset_url.py never make a template transactional.

  • Test seams: rewrite_template_content, the messaging facade function the command calls for message templates, and the command itself for drafts.
  • A second case in TestRewriteEmailAssetUrl (products/workflows/backend/test/test_rewrite_email_asset_url.py) catches a rewrite that skips a template whose draft alone holds the URL. Red before the fix: the command matched 0 rows.
  • Red then green: on the test commit with base code, the workflow stayed at version 1 (assert 1 == 2). At HEAD, the transactional email, template API, design operation and rewrite command test files all pass. They pass again after the rebase onto feat(workflows): draft and publish edits to transactional email templates #118664.
  • Live run: the real management command against a local database, on rows written by base code and by HEAD (below).

Live run

rewrite_email_asset_url on a local database, each template made transactional first. URLs are https://assets.example.com/{old,new}.png.

Code Run Template Workflow
base --id A new URL version 1, old URL
HEAD --id B, row written by base code new URL version 2, new URL
HEAD --targets message_templates, row written by HEAD new URL version 2, new URL
HEAD same run again unchanged still version 2 (no-op)

A second run covers a transactional template whose draft alone holds https://assets.example.com/draft-old.png.

Code Run Draft Workflow
before the draft fix --id A 0 rows matched, old URL version 1
HEAD --id B new URL version 1
HEAD same run again 0 rows matched (no-op) version 1

Not run: the plugin server, so the worker reload after commit was not observed. It is scheduled with on_commit, as the API save already does.

Proofs (per qa-swarm PROOF.md)

Proof Status
Live run Done. Tables above.
Prod shape Accepted gap: read-only production access is not available until 2026-11-02. Unknown: the share of stored transactional templates whose category or content would now fail the rewrite.
Cross-version Not owed. No stored or queued format changes.
Shadow diff Not owed. The grade is not high, and the change is meant to alter behavior.

Release status

  • No feature flag controls this change

Docs update

None. The command's behavior is documented in its own help text, which is unchanged.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Agent: Claude Code, Claude Opus 5.5 (claude-opus-5-5)

Decisions

Question Answer Who Where
Route the rewrite through the update's save path, or add a sync call to the rewrite? Extract the shared save path, so there is only one sync path 👤 Human Side-fix brief from the conductor thread
Which shape for the extracted function? #118924's template_saves.py names and signatures, so the rebase is a straight conflict resolution 🤖 Agent This PR
Rewrite template drafts here or in a separate PR? Here 👤 Human This thread, after the rebase onto #118664
What does the rewrite do when a transactional template can't send any more? Fail that row and keep the old URL, rather than save a template its workflow can't match 🤖 Agent This PR

Involvement: no grilling rounds, no prototypes. Review: one GPT-6.1 Sol review (high effort) on c90e0ca, no findings; one note on sender reuse corrected this description. Final GPT-6.1 Sol reads (high effort) of the rebase onto #118664 and of the draft rewrite found nothing. Proof: live run captured.

🤖 Generated with Claude Code

@trunk-io

trunk-io Bot commented Oct 17, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

Silthus and others added 2 commits October 20, 2026 03:18
…ts workflow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rite changes a template

rewrite_email_asset_url saved message template content directly, so a
transactional template's workflow kept its old copy. Template saves from
the API, the design patch and the rewrite now share one locked
save-and-sync path in services/template_saves.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Silthus
Silthus force-pushed the fix/rewrite-email-asset-url-sync branch from c90e0ca to 9eee7cf Compare October 20, 2026 03:27
Silthus and others added 2 commits October 20, 2026 03:35
…d draft

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A draft saved before a rewrite kept the old URL, and publishing it brought
that URL back. The rewrite now finds and rewrites draft_content too. A
draft-only rewrite saves the draft alone and writes no workflow version,
because sends never read the draft.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant