Skip to content

feat(core): experimental blob API backed by SQLite - #67016

Open
rekram1-node wants to merge 1 commit into
devfrom
blob-catalog
Open

rekram1-node wants to merge 1 commit into
devfrom
blob-catalog

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Adds an experimental blob API: upload, list by session, read, edit name and metadata, delete. BlobStore now uses two SQLite tables instead of loose files that are deleted after a week. Generated videos already go through it, and nothing deletes a blob any more except DELETE.

Plan and decisions: https://share.opncd.ai/BTqfjUQuu5Zq. This is part A of that plan. Making video and image generation label their blobs, and letting read accept blob IDs, comes next.

Size

GitHub reports about +5.4k / −2.8k, but most of that is one generated file. packages/protocol/openapi.json is about 6.1k of the changed lines in GitHub's diff, which aligns repetitive JSON badly. With git diff --diff-algorithm=histogram, its real change is +853 / −13: three blob paths replacing the old one, nine new Blob.* schemas, and the flag in the config schema. The hand-written change is about 750 lines of code plus about 230 of tests. Everything else is regenerated clients, the database snapshot and API docs, all marked linguist-generated.

Routes

All of them are under /api/experimental/, follow the session and artifact groups, and return 403 unless experimental.blob_catalog is on:

Method Path Identifier
GET /blob?sessionID=&limit=&cursor= experimental.blob.list → { data, cursor: { previous, next } }
POST /blob?name=&sessionID= (raw body) experimental.blob.create → { data: Blob.Info }
GET /blob/:blobID experimental.blob.get → { data: Blob.Info }
GET /blob/:blobID/content experimental.blob.content → bytes
PATCH /blob/:blobID { name?, metadata? } experimental.blob.update → { data: Blob.Info }
DELETE /blob/:blobID experimental.blob.remove

Breaking (experimental) change:

- GET /api/experimental/blob/:id             → bytes, no flag
+ GET /api/experimental/blob/:blobID         → { data: Blob.Info }
+ GET /api/experimental/blob/:blobID/content → bytes (Range, ETag "<id>", immutable cache, as before)

No client in this repo fetched the old route. The video tool reads the store directly, so the flag doesn't affect it. Outside callers of the experimental video API need the flag to read the bytes.

Blob.Info is a union on type, taken from the MIME type, so each kind carries only the facts it can have:

type BlobInfo = BlobImage | BlobVideo | BlobAudio | BlobFile
// shared: id, sessionID?, mime, bytes, name?, metadata?, time: { created, updated }
BlobImage  { type: "image", size?: { width, height } }
BlobVideo  { type: "video", size?: { width, height }, durationMs? }
BlobAudio  { type: "audio", durationMs? }
BlobFile   { type: "file" }   // everything else

The table stays flat (nullable width, height, duration_ms columns), and only the API shape is a union. This PR doesn't fill size or durationMs yet: reading them from file headers is a follow-up, done when Kit's blob API is removed. Until then they're always missing.

How it works

write(stream, { sessionID?, name?, mime? })
  buffer chunks in memory
  past 1 MB → move them to <data>/blobs/<id>.part, stream the rest there, fsync, rename to <id>
  mime   = type sniffed from the first 64 bytes ?? request content-type ?? octet-stream
  insert blob_v2 row (+ blob_data row when ≤ 1 MB) in one transaction
  any failure → remove .part and file; no row
  • blob_v2: id, session_id?, mime, bytes, name?, width?, height?, duration_ms?, metadata? (JSON), timestamps. Index on (session_id, time_created) where session_id is not null.
  • blob_data: (blob_id, seq, data) holding blobs of 1 MB or less, of any type. It cascades on delete.
  • list: newest first by time_created, with id only breaking ties. An ID's 48-bit clock wraps about every 795 days (next on 2028-10-17), so IDs alone don't sort by age. Pages work like session.list: 50 by default, with an opaque cursor that carries its filter.
  • session_id: a label only, with no foreign key. Deleting a session keeps its blobs, which stay listable by its ID. create returns 404 for a session that doesn't exist.
  • metadata: belongs to the client. PATCH replaces it, as S3 does, and the handler rejects more than 100 KB of JSON with a 400. That keeps a page of list from growing without bound.
  • create: a raw handler, so the body streams into the store and its content-type is only a hint. An upload that breaks off mid-body answers 400 rather than logging a server error.
  • A large blob whose file is missing answers 404.
  • The flag is read from the server's base config, as the machines flag is, because blobs aren't tied to a project.

Migrating the old store

Before this PR, BlobStore kept each video as a loose file in <data>/blobs/ with no row, and video job records point at them by ID. The blob_catalog migration gives each existing file a row, with its type sniffed from the file, its byte size, and time_created set to its modification time, so videos generated before the upgrade stay readable.

Effect client

Raw uploads were left out of the Effect client because the generator cast their request to a type that also allows the raw-response mode, so .data didn't typecheck. The other generator path already pins responseMode: "decoded-only". This applies the same fix in packages/httpapi-codegen, and blob.create and blob.content are now in the Effect client. fs.write and the other raw routes are left as they are.

Trade-offs

  • No expiry. The 7-day sweep from fix(core): delete blobs a week after they were written #65671 is gone. Blobs disappearing was the media tab's problem, so they now stay until deleted, and the folder grows until history linking adds cleanup.
  • No media facts yet. A header probe was written and reviewed (Neon and Astra found 18 issues on crafted and edge-case files), then taken out of this PR to keep it to the store and API. The columns and the union are in place, so adding it later doesn't change the data model.
  • Plugins can't call create, the same as fs.write: the plugin runtimes don't send raw bodies.
  • Workerd still reports blob storage as unsupported. Durable Object storage comes later.

Verification

  • packages/core/test/blob-store.test.ts: inline versus on-disk split, sniffed type over the hint, range reads, paging forward and back, a missing file reading as missing, delete, a failed write leaving nothing behind, and the migration adopting an old file while skipping a non-blob file.
  • packages/server/test/video-generate.test.ts: video job → /content with Range, ETag and 304, then DELETE; upload to a real session → 404 for an unknown session → list with cursor → PATCH metadata → 400 over 100 KB; and 403 on list, get and content with the flag off.
  • packages/httpapi-codegen and packages/client tests pass, as does the cli-rs operation count test. bun run check passes.

Added by the opencode bot (AI-generated).

Public API: replaces GET /api/experimental/blob/:id bytes with /blob/:blobID (info) + /blob/:blobID/content (bytes), adds paged blob list/create/update/delete behind experimental.blob_catalog, and makes Blob.Info a union on type (breaking for experimental routes only)

@opencodejr opencodejr 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.

Looks sound. I ran the core blob-store tests and the server video-generate tests (all pass). I also called the routes directly through the server:

  • With the flag off, list and create return 403.
  • A 3 MB upload goes to disk, and Range reads return the correct bytes from both the on-disk file and the SQLite-stored copy.
  • An upload with an HTML content-type is served as a text/plain download with nosniff and the sandbox CSP.
  • PATCH moves time.updated forward, and deleting twice gives 204 then 404.

I couldn't run the type checks in this environment: the typecheck scripts call bun check, which this Bun doesn't have.

Public API impact (all experimental):

  • Changed, breaking: GET /api/experimental/blob/:id now returns { data: Blob.Info }. The bytes moved to GET /api/experimental/blob/:blobID/content, with the same Range, ETag and 304 support. Anyone fetching the old URL for bytes has to add /content.
  • Added: GET /api/experimental/blob?sessionID= (experimental.blob.list), POST /api/experimental/blob?name=&sessionID= (experimental.blob.create), PATCH /api/experimental/blob/:blobID (experimental.blob.update), DELETE /api/experimental/blob/:blobID (experimental.blob.remove), and the config flag experimental.blob_catalog.
  • Widened: Blob.Info gains sessionID, name, metadata, width, height, durationMs and time. The generated TS, Rust and Luau clients change to match.
  • Behaviour change: blobs no longer expire after 7 days. Blobs written by the old store have no row, so they can't be read through the API any more, as the description notes.

No blocking concerns.

@opencodejr opencodejr 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.

The new commits look good:

  • List order: blobs are now listed newest first by time_created, with id only breaking ties. Blobs created in the same millisecond still come out newest first.
  • Index: the session index now matches that order.
  • Generated files: the OpenAPI spec and generated clients were regenerated after the routes moved earlier in the group.

I re-ran the core blob-store tests and the server video-generate tests, and they pass. The public API is the same as in my last review; only the order of operations in the generated files changed. No new concerns.

@opencodejr opencodejr 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.

The new commit only updates the operation counts in the cli-rs plugin test: 195 operations, with the raw-body blob.create upload added to the ones plugins can't call. client-samples.json now has 195 entries, which matches. I couldn't run the Rust test here because cargo isn't installed. The public API is unchanged since my last review, and I have no new concerns.

@opencodejr opencodejr 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.

The metadata limit change is consistent: the constant, the route description, the API docs page and the test all move to 100 KB. The server video-generate test passes, including the 400 for metadata over the limit.

Public API: PATCH /api/experimental/blob/:blobID now accepts up to 100 KB of metadata instead of 4 KB. That's backwards compatible.

One side effect, not blocking: list returns every blob's metadata and has no pagination yet, so one response can now carry up to 100 KB per blob. Worth remembering when the cursor is added.

@opencodejr opencodejr 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.

The PR was rebased onto newer dev. The blob commits carried over without content changes, and the new chore(cli-rs) commit only regenerates the Rust client and compat.js to pick up the 503 responses that dev added to the fs and pty routes. After the rebase, the core blob-store tests and the server video-generate and workerd tests pass. The public API is the same as in my last review, and I have no new concerns.

@opencodejr opencodejr 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.

The new commit works as described. On a real server, the migration gave an old-style blob file a row and it read back as video/mp4, while a stray .part file was skipped. Paging with cursors moved through four blobs in pages of 2, 2 and 0, and a garbage cursor answered 400. The core blob-store, server video-generate and httpapi-codegen tests pass.

Public API changes (all experimental):

  • GET /api/experimental/blob now takes limit and cursor and returns { data, cursor: { previous, next } } instead of { data }. It can answer 400 for a bad cursor. This breaks callers that only read { data } and expect every blob in one page.
  • POST /api/experimental/blob returns 404 for a session that doesn't exist, and 400 when the upload breaks off mid-body.
  • GET /blob/:blobID and GET /blob/:blobID/content now return 403 unless experimental.blob_catalog is on. Outside callers of the video API need the flag to read video bytes.
  • The Effect client gains blob.create and blob.content.

One concern, inline: a single old file that can't be opened stops the migration, and with it server startup.

const names = yield* Effect.promise(() => readdir(directory).catch(() => [] as string[]))
yield* Effect.forEach(names.filter(isID), (id) =>
Effect.acquireUseRelease(
Effect.promise(() => open(path.join(directory, id))),

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.

Effect.promise turns a failed open or stat into a defect. That fails the whole migration transaction, and the server then won't start. All it takes is one file in <data>/blobs that can't be read (permissions, or something deleting it during the scan). These files are only best-effort recoveries of expiring videos, so it would be safer to log and skip a file that can't be read than to block startup. For example, use Effect.tryPromise and Effect.catch, then log a warning for each file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in e40aae5: each file is read inside its own Effect.catchCause, so a file that can't be read logs a warning and is skipped. The test now includes a directory named like a blob.

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.

Looks good. In e40aae5 the Effect.catchCause wraps the whole acquireUseRelease for each file (20261128193337_blob_catalog.ts:79-81). If an open, stat or read fails, or probe hits a defect, the migration logs a warning, skips that file and keeps going. The upgrade test passes: I ran bun test test/blob-store.test.ts and got 3 pass.

Two small notes, neither blocking:

  • On Linux, open() on a directory works and the failure comes from the first read (EISDIR). So the new test covers a failed read, not a failed open. The same catch handles both, so that's fine.
  • The catch also covers the INSERT OR IGNORE, so a database error on one row would be logged and skipped rather than aborting the upgrade. SQLite only undoes that one failed statement, not the transaction, so this is consistent with "best effort", and I think that's the right call.

I didn't change anything in the checkout.

@opencodejr opencodejr 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.

Thread 1 is fixed: if the migration can't read an old blob file, it now logs a warning and skips it, so one bad file no longer stops the server from starting. The updated migration test, which adds a directory named like a blob, passes. The public API is the same as in my last review, and I have no new concerns.

@opencodejr opencodejr 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.

Thread 1 was already fixed by the previous commit: if the migration can't read an old blob file, it logs a warning and skips it.

The new commit works as described. A WAV and a FLAC stored through the blob store each came back as type: "audio" with the right durationMs, and the core blob-store and server video-generate tests pass.

Public API change (experimental): Blob.Info is now a union on type (image, video, audio, file). The flat width and height fields become size: { width, height } on images and videos, and durationMs now appears only on videos and audio. Anyone reading the old flat fields has to switch, including readers of the video API's blobs list. The OpenAPI spec and generated clients are regenerated to match.

No new concerns.

@opencodejr opencodejr 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.

Thread 1 stays fixed: the migration still logs and skips an old blob file it can't read.

The new commit removes the header parsing; a blob's type is still sniffed from its first 64 bytes. The core blob-store and server video-generate tests pass.

Public API: since my last review, size and durationMs are never filled in for now. They were already optional, so this is backwards compatible, and the Blob.Info union shape is unchanged. No new concerns.

@opencodejr opencodejr 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.

Thread 1 stays fixed: the migration still logs and skips an old blob file it can't read.

The latest push is a rebase onto current dev with the commits squashed into one. Comparing the PR's changes file by file, only openapi.json and the generated Rust client differ, and those differences come from dev itself (the fs and pty 503 responses already merged there, and the machine-list description dev changed). The core blob-store and server video-generate tests pass. The public API is the same as in my last review, and I have no new concerns.

@rekram1-node
rekram1-node force-pushed the blob-catalog branch 2 times, most recently from 1aee650 to a1ae97c Compare November 30, 2026 03:58

@opencodejr opencodejr 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.

Thread 1 stays fixed: the migration still logs and skips an old blob file it can't read.

This push is a rebase onto newer dev. The only code change is that the blob_catalog migration is now typed DatabaseMigration.Imperative, the type dev now uses for migrations that run code. openapi.json and compat.js were regenerated, and the spec check (generate-openapi.ts --check) passes. The core blob-store and server video-generate tests pass. The public API is the same as in my last review, and I have no new concerns.

@opencodejr opencodejr 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.

Thread 1 stays fixed: the migration still logs and skips an old blob file it can't read.

This push only rebases the PR onto one new dev commit (#67251), which pins @effect/platform-node-shared. The PR's own changes are byte-for-byte the same. The core blob-store and server video-generate tests pass after bun install. The public API is the same as in my last review, and I have no new concerns.

This branch has not been deployed

No deployments
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