Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions services/platform/backend/domains/files/blob-holders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// @vitest-environment node

import type { Fragment, Sql } from 'postgres';
import { describe, expect, it } from 'vitest';

import { blobRefHeld } from './blob-holders.ts';

/**
* The one answer to "may these bytes go?" for every lane that deletes bytes
* outside the release seam, as the statement states it: a file row in any
* lifecycle, a document's current file or retained version, or a task's
* attachment or deliverable — each scoped to the organization.
*/

interface Rendered {
text: string;
values: unknown[];
}

const RENDERED = Symbol('rendered');

function isRendered(value: unknown): value is Rendered & { [RENDERED]: true } {
return (
typeof value === 'object' &&
value !== null &&
(value as { [RENDERED]?: true })[RENDERED] === true
);
}

/** A tag double that renders nested fragments the way postgres.js does. */
function tag(): Sql {
const render = (strings: TemplateStringsArray, ...values: unknown[]) => {
let text = '';
const flat: unknown[] = [];
strings.forEach((part, index) => {
text += part;
if (index >= values.length) return;
const value = values[index];
if (isRendered(value)) {
text += value.text;
flat.push(...value.values);
} else {
text += '?';
flat.push(value);
}
});
return {
[RENDERED]: true,
text: text.replace(/\s+/g, ' ').trim(),
values: flat,
};
};
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- test double for the postgres.js tag
return render as unknown as Sql;
}

function rendered(fragment: Fragment): Rendered {
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the double answers rendered text, not a query
return fragment as unknown as Rendered;
}

describe('blobRefHeld', () => {
it('holds the bytes for a file row, a document (current or retained) or a task of the org', () => {
const sql = tag();
const held = rendered(blobRefHeld(sql, 'org_1', sql`i.s3_ref`));
expect(held.text).toBe(
"(EXISTS ( SELECT 1 FROM app.file_metadata held_file WHERE held_file.org_id = ? AND held_file.storage_ref = i.s3_ref ) OR EXISTS ( SELECT 1 FROM app.documents held_doc WHERE held_doc.org_id = ? AND (held_doc.file_ref = i.s3_ref OR held_doc.history_files @> ARRAY[i.s3_ref::text]) ) OR EXISTS ( SELECT 1 FROM app.tasks held WHERE held.org_id = ? AND (coalesce(held.attachments, '[]'::jsonb) || coalesce(held.outputs, '[]'::jsonb)) @> jsonb_build_array(jsonb_build_object('fileId', i.s3_ref::text)) ))",
);
expect(held.values).toEqual(['org_1', 'org_1', 'org_1']);
});

it('takes the ref as a parameter, bound once per arm', () => {
const sql = tag();
const ref = 's3:blobs/acme/brief.pdf';
const held = rendered(blobRefHeld(sql, 'org_1', sql`${ref}`));
expect(held.text).toContain('held_file.storage_ref = ?');
expect(held.text).toContain(
'held_doc.file_ref = ? OR held_doc.history_files @> ARRAY[?::text]',
);
expect(held.text).toContain("jsonb_build_object('fileId', ?::text)");
expect(held.values).toEqual([
'org_1',
ref,
'org_1',
ref,
ref,
'org_1',
ref,
]);
});
});
44 changes: 44 additions & 0 deletions services/platform/backend/domains/files/blob-holders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Fragment, Sql, TransactionSql } from 'postgres';

import { taskHoldsBlobRef } from '../tasks/blob-holders.ts';

/**
* May the BYTES of a blob ref go? Not while some row of the organization
* still holds the ref:
*
* - a file row (`app.file_metadata.storage_ref`), whatever its lifecycle —
* a trashed row is restorable, and its purge goes through the release
* seam, which judges the bytes again then;
* - a document, as its current file (`file_ref`) or as a retained version
* (`history_files` — controlled-record snapshots need their bytes);
* - a task, as an attachment or a deliverable (`tasks/blob-holders.ts`).
*
* ONE predicate for every lane that deletes bytes outside the release seam:
* `deleteFile` once its own row is gone, the abandoned-upload sweep and the
* rejected-upload reclaim (`upload-intents.ts`). Each lane used to carry its
* own copy, and a copy that missed a holder deleted that holder's bytes: the
* reclaim asked about file rows alone and took a task's attachment
* (2026-10-03, #4104). A new holder joins here, once, for all of them. The
* release seam (`knowledge/liveness.ts`) asks a sibling question in the
* middle of a purge — live rows only, minus the document or file row being
* purged — and keeps its own statement for that reason.
*
* `ref` is a fragment, as in `taskHoldsBlobRef`: a parameter
* (``sql`${ref}` ``) or a column of the caller's query (``sql`i.s3_ref` ``).
*/
export function blobRefHeld(
sql: Sql | TransactionSql,
organizationId: string,
ref: Fragment,
): Fragment {
return sql`(EXISTS (
SELECT 1 FROM app.file_metadata held_file
WHERE held_file.org_id = ${organizationId}
AND held_file.storage_ref = ${ref}
) OR EXISTS (
SELECT 1 FROM app.documents held_doc
WHERE held_doc.org_id = ${organizationId}
AND (held_doc.file_ref = ${ref}
OR held_doc.history_files @> ARRAY[${ref}::text])
) OR ${taskHoldsBlobRef(sql, organizationId, ref)})`;
}
Loading