Skip to content

fix(policies): delete detached PDF objects when regenerating a draft#3474

Merged
tofikwest merged 3 commits into
mainfrom
tofik/policy-regen-s3-cleanup
Jul 22, 2026
Merged

fix(policies): delete detached PDF objects when regenerating a draft#3474
tofikwest merged 3 commits into
mainfrom
tofik/policy-regen-s3-cleanup

Conversation

@github-actions

@github-actions github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Fixes the last open finding on the production deploy PR (#3463): regenerating an uploaded draft policy (#3471's draft path) cleared its pdfUrl references but never deleted the objects from S3, orphaning one document per regeneration.

What changed

  • The draft-regeneration path in update-policy-helpers.ts now captures the keys it is about to detach (policy-level + current-version pdfUrl, deduplicated) and deletes them from S3 after the database transaction commits — a failed delete can never roll back the content update.
  • Deletion is best-effort per key: failures are logged with the key (so orphans can be cleaned up later) and never fail the regeneration, matching the policies controller's existing delete semantics. When APP_AWS_BUCKET_NAME isn't configured, cleanup is skipped with a warning.
  • Uses a static @aws-sdk/client-s3 import, the same pattern as the other tested Trigger.dev tasks (evidence export).

Verification

  • 9/9 src/trigger/policies jest tests green — 5 new: deletes detached keys after commit, deduplicates a shared key, S3 failure doesn't fail the regeneration, missing bucket skips with a warning, editor-mode drafts never touch S3.
  • No type errors in the module.

Closes the update-policy-helpers.ts thread on #3463.


Summary by cubic

Deletes detached uploaded PDF files from S3 when regenerating a draft policy to prevent orphaned objects. Captures keys under a row lock and uses APP_AWS_* config; adds tests and keeps regeneration resilient to S3 errors.

  • Bug Fixes
    • Best-effort delete after the DB commit; failures are logged and never fail regeneration.
    • Captures detached keys inside the transaction under a row lock to avoid races with concurrent uploads.
    • Deduplicates policy-level and version pdfUrl keys before deleting.
    • Uses explicit APP_AWS_* S3 config (region, credentials, optional endpoint); skips cleanup if APP_AWS_BUCKET_NAME isn't set.
    • Adds tests with mocked @aws-sdk/client-s3 covering capture, dedupe, failure, skip, and editor-only drafts.

Written for commit 5c97145. Summary will update on new commits.

Review in cubic

Regenerating an uploaded draft cleared its PDF references but left the
objects in S3, orphaning one per regeneration. The detached keys
(policy + current version, deduplicated) are now removed best-effort
after the database update commits; failures are logged with their keys
instead of failing the regeneration.
@tofikwest tofikwest changed the title [dev] [tofikwest] tofik/policy-regen-s3-cleanup fix(policies): delete detached PDF objects when regenerating a draft Jul 22, 2026
@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
comp-framework-editor Ready Ready Preview, Comment Jul 22, 2026 2:58am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
app Skipped Skipped Jul 22, 2026 2:58am
portal Skipped Skipped Jul 22, 2026 2:58am

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Confidence score: 5/5

  • Safe to merge after the addressed issues were fixed.

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/trigger/policies/update-policy-helpers.ts Outdated
Comment thread apps/api/src/trigger/policies/update-policy-helpers.ts Outdated
…PP_AWS config

The cleanup client now mirrors the other trigger tasks' APP_AWS_*
configuration (region, credentials, optional endpoint) instead of the
default chain, and the detached keys are captured inside the
transaction after a SELECT ... FOR UPDATE on the policy row — a
concurrently uploaded PDF can no longer slip past the cleanup as an
untracked orphan.
@vercel
vercel Bot temporarily deployed to Preview – portal July 22, 2026 02:56 Inactive
@vercel
vercel Bot temporarily deployed to Preview – app July 22, 2026 02:56 Inactive
@tofikwest
tofikwest merged commit ecd1bd0 into main Jul 22, 2026
9 checks passed
@tofikwest
tofikwest deleted the tofik/policy-regen-s3-cleanup branch July 22, 2026 03:01
claudfuen pushed a commit that referenced this pull request Jul 22, 2026
# [3.106.0](v3.105.0...v3.106.0) (2026-07-22)

### Bug Fixes

* **auth:** attribute API-key mutations to the key's creator, not the org owner ([#3472](#3472)) ([206ed96](206ed96)), closes [hi#risk](https://github.com/hi/issues/risk)
* **deps:** bump adm-zip 0.5.18 -> 0.6.0 in apps/api (Dependabot [#88](https://github.com/trycompai/comp/issues/88)/[#89](https://github.com/trycompai/comp/issues/89)) ([#3462](#3462)) ([300f2a1](300f2a1)), closes [#3451](#3451)
* **deps:** override tar to ^7.5.19 to clear node-tar Dependabot alerts ([#94](https://github.com/trycompai/comp/issues/94)-[#104](https://github.com/trycompai/comp/issues/104)) ([#3466](#3466)) ([8ab5709](8ab5709))
* **deps:** patch engine.io ([#93](#93)) and body-parser ([#92](#92)) Dependabot alerts ([#3464](#3464)) ([94c33b1](94c33b1))
* **isms:** harden internal-audit validation and edge cases from deploy review ([#3473](#3473)) ([c6c7379](c6c7379))
* **policies:** create draft version on policy regenerate instead of overwriting published ([#3471](#3471)) ([ff31dbd](ff31dbd))
* **policies:** delete detached PDF objects when regenerating a draft ([#3474](#3474)) ([ecd1bd0](ecd1bd0))
* **policies:** rename CreateVersionDto to avoid swagger collision with automations ([#3469](#3469)) ([2d5290a](2d5290a))

### Features

* **isms:** internal audit programme, plan and report — clause 9.2 (CS-724) ([#3468](#3468)) ([42e5ebd](42e5ebd)), closes [hi#impact](https://github.com/hi/issues/impact)
@claudfuen

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 3.106.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants