Skip to content

Write a clip's title without holding the edit lock - #180

Merged
A21-devs merged 2 commits into
mainfrom
fix/title-without-edit-lock
Dec 5, 2026
Merged

A21-devs merged 2 commits into
mainfrom
fix/title-without-edit-lock

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Closes #112.

What

"Write title" on the review page ran write_clip_copy inside begin_edit + editing(), so the job's edit lock and the folder's lock were held for the whole AI call. Meanwhile Keep/Drop on any other clip waited KEEP_WAIT and got 409, and Re-render got 409 right away.

  • pipeline.draft_clip_copy(manifest, n, backend, model) does the validation, reads the clip's range and transcript, and asks the backend. It writes nothing and returns a CopyDraft(n, file, start, end, post).
  • pipeline.save_clip_copy(manifest, draft) re-reads manifest.json, and if the clip still has the same file and range it saves copy, writes <clip>.txt and the manifest. If the clip was trimmed or re-rendered in between, it raises ValueError("clip N changed while its title was being written, write it again") and saves nothing. A title for 10-15 s shouldn't land on a clip that's now 10-18 s.
  • write_clip_copy is draft + save, same signature and behavior as before, for anything that calls it directly.
  • The endpoint drafts first, mapping errors the way it did before (400 bad request, 502 backend failure with the no title: log line, 500 otherwise), then takes the locks only for save_clip_copy. The save uses the KEEP_WAIT budget like a keep, since it takes milliseconds.

The MCP server doesn't write titles for single clips, so it isn't touched.

Testing

  • uv run ruff check src tests, uv run mypy, uv run pytest -q tests/test_server.py tests/test_write_title.py tests/test_mcp_server.py: 159 passed.

  • New tests in tests/test_write_title.py:

    • drafting leaves manifest.json byte-for-byte unchanged and writes no .txt
    • a Keep saved between draft and save survives the save (the manifest is read again)
    • a draft for a range that changed meanwhile is refused and nothing is written
    • endpoint, with a backend that blocks: Keep on clip 2 answers 200 in under 1 s, and the title still saves afterwards with the keep in place
    • endpoint: the MCP server trims clip 1 (under pipeline.editing) while its title is written, and the title request gets 400 with the "changed while" message and saves nothing
  • Real run of the repro from the issue: clipwright serve with CLIPWRIGHT_OPENAI_URL pointing at a local stub that sleeps 15 s before answering. Job on a 240 s synthetic video (TTS speech over Roblox gameplay, whisper on), 2 clips. While "Write title" ran on clip 1, I sent Keep and then Drop on clip 2:

    Keep clip 2 Drop clip 2 title clip 2 afterwards
    main (b99e5df) 409 after 2.02 s 409 after 2.03 s 200 after 15.1 s unchanged
    this branch 200 in 0.03 s 200 in 0.06 s 200 after 15.1 s dropped, as clicked

    The .txt next to the clip had the stub's title, description and hashtag.

The review page held the job's edit lock (and the folder's) while the AI
backend answered, so Keep/Drop on other clips waited 2 s and got 409.
write_clip_copy is now draft_clip_copy (reads the range and transcript,
asks the backend, writes nothing) and save_clip_copy (re-reads
manifest.json under the lock and saves, or refuses if the clip's range
changed meanwhile). The endpoint drafts first and only locks to save.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Checked:

  • The range check compares file, start and end from a fresh read under the lock. A layout-only re-render keeps all three and the title stays valid, which is right, since the title doesn't depend on the layout.
  • A superseded job: the draft happens before begin_edit, so the backend is asked and then the save gets the usual 409 "a newer run replaced these clips". That wastes one AI call in a rare case. Fine.
  • Two title requests for the same clip can't overlap from the page, because the button is disabled while one runs (index.html 423).
  • write_clip_copy keeps its signature, and the old pipeline tests pass unchanged.

One real problem with the save's wait budget, inline.

Comment thread src/clipwright/server.py Outdated
# and saving checks the clip's range didn't change in between.
draft = self._draft_copy(st, manifest, int(number), backend, model)
action = partial(save_clip_copy, manifest, draft, log=st.add_log)
wait = KEEP_WAIT # saving takes milliseconds, like a keep

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The save takes milliseconds, but the lock it waits for may not be free: a Re-render or layout change of another clip holds it for the whole ffmpeg run (7-8 s per 20 s clip in the run from the description, longer for longer clips). If the AI answers during one, the save waits KEEP_WAIT = 2 s, gets 409 "busy", and the title the user waited 15-30 s for (and may have paid for) is thrown away. Before this PR the request was refused before asking the backend, so nothing was lost. The budget here should cover a re-render, e.g. a separate SAVE_WAIT of a minute. The request thread is already parked for the AI call anyway.

@A21-devs

A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner Author

Fixed in 9720c52. Saving a title now has its own SAVE_WAIT = 60.0 instead of KEEP_WAIT, so it waits out a re-render rather than dropping the answer.

Verified:

  • New test test_a_title_waits_out_a_rerender_instead_of_being_thrown_away: the backend answers while another process holds the folder's edit lock for KEEP_WAIT + 1 s, and the title is saved with 200. On the previous commit the same test fails with 409.
  • Real run with the 15 s stub backend: Write title on clip 1, then 11 s later Re-render of clip 2 (start +1 s). The re-render took 8.0 s and returned 200. The answer came in while it was running, the title request returned 200 after 19.0 s (15.1 s without a render in the way), and manifest.json has both the title on clip 1 and the new range on clip 2 (145.75-167.75).
  • ruff, mypy clean; tests/test_write_title.py 18 passed.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Second pass over 9720c52. Fixed: the title save now waits SAVE_WAIT (60 s). I checked where the budget starts. deadline = time.monotonic() + wait is computed after _draft_copy returns, so the 60 s count from when the answer arrives, not from the request, and a slow backend doesn't eat into it. The same deadline covers begin_edit and the cross-process editing() lock, which is what the new test holds. Keep/Drop still use KEEP_WAIT. Nothing else changed. Clean.

The save used KEEP_WAIT (2 s), so an answer that arrived during another
clip's re-render was thrown away with a 409.
@A21-devs
A21-devs merged commit ab387db into main Dec 5, 2026
4 checks passed
@A21-devs
A21-devs deleted the fix/title-without-edit-lock branch December 5, 2026 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Writing a title holds the job's edit lock for the whole AI call

1 participant