@elytracms/studio 0.0.97 → 0.0.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/convex/assets.ts +16 -2
  2. package/convex/ingest-attempt.test.ts +161 -0
  3. package/convex/ingest.test.ts +185 -9
  4. package/convex/ingest.ts +136 -24
  5. package/convex/prototype-key.test.ts +92 -0
  6. package/convex/records-zod-refinements.convex.test.ts +94 -0
  7. package/dist/components/cms/cms-store.d.ts +2 -4
  8. package/dist/components/cms/cms-store.js +4 -22
  9. package/dist/components/cms/cms-store.js.map +1 -1
  10. package/dist/components/cms/field-control.d.ts +12 -1
  11. package/dist/components/cms/field-control.js +16 -1
  12. package/dist/components/cms/field-control.js.map +1 -1
  13. package/dist/components/cms/import-batch.d.ts +23 -0
  14. package/dist/components/cms/import-batch.js +43 -0
  15. package/dist/components/cms/import-batch.js.map +1 -0
  16. package/dist/components/cms/rich-text/editor.js +13 -10
  17. package/dist/components/cms/rich-text/editor.js.map +1 -1
  18. package/dist/components/cms/rich-text/value.d.ts +9 -0
  19. package/dist/components/cms/rich-text/value.js +13 -0
  20. package/dist/components/cms/rich-text/value.js.map +1 -1
  21. package/dist/components/cms/use-document-draft.d.ts +35 -2
  22. package/dist/components/cms/use-document-draft.js +42 -15
  23. package/dist/components/cms/use-document-draft.js.map +1 -1
  24. package/dist/components/records/record-lens.js.map +1 -1
  25. package/dist/lib/engine-version.d.ts +1 -1
  26. package/dist/lib/engine-version.js +1 -1
  27. package/dist/lib/engine-version.js.map +1 -1
  28. package/dist/lib/i18n/messages.de.js +3 -0
  29. package/dist/lib/i18n/messages.de.js.map +1 -1
  30. package/dist/lib/i18n/messages.en.d.ts +3 -0
  31. package/dist/lib/i18n/messages.en.js +3 -0
  32. package/dist/lib/i18n/messages.en.js.map +1 -1
  33. package/dist/lib/operations/cms-persistence.d.ts +9 -4
  34. package/dist/lib/operations/cms-persistence.js +132 -29
  35. package/dist/lib/operations/cms-persistence.js.map +1 -1
  36. package/dist/lib/persona/labels.js +1 -0
  37. package/dist/lib/persona/labels.js.map +1 -1
  38. package/package.json +4 -3
package/convex/assets.ts CHANGED
@@ -5,6 +5,7 @@ import { liftLegacyAlt as legacyAltLift, rejectedWriteMessage, resolveAssetWrite
5
5
  import type { DataModel } from './functions'
6
6
  import { internalMutation, mutation, query } from './functions'
7
7
  import { requireCapability } from './guard'
8
+ import { ingestBindingHolds } from './ingest'
8
9
 
9
10
  /**
10
11
  * Asset metadata persistence (EC-044) + byte storage (EC-151). Bytes live in
@@ -210,15 +211,28 @@ export const getServeUrl = query({
210
211
  },
211
212
  })
212
213
 
213
- /** Delete stored bytes. Metadata removal is a separate `assets:remove` call. */
214
+ /**
215
+ * Delete stored bytes. Metadata removal is a separate `assets:remove` call.
216
+ *
217
+ * EC-685 — unless an ingest binding holds them: an editor may create an asset over a form
218
+ * submission's uploaded file (`upsert` allows it — promoting an upload is a legitimate edit),
219
+ * and deleting that asset must not take the submission's file with it. Such bytes are KEPT,
220
+ * read in this transaction, and the call returns normally (`{ deleted: false, heldBy:
221
+ * 'ingest' }`): the studio deletes an asset's bytes BEFORE its row (`deleteAsset`), so a
222
+ * refusal would leave the asset undeletable, while keeping them is the correct ownership —
223
+ * the asset row goes, the submission keeps its file, and removing the submission deletes it
224
+ * (`releaseIngestUploads`, the mirror check).
225
+ */
214
226
  export const deleteBlob = mutation({
215
227
  args: { storageKey: v.string() },
216
- handler: async (ctx, { storageKey }) => {
228
+ handler: async (ctx, { storageKey }): Promise<{ deleted: true } | { deleted: false; heldBy: 'ingest' }> => {
217
229
  // EC-150 / EC-304: destructive — gate at `editor`, matching `assets:remove`.
218
230
  // Under the old `requireUserId` an authenticated non-member could delete the
219
231
  // bytes of any published asset (storageKeys are harvestable from the public
220
232
  // `delivery:snapshot`), knocking every image off the live site.
221
233
  await requireCapability(ctx, 'content.write')
234
+ if (await ingestBindingHolds(ctx, storageKey)) return { deleted: false, heldBy: 'ingest' }
222
235
  await ctx.storage.delete(asStorageId(storageKey))
236
+ return { deleted: true }
223
237
  },
224
238
  })
@@ -0,0 +1,161 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { convexTest } from 'convex-test'
3
+ import { makeFunctionReference } from 'convex/server'
4
+ import { v } from 'convex/values'
5
+ import { z } from 'zod'
6
+ import { INGEST_RECEIVER_PATH, signedIngestRequest } from '@elytracms/core/ingest'
7
+ import type { IngestHandlerResult } from '@elytracms/core/ingest'
8
+ import { defineRecordKind } from '@elytracms/core/persistence'
9
+ import schema from './schema'
10
+ import { internalMutation } from './functions'
11
+ import { defineStudioHttp } from './http'
12
+ import { INGEST_HANDLER_ATTEMPTS, ingestHandlerArgs, parseIngestPayload } from './ingest'
13
+ import type { IngestHandlerRef } from './ingest'
14
+ import { saveRecord } from './record-functions'
15
+
16
+ /**
17
+ * EC-618 (the ratified low-volume lever, the critic's correction 1) — the receiver RETRIES a
18
+ * handler whose mutation THREW, with an additive, optional `attempt` argument. A Convex mutation
19
+ * cannot observe its own optimistic-concurrency conflict: Convex re-executes it, and exhaustion
20
+ * surfaces only to the caller — the receiver's `ctx.runMutation` (measured on the dev deployment
21
+ * in wave-0095: every receiver 500 of the burst was an `analytics:ingest` execution that failed
22
+ * after its retries). So the receiver is where a handler gets its next attempt; analytics uses
23
+ * it to open an overflow shard, every other handler ignores it.
24
+ *
25
+ * convex-test SERIALISES mutations, so no write conflict can happen here: the failure is
26
+ * INJECTED (a handler that writes, then throws on its early attempts). What this proves is the
27
+ * receiver's contract — attempt 1 carries no `attempt` key, a throw is retried with `attempt: n`,
28
+ * a thrown attempt committed nothing, a refusal is final, the attempts are bounded — never
29
+ * Convex's OCC (that is the dev-deployment burst, `pnpm dev:ingest rosy-armadillo-302 ec618-burst`).
30
+ */
31
+
32
+ const SECRET = 'ec618-attempt-secret'
33
+ const env = () => (globalThis as { process: { env: Record<string, string | undefined> } }).process.env
34
+
35
+ const hitKind = defineRecordKind({
36
+ kind: 'testHit',
37
+ module: 'testAttempt',
38
+ keys: ['label'],
39
+ fields: { label: z.string().min(1), attempt: z.number().int().positive() },
40
+ })
41
+
42
+ /** Every execution the handlers saw, with the `attempt` they were given (`undefined` = the key was absent). */
43
+ let seen: { handler: string; attempt: number | undefined; keys: string[] }[] = []
44
+ /** The flaky handler throws on every attempt below this one (after writing — the write must not commit). */
45
+ let succeedFrom = 1
46
+
47
+ async function writeHit(ctx: Parameters<typeof saveRecord>[0], label: string, attempt: number, receivedAt: string) {
48
+ const id = `hit_${(await ctx.db.query('records').collect()).length + 1}`
49
+ await saveRecord(ctx, hitKind, id, { label, attempt }, { createdAt: receivedAt, updatedAt: receivedAt })
50
+ return id
51
+ }
52
+
53
+ const testAttemptModule = {
54
+ /** Writes its hit, then throws while `attempt < succeedFrom` — an exhausted write conflict, injected. */
55
+ flaky: internalMutation({
56
+ args: ingestHandlerArgs,
57
+ handler: async (ctx, args): Promise<IngestHandlerResult> => {
58
+ seen.push({ handler: 'flaky', attempt: args.attempt, keys: Object.keys(args).sort() })
59
+ const payload = parseIngestPayload(args.payload)
60
+ const attempt = args.attempt ?? 1
61
+ const id = await writeHit(ctx, payload.kind === 'event' ? String(payload.data['name']) : payload.kind, attempt, args.receivedAt)
62
+ if (attempt < succeedFrom) throw new Error(`injected: the write conflicted on every retry (attempt ${attempt})`)
63
+ return { ok: true, id }
64
+ },
65
+ }),
66
+ /** A handler declared with the PRE-attempt arguments (strict: an extra key would be refused). */
67
+ legacy: internalMutation({
68
+ args: { payload: v.any(), receivedAt: v.string() },
69
+ handler: async (ctx, args): Promise<IngestHandlerResult> => {
70
+ seen.push({ handler: 'legacy', attempt: undefined, keys: Object.keys(args).sort() })
71
+ return { ok: true, id: await writeHit(ctx, 'legacy', 1, args.receivedAt) }
72
+ },
73
+ }),
74
+ /** A refusal is an ANSWER, not a failure: never retried. */
75
+ refusing: internalMutation({
76
+ args: ingestHandlerArgs,
77
+ handler: async (_ctx, args): Promise<IngestHandlerResult> => {
78
+ seen.push({ handler: 'refusing', attempt: args.attempt, keys: Object.keys(args).sort() })
79
+ return { ok: false, status: 409, message: 'Refused on purpose.' }
80
+ },
81
+ }),
82
+ }
83
+
84
+ const ref = (name: string) => makeFunctionReference<'mutation'>(`testAttempt:${name}`) as unknown as IngestHandlerRef
85
+ const silentAuth = { addHttpRoutes: () => {} }
86
+
87
+ function backend(event: IngestHandlerRef) {
88
+ return convexTest(schema, {
89
+ ...import.meta.glob(['./**/*.ts', '!./**/*.test.ts', '!./_generated/**']),
90
+ './_generated/api.ts': async () => ({}),
91
+ './testAttempt.ts': async () => testAttemptModule,
92
+ './http.ts': async () => ({ default: defineStudioHttp({ auth: silentAuth, ingest: { event } }) }),
93
+ })
94
+ }
95
+
96
+ async function post(t: ReturnType<typeof backend>, name = 'pageview') {
97
+ const signed = await signedIngestRequest({ kind: 'event', data: { name } }, SECRET)
98
+ return t.fetch(INGEST_RECEIVER_PATH, { method: 'POST', headers: signed.headers, body: signed.body })
99
+ }
100
+
101
+ async function hits(t: ReturnType<typeof backend>) {
102
+ return t.run(async (ctx) =>
103
+ (await ctx.db.query('records').collect()).filter((row) => row.kind === hitKind.kind).map((row) => row.data as { label: string; attempt: number }),
104
+ )
105
+ }
106
+
107
+ beforeEach(() => {
108
+ vi.useFakeTimers()
109
+ vi.setSystemTime(new Date('2026-10-28T12:00:00.000Z'))
110
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
111
+ seen = []
112
+ succeedFrom = 1
113
+ })
114
+ afterEach(() => {
115
+ vi.useRealTimers()
116
+ delete env()['ELYTRA_INGEST_SECRET']
117
+ })
118
+
119
+ describe('the receiver retries a handler that THREW, with an additive `attempt` (EC-618)', () => {
120
+ it('attempt 1 carries no `attempt` key — a handler declared with the pre-attempt arguments runs unchanged', async () => {
121
+ const t = backend(ref('legacy'))
122
+ expect((await post(t)).status).toBe(200)
123
+ expect(seen).toEqual([{ handler: 'legacy', attempt: undefined, keys: ['payload', 'receivedAt'] }])
124
+ expect(await hits(t)).toEqual([{ label: 'legacy', attempt: 1 }])
125
+ })
126
+
127
+ it('a throw is retried with `attempt: 2`; the thrown attempt committed nothing (no double count)', async () => {
128
+ succeedFrom = 2
129
+ const t = backend(ref('flaky'))
130
+ const response = await post(t)
131
+ expect(response.status).toBe(200)
132
+ expect(seen.map((entry) => entry.attempt)).toEqual([undefined, 2])
133
+ expect(seen[1]!.keys).toEqual(['attempt', 'payload', 'receivedAt'])
134
+ // Attempt 1 wrote its hit and threw: rolled back. Only attempt 2's write is stored.
135
+ expect(await hits(t)).toEqual([{ label: 'pageview', attempt: 2 }])
136
+ })
137
+
138
+ it('the attempts are bounded: a handler that throws every time is run INGEST_HANDLER_ATTEMPTS times, then the failure surfaces', async () => {
139
+ succeedFrom = Number.POSITIVE_INFINITY
140
+ const t = backend(ref('flaky'))
141
+ // convex-test rethrows a throwing httpAction to the caller; a deployment answers 500.
142
+ await expect(post(t)).rejects.toThrow(/injected: the write conflicted on every retry \(attempt 3\)/)
143
+ expect(INGEST_HANDLER_ATTEMPTS).toBe(3)
144
+ expect(seen.map((entry) => entry.attempt)).toEqual([undefined, 2, 3])
145
+ expect(await hits(t)).toEqual([]) // nothing committed by any of them
146
+ })
147
+
148
+ it('a refusal is final — relayed as it was, never retried', async () => {
149
+ const t = backend(ref('refusing'))
150
+ const response = await post(t)
151
+ expect(response.status).toBe(409)
152
+ expect(seen).toHaveLength(1)
153
+ })
154
+
155
+ it('control — without a failure every request runs its handler exactly once (the instrument can count one)', async () => {
156
+ const t = backend(ref('flaky'))
157
+ for (let i = 0; i < 5; i += 1) expect((await post(t, `e${i}`)).status).toBe(200)
158
+ expect(seen.map((entry) => entry.attempt)).toEqual([undefined, undefined, undefined, undefined, undefined])
159
+ expect((await hits(t)).map((hit) => hit.attempt)).toEqual([1, 1, 1, 1, 1])
160
+ })
161
+ })
@@ -6,6 +6,7 @@ import {
6
6
  INGEST_MAX_BODY_BYTES,
7
7
  INGEST_RECEIVER_PATH,
8
8
  INGEST_SIGNATURE_HEADER,
9
+ ingestUploadSha256,
9
10
  signedIngestRequest,
10
11
  } from '@elytracms/core/ingest'
11
12
  import type { IngestHandlerResult, IngestPayload } from '@elytracms/core/ingest'
@@ -186,6 +187,13 @@ async function storeFile(t: ReturnType<typeof backend>, bytes: number, contentTy
186
187
  })
187
188
  }
188
189
 
190
+ /** EC-685: the SHA-256 an upload declares for the zero-filled `bytes` bytes {@link storeFile} stores. */
191
+ async function sha256Of(bytes: number): Promise<string> {
192
+ const digest = await ingestUploadSha256(new Uint8Array(bytes))
193
+ if (!digest.ok) throw new Error(digest.message)
194
+ return digest.sha256
195
+ }
196
+
189
197
  /** Whether stored bytes exist (`storage.get` yields a Blob, which is not a Convex value: answer a boolean). */
190
198
  function stored(t: ReturnType<typeof backend>, storageId: string): Promise<boolean> {
191
199
  return t.run(async (ctx) => (await ctx.storage.get(storageId as never)) !== null)
@@ -265,7 +273,7 @@ describe('the ingest receiver — dispatch (EC-617)', () => {
265
273
  it('an upload the handler accepts gets ONE storage URL and an upload id; one it refuses gets none', async () => {
266
274
  env()['ELYTRA_INGEST_SECRET'] = SECRET
267
275
  const t = backend()
268
- const upload = { kind: 'upload', target: 'contact', field: 'cv', contentType: 'image/png', size: 500 } as const
276
+ const upload = { kind: 'upload', target: 'contact', field: 'cv', contentType: 'image/png', size: 500, sha256: await sha256Of(500) } as const
269
277
  const issued = await signedPost(t, upload)
270
278
  expect(issued.status).toBe(200)
271
279
  const body = (await issued.json()) as { uploadId: string; uploadUrl: string; expiresInSeconds: number }
@@ -274,23 +282,45 @@ describe('the ingest receiver — dispatch (EC-617)', () => {
274
282
  expect(body.expiresInSeconds).toBe(3600)
275
283
  expect((await signedPost(t, { ...upload, size: 5000 })).status).toBe(413) // the handler's policy
276
284
  expect((await signedPost(t, { ...upload, contentType: 'application/x-msdownload' })).status).toBe(415)
285
+ // EC-685: the intent records the declared digest (what the claim compares).
286
+ const intent = await t.run(async (ctx) => (await ctx.db.query('records').collect()).find((row) => row.recordId === body.uploadId))
287
+ expect((intent!.data as { sha256: string }).sha256).toBe(upload.sha256)
288
+ })
289
+
290
+ it('EC-685 wire: an upload that declares no sha256, or one in another encoding, is a 400 — no intent, no URL', async () => {
291
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
292
+ const t = backend()
293
+ const base = { kind: 'upload', target: 'contact', field: 'cv', contentType: 'image/png', size: 500 } as const
294
+ const hex = await sha256Of(500)
295
+ for (const payload of [base, { ...base, sha256: hex.toUpperCase() }, { ...base, sha256: hex.slice(1) }]) {
296
+ const raw = JSON.stringify(payload)
297
+ const answer = await t.fetch(INGEST_RECEIVER_PATH, {
298
+ method: 'POST',
299
+ headers: { 'content-type': 'application/json', [INGEST_SIGNATURE_HEADER]: await signedIngestRequestRaw(raw) },
300
+ body: raw,
301
+ })
302
+ expect(answer.status, raw).toBe(400) // the receiver's union (payload | probe) reports it at the root; the route's read names `sha256` (core test)
303
+ }
304
+ expect(await t.run(async (ctx) => (await ctx.db.query('records').collect()).length)).toBe(0)
305
+ // Control: the declared lowercase hex is accepted.
306
+ expect((await signedPost(t, { ...base, sha256: hex })).status).toBe(200)
277
307
  })
278
308
 
279
309
  it('a submission claims its upload: the stored file must match the declaration; a mismatch is refused, never deleted', async () => {
280
310
  env()['ELYTRA_INGEST_SECRET'] = SECRET
281
311
  const t = backend()
282
- const declare = async (contentType: string, size: number) =>
283
- ((await (await signedPost(t, { kind: 'upload', target: 'contact', field: 'cv', contentType, size })).json()) as {
312
+ const declare = async (contentType: string, size: number, bytesOf: number) =>
313
+ ((await (await signedPost(t, { kind: 'upload', target: 'contact', field: 'cv', contentType, size, sha256: await sha256Of(bytesOf) })).json()) as {
284
314
  uploadId: string
285
315
  }).uploadId
286
- const good = await declare('image/png', 500)
316
+ const good = await declare('image/png', 500, 400)
287
317
  const goodFile = await storeFile(t, 400, 'image/png')
288
318
  const ok = await signedPost(t, { ...SUBMISSION, uploads: [{ uploadId: good, storageId: goodFile }] })
289
319
  expect(ok.status).toBe(200)
290
320
  // A second claim of the same upload is a conflict.
291
321
  expect((await signedPost(t, { ...SUBMISSION, uploads: [{ uploadId: good, storageId: goodFile }] })).status).toBe(409)
292
322
 
293
- const liar = await declare('image/png', 500)
323
+ const liar = await declare('image/png', 500, 10) // its own bytes: the mismatch is the type
294
324
  const wrongType = await storeFile(t, 10, 'text/html')
295
325
  expect((await signedPost(t, { ...SUBMISSION, uploads: [{ uploadId: liar, storageId: wrongType }] })).status).toBe(415)
296
326
  // EC-671: refused and KEPT — a claim never deletes the id it names (it is the visitor's word).
@@ -301,7 +331,7 @@ describe('the ingest receiver — dispatch (EC-617)', () => {
301
331
  it('an upload no submission claims is listed once its life is over (the visible orphan finding)', async () => {
302
332
  env()['ELYTRA_INGEST_SECRET'] = SECRET
303
333
  const t = backend()
304
- await signedPost(t, { kind: 'upload', target: 'contact', field: 'cv', contentType: 'image/png', size: 10 })
334
+ await signedPost(t, { kind: 'upload', target: 'contact', field: 'cv', contentType: 'image/png', size: 10, sha256: await sha256Of(10) })
305
335
  const later = (offsetMs: number) => new Date(Date.parse('2026-10-26T12:00:00.000Z') + offsetMs).toISOString()
306
336
  expect(await t.run((ctx) => unclaimedIngestUploads(ctx, 'contact', later(30 * 60 * 1000)))).toEqual([])
307
337
  const orphans = await t.run((ctx) => unclaimedIngestUploads(ctx, 'contact', later(2 * 60 * 60 * 1000)))
@@ -318,8 +348,9 @@ describe('the ingest receiver — dispatch (EC-617)', () => {
318
348
  */
319
349
  describe('the claim binds only this intent’s bytes; release deletes only bound bytes (EC-671)', () => {
320
350
  const PNG = { kind: 'upload', target: 'contact', field: 'cv', contentType: 'image/png', size: 500 } as const
321
- async function declare(t: ReturnType<typeof backend>): Promise<string> {
322
- return ((await (await signedPost(t, PNG)).json()) as { uploadId: string }).uploadId
351
+ /** An intent declaring a png of up to 500 bytes whose content is the `bytesOf` zero bytes the test then stores (EC-685). */
352
+ async function declare(t: ReturnType<typeof backend>, bytesOf = 400): Promise<string> {
353
+ return ((await (await signedPost(t, { ...PNG, sha256: await sha256Of(bytesOf) })).json()) as { uploadId: string }).uploadId
323
354
  }
324
355
  const claimWith = (t: ReturnType<typeof backend>, uploadId: string, storageId: string) =>
325
356
  signedPost(t, { ...SUBMISSION, uploads: [{ uploadId, storageId }] })
@@ -388,7 +419,7 @@ describe('the claim binds only this intent’s bytes; release deletes only bound
388
419
  it('(3) a file an asset row references — as its file or as its preview — is refused; control: an unreferenced one is claimed', async () => {
389
420
  env()['ELYTRA_INGEST_SECRET'] = SECRET
390
421
  const t = backend()
391
- const [asFile, asThumb, plain] = [await declare(t), await declare(t), await declare(t)]
422
+ const [asFile, asThumb, plain] = [await declare(t), await declare(t, 40), await declare(t)]
392
423
  const original = await storeFile(t, 400, 'image/png') // all stored AFTER the three intents: check (1) passes
393
424
  const preview = await storeFile(t, 40, 'image/png')
394
425
  const unreferenced = await storeFile(t, 400, 'image/png')
@@ -414,6 +445,151 @@ describe('the claim binds only this intent’s bytes; release deletes only bound
414
445
  expect(await bindings(t)).toEqual([])
415
446
  })
416
447
 
448
+ // EC-685 (Codex r4 F20, ordering 2): a fact checked at claim time is not permanent — an
449
+ // asset may acquire a bound file afterwards. Each delete path re-checks the OTHER owner.
450
+ async function asEditor(t: ReturnType<typeof backend>) {
451
+ await t.run(async (ctx) => {
452
+ await ctx.db.insert('members', { userId: 'user_editor', role: 'admin', createdAt: '2026-10-26T12:00:00.000Z', updatedAt: '2026-10-26T12:00:00.000Z' })
453
+ })
454
+ return t.withIdentity({ subject: 'user_editor', issuer: 'https://convex.test' })
455
+ }
456
+ const deleteBlob = makeFunctionReference<'mutation', { storageKey: string }, unknown>('assets:deleteBlob')
457
+
458
+ it('EC-685: release KEEPS a bound file an asset references now — the binding is spent, the bytes go with the asset', async () => {
459
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
460
+ const t = backend()
461
+ const [asFile, asThumb, plain] = [await declare(t), await declare(t, 40), await declare(t)]
462
+ const promoted = await storeFile(t, 400, 'image/png')
463
+ const preview = await storeFile(t, 40, 'image/png')
464
+ const unreferenced = await storeFile(t, 400, 'image/png')
465
+ for (const [uploadId, file] of [[asFile, promoted], [asThumb, preview], [plain, unreferenced]] as const) {
466
+ expect((await claimWith(t, uploadId, file)).status).toBe(200)
467
+ }
468
+ // An editor promotes two of the uploads to an asset (its file and its preview) — after the claims.
469
+ await insertAsset(t, { storageKey: promoted, thumb: preview })
470
+ const released = await t.run((ctx) => releaseIngestUploads(ctx, [promoted, preview, unreferenced]))
471
+ expect(released).toEqual({ deleted: [unreferenced], kept: [promoted, preview] })
472
+ expect([await stored(t, promoted), await stored(t, preview), await stored(t, unreferenced)]).toEqual([true, true, false])
473
+ expect(await bindings(t)).toEqual([]) // ownership passed: no binding is left to hold the asset's bytes
474
+ // …so the asset's own delete now deletes them (control: deleteBlob deletes unbound bytes).
475
+ const editor = await asEditor(t)
476
+ expect(await editor.mutation(deleteBlob, { storageKey: promoted })).toEqual({ deleted: true })
477
+ expect(await stored(t, promoted)).toBe(false)
478
+ })
479
+
480
+ it('EC-685 mirror: assets:deleteBlob KEEPS bytes an ingest binding holds and returns normally; release then deletes them', async () => {
481
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
482
+ const t = backend()
483
+ const uploadId = await declare(t)
484
+ const own = await storeFile(t, 400, 'image/png')
485
+ expect((await claimWith(t, uploadId, own)).status).toBe(200)
486
+ await insertAsset(t, { storageKey: own }) // promoted to an asset while the submission still holds it
487
+ const editor = await asEditor(t)
488
+ expect(await editor.mutation(deleteBlob, { storageKey: own })).toEqual({ deleted: false, heldBy: 'ingest' })
489
+ expect(await stored(t, own)).toBe(true)
490
+ expect(await bindings(t)).toEqual([{ storageId: own, uploadId }])
491
+ // The asset row goes (the studio's `assets:remove`); the submission's release deletes the bytes.
492
+ await t.run(async (ctx) => {
493
+ for (const row of await ctx.db.query('assets').collect()) await ctx.db.delete(row._id)
494
+ })
495
+ expect(await t.run((ctx) => releaseIngestUploads(ctx, [own]))).toEqual({ deleted: [own], kept: [] })
496
+ expect(await stored(t, own)).toBe(false)
497
+ })
498
+
499
+ // EC-685 (Codex r4 F20, ordering 1): checks (1)–(3) are eligibility; (4) is causation.
500
+ async function storeBytes(t: ReturnType<typeof backend>, bytes: Uint8Array<ArrayBuffer>, contentType: string) {
501
+ return t.run(async (ctx) => {
502
+ const id = await ctx.storage.store(new Blob([bytes], { type: contentType }))
503
+ await (ctx.db as unknown as { patch(id: unknown, value: unknown): Promise<void> }).patch(id, { contentType })
504
+ return id
505
+ })
506
+ }
507
+ const setStoredDigest = (t: ReturnType<typeof backend>, storageId: string, sha256: string) =>
508
+ t.run(async (ctx) => {
509
+ await (ctx.db as unknown as { patch(id: unknown, value: unknown): Promise<void> }).patch(storageId, { sha256 })
510
+ })
511
+
512
+ it('(4) EC-685: a file stored after the intent, unbound, no asset’s — but not the DECLARED bytes — is refused, rejected and kept', async () => {
513
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
514
+ const t = backend()
515
+ const uploadId = await declare(t) // declares 400 zero bytes
516
+ const other = await storeBytes(t, new Uint8Array(400).fill(7), 'image/png') // same type and size, other bytes
517
+ const refused = await claimWith(t, uploadId, other)
518
+ expect(refused.status).toBe(422)
519
+ // The ONE answer of checks (1)–(4): which one failed is not the visitor's to learn.
520
+ expect(await refused.json()).toEqual({ error: `Upload “${uploadId}” did not store the file “${other}”.` })
521
+ expect(await intentState(t, uploadId)).toEqual({ state: 'rejected', storageId: other })
522
+ expect(await stored(t, other)).toBe(true)
523
+ expect(await bindings(t)).toEqual([])
524
+ // Control: the declared bytes, stored after their intent, are claimed.
525
+ const fresh = await declare(t)
526
+ const own = await storeFile(t, 400, 'image/png')
527
+ expect((await claimWith(t, fresh, own)).status).toBe(200)
528
+ })
529
+
530
+ it('EC-685: the comparator reads the stored digest by its FORM — convex-test’s base64 (measured) and hex both match', async () => {
531
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
532
+ const t = backend()
533
+ const first = await declare(t)
534
+ const asStored = await storeFile(t, 400, 'image/png')
535
+ const stamped = await t.run(async (ctx) => ((await ctx.db.system.get(asStored as never)) as { sha256: string }).sha256)
536
+ expect(stamped).toMatch(/^[A-Za-z0-9+/]{43}=$/) // convex-test 0.0.54 fills `_storage.sha256` with base64 (`blobSha`)
537
+ expect((await claimWith(t, first, asStored)).status).toBe(200)
538
+ // The same digest in hex (`storage.getMetadata`'s documented form) reads the same.
539
+ const second = await declare(t)
540
+ const asHex = await storeFile(t, 400, 'image/png')
541
+ await setStoredDigest(t, asHex, (await sha256Of(400)).toUpperCase())
542
+ expect((await claimWith(t, second, asHex)).status).toBe(200)
543
+ })
544
+
545
+ it('EC-685: a stored digest in NEITHER form is refused visibly with its own reason, never read as a match', async () => {
546
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
547
+ const t = backend()
548
+ const uploadId = await declare(t)
549
+ const own = await storeFile(t, 400, 'image/png')
550
+ // A base64url spelling of the right digest is still neither form: refused, not guessed at.
551
+ const url = btoa(String.fromCharCode(...Array.from((await sha256Of(400)).match(/../g)!, (pair) => parseInt(pair, 16))))
552
+ .replace(/\+/g, '-')
553
+ .replace(/\//g, '_')
554
+ .replace(/=+$/, '')
555
+ await setStoredDigest(t, own, url)
556
+ const refused = await claimWith(t, uploadId, own)
557
+ expect(refused.status).toBe(422)
558
+ expect(await refused.json()).toEqual({
559
+ error: `The deployment's stored digest of “${own}” is in neither form the claim reads (64 hex or 44 base64 characters), so the upload cannot be verified.`,
560
+ })
561
+ expect(await intentState(t, uploadId)).toEqual({ state: 'rejected', storageId: own })
562
+ expect(await bindings(t)).toEqual([])
563
+ })
564
+
565
+ it('LEGACY (EC-685): an intent issued before uploads declared a digest is refused before any write — no crash, the row untouched', async () => {
566
+ env()['ELYTRA_INGEST_SECRET'] = SECRET
567
+ const t = backend()
568
+ const issuedAt = '2026-10-26T12:00:00.000Z'
569
+ await t.run(async (ctx) => {
570
+ // A row as the previous release wrote it: no `sha256` column (the kind now requires one).
571
+ await ctx.db.insert('records', {
572
+ kind: 'ingestUpload',
573
+ recordId: 'upload_legacy',
574
+ data: { target: 'contact', field: 'cv', contentType: 'image/png', size: 500, state: 'issued', issuedAt },
575
+ key1: 'contact',
576
+ key2: 'issued',
577
+ at: issuedAt,
578
+ createdAt: issuedAt,
579
+ updatedAt: issuedAt,
580
+ })
581
+ })
582
+ const file = await storeFile(t, 400, 'image/png')
583
+ const refused = await claimWith(t, 'upload_legacy', file)
584
+ expect(refused.status).toBe(422)
585
+ expect(await refused.json()).toEqual({
586
+ error: 'Upload “upload_legacy” was issued before uploads declared their SHA-256 — upload the file again.',
587
+ })
588
+ expect(await intentState(t, 'upload_legacy')).toEqual({ state: 'issued', storageId: undefined })
589
+ expect(await bindings(t)).toEqual([])
590
+ expect(await stored(t, file)).toBe(true)
591
+ })
592
+
417
593
  it('the binding row is flat — its stored nesting is far inside Convex’s limit', async () => {
418
594
  env()['ELYTRA_INGEST_SECRET'] = SECRET
419
595
  const t = backend()