Skip to content

fix(platform): reclaim a rejected upload only when nothing holds it - #4109

Merged
yannickmonney merged 3 commits into
mainfrom
fix/reject-blob-held-refs
Oct 3, 2026
Merged

yannickmonney merged 3 commits into
mainfrom
fix/reject-blob-held-refs

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

POST /api/app/files/reject-blob deleted the bytes of an upload that a task already listed as an attachment. The reclaim decided "never bound" by consuming the caller's open upload intent, and it checked file rows only. But the task door, the document multi-bind and the outbound-mail door bind without consuming: they stamp bound_at_ms instead (#3191). The reclaim also never asked #4085's taskHoldsBlobRef.

Now one holder rule decides "may these bytes go?" for every lane that deletes bytes outside the release seam. The reclaim claims the caller's intent in a single statement on its row, and only when nothing holds the blob.

Closes #4104

What changed

  • One holder rule: blobRefHeld (new backend/domains/files/blob-holders.ts). A ref is held when any of these names it:

    deleteFile and both abandoned-upload sweep statements now ask this rule instead of carrying their own copies. As a result, deleteFile now also keeps retained-version bytes, and the sweep now also keeps a ref a document holds. Both changes keep bytes; neither deletes anything new. The knowledge release seam (assessRefLiveness) keeps its own statement: it judges in the middle of a purge (live rows only, minus the row being purged), and the new module says so.

  • The reclaim claims atomically: claimRejectedUpload (upload-intents.ts). It is one DELETE … RETURNING on the caller's intent row. It succeeds only if the intent is the caller's, unconsumed, inside its TTL, not vouched for (bound_at_ms IS NULL) and not held (blobRefHeld). deleteRejectedUploadBlob asks nothing else, then deletes the key.

    • A bind racing the reclaim is safe. A bind still committing (a timed-out document create the UI follows with reject-blob, or the task door mid-transaction) holds the row's lock. The claim waits, then re-reads the stamp.
    • Delete, not consume. ownsUploadedBlob still accepts a consumed intent for the rest of its TTL, so on main a reclaimed ref can still be attached to a task. Deleting the row means it can't.
  • consumeUploadIntent's any-purpose form lost its only caller (the reclaim), so purpose is now required.

  • Response unchanged: still {deleted: boolean}. No migration, no new error code, no user-visible string.

Not chosen: consuming the intent at the task bind (acceptance check 3). ownsUploadedBlob deliberately doesn't consume: one upload can become one document per team, or a task plus a document, or a mail attachment. Consuming at one door would break those multi-binds. The claim-side rule covers every non-consuming lane (tasks, document multi-bind, outbound mail) at once.

Finding → commit → test

Finding (#4104) Commit Test
P2: files/upload → task attach → reject deleted the bytes 719ee861a (fix) checkRejectedUploadReclaim: "keeps the bytes of an upload a task lists"; upload-intents.test.ts › claimRejectedUpload
The reclaim ignored the holders (taskHoldsBlobRef, documents.file_ref) 719ee861a lane: "keeps the bytes a document or a task holds, stamped or not" (no stamp: file_ref, history_files, task outputs); blob-holders.test.ts
The reclaim ignored the vouch stamp (mail door, multi-bind) 719ee861a lane: "keeps the bytes a non-consuming bind vouched for" (firstForeignUpload, the mail door's proof)
A bind racing the reclaim (found while fixing) 719ee861a lane: "racing a bind that commits keeps the bytes": the reclaim is shown waiting on the bind's lock in pg_stat_activity
A reclaimed ref could still be attached (found while fixing) 719ee861a lane: "a reclaimed upload can no longer be attached to a task"
Controls C1 (unbound: deleted), C2 (foreign: refused), C3 (registered: refused), P1 (UI lane) d96d728bc (lane) lane: "reclaims the caller's unbound upload and refuses a foreign or registered one"; P1 inside the P2 check
The reclaim must decide nothing beside the claim 719ee861a service.reject-blob.test.ts
One shared rule for deleteFile and the sweep 719ee861a upload-intents.test.ts (sweep statements); checkFiles, checkTaskHeldBlobOutlivesFileRow, checkAbandonedUploadReclaim (real Postgres)

Test plan

These ran locally against real PostgreSQL 16.14 (pgvector 0.8.6, no pg_search) and the CLI's object-store pin: MinIO RELEASE.2025-04-22T22-12-26Z, pulled from ghcr.io/tale-project/ops/minio, binary sha256 53e2a2cb…bb817c matching the image's own minio.sha256sum. Each run got a fresh database pair and a fresh MinIO data directory. MinIO's audit webhook recorded every S3 call.

cd services/platform
DATABASE_URL=… KNOWLEDGE_DATABASE_URL=… TALE_CONFIG_DIR=… TALE_CONFIG_BUILTIN_DIR=… \
ITEST_S3_ENDPOINT=http://127.0.0.1:59487 ITEST_S3_ACCESS_KEY=… ITEST_S3_SECRET_KEY=… \
ITEST_LANES=checkRejectedUploadReclaim bun run backend:integration

Fails on main + the lane (8b916c0b9 + d96d728bc): 17/23

PASS  reject-blob reclaims the caller’s unbound upload and refuses a foreign or registered one — unbound=true/gone=true (want true/true), foreign=false/kept=true (want false/true), registered=false/kept=true (want false/true)
FAIL  reject-blob keeps the bytes of an upload a task lists (#4104) — registered+attached: attach=200 reject=false kept=true (want 200/false/true); upload+attached: attach=200 reject=true kept=false listed=true (want 200/false/true/true), intent bound=true,consumed=true
FAIL  reject-blob keeps the bytes a document or a task holds, stamped or not — document file_ref=true/kept=false, document history_files=true/kept=false, task outputs=true/kept=false (want false/true each)
FAIL  reject-blob keeps the bytes a non-consuming bind vouched for — proof=owned (want owned), reject=true/kept=false (want false/true), intent bound=true,consumed=true
FAIL  a reclaimed upload can no longer be attached to a task — reject=true (want true), intent bound=false,consumed=true, attach=200 (want 403)
FAIL  reject-blob racing a bind that commits keeps the bytes — reclaim waited on the bind=true (want true), bind=true (want true), reject=true/kept=false (want false/true)

Passes on the head (719ee861a): 22/23

PASS  reject-blob reclaims the caller’s unbound upload and refuses a foreign or registered one — unbound=true/gone=true (want true/true), foreign=false/kept=true (want false/true), registered=false/kept=true (want false/true)
PASS  reject-blob keeps the bytes of an upload a task lists (#4104) — registered+attached: attach=200 reject=false kept=true (want 200/false/true); upload+attached: attach=200 reject=false kept=true listed=true (want 200/false/true/true), intent bound=true,consumed=false
PASS  reject-blob keeps the bytes a document or a task holds, stamped or not — document file_ref=false/kept=true, document history_files=false/kept=true, task outputs=false/kept=true (want false/true each)
PASS  reject-blob keeps the bytes a non-consuming bind vouched for — proof=owned (want owned), reject=false/kept=true (want false/true), intent bound=true,consumed=false
PASS  a reclaimed upload can no longer be attached to a task — reject=true (want true), intent gone, attach=403 (want 403)
PASS  reject-blob racing a bind that commits keeps the bytes — reclaim waited on the bind=true (want true), bind=true (want true), reject=false/kept=true (want false/true)
  • What the store saw. MinIO's audit log records 19 DeleteObject calls on main and 13 on the head. The lane's own cleanup accounts for 11 in each run. So the backend's reclaim deleted 8 objects on main (C1 and the seven held or raced refs) and 2 on the head (C1 and the reclaimed-then-attached ref).
  • The neighbouring blob lanes. checkFiles, checkDocuments (which includes the existing "reject-blob reclaim (orphan deletes, bound refuses)" check), checkBlobRefAuthority, checkAbandonedUploadReclaim, checkTaskHeldBlobOutlivesFileRow, checkRejectedUploadReclaim and checkDocumentWriteGuards ran on fresh databases:
    • head: 74/76;
    • main + lane: 69/76, the 5 lane failures above plus the same two the head has.
  • The two failures both trees share are local only:
    • "knowledge BM25 indexes verified at boot": no pg_search in this local Postgres;
    • "agent upsert: … foreign project": this filtered set has no lane that creates another organization's project.
  • Unit tests. bunx vitest run over the 40 test files of the files domain and of every test that imports the changed modules: 535/535.
  • Other gates:
    • oxfmt --check and oxlint --type-aware on the changed files: clean;
    • tsc --noEmit over the changed files with integration-check.ts: clean;
    • tests/guards/integration-scope and integration-skips guards: pass;
    • bun run lint:manual, bun run knip:check: pass;
    • Opengrep (tools/opengrep/run.sh) on the changed sources: 0 findings.

CI at the exact head

  • Head f36e3adcb11b7cc44bce595a6df570ec21cc0e34: 50 pass, 12 skipped, 0 fail.
    • Backend integration job 111132304097: 1487/1487 checks across 218/218 lanes, including all six checkRejectedUploadReclaim checks.
  • f36e3adcb is an empty chore: commit. The first run at 719ee861a (run 37097165795) failed 1 of 1487 checks, an unrelated timing flake:
    • The check: checkRagWatchdogBatch's "a row lock held past the settle lock timeout defers that row alone" read locked=failed.
    • The cause: a second, concurrent rag reconcile tick (two failed 2 lines, 5 ms apart) failed the row as soon as the test released its lock.
    • Why it's unrelated: that lane runs before this change's lane and imports none of the changed modules. It passed on main's CI run 37085344227, locally on this head, and in the rerun.
    • Why an empty commit: the repository token can't rerun a job.

Checklist

  • bun run check: not run whole. The sandbox has a 4 GiB limit, so I ran the targeted gates above and leave the full set to CI.
  • bun run lint:sast: Opengrep on the changed sources, 0 findings.
  • Translations: N/A. No user-visible string changed, and the response shape is unchanged.
  • Docs: N/A. The endpoint is internal to the app and undocumented. The manual layer's reference/automation.md row for task-held blobs now names the reclaim, its tests and its lane.
  • READMEs: N/A.

Out of scope

A real-Postgres lane, checkRejectedUploadReclaim, drives
POST /api/app/files/reject-blob through the session doors against the
object-store pin and judges the bytes by HEAD: the caller's unbound
upload goes; a foreign or registered one stays (the controls); and an
upload a task lists (#4104's P2), one a document or a task holds without
a stamp, one a non-consuming bind vouched for, a reclaimed ref attached
afterwards and a bind racing the reclaim must all keep their bytes.

On main the last five fail: the reclaim deletes the bytes, and a
reclaimed ref can still be attached to a task.
POST /api/app/files/reject-blob proved "never bound" by consuming the
caller's open upload intent and asked about file rows alone. The task
door, the document multi-bind and the outbound-mail door bind without
consuming (they stamp bound_at_ms), so an upload attached to a task and
then rejected lost its bytes while the card kept listing it (#4104).

One holder rule now answers "may these bytes go?" for every lane that
deletes bytes outside the release seam: blobRefHeld (files/blob-holders.ts)
holds a ref named by a file row, a document's current file or retained
version, or a task. deleteFile and the abandoned-upload sweep ask it in
place of their own copies.

The reclaim claims the caller's intent in one statement on its row
(claimRejectedUpload): unconsumed, inside its TTL, not vouched for and
not held, or nothing goes. A bind still committing holds the row's lock,
so the claim waits and re-reads the stamp; the claim deletes the intent
rather than consuming it, so a reclaimed ref is no longer the caller's
to attach. consumeUploadIntent's any-purpose form lost its last caller
and now requires a purpose.

Closes #4104
Backend integration run 37097165795 failed one of 1487 checks:
checkRagWatchdogBatch's "a row lock held past the settle lock timeout
defers that row alone" read locked=failed because a second, concurrent
rag reconcile tick (two "failed 2" lines at 04:42:26.281 and .286) failed
the row the moment the test released its lock. The lane runs before this
change's lane, imports none of the changed modules, passed on main's CI
run 37085344227 and passes locally on this head. The token cannot rerun
a job, hence this empty commit.
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Independent review of PR #4109 at f36e3adcb11b7cc44bce595a6df570ec21cc0e34: accepted. It has the same tree as 719ee861a, and the head commit is the author's empty re-run. One pre-existing MEDIUM of the same class goes to a follow-up.

  • Reviewer: Tale agent update dashboard ui (#508) #2 3f9fdcee-79e7-430b-a430-ba179d5b5132, TALE-359 run 567b3cd8-ee73-4456-a2ad-7836fbfba1f3.
  • Implementation: agent optimize rag action #4 5f5307c9, run 4e44cabc. This run is distinct.
  • Method: my own runs on real PostgreSQL 16 with moto S3, plus one read-only review agent for holder completeness and races.

Local runtime evidence. Lanes checkFiles,checkRejectedUploadReclaim, a fresh database and a fresh moto store for every run:

Tree Result
8b916c0b9 + the regression commit d96d728bc 23/30. All 5 new holder checks fail as the issue describes: upload+attached … reject=true kept=false listed=true, intent bound=true,consumed=true. Document file_ref, history_files and task outputs are deleted. The vouched-for ref is deleted. A reclaimed ref then attaches (attach=200). The racing bind loses its bytes. C1–C3 pass.
head f36e3adcb 28/30: all 6 reclaim checks pass. The 2 failures are environmental: BM25 needs pg_search, and moto has a known checkFiles delete quirk.
M1: the claim ignores bound_at_ms 26/30: the vouched and race checks fail
M2: the claim ignores blobRefHeld 27/30: the held-without-stamp check fails, for all three holders
M3: blobRefHeld without history_files 27/30: only history_files fails
M4: the claim consumes the intent instead of deleting it 27/30: reclaimed, then attached → attach=200
  • Unit tests: the files-domain tests and tasks/blob-holders pass, 12 files and 95 tests.
  • CI at the exact head: 50 success, 11 skipped by design and Trivy neutral.
  • Composition with main 669219ce7 (fix(deps): clear the braces, OpenTelemetry core and colord advisories #4108, dependencies only): git merge-tree is clean, with no file overlap, and the merged manual reads 1082, matching its table.

Verified correct, from source (the review agent, cross-checked):

  • One statement. The claim is a single DELETE … RETURNING on the caller's own intent row. That row is unique by s3_ref, so the stamp and the claim lock the same row.
  • Every binder stamps or consumes before or with its holder write: the task door, document create and the API sync stamp inside their transactions; registration, skills and automations consume; the mail door stamps in a committed write before the message is written.
  • Bind first: the claim waits on the row lock, then refuses. The lane observes that wait.
  • Claim first: the bind is refused with 403.
  • Locks: no deadlock is possible, because the claim holds at most one row lock.
  • Delete order: the object is deleted only after the claim commits.
  • Refs and org: stored refs and the compared refs use the same s3:<key> form, and every arm is org-scoped.
  • The other lanes got only more cautious: deleteFile adds history_files, and the sweep adds documents.
  • Contract: the {deleted} response is unchanged.

MEDIUM (pre-existing on main, same class as #4104): product images keep their own copy of the holder check.

  • Where: products/service.ts:326-336 (releaseManagedImage) asks only about file rows and documents.file_ref, with no task and no history_files. The routes then delete the released refs once the update commits (products/routes.ts:273-276, :287-289).
  • Path, from source:
    1. A user uploads a product image, which creates a file row with uploaded_by set to them.
    2. They read its storageRef through GET /files/<id>.
    3. They attach it to a task. ownsUploadedBlob's file-row arm allows that.
    4. They replace the image. The bytes are deleted while the task still lists them.
    • I didn't check whether the UI can reach step 3.
  • Docstring: the new module's text, "ONE predicate for every lane that deletes bytes outside the release seam", isn't true while this copy exists.
  • Fix: use blobRefHeld there, with a lane case (product image → task attach → replace → bytes present).

LOW and INFO, all pre-existing unless noted:

  • LOW: blobRefHeld doesn't know outbound-mail or chat attachment refs. A registered file attached to a reply can be deleted with deleteFile inside the undo window, and the send then can't find its bytes. The reclaim itself is safe there, because the mail door stamps.
  • INFO: race safety rests on the stamp or consume on the intent row, not on blobRefHeld. Under READ COMMITTED, the holder subqueries keep the statement's snapshot.
    • The test comment at upload-intents.test.ts:305 ("holders … are read under the row's lock") overstates it. The source docstring is right.
    • A future door that writes a holder without touching the intent row would race. A guard test listing the binding doors would hold that line.
  • INFO: the lane leaves two conditions unexercised on their own: a file-row-only holder with an open, unstamped intent, and a consumed intent with no holder. Only the exact-SQL unit assertions pin them.
  • INFO: an S3 delete that fails after the claim orphans the bytes with only a console.warn. That was effectively true on main too.
  • INFO: the task arm scans the organization's tasks with no GIN index. That has been so since fix(platform): keep task attachment bytes alive and name a gone one #4085.
  • INFO: a mail send refused after its stamp leaves the upload un-reclaimable.

Verdict: accepted. #4104's acceptance checks 1 and 2 are met and pinned. Check 3, the optional consume at the task bind, was rightly not taken. The product-image MEDIUM goes to the manager as a follow-up.

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.

bug(platform): /files/reject-blob deletes the bytes of an upload a task already attached

1 participant