GeezerVison is showing github.com — feat(core): durable video jobs by rekram1-node · Pull Request #64546 · anomalyco/opencode
Conversation
rekram1-node
commented
Nov 18, 2026
•
edited by opencodejr
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Issue for this PR
Prepares #63666 (the agent's video tool).
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
Video jobs now survive a server restart. Until now, running and failed jobs lived only in memory: a restart lost them, and a running job's videos were never collected. Each job now has a record in the kv table, and the server resumes polling running jobs when it starts.
The video ID is still the only handle. The provider's job ID (remote) stays inside the video service.
Record, key video:<videoID>, decoded with a schema on read:
{ status: "submitting" | "running" | "completed" | "error",
model, location, created,
remote?, // running
blobs?, // completed: blob IDs
reason?, error?, // error
finished? } // completed, error
Lifecycle:
start write submitting -> provider accepts -> write running (remote) -> return { id, status: "running" }
provider rejects -> remove the record, return the error
done save each video as a new blob -> write completed (blobs, finished)
any download fails -> remove the blobs saved so far -> write error
startup running -> poll again until 2 h after created (already past: error "unknown")
submitting -> error "unknown": never resubmitted, it's paid and not idempotent
hourly remove records 24 h after finished (or 26 h after created if they never finished)
get reads the record. For a completed job it reads each blob, drops the ones that no longer exist, and returns not found if none remain. Memory holds only the polling fibers.
start takes an optional location, stored on the record and used for every later provider call, including after a restart. The HTTP route still uses the server's base configuration; the agent tool will pass the session's location.
API differences (/api/experimental/generate/video):
- Blob IDs are generic again:
blob_ + 26 characters, instead of <videoID>_<n>.
GET /generate/video/:id returns running, failed and completed jobs after a restart, and 404 from 24 hours after the job finished.
- Blobs are kept until removed: no more 7-day cleanup, and
Blob.Info drops expiresAt. They're written straight to blob_… and flushed to disk (no .part file); a failed write deletes its file. The blob route's cache lifetime goes from 7 days to a year, since a blob ID never changes content.
Servers sharing a database (the background service plus opencode serve) never poll the same job. A job has a lease in kv (video-lease:<id> = { owner, until }), taken with a conditional write the same way automation elects its scheduler:
owner renews every 30 s (until = now + 1 min); stops the job if the lease was taken
every server, at startup and every minute
unfinished job whose lease expired → claim it → poll it (or fail an interrupted submission)
So when a server dies, another one that's still running picks up its jobs within about 1–2 minutes, instead of waiting for a restart.
Still behind the video_generate flag.
Known limits, accepted for now:
- After a hard crash, the restarted server waits for the old lease to expire (up to a minute) before it resumes a job.
- If the server crashes between saving a blob and updating the record, the next start downloads the videos again and the first blob stays on disk. A crash mid-download can also leave a partial file that no job references. Nothing removes blobs yet; a delete endpoint comes later.
How did you verify your code works?
packages/core/test/video-generation.test.ts (one test): a job with two videos completes with two blob_ IDs; a fresh layer on the same data dir and database still reads it; a job running when the first process stops completes in the next one; a submitting record becomes an error on startup; records are gone 25 hours later (TestClock). Passed with --rerun-each 10.
packages/server/test/video-generate.test.ts updated for blob_ IDs and reading the job back after a restart.
bun typecheck in core, server, schema and protocol; bun run check at the root.
- Live, against xAI (
grok-imagine-video-1.5-lite): two jobs started, server stopped mid-generation, restarted; both completed and their blobs stream as H.264 MP4. Then two servers on one data dir: started a job on A, killed A with SIGKILL; B claimed the expired lease and completed the job about two minutes later.
Screenshots / recordings
Not a UI change.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Added by the opencode bot (AI-generated).
Public API: experimental video API: Blob.ID changes from video_<id>_<n> to blob_…, Blob.Info.expiresAt is removed, blob cache lifetime goes from 7 days to 1 year, and video jobs survive restarts
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found no blocking problems; there's one small inline comment.
I ran the new core test 10 times in a row (--rerun-each 10) and all runs passed; it restarts the service on the same data dir and database with a fake provider. The server video tests also pass, and core and server type-check with tsc -b. I couldn't run a real video job end to end because there are no provider credentials.
Public API (experimental, behind video_generate):
Blob.ID changes from video_<id>_<n> to blob_ + 26 characters. Blob IDs from v2.3.1 no longer pass validation, so GET /api/experimental/blob/:id rejects them with 400.
GET /api/experimental/generate/video/:id now returns running and failed jobs after a restart, and 404 from 24 h after the job finished.
VideoGeneration.start (core) gains an optional { location }.
- OpenAPI changes only in descriptions and the
Blob.ID pattern.
This isn't backwards compatible for anyone holding old blob IDs, which seems fine for an experimental API.
The only concern is the blob cleanup in blob-store.ts: blob files written by v2.3.1 are no longer swept (see the inline comment).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
FileRetention.cleanup(
|
|
|
fs,
|
|
|
entries.filter((entry) => entry.startsWith("video_")).map((entry) => path.join(directory, entry)),
|
|
|
entries.filter((entry) => entry.startsWith("blob_")).map((entry) => path.join(directory, entry)),
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v2.3.1 already shipped video_<id>_<n> blob files (behind the flag). With the cleanup now matching only blob_, those files stay on disk forever after upgrading. Should the sweep also match the old video_ prefix, at least for one retention period?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The earlier point is fixed: blob cleanup now also sweeps video_ files left by v2.3.1. The core video test and the core type-check pass. The new commit doesn't change the public API, and I have no new concerns.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest push moves the branch back to 144478dee and drops 65bfd14ec. That commit had added the video_ prefix to the blob cleanup, so v2.3.1 blob files are left on disk again and the earlier thread on blob-store.ts is still open. Nothing else changed since that first review: the rest of the code is unchanged and the public API impact is the same.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lease approach works: a server that shuts down cleanly releases its leases, a crashed server's leases expire after a minute, and the takeover loop picks those jobs up. The tests and the core type-check pass. Two-server takeover isn't tested; I checked it by reading the code.
One concern: when another server takes over a job, it acts on a record it read before claiming the lease, so it can overwrite a job that just finished (inline comment).
The new commit doesn't change the public API. The video-lease:<id> kv keys are internal.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
const job = Option.getOrUndefined(decode(row.value))
|
|
|
if (!id || !job || (job.status !== "running" && job.status !== "submitting")) return
|
|
|
if (yield* FiberMap.has(fibers, id)) return
|
|
|
if (!(yield* claim(id))) return
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
job comes from the rows snapshot, which was read before claim. Suppose the owning server finishes the job (finish, then release) between that scan and this claim. The claim then succeeds, and this server acts on a stale record:
- If the stale status is
running, settle runs again. It downloads the videos a second time and replaces the completed record's blob IDs; the first set of blobs is orphaned.
- If the stale status is
submitting, this server overwrites a completed job with the error "server stopped while submitting".
Re-reading and decoding PREFIX + id after a successful claim, and releasing if it's no longer unfinished, would close the gap.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stale-record race in the takeover loop is fixed: after claiming a lease, the server reads the job again and releases the lease if the job already finished. The core video test passed 5 times out of 5, and the core type-check passes. This commit doesn't change the public API, and I have no new concerns.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blobs are now kept until removed, and the change works as described. The core and server video tests pass, core and server type-check, and the OpenAPI spec and generated client types match the schema.
Public API (experimental, behind video_generate):
Blob.Info.expiresAt is removed (OpenAPI and @opencode/client BlobInfo). Clients that read it break.
GET /api/experimental/blob/:id cache-control goes from max-age=604800 (7 days) to max-age=31536000 (a year).
- The route descriptions drop the 7-day retention promise.
The earlier thread about old video_ blob files stays open: this commit stops all blob cleanup instead of sweeping them.
One concern: with no cleanup and no delete route yet, the blobs directory only grows (inline comment).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
* Files under `<data>/blobs/`, deleted 7 days after they're written, at startup and hourly. Nothing tracks who uses a
|
|
|
* blob, so a caller that keeps one longer copies it.
|
|
|
*/
|
|
|
/** Files under `<data>/blobs/`, kept until something removes them. */
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing calls remove except the failure paths in write and download, and there's no delete route yet. Every generated video, the extra copy written after a crash, and any partial file from an interrupted write now stay on disk for good. The video: record is deleted 24 hours after the job finishes; after that nothing in the server links a blob to its job, so a delete route added later couldn't clean up existing files by job. Could some sweep stay until the delete route lands, for example blobs not referenced by any live record and older than a cutoff?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|