<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Udit Rawat on Medium]]></title>
        <description><![CDATA[Stories by Udit Rawat on Medium]]></description>
        <link>https://medium.com/@udit.rawat03?source=rss-50b0e74af3fe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*i3iXIs01AFbkGoSeXbgLeA.png</url>
            <title>Stories by Udit Rawat on Medium</title>
            <link>https://medium.com/@udit.rawat03?source=rss-50b0e74af3fe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 03:12:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@udit.rawat03/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Best AI Video Clip Can Still Be Useless in the Edit]]></title>
            <link>https://medium.com/@udit.rawat03/the-best-ai-video-clip-can-still-be-useless-in-the-edit-168e631bf514?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/168e631bf514</guid>
            <category><![CDATA[creative-production]]></category>
            <category><![CDATA[ai-video-generator]]></category>
            <category><![CDATA[video-editing]]></category>
            <category><![CDATA[content-creation]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Tue, 17 Nov 2026 04:31:05 GMT</pubDate>
            <atom:updated>2026-11-17T04:31:05.058Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Generate for cut points, coverage, and revision room instead of judging one polished frame.</em></p><figure><img alt="A filmstrip rests on a dark editing desk, with a warmly lit central product shot and extra frames visible on both sides." src="https://cdn-images-1.medium.com/max/1024/1*wCQVaNquuHV-_5-aKGfkxw.png" /><figcaption><em>AI-generated image created for this article to illustrate editable frames around a selected video moment.</em></figcaption></figure><p>A lean marketing team is building a 20-second product video. The generated hero shot looks excellent: warm light, controlled camera movement, a clean product reveal. Everyone approves it in the review gallery.</p><p>The trouble appears immediately. The action begins before the incoming narration has finished. The product reaches its best position too late. There is no steady moment for the name to land, and the final movement runs straight into the cut. Trimming either end breaks the motion. A dissolve exposes repeated frames. The most attractive clip in the batch is also the least useful clip in the production.</p><p>This is not mainly a prompting failure. It is a planning failure. The team asked for a picture that moves, then reviewed it as a picture. The editor needed material with choices.</p><h3>A generated clip is source material, not a finished shot</h3><p>When people review AI video in a grid, the strongest thumbnail tends to win. That view rewards composition, subject fidelity, lighting, and spectacle. It hides the time around the attractive moment.</p><p>An editor sees something else. Where can the clip start without feeling abrupt? How long does the useful action last? Can the shot leave early if the narration changes? Is there enough material after the chosen out point to soften a transition? Does the camera settle, or does every frame depend on continuous motion?</p><p>Conventional editing tools make this requirement visible. Adobe&#39;s guide to <a href="https://helpx.adobe.com/uk/premiere/desktop/add-video-effects/apply-video-transitions/clip-handles-settings.html">clip handles in Premiere</a> explains that transitions need media beyond the visible in and out points. Its guide to <a href="https://helpx.adobe.com/premiere/desktop/edit-projects/trim-clips/perform-rolling-edits.html">rolling edits</a> describes moving an edit point by trimming the outgoing and incoming clips together while keeping the sequence duration unchanged. Both operations depend on having usable frames outside the first cut.</p><p>AI generation does not remove that editorial need. It makes the need easier to overlook because the output often arrives as a short, self-contained performance. If the entire clip is built around one tightly timed action, the editor may have no real handles at all.</p><p>The practical shift is simple: stop defining a shot only by what should appear. Define how the shot must behave in the timeline.</p><h3>Brief the useful window, not only the ideal moment</h3><ul><li>a calm opening state before the main movement begins;</li><li>the reveal to develop through the middle of the clip;</li><li>a readable product position that holds long enough for the spoken claim;</li><li>continued stable motion or a clean settle after the likely out point.</li></ul><p>This is better than asking for “an eight-second cinematic reveal.” The latter describes duration and style but says nothing about editorial control. The former gives the team a way to judge whether the returned clip contains a usable four-second shot with room around it.</p><p>The timing does not need to pretend that a generative model will obey a frame-accurate cue sheet. It should identify priorities. If the reveal starts immediately, the clip may fail even when it looks good. If the camera never settles, the editor should know that before approving it. The brief is an evaluation map, not a guarantee.</p><figure><img alt="Diagram comparing an 8-second generated clip that reserves a 4-second useful window with opening and ending handles, against a tight performance that fills the entire duration and leaves no room to trim." src="https://cdn-images-1.medium.com/max/1024/1*E_SaJZgNHkOgwe8I_Y0fYw.png" /><figcaption><em>Brief the useful window inside the source clip. If the action fills every frame, the editor has no handles.</em></figcaption></figure><h3>Give each boundary a job</h3><p>The opening and closing frames should connect to the production around them. That means describing entry and exit conditions, not only the subject in the middle.</p><p>An entry condition answers questions such as: What visual weight comes from the previous shot? Is the viewer arriving from a wide scene or a close detail? Should movement continue in the same direction, oppose it, or begin from rest? Does the voiceover need the image to stay quiet for a beat?</p><p>An exit condition looks forward. Does the next shot begin with fast motion? Will a title appear over the final frames? Is the editor likely to cut on an action, a musical beat, or a spoken word? Does the composition need negative space near the end?</p><p>These conditions can remain plain. For the product example, the brief might say: “Begin with the object already visible but visually quiet; introduce the light change after the opening hold; end with the label facing camera and movement reduced enough for a clean cut.” That sentence gives the generator a clearer task and gives the reviewer specific failure conditions.</p><p>It also protects the edit from a familiar mistake: approving a clip because its middle is strong while ignoring unusable edges.</p><h3>Generate coverage around the decision</h3><p>Coverage does not mean producing random alternatives. It means creating deliberate options for the places where the edit may change.</p><p>The Blackmagic Design <a href="https://documents.blackmagicdesign.com/UserManuals/DaVinci-Resolve-20-Editors-Guide.pdf">Editor’s Guide to DaVinci Resolve 20</a> teaches B-roll as material placed against marked timeline ranges, with in and out points defining the duration the edit needs. The lesson is useful beyond filmed footage: the timeline requirement should shape the material request.</p><p>For a generated sequence, useful coverage might include a version with an earlier action, one with a longer settled ending, and a simpler insert that can cover a narration change. These are not three aesthetic directions. They solve three editorial risks.</p><p>A small team can apply the same thinking without multiplying every shot. First identify the expensive or fragile edit points: a reveal tied to a word, a transition between unlike scenes, a product detail that carries a claim, or a closing frame that must also support text. Generate options there. A low-risk atmospheric cutaway may need only one usable result.</p><p>This is also where conventional assets remain valuable. A still image with a controlled move, a filmed detail, a graphic card, or licensed stock may provide better edit control than another generated clip. Production planning should choose the material that serves the cut, not force generation into every gap.</p><h3>Review candidates inside the rough cut</h3><p>A gallery can help reject obvious failures, but it should not grant final approval. Put the candidate into the timeline with the current narration, music, captions, and adjacent shots. Then test more than one cut.</p><p>Try the planned in and out points. Move the cut earlier. Hold it later. Remove the transition. Listen with the picture hidden, then watch without sound. If a small script revision would make the clip collapse, the team should either generate more room or record the dependency explicitly.</p><p>The review note should describe editorial behavior rather than taste alone. “The reveal feels slow” is a start, but “the label becomes readable after the narration names the product” points to a fix. “The ending is awkward” is weaker than “the camera is still accelerating at the required out point, so there is no clean cut into the static end card.”</p><p>Specific review language prevents the next attempt from changing everything. The team can preserve the approved lighting, framing, and product orientation while requesting an earlier reveal or a longer settle.</p><figure><img alt="Diagram comparing gallery review, where the strongest still wins, with timeline review, where a candidate clip is tested between adjacent shots and against the narration, including earlier and later cut points." src="https://cdn-images-1.medium.com/max/1024/1*bU0hkENhyrtRJiCpY6xGyA.png" /><figcaption><em>A gallery can reject obvious failures. Final approval belongs in the rough cut, with more than one in and out point tested.</em></figcaption></figure><h3>Preserve the reason a clip was approved</h3><p>Once a candidate works, keep more than the exported file. Record the timeline version, intended range, approved in and out points, adjacent shots, narration line, relevant references, and the reason it was selected. Also keep the source clip rather than only the trimmed timeline segment.</p><p>That record matters when the voiceover changes, a social variant needs a different pace, or a later reviewer asks for a shorter cut. Without it, the team sees an asset named final_07.mp4 and has to rediscover its limits. With it, the production can decide whether to recut, regenerate, or replace the shot without restarting the creative reasoning.</p><p>This is the production-orchestration view behind <a href="https://moosky.ai/">Moosky AI</a>: generation is one step inside a workflow that must preserve intent, coordinate assets and revisions, and keep approval decisions visible to the people directing the work. A successful model response is not the same as an approved, editable contribution to a finished production.</p><h3>The real quality test is optionality</h3><p>A polished frame can prove that a model produced something attractive. It cannot prove that the clip can survive an edit.</p><p>Before approving generated video, ask four practical questions: Can the shot start in more than one place? Can it end early without breaking? Does the important action align with the timeline’s actual need? If the surrounding cut changes, is there another usable path?</p><p>The goal is not endless flexibility. It is enough room to make normal editorial decisions without turning every small revision into another generation cycle. For creators, lean teams, and small agencies, that room is often more valuable than one perfect moment.</p><p><a href="https://moosky.ai/">Moosky AI</a> is building production orchestration that coordinates specialized AI agents and models while keeping creative work editable, reviewable, and under human approval.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=168e631bf514" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Retries Need a Deadline, Not Just a Timeout]]></title>
            <link>https://medium.com/@udit.rawat03/retries-need-a-deadline-not-just-a-timeout-a7e839b576d8?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7e839b576d8</guid>
            <category><![CDATA[reliability]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[asyncio]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Mon, 16 Nov 2026 03:40:35 GMT</pubDate>
            <atom:updated>2026-11-16T03:40:35.202Z</atom:updated>
            <content:encoded><![CDATA[<h4>Build budget-aware async retries that keep attempts and backoff inside one caller-visible deadline.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_20Em4xKYclCpWU92kzhpA.png" /></figure><p>A timeout on one network call does not put a time limit on the operation the caller experiences. If code makes four attempts with a five-second timeout, then sleeps between them, its real limit is the sum of those attempts and delays. Add connection-pool waits or a server-provided retry interval and the gap grows again.</p><p>The fix is to carry one absolute deadline through the retry loop. Each attempt receives only the time still available. Backoff also consumes that budget. When there is not enough time for another useful attempt, the loop stops instead of starting work that the caller no longer has time to receive.</p><p>This article targets CPython 3.14.7 and uses only the standard library. The core APIs, asyncio.timeout_at() and the event loop’s monotonic clock, have been available since Python 3.11. The examples use a small retry layer rather than an HTTP client so the timing policy stays visible.</p><h3>Per-attempt timeouts multiply</h3><p>A common retry loop gives every call the same timeout:</p><pre>import asyncio<br><br><br>async def unreliable_call(attempt: int) -&gt; str:<br>    await asyncio.sleep(0.18)<br>    raise ConnectionError(f&quot;attempt {attempt} failed&quot;)<br><br><br>async def fetch() -&gt; str:<br>    for attempt in range(1, 4):<br>        try:<br>            async with asyncio.timeout(0.20):<br>                return await unreliable_call(attempt)<br>        except (ConnectionError, TimeoutError):<br>            if attempt == 3:<br>                raise<br>            await asyncio.sleep(0.10)<br>    raise AssertionError(&quot;unreachable&quot;)<br><br><br>async def main() -&gt; None:<br>    loop = asyncio.get_running_loop()<br>    started = loop.time()<br>    try:<br>        await fetch()<br>    except ConnectionError as error:<br>        print(error)<br>    print(f&quot;total: {loop.time() - started:.2f}s&quot;)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    asyncio.run(main())</pre><p>The code is bounded, but not by 0.20 seconds. Three failed calls take about 0.54 seconds, and two backoffs add about 0.20 seconds. The exact output depends on scheduling, which is why elapsed-time assertions should not depend on a perfect decimal. The structural problem does not depend on timing precision: the loop resets its allowance before every attempt.</p><p>Reducing the attempt timeout is not a complete fix. A short attempt limit can reject work that would have succeeded within the caller’s real deadline, while an unbounded backoff can still consume the rest. We need two explicit limits: a total budget for the logical operation and, optionally, a smaller cap for any one attempt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nQ0FaB4Zjb7VokzPA5FQ4w.png" /><figcaption><em>Resetting the timeout per attempt lets the total grow. A shared deadline caps attempts and backoff together.</em></figcaption></figure><h3>Convert the budget to an absolute deadline once</h3><p>Relative durations are useful at an API boundary: “finish within two seconds” is easy to configure. Inside the retry loop, convert that duration once to an absolute value from `loop.time()`. Python documents this as the event loop’s internal monotonic clock. Unlike wall-clock time, it is intended for measuring intervals and scheduling loop work.</p><p>Recomputing now + budget inside the loop would recreate the original bug. The deadline must remain unchanged while attempts and sleeps spend it.</p><p>Here is a complete retry module:</p><pre>from __future__ import annotations<br><br><br>import asyncio<br>import random<br>from collections.abc import Awaitable, Callable<br>from dataclasses import dataclass<br>from typing import TypeVar<br><br>T = TypeVar(&quot;T&quot;)<br><br><br>class RetryBudgetExceeded(TimeoutError):<br>    pass<br><br><br>class RetryableError(Exception):<br>    def __init__(self, message: str, retry_after: float | None = None) -&gt; None:<br>        super().__init__(message)<br>        self.retry_after = retry_after<br><br><br>@dataclass<br>class RetryPolicy:<br>    budget: float<br>    attempt_timeout: float<br>    max_attempts: int<br>    base_delay: float = 0.1<br>    max_delay: float = 2.0<br><br>    def __post_init__(self) -&gt; None:<br>        if self.budget &lt;= 0 or self.attempt_timeout &lt;= 0:<br>            raise ValueError(&quot;timeouts must be positive&quot;)<br>        if self.max_attempts &lt; 1:<br>            raise ValueError(&quot;max_attempts must be at least 1&quot;)<br>        if self.base_delay &lt; 0 or self.max_delay &lt; 0:<br>            raise ValueError(&quot;delays must be non-negative&quot;)<br><br><br>def full_jitter_delay(<br>    attempt: int,<br>    policy: RetryPolicy,<br>    rand: Callable[[float, float], float] = random.uniform,<br>) -&gt; float:<br>    cap = min(policy.max_delay, policy.base_delay * (2 ** (attempt - 1)))<br>    return rand(0.0, cap)<br>async def retry(<br>    operation: Callable[[], Awaitable[T]],<br>    policy: RetryPolicy,<br>    *,<br>    rand: Callable[[float, float], float] = random.uniform,<br>) -&gt; T:<br>    loop = asyncio.get_running_loop()<br>    deadline = loop.time() + policy.budget<br>    last_error: BaseException | None = None<br><br><br>    for attempt in range(1, policy.max_attempts + 1):<br>        remaining = deadline - loop.time()<br>        if remaining &lt;= 0:<br>            raise RetryBudgetExceeded(&quot;retry budget exhausted&quot;) from last_error<br><br>        attempt_deadline = min(deadline, loop.time() + policy.attempt_timeout)<br>        try:<br>            async with asyncio.timeout_at(attempt_deadline):<br>                return await operation()<br>        except (RetryableError, TimeoutError) as error:<br>            last_error = error<br>            if attempt == policy.max_attempts:<br>                raise<br><br>        remaining = deadline - loop.time()<br>        if remaining &lt;= 0:<br>            raise RetryBudgetExceeded(&quot;retry budget exhausted&quot;) from last_error<br><br>        if isinstance(last_error, RetryableError) and last_error.retry_after is not None:<br>            delay = last_error.retry_after<br>        else:<br>            delay = full_jitter_delay(attempt, policy, rand)<br><br>        if delay &lt; 0:<br>            raise ValueError(&quot;retry delay must be non-negative&quot;)<br>        if delay &gt;= remaining:<br>            raise RetryBudgetExceeded(<br>                &quot;not enough budget for the next retry&quot;<br>            ) from last_error<br><br>        await asyncio.sleep(delay)<br><br>    raise AssertionError(&quot;unreachable&quot;)<br><br><br>async def main() -&gt; None:<br>    attempts = 0<br><br>    async def operation() -&gt; str:<br>        nonlocal attempts<br>        attempts += 1<br>        if attempts &lt; 3:<br>            raise RetryableError(&quot;temporary failure&quot;)<br>        return &quot;ready&quot;<br><br>    policy = RetryPolicy(<br>        budget=1.0,<br>        attempt_timeout=0.25,<br>        max_attempts=4,<br>        base_delay=0.05,<br>        max_delay=0.20,<br>    )<br>    result = await retry(operation, policy, rand=lambda low, high: high)<br>    print(result, attempts)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    asyncio.run(main())<br></pre><p>attempt_deadline is the earlier of the overall deadline and the per-attempt cap. The last attempt can therefore receive less time than earlier attempts. That is intentional. Giving it a fresh allowance would violate the caller’s budget.</p><p>The retry boundary is narrow. Only RetryableError and TimeoutError trigger another attempt. A programming error such as TypeError escapes immediately. asyncio.CancelledError also escapes because it directly subclasses BaseException, not Exception. Cancellation from a disconnected request or service shutdown must not be converted into a retry.</p><p>The delay uses full jitter: choose a random value between zero and an exponentially growing cap. Randomness is injected so tests can replace it. The policy also has max_attempts because a generous time budget should not permit unlimited rapid failures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TgpdTc-aCtGR8-6ORfiNpA.png" /><figcaption><em>The loop returns, propagates, or stops early. It never starts an attempt or a sleep the budget cannot cover.</em></figcaption></figure><h3>Retry only failures whose meaning you understand</h3><p>Timing policy cannot decide whether another attempt is correct. That decision belongs to the adapter that understands the dependency. A closed connection before any response, an explicit service-unavailable response, and a local attempt timeout may be reasonable transient categories for a read. Invalid credentials, malformed input, a missing resource, and a rejected business rule usually are not. Putting every `Exception` through the retry loop hides defects and spends the budget on work that cannot succeed.</p><p>The distinction becomes harder for writes. A connection can fail after the remote system commits a change but before the client receives the response. Retrying that request is safe only if the operation is naturally idempotent or the protocol has a stable idempotency key. The retry helper cannot infer either property from an exception type. Its caller must establish the contract.</p><p>I prefer translating library-specific failures at the adapter boundary. The adapter can turn selected transport and response conditions into RetryableError, preserve a server delay when one exists, and let all other errors pass through. This keeps HTTP status rules, database error codes, or SDK exception classes out of a generic timing module. It also gives application code one place to review when a dependency changes its error model.</p><p>Do not use retry classification to erase diagnostics. Chain translated exceptions with raise RetryableError(…) from error, or attach enough structured context for the final failure to explain what happened. The example keeps the custom error small to focus on timing, but production adapters should preserve the original cause.</p><h3>Nested retries need one owner</h3><p>Retries often exist in more than one layer. An SDK may retry connections, a repository may retry the SDK, and a request handler may retry the repository. Each layer can look reasonable in isolation while their attempt counts multiply. Three outer attempts around three inner attempts can issue up to nine calls, and independent backoff policies make the caller-visible duration difficult to predict.</p><p>Choose one layer to own the retry policy for a logical operation. Lower layers can still enforce one-attempt connect, read, or query limits, but they should not silently start a new logical attempt. If a third-party client retries by default, inspect and configure that behavior before adding an application retry loop.</p><p>Deadline propagation matters even when nested components do not retry. Suppose a handler has 800 milliseconds left and calls a repository with a fresh one-second timeout. The repository has already been given more time than its caller can use. Pass the existing absolute deadline within the async call graph, or pass the computed remaining duration into an API that accepts only relative timeouts. Never reconstruct the original budget lower in the stack.</p><p>An absolute event-loop deadline is local process state. Do not serialize `loop.time()` into a queue message or send it to another service because monotonic clocks do not share an epoch across processes. Across a network boundary, send a duration or a protocol-defined wall-clock deadline, validate it, then convert it to a new local monotonic deadline on receipt.</p><h3>A server delay is a constraint, not spare time</h3><p>HTTP Retry-After can be an HTTP date or a non-negative number of seconds. The HTTP adapter should parse that protocol detail and pass a relative delay into the retry layer. Keep wall-clock parsing outside the retry engine, then continue measuring the operation with the loop’s monotonic clock.</p><pre>from datetime import datetime, timezone<br>from email.utils import parsedate_to_datetime<br><br><br>def parse_retry_after(value: str, *, now: datetime | None = None) -&gt; float:<br>    value = value.strip()<br>    if value.isascii() and value.isdigit():<br>        return float(value)<br><br>    current = now or datetime.now(timezone.utc)<br>    retry_at = parsedate_to_datetime(value)<br>    if retry_at.tzinfo is None:<br>        retry_at = retry_at.replace(tzinfo=timezone.utc)<br>    return max(0.0, (retry_at - current).total_seconds())<br><br><br>def main() -&gt; None:<br>    fixed_now = datetime(2026, 11, 16, 12, 0, tzinfo=timezone.utc)<br>    print(parse_retry_after(&quot;15&quot;, now=fixed_now))<br>    print(<br>        parse_retry_after(<br>            &quot;Mon, 16 Nov 2026 12:00:30 GMT&quot;,<br>            now=fixed_now,<br>        )<br>    )<br><br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>This parser handles both forms defined by RFC 9110. Invalid dates raise ValueError, which lets the HTTP adapter decide whether malformed response metadata should be ignored or treated as a protocol error.</p><p>Notice what the retry module does when Retry-After is longer than the remaining budget: it stops. Sleeping for a shorter period would disregard the server’s requested minimum. Sleeping for the full period would break the caller’s deadline. There is no valid retry inside the current operation, so failing is the accurate result.</p><h3>Test decisions, not scheduler precision</h3><p>Timing code needs tests, but tests that sleep for exact intervals are slow and fragile. Most retry behavior can be checked with immediate operations, zero backoff, and injected randomness. Reserve a small number of integration tests for actual timeout behavior.</p><p>Save the previous module as retry_budget.py, then run this test file beside it:</p><pre>import asyncio<br>import unittest<br><br>from retry_budget import (<br>    RetryBudgetExceeded,<br>    RetryPolicy,<br>    RetryableError,<br>    retry,<br>)<br><br><br>class RetryTests(unittest.IsolatedAsyncioTestCase):<br>    async def test_retries_then_returns_value(self) -&gt; None:<br>        attempts = 0<br><br>        async def operation() -&gt; str:<br>            nonlocal attempts<br>            attempts += 1<br>            if attempts &lt; 3:<br>                raise RetryableError(&quot;try again&quot;)<br>            return &quot;ok&quot;<br><br><br>        policy = RetryPolicy(<br>            budget=1.0,<br>            attempt_timeout=0.2,<br>            max_attempts=4,<br>            base_delay=0.0,<br>        )<br>        result = await retry(operation, policy, rand=lambda low, high: 0.0)<br><br>        self.assertEqual(result, &quot;ok&quot;)<br>        self.assertEqual(attempts, 3)<br><br><br>    async def test_rejects_retry_after_beyond_budget(self) -&gt; None:<br>        async def operation() -&gt; str:<br>            raise RetryableError(&quot;busy&quot;, retry_after=5.0)<br><br>        policy = RetryPolicy(<br>            budget=0.1,<br>            attempt_timeout=0.05,<br>            max_attempts=3,<br>        )<br><br>        with self.assertRaises(RetryBudgetExceeded):<br>            await retry(operation, policy)<br><br><br>    async def test_external_cancellation_is_not_retried(self) -&gt; None:<br>        started = asyncio.Event()<br><br>        async def operation() -&gt; str:<br>            started.set()<br>            await asyncio.Event().wait()<br>            return &quot;unreachable&quot;<br><br>        policy = RetryPolicy(<br>            budget=10.0,<br>            attempt_timeout=5.0,<br>            max_attempts=3,<br>        )<br>        task = asyncio.create_task(retry(operation, policy))<br>        await started.wait()<br>        task.cancel()<br><br><br>        with self.assertRaises(asyncio.CancelledError):<br>            await task<br><br><br>if __name__ == &quot;__main__&quot;:<br>    unittest.main()</pre><p>IsolatedAsyncioTestCase gives each test an event loop and accepts coroutine test methods. The first test verifies attempt accounting without real delay. The second proves that a server hint cannot push work past the budget. The third protects a critical cancellation contract: cancelling the caller cancels the in-flight retry task rather than starting another attempt.</p><p>A further test can make an operation sleep past attempt_timeout, but keep the assertion behavioral: expect TimeoutError or another attempt. Do not require completion at an exact millisecond. Event-loop scheduling and machine load are not application contracts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yl3zfUhxAMzYUoSpGyunvw.png" /><figcaption><em>Inject the operation, policy, and randomness to test decisions. Keep real timing in one small integration test.</em></figcaption></figure><h3>Know what the deadline cannot guarantee</h3><p>asyncio.timeout_at() cancels the current task when its deadline expires and transforms the resulting cancellation into TimeoutError outside the context manager. This is cooperative cancellation. If an awaited coroutine catches and suppresses CancelledError, or blocking work continues in a thread, the timeout is not a forceful kill switch.</p><p>The deadline also does not make an unsafe operation safe to retry. Retry reads and other idempotent work by default. For writes, use an idempotency key or another application-level deduplication contract. “The client timed out” does not prove that a remote system made no change.</p><p>Observability should report the logical operation, attempt number, remaining budget, chosen delay, and final reason without changing the policy. Those fields make it possible to distinguish repeated fast failures from one slow attempt. Avoid logging inside a generic helper if your application already has tracing or structured logging at the adapter boundary.</p><p>Choose policy values from the surrounding contract rather than copying round numbers. Start with the caller’s latency allowance, subtract work that must happen after the dependency returns, and use what remains as the retry budget. Set the per-attempt cap from the dependency’s useful response window. Set `max_attempts` to limit request amplification even when failures are immediate. A retry policy is therefore part of capacity and overload control, not only exception handling.</p><p>Budget exhaustion should also remain distinguishable from the final dependency failure. The caller may map both to the same public response, but operations and tests need to know whether the system ran out of time, ran out of attempts, received a non-retryable result, or honored a server delay that did not fit. The example uses RetryBudgetExceeded for the first case and re-raises the last retryable error when the attempt limit is reached.</p><h3>Sources</h3><ul><li>Python 3.14 asyncio timeouts: <a href="https://docs.python.org/3.14/library/asyncio-task.html#timeouts">https://docs.python.org/3.14/library/asyncio-task.html#timeouts</a></li><li>Python 3.14 event loop clock: <a href="https://docs.python.org/3.14/library/asyncio-eventloop.html#asyncio.loop.time">https://docs.python.org/3.14/library/asyncio-eventloop.html#asyncio.loop.time</a></li><li>Python 3.14 asynchronous unittest support: <a href="https://docs.python.org/3.14/library/unittest.html#unittest.IsolatedAsyncioTestCase">https://docs.python.org/3.14/library/unittest.html#unittest.IsolatedAsyncioTestCase</a></li><li>RFC 9110, Retry-After: <a href="https://www.rfc-editor.org/rfc/rfc9110.html#name-retry-after">https://www.rfc-editor.org/rfc/rfc9110.html#name-retry-after</a></li></ul><h3>Before you ship a retry loop</h3><p>Use this pattern when several attempts must fit inside one caller-visible latency budget. Keep a separate per-attempt cap, restrict retries to known transient failures, preserve external cancellation, cap the attempt count, and make backoff consume the same budget. Treat Retry-After as a minimum, not a suggestion to squeeze in one more request.</p><p>Do not add retries when the operation is not safe to repeat, when an upstream layer already retries the same call, or when overload needs backpressure rather than more traffic. A deadline makes retry duration honest. It does not make retrying the right policy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7e839b576d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Do Not Backfill Large Laravel Tables Inside a Migration]]></title>
            <link>https://medium.com/@udit.rawat03/do-not-backfill-large-laravel-tables-inside-a-migration-39882bf31372?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/39882bf31372</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[database-migration]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Sat, 14 Nov 2026 03:51:52 GMT</pubDate>
            <atom:updated>2026-11-14T03:51:52.659Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Use expand, dual-write, restartable backfill, and contract phases for schema changes that must survive mixed application versions.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u2szUI5mLhRpEN-_G1U0YA.png" /><figcaption>Cover: Do Not Backfill Large Laravel Tables Inside a Migration</figcaption></figure><p>A migration is a good place to describe a schema change. It is usually a poor place to run a long data rewrite.</p><p>Consider renaming customers.name to customers.display_name. The direct approach is one migration that renames the column. That is acceptable only when the application can stop completely, every process upgrades together, and the database operation has been tested for the real table and engine. Many production deployments do not have those properties. Web instances overlap during a rolling release, queue workers may still run old code, and rewriting a large table can outlive the deployment command that started it.</p><p>The safer design separates the change into four phases: expand the schema, run compatible code, backfill existing rows, then contract the schema. Each phase has a narrow job and an explicit rollback direction.</p><p>This article targets Laravel 13 and PHP 8.3 or later. The application code is database-neutral, but data definition language behavior is not. Inspect and test the generated SQL on your actual MySQL, PostgreSQL, SQLite, or SQL Server version before treating any schema operation as online.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K5s0fnzGTdzVC68yrXX0lg.png" /><figcaption>One rename split across releases, with a gate before each risky step.</figcaption></figure><h3>Why one migration is the wrong unit of work</h3><p>A migration runs while a deployment is changing the application around it. Combining schema definition, data conversion, and cleanup in one up() method creates several avoidable risks.</p><p>First, the data rewrite has no natural checkpoint. If the process stops after updating part of the table, a normal migration row does not describe which customer was last completed. Second, rollback becomes ambiguous. Reversing a schema operation cannot reconstruct old values that were transformed or deleted. Third, old and new application versions may disagree about which column exists. Finally, a large update can hold locks, create replication lag, produce a large transaction log, or exceed the deployment timeout. The exact effect depends on the database and operation, which is why a framework-level migration cannot promise that a change is operationally cheap.</p><p>The key decision is to decouple compatibility from completion. A deployment should remain correct while some rows and some processes still use the old representation.</p><h3>Phase one: expand without changing meaning</h3><p>Start by adding the new column as nullable. Do not rename or drop the old column yet.</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>use Illuminate\Database\Migrations\Migration;<br>use Illuminate\Database\Schema\Blueprint;<br>use Illuminate\Support\Facades\Schema;<br><br>return new class extends Migration<br>{<br>    public function up(): void<br>    {<br>        Schema::table(&#39;customers&#39;, function (Blueprint $table): void {<br>            $table-&gt;string(&#39;display_name&#39;, 255)-&gt;nullable();<br>        });<br>    }<br><br>    public function down(): void<br>    {<br>        Schema::table(&#39;customers&#39;, function (Blueprint $table): void {<br>            $table-&gt;dropColumn(&#39;display_name&#39;);<br>        });<br>    }<br>};</pre><p>This migration changes capability, not meaning. Existing rows remain valid because display_name may be NULL, and old code can continue reading and writing name. The new column deliberately has the same length as the old one. If the original column has a collation, comment, default, or index that the final column must retain, record those details before the rollout instead of assuming Laravel will infer them later.</p><p>On MySQL, Laravel 13 offers instant() and lock() schema modifiers for compatible operations. They are requests to the database, not universal safety switches. Laravel’s documentation notes that unsupported instant operations or lock modes fail. Use them only after confirming support for the exact server version and table definition. A plain migration in an article cannot choose that policy for every deployment.</p><h3>Phase two: make mixed versions compatible</h3><p>The compatibility release should keep name authoritative and write both columns. This detail matters during a rolling deployment. An old worker can still update only name; if new code preferred an already populated display_name, it could return a stale value. The temporary accessor exposes the new application name while deliberately reading the old physical column:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Models;<br><br>use Illuminate\Database\Eloquent\Casts\Attribute;<br>use Illuminate\Database\Eloquent\Factories\HasFactory;<br>use Illuminate\Database\Eloquent\Model;<br><br>final class Customer extends Model<br>{<br>    use HasFactory;<br><br>    protected function displayName(): Attribute<br>    {<br>        return Attribute::make(<br>            get: static fn (?string $value, array $attributes): string =&gt;<br>                (string) $attributes[&#39;name&#39;],<br>        );<br>    }<br>}<br></pre><p>The unused $value is intentional during this release. Callers can adopt the display_name property without changing which physical value wins while old processes still exist. A later cutover release removes this compatibility accessor and reads the real display_name column. Accessors only shape model reads, and query-builder updates bypass them entirely.</p><p>For writes, use an application action that updates both physical columns:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Actions;<br><br>use App\Models\Customer;<br><br>final class RenameCustomer<br>{<br>    public function execute(Customer $customer, string $displayName): Customer<br>    {<br>        $customer-&gt;forceFill([<br>            &#39;name&#39; =&gt; $displayName,<br>            &#39;display_name&#39; =&gt; $displayName,<br>        ])-&gt;save();<br><br>        return $customer-&gt;refresh();<br>    }<br>}<br></pre><p>The action makes the dual-write period visible. It also gives controllers, commands, and jobs one operation to call. The tradeoff is important: this works only if every writer follows the boundary. Audit direct update() calls, imports, factories, admin tools, scheduled commands, listeners, and raw SQL. Database triggers can enforce dual writes when the writer set cannot be controlled, but they move behavior outside the Laravel codebase and need their own deployment and testing plan.</p><p>Deploy this compatibility release everywhere before relying on the new column. Restart long-running queue workers through the normal deployment process. An old worker that knows only name remains safe because new code still reads name. Do not perform the final reconciliation until deployment evidence shows that no old web process, worker, scheduler, or command can write only the legacy column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xrTmZ0R4FaIsdWq-ssaHVQ.png" /><figcaption>During the compatibility window, name stays authoritative and new code writes both columns.</figcaption></figure><h3>Phase three: backfill as restartable application work</h3><p>The backfill belongs in an explicit command or job, not in the schema migration. It should process bounded batches, be safe to rerun, and update only rows that still need work.</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Console\Commands;<br><br>use App\Models\Customer;<br>use Illuminate\Console\Command;<br>use Illuminate\Database\Eloquent\Collection;<br>use Illuminate\Support\Facades\DB;<br>use InvalidArgumentException;<br><br>final class BackfillCustomerDisplayNames extends Command<br>{<br>    protected $signature = &#39;customers:backfill-display-name {--batch=1000}&#39;;<br><br>    protected $description = &#39;Copy legacy customer names into missing display names&#39;;<br><br>    public function handle(): int<br>    {<br>        $batchSize = filter_var(<br>            $this-&gt;option(&#39;batch&#39;),<br>            FILTER_VALIDATE_INT,<br>            [&#39;options&#39; =&gt; [&#39;min_range&#39; =&gt; 1, &#39;max_range&#39; =&gt; 10000]],<br>        );<br><br>        if ($batchSize === false) {<br>            throw new InvalidArgumentException(&#39;The batch option must be between 1 and 10000.&#39;);<br>        }<br><br>        $totalUpdated = 0;<br><br>        Customer::query()<br>            -&gt;where(function ($query): void {<br>                $query-&gt;whereNull(&#39;display_name&#39;)<br>                    -&gt;orWhereColumn(&#39;display_name&#39;, &#39;!=&#39;, &#39;name&#39;);<br>            })<br>            -&gt;select(&#39;id&#39;)<br>            -&gt;chunkById(<br>                $batchSize,<br>                function (Collection $customers) use (&amp;$totalUpdated): void {<br>                    $updated = DB::table(&#39;customers&#39;)<br>                        -&gt;whereIn(&#39;id&#39;, $customers-&gt;modelKeys())<br>                        -&gt;where(function ($query): void {<br>                            $query-&gt;whereNull(&#39;display_name&#39;)<br>                                -&gt;orWhereColumn(&#39;display_name&#39;, &#39;!=&#39;, &#39;name&#39;);<br>                        })<br>                        -&gt;update([&#39;display_name&#39; =&gt; DB::raw(&#39;name&#39;)]);<br><br>                    $totalUpdated += $updated;<br>                    $this-&gt;line(&quot;Updated {$updated} customers.&quot;);<br>                },<br>                column: &#39;id&#39;,<br>            );<br><br>        $this-&gt;info(&quot;Backfill complete. Updated {$totalUpdated} customers.&quot;);<br><br>        return self::SUCCESS;<br>    }<br>}<br></pre><p>Laravel documents chunkById() for cases where the iteration updates data related to the query filter. It advances by primary key instead of using an offset that can shift while rows leave the result set. The null-or-mismatch predicate is grouped because chunkById() adds its own ID condition. Repeating that predicate in the update avoids rewriting a row that another new process has already synchronized after the batch was selected.</p><p>You may run a preliminary null-only backfill while old versions still exist, but this reconciliation pass belongs after they are gone. Otherwise, an old writer could change name just after its row was processed and recreate a mismatch. After the command finishes, query for nulls and differences again. Only a clean result authorizes the cutover release that reads display_name as the source of truth.</p><p>Each batch uses one set-based update, and every committed batch is a checkpoint. If the command stops, rerunning it skips completed rows. The command is not a throughput guarantee. Choose the batch size from observation, rate-limit between batches when necessary, and monitor database load, lock waits, replication, error rate, and remaining NULL rows. For very large or heavily written tables, a queued coordinator with durable batch state may be better than one console process.</p><p>Do not wrap the entire command in one transaction. That would recreate the long transaction the phased design is trying to avoid. Small transactions reduce the amount of work lost on failure, although they also mean readers can observe a partially completed backfill. The compatibility code makes that intermediate state valid.</p><h3>Prove the compatibility behavior</h3><p>A focused feature test should cover both the old row and the dual-write path:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>use App\Actions\RenameCustomer;<br>use App\Models\Customer;<br><br>it(&#39;reads legacy names and dual-writes renamed customers&#39;, function (): void {<br>    $customer = Customer::factory()-&gt;create([<br>        &#39;name&#39; =&gt; &#39;Legacy Name&#39;,<br>        &#39;display_name&#39; =&gt; null,<br>    ]);<br><br>    expect($customer-&gt;display_name)-&gt;toBe(&#39;Legacy Name&#39;);<br><br>    $renamed = app(RenameCustomer::class)-&gt;execute($customer, &#39;Current Name&#39;);<br><br>    expect($renamed-&gt;name)-&gt;toBe(&#39;Current Name&#39;)<br>        -&gt;and($renamed-&gt;display_name)-&gt;toBe(&#39;Current Name&#39;);<br><br>    Customer::query()<br>        -&gt;whereKey($customer-&gt;getKey())<br>        -&gt;update([&#39;name&#39; =&gt; &#39;Old Worker Name&#39;]);<br><br>    expect($renamed-&gt;refresh()-&gt;display_name)-&gt;toBe(&#39;Old Worker Name&#39;);<br><br>    $this-&gt;assertDatabaseHas(&#39;customers&#39;, [<br>        &#39;id&#39; =&gt; $customer-&gt;getKey(),<br>        &#39;name&#39; =&gt; &#39;Old Worker Name&#39;,<br>        &#39;display_name&#39; =&gt; &#39;Current Name&#39;,<br>    ]);<br>});</pre><p>This test checks the actual transition contract: new writes keep both columns aligned, and a simulated old writer cannot make new code return stale data. The temporary read remains name even though display_name now differs. Add tests for every nonstandard writer found during the audit. A passing model test does not prove that an import using DB::table() follows the action.</p><h3>Phase four: contract only after verification</h3><p>Before making the new column required, verify three things: every deployed process understands display_name, every writer populates it, and no row remains null or differs from name. Then add the database invariant:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>use Illuminate\Database\Migrations\Migration;<br>use Illuminate\Database\Schema\Blueprint;<br>use Illuminate\Support\Facades\DB;<br>use Illuminate\Support\Facades\Schema;<br><br>return new class extends Migration<br>{<br>    public function up(): void<br>    {<br>        $incomplete = DB::table(&#39;customers&#39;)<br>            -&gt;where(function ($query): void {<br>                $query-&gt;whereNull(&#39;display_name&#39;)<br>                    -&gt;orWhereColumn(&#39;display_name&#39;, &#39;!=&#39;, &#39;name&#39;);<br>            })<br>            -&gt;exists();<br><br>        if ($incomplete) {<br>            throw new \LogicException(&#39;display_name reconciliation is incomplete.&#39;);<br>        }<br><br>        Schema::table(&#39;customers&#39;, function (Blueprint $table): void {<br>            $table-&gt;string(&#39;display_name&#39;, 255)-&gt;nullable(false)-&gt;change();<br>        });<br>    }<br><br>    public function down(): void<br>    {<br>        Schema::table(&#39;customers&#39;, function (Blueprint $table): void {<br>            $table-&gt;string(&#39;display_name&#39;, 255)-&gt;nullable()-&gt;change();<br>        });<br>    }<br>};<br></pre><p>The precondition gives a clear failure before the schema builder attempts the change. It is not a substitute for the constraint. A write could race with a check if incompatible writers still exist, which is why deployment order matters.</p><p>Laravel requires every modifier you want to keep to be restated when calling change(). This example preserves the type and length while removing nullability. A real column may also need its default, comment, unsigned state, collation, or other attributes repeated. Inspect the current schema and generated SQL.</p><p>Do not drop name in the same release that first makes display_name required. Remove the read fallback and stop dual-writing in a later application release. Only after that version is fully deployed should a separate destructive migration drop the old column. Keep the rollback window long enough for your deployment policy, because restoring a dropped column does not restore its data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DZiDHVvKFfnjswHJ9NbUkg.png" /><figcaption>Any failed gate stops the rollout before the constraint or the drop.</figcaption></figure><h3>A practical release checklist</h3><p>Use this pattern when a schema change affects existing rows, deployments overlap, workers are long-lived, or the backfill may take longer than the deployment. A direct migration can still be reasonable for a small controlled system with a tested maintenance window and an acceptable rollback plan.</p><p>For a phased change:</p><ol><li>Inventory column attributes, indexes, writers, readers, and database-specific DDL behavior.</li><li>Add the new representation without removing the old one.</li><li>Deploy read compatibility and dual writes to every process type.</li><li>Run a bounded, observable, restartable backfill.</li><li>Verify no old writers remain and no incomplete rows exist.</li><li>Add the new database constraint.</li><li>Remove compatibility code, then drop the old column in a later release.</li></ol><p>The important outcome is not zero downtime as a slogan. It is that every intermediate state has defined behavior, and every destructive step waits for evidence that the previous phase is complete.</p><h3>Sources</h3><ul><li><a href="https://laravel.com/docs/13.x/migrations">Laravel 13 database migrations</a></li><li><a href="https://laravel.com/docs/13.x/eloquent#chunking-results">Laravel 13 Eloquent: chunking results</a></li><li><a href="https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-operations.html">MySQL 8.4 InnoDB online DDL operations</a></li><li><a href="https://www.postgresql.org/docs/current/sql-altertable.html">PostgreSQL ALTER TABLE</a></li></ul><p><em>Udit Rawat is a Senior Software Engineer with more than 10 years of experience and co-founder of Moosky AI.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39882bf31372" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Localize the Timeline Before You Lock the Picture]]></title>
            <link>https://medium.com/@udit.rawat03/localize-the-timeline-before-you-lock-the-picture-5e8a67e63380?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e8a67e63380</guid>
            <category><![CDATA[content-creation]]></category>
            <category><![CDATA[video-production]]></category>
            <category><![CDATA[ai-video]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[localization]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Fri, 13 Nov 2026 04:06:57 GMT</pubDate>
            <atom:updated>2026-11-13T04:06:57.798Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Multilingual video works better when language shapes timing, captions, graphics, and review before the edit becomes expensive to change.</em></h4><figure><img alt="Editorial illustration of one approved script and film timeline branching into three color-coded versions, each pairing the same shots with different audio waveforms and subtitle blocks around a central approval checkpoint." src="https://cdn-images-1.medium.com/max/1024/1*cSFyBsQQ2rKcj7patleqSA.png" /><figcaption><em>AI-generated image created for this article to illustrate a shared video timeline branching into localized versions.</em></figcaption></figure><p>Imagine a lean marketing team finishing a 32-second product film in English. The pacing feels tight. The demonstration lands on the right line, the on-screen copy fits the frame, and the final phrase arrives just as the music resolves. Then the team sends the locked video for Spanish and Japanese versions.</p><p>The translated narration no longer fits the same pauses. One caption needs more room than the lower third allows. A product gesture appears before the matching phrase in one language. The Japanese reviewer wants to restructure a benefit line rather than mirror the English syntax. Every request is reasonable, but the edit was built as if language could be swapped without touching the production.</p><p>This is where localization becomes expensive: not at translation, but at the moment the team discovers that the timeline was never language-neutral.</p><h3>A finished script is still a production variable</h3><p>Localization includes translation, but it also includes adapting content for a particular language, culture, or region. The <a href="https://www.w3.org/International/questions/qa-i18n">W3C’s internationalization guidance</a> makes that distinction directly. In video, the adaptation reaches beyond words. It can affect spoken duration, caption grouping, line breaks, graphics, screen direction, examples, pronunciation, visual emphasis, and the point at which an action should occur.</p><p>Text length is an obvious source of pressure. The W3C’s guidance on <a href="https://www.w3.org/International/articles/article-text-size">text size in translation</a> shows why a layout that fits one language may not fit another, particularly when short source strings expand. The same page also notes that scripts differ in character width and line height. Those are not abstract typography concerns once text is attached to a moving frame with a fixed duration.</p><p>Consider the hypothetical 32-second product film. In the English cut, a voice says the key benefit while a hand completes a three-step action. The caption occupies two short lines, and a callout appears beside the product. A translator can preserve the meaning perfectly and still produce a version that cannot be spoken, read, and seen comfortably inside that shot. Shortening the sentence may weaken a required claim. Speeding up the voice may damage the performance. Shrinking the caption may harm readability. Extending the shot may shift every beat that follows.</p><p>The production decision is therefore larger than “approve the translation.” The team must decide which parts of the message are fixed and which parts of the edit can move.</p><h3>Lock meaning before wording</h3><p>The source script should not become a sacred string of words. It should become an approved meaning specification.</p><p>For each important beat, write down what the audience must understand, what evidence the picture provides, which claims must remain exact, and what tone the line should carry. This gives a language specialist room to write naturally without guessing what can change. It also gives the editor a clear reason to lengthen, split, or move a shot when the localized version needs it.</p><p>For the product film, the team might define the central beat this way:</p><ul><li>Required meaning: the user can complete the setup without specialist help.</li><li>Visual evidence: the three setup actions appear in order.</li><li>Fixed constraint: do not imply a guaranteed completion time.</li><li>Flexible elements: sentence structure, caption breaks, and the duration of the close-up.</li><li>Review owner: a fluent language reviewer who understands the product claim.</li></ul><p>This is more useful than telling a translator to “keep it close to the English.” Close in wording can be far from the intended meaning, and close in duration can still be awkward on screen.</p><h3>Give each language a timing pass</h3><p>A localized script should be read against the actual timeline before final rendering. That pass needs the intended voice style, not just a silent word count. A calm explanation, an excited offer, and a technical instruction create different timing even when the text has similar length.</p><p>Mark where the speaker must enter and leave, where a visual action carries meaning, where a breath is useful, and where silence is part of the edit. Then let the localized performance reveal pressure points. A line may need to start earlier, continue across a cut, or trade places with on-screen text. A shot may need a longer hold. In another language, the same beat may become shorter and leave room for the picture to breathe.</p><p>Captions need their own timing review. The W3C describes captions as synchronized text for prerecorded audio, and its <a href="https://www.w3.org/WAI/WCAG22/Understanding/captions-prerecorded.html">WCAG guidance for captions</a> stresses that the text must provide the audio content people need to understand the video. Netflix’s public <a href="https://partnerhelp.netflixstudios.com/hc/en-us/articles/360051554394-Timed-Text-Style-Guide-Subtitle-Timing-Guidelines">subtitle timing guidance</a> is a useful example of how a distributor treats timing, shot changes, and readable subtitle intervals as production rules rather than an export afterthought.</p><p>A team does not need to copy a broadcaster’s exact specifications for every social clip. It does need an explicit house rule for reading speed, line breaks, sound cues, speaker changes, and collisions with on-screen graphics. Otherwise, each language version quietly invents its own standard near delivery.</p><h3>Treat the frame as shared space</h3><p>Subtitles, product labels, prices, disclaimers, speaker names, and calls to action all compete for a limited area. If the master edit fills every corner with English graphics, localization inherits a geometry problem.</p><p>During the storyboard or rough-cut stage, reserve regions where subtitles can appear without covering the action. Test longer strings rather than the shortest source copy. Keep critical demonstrations away from the lower edge when captions are likely to sit there. If a visual contains baked-in text, decide whether it will be replaced, covered, re-created, or left unchanged for each market.</p><p>The answer may vary by deliverable. A paid social version with open captions has different needs from a web player that supports selectable subtitle tracks. A square crop and a vertical crop may offer different safe areas. These differences should be visible while composition can still change, not discovered after every shot has been approved.</p><p>A simple stress test helps: place deliberately long placeholder text in every planned graphic and caption region. The placeholder is not a prediction of a specific translation. It is a way to expose a frame that only works under ideal English conditions.</p><h3>Build language branches from one approved intent</h3><p>Once localization begins, a production has shared decisions and language-specific decisions. The product claim, visual identity, and required demonstration may remain common. Narration, caption timing, text layout, pronunciation, and even shot duration may branch.</p><p>The branches need clear relationships. When the source team revises a claim, every affected language version should return to review. When a local reviewer changes a phrase for clarity, that choice should not silently rewrite the source meaning for other markets. When an editor extends one localized shot, the change should remain local unless it solves a shared problem.</p><figure><img alt="Diagram: a shared approved intent (beat meaning, product claims, visual identity, required demonstration) branches into language versions that each own narration, caption timing, on-screen text layout and shot duration, with rules for which changes return to review." src="https://cdn-images-1.medium.com/max/1024/1*T_W-KAEPzZX0o4w3MWIofQ.png" /><figcaption><em>Shared intent stays common; each language branch owns its timing and text, and a source claim change sends every branch back to review.</em></figcaption></figure><p>For a small team, this can be managed with a compact language packet for each version:</p><ul><li>approved meaning for every scripted beat;</li><li>localized narration and pronunciation notes;</li><li>timed read or temporary voice track;</li><li>caption file and display rules;</li><li>translated on-screen text;</li><li>market-specific legal or claim review, when required;</li><li>affected shots and timing changes;</li><li>reviewer, decision, and date.</li></ul><p>The packet is useful because it connects language work to the media it changes. A folder containing a document called “Spanish final” cannot show whether the captions match the current voice, whether a later English claim changed, or whether the revised close-up was approved.</p><h3>Review the localized experience, not isolated assets</h3><p>Back-translation can help a team inspect meaning, but it cannot prove that a finished localized video works. The reviewer needs to experience the voice, captions, graphics, and picture together.</p><p>Ask a fluent reviewer to watch the cut at normal speed. Does the narration sound written for that language? Do the captions remain readable while the important action is visible? Does a visual joke, gesture, or example carry the intended meaning? Are product names and technical terms pronounced correctly? Does the final call to action arrive with enough time to understand it?</p><p>The reviewer should also know what authority they have. They may approve linguistic accuracy while a product owner approves claims and an editor approves timing. If those decisions are collapsed into one vague “looks good,” the last reviewer becomes responsible for questions they may not be equipped to answer.</p><p>AI can assist with draft translation, speech generation, subtitle alignment, and variant creation. Those tools increase the number of options a small team can produce. They do not remove the need for fluent review, claim ownership, voice consent where applicable, or a final decision about whether the localized piece communicates naturally.</p><h3>Orchestration should preserve intent across languages</h3><p>The useful unit of work is the campaign beat and its language branches, not a pile of unrelated translated files. Production orchestration can keep the approved meaning, timing dependencies, media variants, and review decisions connected as the work changes.</p><p>This production view also informs <a href="https://moosky.ai/">Moosky AI</a>, which coordinates specialized agents and models while keeping the work editable and reviewable and leaving approval and revision decisions with the user. For localization, the goal is not to press one button and multiply a video into languages. It is to help each version remain faithful to the same creative intent without pretending every language should occupy the same words, frames, or seconds.</p><p>The practical change can start before any new tool is added. Choose one upcoming multilingual video. Before picture lock, create a timed read for each target language, place draft captions over the rough cut, and record which shots or graphics need room to adapt. The exercise will show where the master timeline is too rigid while those decisions are still cheap enough to change.</p><p>A localized video should feel composed for its audience, not squeezed into the leftovers of the source edit.</p><p><a href="https://moosky.ai/">Moosky AI</a> is building production orchestration that coordinates specialized AI agents and models while keeping creative work editable, reviewable, and under human approval.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e8a67e63380" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keep Slow Log Handlers Off the Asyncio Event Loop]]></title>
            <link>https://medium.com/@udit.rawat03/keep-slow-log-handlers-off-the-asyncio-event-loop-f4b42192fd77?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4b42192fd77</guid>
            <category><![CDATA[asyncio]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[observability]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Thu, 12 Nov 2026 07:04:46 GMT</pubDate>
            <atom:updated>2026-11-12T07:04:46.792Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q5NpCU4WKmXg745P01RtsQ.png" /><figcaption>Direct logging blocks the asyncio event-loop thread, while QueueHandler and QueueListener move slow delivery onto a listener thread.</figcaption></figure><p><em>Use QueueHandler and QueueListener to separate log creation from slow delivery without hiding overload or shutdown policy.</em></p><p>A call to logger.info() looks harmless inside a coroutine. It is ordinary synchronous Python, however, and the handlers attached to that logger run before the call returns. A handler that writes to a congested socket, rotates a file, sends an email, or waits on another slow destination can therefore hold the event-loop thread.</p><p>The fix is not to wrap every logging call in asyncio.to_thread(). Python already has a logging boundary for this job: QueueHandler puts records onto a queue, and QueueListener consumes them on a dedicated thread before invoking the real handlers.</p><p>This article targets CPython 3.14.7 and uses only the standard library. The API is small. The engineering work is deciding what gets queued, what happens when producers outrun the listener, and how shutdown proves that accepted records were handled.</p><h3>A synchronous handler blocks the coroutine that logged</h3><p>The following program uses a deliberately slow handler to make the execution boundary visible:</p><pre>import asyncio<br>import logging<br>import time<br><br><br>class SlowHandler(logging.Handler):<br>    def emit(self, record: logging.LogRecord) -&gt; None:<br>        time.sleep(0.15)<br>        print(self.format(record))<br><br><br>async def handle_request(logger: logging.Logger) -&gt; None:<br>    started = time.perf_counter()<br>    logger.info(&quot;request finished&quot;)<br>    elapsed = time.perf_counter() - started<br>    print(f&quot;logging call took {elapsed:.3f} seconds&quot;)<br><br><br>def main() -&gt; None:<br>    logger = logging.getLogger(&quot;blocking-demo&quot;)<br>    logger.handlers.clear()<br>    logger.propagate = False<br>    logger.setLevel(logging.INFO)<br><br>    handler = SlowHandler()<br>    handler.setFormatter(logging.Formatter(&quot;%(levelname)s %(message)s&quot;))<br>    logger.addHandler(handler)<br><br>    asyncio.run(handle_request(logger))<br><br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>SlowHandler.emit() runs on the same thread that called logger.info(). Its sleep completes before logger.info() returns, so the coroutine cannot yield to other tasks during that interval. The example uses time.sleep() only to provide a repeatable slow sink. Real handlers can wait for filesystem or network work for reasons outside the application.</p><p>A lock around the handler would protect shared state, but it would not move the work away from the event loop. Converting emit() into async def would not help either because the logging framework calls handlers synchronously. The useful change is to make the handler on the producer side perform only a queue operation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0fs1EgtqZsf9I0DasVcomw.png" /><figcaption>Side-by-side comparison showing a direct log handler blocking the asyncio event-loop thread while a QueueHandler passes the record to a QueueListener thread for slow delivery.</figcaption></figure><p><em>A direct handler runs on the event-loop thread. A QueueHandler returns after enqueueing, and a QueueListener thread performs the slow delivery.</em></p><h3>Put the delivery work behind a queue</h3><p>QueueHandler prepares a LogRecord and enqueues it. QueueListener waits for records in an internal thread and passes each one to its downstream handlers. In Python 3.14, QueueListener is also a context manager: entering starts the listener, and exiting stops it.</p><pre>import asyncio<br>import logging<br>import queue<br>from logging.handlers import QueueHandler, QueueListener<br><br><br>def configure_logging() -&gt; tuple[logging.Logger, QueueListener]:<br>    records: queue.SimpleQueue[logging.LogRecord] = queue.SimpleQueue()<br><br>    queue_handler = QueueHandler(records)<br><br>    console_handler = logging.StreamHandler()<br>    console_handler.setLevel(logging.INFO)<br>    console_handler.setFormatter(<br>        logging.Formatter(&quot;%(asctime)s %(levelname)s %(message)s&quot;)<br>    )<br><br>    listener = QueueListener(<br>        records,<br>        console_handler,<br>        respect_handler_level=True,<br>    )<br><br>    logger = logging.getLogger(&quot;queued-demo&quot;)<br>    logger.handlers.clear()<br>    logger.addHandler(queue_handler)<br>    logger.setLevel(logging.INFO)<br>    logger.propagate = False<br>    return logger, listener<br><br><br>async def process_item(item_id: int, logger: logging.Logger) -&gt; None:<br>    await asyncio.sleep(0.01)<br>    logger.info(&quot;processed item %s&quot;, item_id)<br><br><br>async def run(logger: logging.Logger) -&gt; None:<br>    await asyncio.gather(<br>        *(process_item(item_id, logger) for item_id in range(5))<br>    )<br><br><br>def main() -&gt; None:<br>    logger, listener = configure_logging()<br>    with listener:<br>        asyncio.run(run(logger))<br><br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>The coroutine now performs record creation, QueueHandler preparation, and a queue insertion. Standard preparation merges the message and its arguments on the producer thread. The downstream handler applies its output formatting and performs delivery on the listener thread. This reduces the work on the event-loop thread, but it does not make logging free. Record preparation, queue contention, and memory allocation still happen on the producer side.</p><p>respect_handler_level=True makes the listener check each downstream handler&#39;s level before handing it a record. Without it, the listener offers every queued record to every handler, matching the class&#39;s older default behavior. Filters and levels on the producer logger still apply before the record reaches the queue.</p><p>One listener also creates one delivery lane. It receives a record and offers that record to its downstream handlers from the same internal thread. If the first downstream handler waits, later handlers and later records wait behind it. That may be exactly what you want when destinations should observe the same ordering. It is weaker isolation when a remote alert handler must not delay a local file handler.</p><p>For independently slow destinations, consider separate queues and listeners. Each producer-side QueueHandler can feed one delivery lane, with its own capacity, filtering, and failure policy. The cost is duplicated record preparation, more threads, and a larger shutdown surface. Do not split handlers merely because the API allows it. Split them when the destinations have meaningfully different latency, durability, or overload requirements, and test the ordering that callers or operators actually depend on.</p><p>The context manager is more than convenient syntax. A listener that is never started leaves records sitting in the queue. A listener that is not stopped can leave accepted records unprocessed when the process exits. Put the listener’s lifetime around the tasks that can log, then stop it before closing files, sockets, or other resources used by downstream handlers.</p><h3>The queue moves latency, not capacity</h3><p>An unbounded SimpleQueue keeps producer code simple, but it permits the backlog to grow when records arrive faster than handlers can deliver them. A bounded queue.Queue limits memory growth, but QueueHandler uses put_nowait() by default. The Python documentation states that a full queue reaches handleError(), which may drop the record silently when logging.raiseExceptions is false or report an error to standard error when it is true.</p><p>That is not an overload policy I would leave implicit. Choose one and make it observable:</p><ol><li>Use an unbounded in-process queue when bursts are limited by another control and memory risk is acceptable.</li><li>Use a bounded queue with an explicit dropped-record counter when preserving application responsiveness matters more than every low-severity log.</li><li>Block or wait briefly only if delaying the producer is acceptable. On an event loop, that choice reintroduces the latency the queue was meant to isolate.</li><li>Route audit records through a durable system with its own acceptance contract. Application logging is not automatically a lossless audit trail.</li></ol><p>A bounded queue also changes shutdown behavior. The listener must be able to enqueue its stop sentinel. If the queue is full, custom enqueue and stop behavior need to agree rather than treating queue.Full as an incidental exception. Test this path under a stalled sink before relying on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWpNDY7-7LvXq-_RPlxWKg.png" /><figcaption>Flow diagram of log producers, queue capacity, a listener, and explicit choices for handling a full queue, including drop, block, spill, and reroute.</figcaption></figure><p><em>Queued logging moves delivery latency. A full queue still needs an explicit drop, block, spill, or reroute policy.</em></p><h3>Know what QueueHandler changes</h3><p>The standard QueueHandler.prepare() implementation formats the record before queuing it, merges the message and arguments, and clears fields including args, exc_info, and exc_text. This makes the record safer to pickle and avoids a second formatting pass, but it means a downstream handler no longer has the original exception tuple or structured message arguments.</p><p>For a strictly in-process queue, a small subclass can preserve the original fields by queuing a shallow copy:</p><pre>import copy<br>import logging<br>import queue<br>from logging.handlers import QueueHandler, QueueListener<br><br><br>class LocalQueueHandler(QueueHandler):<br>    def prepare(self, record: logging.LogRecord) -&gt; logging.LogRecord:<br>        return copy.copy(record)<br><br><br>def main() -&gt; None:<br>    records: queue.SimpleQueue[logging.LogRecord] = queue.SimpleQueue()<br>    queue_handler = LocalQueueHandler(records)<br><br>    console_handler = logging.StreamHandler()<br>    console_handler.setFormatter(<br>        logging.Formatter(&quot;%(levelname)s %(message)s&quot;)<br>    )<br><br>    listener = QueueListener(records, console_handler)<br><br>    logger = logging.getLogger(&quot;local-record-demo&quot;)<br>    logger.handlers.clear()<br>    logger.addHandler(queue_handler)<br>    logger.setLevel(logging.INFO)<br>    logger.propagate = False<br><br>    with listener:<br>        try:<br>            1 / 0<br>        except ZeroDivisionError:<br>            logger.exception(&quot;calculation failed for item %s&quot;, 42)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>The copy prevents preparation from mutating the record seen by other handlers. Because the queue stays inside one process, this example does not need to pickle the record. The downstream formatter retains access to the original arguments and exception information.</p><p>This override has limits. It is a shallow copy, so mutable objects referenced by the record are still shared. More importantly, it is not suitable as written for a multiprocessing.Queue: message arguments or custom record attributes may not be pickleable. Cross-process logging needs a deliberate serialization format and a rule for every extra field.</p><p>The standard documentation also warns against sending the internal multiprocessing logger through a QueueHandler that uses the same multiprocessing.Queue. Queue operations can emit debug logs, creating recursion or deadlock. An in-process queue.SimpleQueue, as used above, does not have that specific relationship.</p><h3>Test the boundary without timing guesses</h3><p>A useful test should show that the producer can return while the downstream handler is blocked, then prove that listener shutdown drains an already accepted record. Events make those states explicit without asserting that a machine finishes within a tiny performance threshold.</p><pre>import logging<br>import queue<br>import threading<br>import unittest<br>from logging.handlers import QueueHandler, QueueListener<br><br><br>class GateHandler(logging.Handler):<br>    def __init__(self) -&gt; None:<br>        super().__init__()<br>        self.entered = threading.Event()<br>        self.release = threading.Event()<br>        self.messages: list[str] = []<br><br>    def emit(self, record: logging.LogRecord) -&gt; None:<br>        self.entered.set()<br>        if not self.release.wait(timeout=2.0):<br>            raise TimeoutError(&quot;test did not release the handler&quot;)<br>        self.messages.append(record.getMessage())<br><br><br>class QueueLoggingTests(unittest.TestCase):<br>    def test_producer_returns_and_context_exit_drains(self) -&gt; None:<br>        records: queue.SimpleQueue[logging.LogRecord] = queue.SimpleQueue()<br>        queue_handler = QueueHandler(records)<br>        gate_handler = GateHandler()<br>        listener = QueueListener(records, gate_handler)<br><br>        logger = logging.getLogger(&quot;queue-test&quot;)<br>        logger.handlers.clear()<br>        logger.addHandler(queue_handler)<br>        logger.setLevel(logging.INFO)<br>        logger.propagate = False<br><br>        with listener:<br>            logger.info(&quot;first&quot;)<br>            self.assertTrue(gate_handler.entered.wait(timeout=1.0))<br>            try:<br>                logger.info(&quot;second&quot;)<br>            finally:<br>                gate_handler.release.set()<br><br>        self.assertEqual(gate_handler.messages, [&quot;first&quot;, &quot;second&quot;])<br><br><br>if __name__ == &quot;__main__&quot;:<br>    unittest.main()</pre><p>The first event proves that the listener thread entered the real handler. While that handler waits, the second logger.info() still returns and places its record behind the first. Releasing the gate lets the listener continue. Exiting the context stops the listener, and the final assertion proves that the second accepted record was handled before shutdown completed.</p><p>The finally block prevents a failed assertion from leaving the listener thread blocked. The two-second timeout is a test escape hatch, not a performance claim. For production code, add tests for a full bounded queue, a downstream handler that raises, repeated startup or shutdown calls, and logging during application teardown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BymFkdUzksfM7BjX3yV2mg.png" /><figcaption>Sequence diagram showing a QueueListener starting before request tasks, draining queued records after producers finish, and stopping before the output resource closes.</figcaption></figure><p><em>Start the listener before accepting work, drain accepted records, then close downstream resources.</em></p><h3>Treat queued logging as a small subsystem</h3><p>Queued logging is a good fit when synchronous handlers can wait on I/O and the calling thread must remain responsive. It is especially useful around asyncio services because one blocked event-loop thread delays unrelated coroutines on that loop.</p><p>It is not a durability layer. A process crash can lose records still held in memory. It is also not automatic backpressure. An unbounded queue accepts growth, while a bounded queue forces a drop, block, spill, or reject decision.</p><p>Before shipping the configuration, check these points:</p><ol><li>Attach only QueueHandler to loggers used on latency-sensitive paths.</li><li>Keep the queue and listener owned by application startup and shutdown code.</li><li>Start the listener before accepting work and stop it after producers finish logging.</li><li>Set levels and filters deliberately on both producer and downstream sides.</li><li>Choose a queue capacity and overload policy, then expose dropped records or backlog where applicable.</li><li>Decide whether standard record preparation is sufficient before depending on structured arguments or custom exception formatting.</li><li>Keep cross-process serialization and the multiprocessing internal logger warning in scope if processes share the pipeline.</li><li>Test blocked handlers and shutdown ordering with synchronization primitives, not fragile timing assertions.</li></ol><p>The central design change is simple: application code creates a record, while another owned component delivers it. Once that boundary is explicit, slow sinks no longer have to occupy the event-loop thread, and overload and shutdown become policies you can inspect instead of surprises hidden inside logger.info().</p><h3>Sources</h3><ul><li><a href="https://docs.python.org/3.14/library/logging.handlers.html#queuehandler">Python 3.14 QueueHandler and QueueListener documentation</a></li><li><a href="https://docs.python.org/3.14/howto/logging-cookbook.html#dealing-with-handlers-that-block">Python 3.14 Logging Cookbook: Dealing with handlers that block</a></li><li><a href="https://docs.python.org/3.14/library/logging.html">Python 3.14 logging documentation</a></li></ul><p><em>Udit Rawat is a Senior Software Engineer with more than 10 years of experience and co-founder of Moosky AI.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4b42192fd77" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prevent Laravel Deadlocks With Consistent Row Lock Ordering]]></title>
            <link>https://medium.com/@udit.rawat03/prevent-laravel-deadlocks-with-consistent-row-lock-ordering-172aea0ea07e?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/172aea0ea07e</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[database-transaction]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Wed, 11 Nov 2026 03:12:05 GMT</pubDate>
            <atom:updated>2026-11-11T03:12:05.564Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rte885Yr-YQjgi0YfuCdTQ.png" /><figcaption>Two transfers locking rows in conflicting order versus one ascending order.</figcaption></figure><p><em>Acquire database rows in one canonical order, keep transactions small, and treat retries as a safety net rather than the design.</em></p><p>A database deadlock is not the same as a slow query. It is a cycle: one transaction holds a lock that another transaction needs, while the second transaction holds a lock that the first one needs. Neither can continue. The database detects the cycle and aborts one participant so the other can finish.</p><p>Laravel can retry a transaction that loses this contest. That is useful, but retrying alone leaves the application generating avoidable deadlocks under load. The stronger design is to make every code path acquire the same set of locks in the same order. Retries then handle the deadlocks that remain, rather than compensating for an inconsistent locking policy.</p><p>This article targets Laravel 13 and PHP 8.3 or later. The example transfers an integer number of cents between two accounts. It uses Eloquent, DB::transaction, and lockForUpdate(). The same lock-ordering rule applies to inventory moves, reservation swaps, parent-child updates, and any operation that locks more than one row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j-yPguSr5VlvayOj-O1C1g.png" /><figcaption>Two opposite account transfers each hold one row lock and wait for the other row, forming a deadlock cycle.</figcaption></figure><p><em>Locking the source first makes the lock order depend on transfer direction.</em></p><h3>The deadlock is in the order, not the transfer</h3><p>Consider two requests running at almost the same time:</p><ul><li>Request A transfers from account 10 to account 20.</li><li>Request B transfers from account 20 to account 10.</li></ul><p>A natural implementation locks the source account first and the destination account second:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Actions;<br><br>use App\Models\Account;<br>use Illuminate\Support\Facades\DB;<br>use InvalidArgumentException;<br><br>final class UnsafeTransferFunds<br>{<br>    public function execute(int $fromId, int $toId, int $amountCents): void<br>    {<br>        if ($fromId === $toId || $amountCents &lt;= 0) {<br>            throw new InvalidArgumentException(&#39;Transfer details are invalid.&#39;);<br>        }<br><br>        DB::transaction(function () use ($fromId, $toId, $amountCents): void {<br>            $source = Account::query()<br>                -&gt;whereKey($fromId)<br>                -&gt;lockForUpdate()<br>                -&gt;firstOrFail();<br><br>            $destination = Account::query()<br>                -&gt;whereKey($toId)<br>                -&gt;lockForUpdate()<br>                -&gt;firstOrFail();<br><br>            if ($source-&gt;balance_cents &lt; $amountCents) {<br>                throw new InvalidArgumentException(&#39;Insufficient funds.&#39;);<br>            }<br><br>            $source-&gt;decrement(&#39;balance_cents&#39;, $amountCents);<br>            $destination-&gt;increment(&#39;balance_cents&#39;, $amountCents);<br>        });<br>    }<br>}</pre><p>The transaction protects each transfer from reading balances while another transfer changes them. The problem is that “source first” is not a global order. For request A, source first means account 10. For request B, source first means account 20.</p><p>One timing creates the cycle. A locks 10. B locks 20. A waits for 20. B waits for 10. MySQL InnoDB and PostgreSQL both detect deadlocks and abort one transaction, but the application still pays for rolled-back work, an exception or retry, and additional lock contention.</p><p>Removing lockForUpdate() is not a fix. Without an appropriate concurrency rule, two requests can read the same balance and both approve changes based on stale state. The lock is doing useful work. The acquisition policy is wrong.</p><h3>Define a canonical order independent of direction</h3><p>The application needs one rule that every transfer follows. For numeric primary keys, ascending account ID is a straightforward choice. Both transfers between accounts 10 and 20 must lock 10, then 20, regardless of which account sends money.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x4Pi6M50fX1k9qSRFP4E6g.png" /><figcaption>Both opposite transfers acquire account 10 before account 20, so neither can hold account 20 while waiting for account 10.</figcaption></figure><p><em>Ascending account ID gives every transfer the same lock order.</em></p><p>Here is a corrected action:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Actions;<br><br>use App\Models\Account;<br>use App\Models\Transfer;<br>use Illuminate\Database\Eloquent\Collection;<br>use Illuminate\Support\Facades\DB;<br>use InvalidArgumentException;<br><br>final class TransferFunds<br>{<br>    public function execute(int $fromId, int $toId, int $amountCents): Transfer<br>    {<br>        if ($fromId === $toId) {<br>            throw new InvalidArgumentException(&#39;Accounts must be different.&#39;);<br>        }<br><br>        if ($amountCents &lt;= 0) {<br>            throw new InvalidArgumentException(&#39;Amount must be positive.&#39;);<br>        }<br><br>        return DB::transaction(<br>            function () use ($fromId, $toId, $amountCents): Transfer {<br>                $accounts = $this-&gt;lockAccounts($fromId, $toId);<br>                $source = $accounts-&gt;get($fromId);<br>                $destination = $accounts-&gt;get($toId);<br><br>                if (! $source instanceof Account || ! $destination instanceof Account) {<br>                    throw new InvalidArgumentException(&#39;An account does not exist.&#39;);<br>                }<br><br>                if ($source-&gt;balance_cents &lt; $amountCents) {<br>                    throw new InvalidArgumentException(&#39;Insufficient funds.&#39;);<br>                }<br><br>                $source-&gt;decrement(&#39;balance_cents&#39;, $amountCents);<br>                $destination-&gt;increment(&#39;balance_cents&#39;, $amountCents);<br><br>                return Transfer::query()-&gt;create([<br>                    &#39;from_account_id&#39; =&gt; $fromId,<br>                    &#39;to_account_id&#39; =&gt; $toId,<br>                    &#39;amount_cents&#39; =&gt; $amountCents,<br>                ]);<br>            },<br>            attempts: 5,<br>        );<br>    }<br><br>    /** @return Collection&lt;int, Account&gt; */<br>    private function lockAccounts(int $leftId, int $rightId): Collection<br>    {<br>        $accounts = new Collection();<br>        $ids = [$leftId, $rightId];<br>        sort($ids, SORT_NUMERIC);<br><br>        foreach ($ids as $id) {<br>            $account = Account::query()<br>                -&gt;whereKey($id)<br>                -&gt;lockForUpdate()<br>                -&gt;first();<br><br>            if ($account instanceof Account) {<br>                $accounts-&gt;put($id, $account);<br>            }<br>        }<br><br>        return $accounts;<br>    }<br>}</pre><p>sort() runs before either locking query. The helper then requests the two rows in ascending ID order. It stores the models by ID so the business meaning remains intact: the lower ID is not necessarily the source. Lock order and transfer direction are separate concerns.</p><p>The helper issues two small queries instead of one WHERE IN query. That extra round trip makes the lock acquisition sequence explicit. A single query with orderBy(&#39;id&#39;)-&gt;lockForUpdate() can be appropriate, but its locking behavior must be understood for the production database, chosen plan, indexes, and isolation level. Explicitly acquiring one known primary-key row at a time is easier to review when the set is small.</p><p>The transaction passes attempts: 5. Laravel documents this argument as a deadlock retry limit. If all attempts are exhausted, Laravel throws the exception. Five is an example, not a universal setting. A retry repeats the transaction closure, so the closure must contain only work that is safe to roll back and run again.</p><h3>Keep irreversible effects outside the retried closure</h3><p>Database changes on the same connection roll back when a deadlock victim is aborted. An HTTP request, email, file upload, or call to a payment provider does not. If the closure performs one of those effects and Laravel retries it, the outside system may receive the operation more than once.</p><p>Keep the transaction focused on database state, then arrange follow-up work after commit. A job that depends on the committed transfer can be dispatched like this:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Services;<br><br>use App\Actions\TransferFunds;<br>use App\Jobs\SendTransferReceipt;<br>use App\Models\Transfer;<br><br>final class TransferService<br>{<br>    public function __construct(private readonly TransferFunds $transferFunds)<br>    {<br>    }<br><br>    public function transfer(int $fromId, int $toId, int $amountCents): Transfer<br>    {<br>        $transfer = $this-&gt;transferFunds-&gt;execute(<br>            fromId: $fromId,<br>            toId: $toId,<br>            amountCents: $amountCents,<br>        );<br><br>        SendTransferReceipt::dispatch($transfer-&gt;getKey())-&gt;afterCommit();<br><br>        return $transfer;<br>    }<br>}</pre><p>The action returns only after its transaction commits. afterCommit() also protects this service if it is later called inside a larger transaction: Laravel waits for the open parent transaction to commit before dispatching the job. If that transaction rolls back, the pending dispatch is discarded.</p><p>This timing rule does not make the job exactly once. Queue delivery and worker execution can still repeat. The job needs its own idempotency design when duplicate receipts or another repeated effect would be harmful. Lock ordering solves database contention inside the transfer. It does not replace queue safety.</p><p>Also avoid logging a deadlock as an application defect on every internal retry. Laravel retries inside DB::transaction; code outside the call sees only the final result. Monitor database deadlocks and final failures, but do not create noisy alerts that hide whether the retry succeeded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D3vEdgw5T5gTtcepa5vtoA.png" /><figcaption>A deadlocked database transaction rolls back and retries, while the receipt job is dispatched only after a successful commit.</figcaption></figure><p><em>The retried closure holds only database work; the receipt job starts after commit.</em></p><h3>Keep transactions short and indexed</h3><p>Consistent order removes a common circular wait, but it does not eliminate every possible deadlock. Other transactions may lock the same rows through another path, touch additional tables, or lock index ranges. MySQL notes that deadlocks can involve row and gap locks. PostgreSQL likewise warns that row-level locks can deadlock even without explicit table locking.</p><p>Every writer therefore needs to follow the policy. If a console command locks accounts in descending order while the HTTP action uses ascending order, the cycle is back. Put multi-account mutation behind one application boundary, or give every boundary the same shared lock helper.</p><p>The lookup columns also need suitable indexes. whereKey() uses the model primary key, which is the right access path for this example. A broad WHERE status = &#39;open&#39; FOR UPDATE can lock more rows than expected when the predicate is poorly indexed. Inspect the generated SQL and query plan for the actual database engine.</p><p>Do validation and remote reads before opening the transaction when they do not depend on locked state. Inside the transaction, lock the required rows, recheck state that can change, write the result, and return. Do not hold database locks while waiting for an HTTP API or performing slow serialization.</p><h3>Make the ordering rule an application contract</h3><p>A private helper is enough for one action, but the rule becomes fragile when several features can lock the same records. Document the canonical order beside the application boundary and give other writers one shared way to acquire those locks. A method named lockAccountsInIdOrder() communicates more than a generic repository method named findForUpdate() because it exposes the concurrency policy to the caller and reviewer.</p><p>Some operations lock different resource types. An inventory move may lock a warehouse row, two stock rows, and a reservation. In that case, define both a resource order and a row order, for example: warehouse first, stock rows by product ID second, reservation last. Every competing transaction must follow the same sequence. Sorting IDs inside each repository is not enough if two services call the repositories in opposite orders.</p><p>Keep the policy narrow. Do not create a global lock coordinator for every model before the application needs one. Start with the set of operations that actually contend, name their shared resources, and test those paths together. If the resource set grows dynamically, sort the complete identifier list before acquiring the first lock. Discovering and locking one row, doing more work, then discovering another row makes the final order dependent on runtime timing again.</p><p>Code review should treat a new lockForUpdate() call as a change to this contract. The relevant question is not only whether the query is inside a transaction. Ask which other transactions can touch the same rows, which lock each path acquires first, and whether the new query expands the locked set or duration.</p><h3>Test invariants, then run a real concurrency probe</h3><p>A normal feature test can prove balance and rollback behavior. It cannot prove the absence of deadlocks because one PHP process executes the calls sequentially.</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>use App\Actions\TransferFunds;<br>use App\Models\Account;<br>use Illuminate\Foundation\Testing\RefreshDatabase;<br><br>uses(RefreshDatabase::class);<br><br>it(&#39;moves funds and preserves the combined balance&#39;, function (): void {<br>    $source = Account::factory()-&gt;create([&#39;balance_cents&#39; =&gt; 10_000]);<br>    $destination = Account::factory()-&gt;create([&#39;balance_cents&#39; =&gt; 2_500]);<br><br>    $transfer = app(TransferFunds::class)-&gt;execute(<br>        fromId: $source-&gt;id,<br>        toId: $destination-&gt;id,<br>        amountCents: 1_500,<br>    );<br><br>    expect($source-&gt;fresh()-&gt;balance_cents)-&gt;toBe(8_500)<br>        -&gt;and($destination-&gt;fresh()-&gt;balance_cents)-&gt;toBe(4_000)<br>        -&gt;and($transfer-&gt;amount_cents)-&gt;toBe(1_500)<br>        -&gt;and(<br>            $source-&gt;fresh()-&gt;balance_cents<br>            + $destination-&gt;fresh()-&gt;balance_cents,<br>        )-&gt;toBe(12_500);<br>});<br><br>it(&#39;rolls back when the source lacks funds&#39;, function (): void {<br>    $source = Account::factory()-&gt;create([&#39;balance_cents&#39; =&gt; 500]);<br>    $destination = Account::factory()-&gt;create([&#39;balance_cents&#39; =&gt; 2_500]);<br><br>    expect(fn () =&gt; app(TransferFunds::class)-&gt;execute(<br>        fromId: $source-&gt;id,<br>        toId: $destination-&gt;id,<br>        amountCents: 1_500,<br>    ))-&gt;toThrow(\InvalidArgumentException::class);<br><br>    expect($source-&gt;fresh()-&gt;balance_cents)-&gt;toBe(500)<br>        -&gt;and($destination-&gt;fresh()-&gt;balance_cents)-&gt;toBe(2_500);<br>});</pre><p>The first test checks the accepted mutation and the conserved total. The second proves that a rejected transfer leaves both rows unchanged. Run these tests against the same database engine used in production when locking behavior matters. SQLite is useful for many feature tests, but it is not evidence for InnoDB or PostgreSQL row-lock behavior.</p><p>Add a separate concurrency probe that starts opposite transfers on two database connections at the same time. Record completion, deadlock retries, final balances, and transfer rows. Such a test belongs in an integration suite because it needs real concurrent processes and production-like transaction semantics. Do not make a single-process test sleep and call it a concurrency test.</p><h3>Practical review checklist</h3><p>Use pessimistic row locks when a decision depends on mutable state that must not change before the write. When one operation locks several rows, define a canonical order based on stable identifiers, not on request roles such as source and destination.</p><p>Before shipping the path, verify:</p><ul><li>Every code path that locks the same row set uses the same ordering rule.</li><li>Rows are found through selective, indexed predicates.</li><li>The transaction performs no irreversible remote side effects.</li><li>Retried closures are safe to roll back and execute again.</li><li>Business state is rechecked after locks are acquired.</li><li>Transactions stay small and do not wait on network calls.</li><li>Retry exhaustion remains a visible failure.</li><li>Feature tests cover state invariants, while integration tests use the production database engine for real concurrency.</li></ul><p>Consistent lock ordering prevents one transaction from holding the “second” row while waiting for the “first.” It reduces a structural source of deadlocks. Keep the retry limit anyway, because the database has more participants and lock types than one action can control.</p><h3>Sources</h3><ul><li><a href="https://laravel.com/docs/13.x/database#handling-deadlocks">Laravel 13 database documentation: transactions and deadlock retries</a></li><li><a href="https://laravel.com/docs/13.x/queries#pessimistic-locking">Laravel 13 query builder documentation: pessimistic locking</a></li><li><a href="https://laravel.com/docs/13.x/queues#jobs-and-database-transactions">Laravel 13 queue documentation: dispatching after database transactions</a></li><li><a href="https://dev.mysql.com/doc/refman/8.4/en/innodb-deadlocks.html">MySQL 8.4 Reference Manual: deadlocks in InnoDB</a></li><li><a href="https://www.postgresql.org/docs/current/explicit-locking.html#LOCKING-DEADLOCKS">PostgreSQL documentation: explicit locking and deadlocks</a></li></ul><p><em>Udit Rawat is a Senior Software Engineer with more than 10 years of experience and co-founder of Moosky AI.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=172aea0ea07e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Your AI Video Workflow Needs a Model Exit Plan]]></title>
            <link>https://medium.com/@udit.rawat03/your-ai-video-workflow-needs-a-model-exit-plan-a40b3e8a8cf6?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/a40b3e8a8cf6</guid>
            <category><![CDATA[content-creation]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[generative-ai]]></category>
            <category><![CDATA[creative-workflow]]></category>
            <category><![CDATA[video-production]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Tue, 10 Nov 2026 20:26:06 GMT</pubDate>
            <atom:updated>2026-11-10T20:26:06.006Z</atom:updated>
            <content:encoded><![CDATA[<h4>When a generation API disappears, preserved intent, assets, approvals, and fallback criteria keep the production alive.</h4><figure><img alt="Abstract production assets flow into a routing junction where one rendering path is disconnected and two alternatives converge on an approved film frame." src="https://cdn-images-1.medium.com/max/1024/1*DgbD4I6vLUEp5H1uS2TiPQ.png" /><figcaption>AI-generated image created for this article to illustrate a provider-independent creative-production route.</figcaption></figure><p>Imagine a small agency halfway through a campaign. The script is approved, the visual references are cleared, several shots are in review, and one difficult transition is finally working. Then the video model behind that transition is retired. The endpoint no longer accepts jobs, and there is no direct replacement. The team has not merely lost a tool. It has discovered how much of the production was trapped inside that tool.</p><p>Disclosure: This article was created with AI assistance.</p><p>This is no longer a theoretical edge case. OpenAI’s current video-generation documentation says its Sora 2 models and Videos API were shut down on September 24, 2026, with no one-to-one replacement API. Google maintains a formal Gemini API deprecation schedule. Runway’s API documentation records a change to accepted output-ratio values, with older API behavior scheduled to be phased out. The details differ, but the production lesson is consistent: model access, identifiers, parameters, and interfaces have lifecycles.</p><p>A team cannot prevent a provider from changing its product. It can prevent that change from erasing the production logic around a shot.</p><h3>A model shutdown becomes expensive when context is trapped</h3><p>A generation request often looks deceptively complete. It may contain a prompt, an image reference, a duration, an aspect ratio, a model name, and a seed. Save the request and it seems as if the work can be recreated later.</p><p>But a provider request rarely explains why the shot exists. It does not say which part of the script the image must carry, what the editor needs on either side, which visual detail has already been approved, or what kind of variation is acceptable. It may not preserve the reference rights, the sound plan, the intended destination, or the reason one output was accepted while five similar outputs were rejected.</p><p>When that information lives in chat threads, memory, or provider-specific controls, switching models becomes a reconstruction exercise. The team must infer old decisions while learning a new interface. That is where deadlines slip and continuity breaks.</p><p>The safer approach is to treat the model call as one replaceable operation inside a larger production record. The record should survive even when the operation does not.</p><h3>Keep the shot brief independent of the model</h3><p>Before translating a shot into any provider’s request format, write a portable shot brief. It should be specific enough for another operator or system to understand the creative obligation without seeing the original API call.</p><p>For a six-second product reveal, that brief might include:</p><ul><li>the shot’s job in the story: reveal the product after the problem is established;</li><li>required duration and delivery format;</li><li>opening composition and intended ending composition;</li><li>camera behavior, subject motion, and pace;</li><li>product details that must not change;</li><li>continuity inherited from the preceding shot;</li><li>safe areas needed for captions or later reframing;</li><li>sound expectations and whether generated audio may be used;</li><li>approved references, their purpose, and any usage restrictions;</li><li>acceptance criteria, such as a readable label, stable geometry, and an editable final hold;</li><li>the human decision required before the shot can move forward.</li></ul><p>None of those fields needs to mention a model. They describe the production requirement.</p><p>Provider-specific settings should sit beside that brief, not replace it. One adapter may express landscape output as a resolution pair, another as an aspect-ratio label. One model may accept first and last frames, while another may use a reference image and a text instruction. Keeping those translations separate makes the loss visible: if an endpoint disappears, the team loses a translation and a rendering option, not the meaning of the shot.</p><h3>Separate portable requirements from provider translations</h3><p>A useful routing boundary has three layers.</p><p>The first layer is the portable requirement: what the production needs. The second is the provider translation: how a specific model is asked to attempt it. The third is the evaluation record: what came back, whether it passed technical checks, and whether a person approved it creatively.</p><p>This separation matters because “successful job” and “usable shot” are different outcomes. A provider can return a valid file that fails the brief. The product may drift, the transition may not leave enough room for an edit, or the motion may contradict the adjacent shot. Conversely, a replacement model may use different parameters yet still satisfy the same creative requirement.</p><figure><img alt="Three production layers: a portable requirement that stays stable, a replaceable provider translation, and an evaluation record that distinguishes a successful job from a usable shot." src="https://cdn-images-1.medium.com/max/1024/1*2KR2TccsqoRY8-hwH9zwEQ.png" /><figcaption>The shot brief and the approval record should survive the loss of any one provider translation.</figcaption></figure><p>Do not force every provider into a fiction of identical capabilities. A common contract should describe only what can remain stable: the shot identifier, required inputs, output destination, status, error category, and link back to the portable brief. Capability differences should be explicit. If a model cannot accept the required reference type or output format, routing should reject it before spending time on a doomed render.</p><h3>Qualify fallbacks by shot type, not by reputation</h3><p>An emergency model switch is a bad time to discover what “alternative” meant.</p><p>A fallback should be qualified against representative production tasks while the primary route still works. Pick a small set that reflects the actual work: a product close-up, a camera move, a character-consistency shot, a graphic transition, or a clip that must begin and end on defined frames. The set will differ by team. The important part is that it represents obligations, not a generic demo prompt.</p><p>Evaluate each route against the same brief. Check whether it accepts the necessary inputs, supports the delivery shape, preserves required details, produces material the editor can use, and exposes enough job state to recover from failure. Record the limitations. A model that works well for atmospheric cutaways may be unsuitable for a branded product shot. It can still be a valid fallback, but only for the right class of work.</p><p>This produces a routing table grounded in production decisions:</p><ul><li>preferred route for this shot type;</li><li>acceptable fallback routes;</li><li>unsupported requirements;</li><li>translation notes;</li><li>review requirements that become stricter after rerouting;</li><li>conditions that require a new human approval.</li></ul><p>The table should never promise equivalent results. Its job is to make tradeoffs explicit before pressure turns them into guesses.</p><h3>Preserve evidence, not just final exports</h3><p>A finished MP4 is necessary, but it is not enough to resume a production. For every approved or still-active shot, retain the portable brief, source references, rights notes, provider request, model identifier, relevant settings, job identifier, returned files, evaluation notes, and approval history. Record which output is current and which versions it replaces.</p><p>This does not mean keeping every accidental variation forever. Retention should follow the project’s legal, privacy, and storage requirements. It does mean that an approved shot should not depend on a temporary provider URL or a prompt hidden in one person’s account.</p><p>Archive inputs and accepted outputs in storage the production controls. Keep provider job references for traceability, but do not treat them as the archive. If the provider changes retention rules, account access, or response formats, the project should still know what was made, from which approved material, and why it was accepted.</p><h3>Run an exit drill before you need one</h3><p>A practical exit drill can be small. Choose one active or representative project and pretend the primary model is unavailable.</p><p>First, disable that route for the drill. Then ask the team to identify affected shots from the production record, not from memory. Select approved fallbacks only where requirements match. Translate the portable brief into the fallback request, generate a new candidate, and review it in context with adjacent shots, audio, captions, and delivery format.</p><p>The drill should expose gaps rather than produce a perfect replacement. Perhaps the reference files were never archived. Perhaps the fallback cannot create the required duration. Perhaps provider settings are mixed into the creative brief, or nobody knows whether rerouting invalidates an earlier approval. Each gap becomes a concrete workflow fix.</p><p>Also define the recovery states. “Primary unavailable” is not one condition. A route may be temporarily degraded, permanently retired, restricted for a certain input, or replaced by a new version that needs qualification. Each state should have an owner, an allowed action, and an approval boundary. Automatic rerouting may be reasonable for low-risk drafts. Final branded shots may require a person to approve any provider change.</p><h3>Production orchestration is the durable layer</h3><p>The larger lesson is not that teams need more model integrations. They need production context that outlives any one integration.</p><p>That is the point of view behind <a href="https://moosky.ai/">Moosky AI</a>: creative work should be coordinated across specialized agents and models in an editable, reviewable workflow, with people controlling approvals and revisions. A provider-independent architecture matters, but the adapter is only part of the solution. The durable value is the connected record of intent, assets, state, decisions, and deliverables around the render.</p><p>A generation model can be impressive and still be temporary. Treat it as a replaceable production resource. Preserve the brief that gives the shot meaning, the evidence that makes it traceable, and the approval that makes it usable. Then a shutdown is still disruptive, but it does not get to rewrite the project.</p><h3>Sources</h3><ul><li><a href="https://platform.openai.com/docs/guides/video-generation">OpenAI API: Video generation with Sora</a></li><li><a href="https://ai.google.dev/gemini-api/docs/deprecations">Google AI for Developers: Gemini deprecations</a></li><li><a href="https://docs.dev.runwayml.com/api-details/versions/2024-11-06/">Runway Dev: API version 2024–11–06</a></li></ul><p><em>Moosky AI is building production orchestration that coordinates specialized agents and models across editable, reviewable creative workflows while keeping people in control of approvals and revisions. Learn more at </em><a href="https://moosky.ai/"><em>Moosky AI.</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a40b3e8a8cf6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Cache::remember Does Not Prevent a Stampede]]></title>
            <link>https://medium.com/@udit.rawat03/laravel-cache-remember-does-not-prevent-a-stampede-1ee37fefe8d6?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ee37fefe8d6</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[caching]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Sat, 07 Nov 2026 03:28:03 GMT</pubDate>
            <atom:updated>2026-11-07T03:28:03.489Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Six application requests converge on a shared cache miss and rebuild the same expensive value without coordination." src="https://cdn-images-1.medium.com/max/1024/1*7pIA1UgFuTzLHvL3qovQvQ.png" /><figcaption>Use a double-checked atomic lock for cold misses and choose stale data deliberately.</figcaption></figure><p>Caching an expensive query looks simple in Laravel:</p><pre>$catalog = Cache::remember(&#39;catalog:v1&#39;, 600, function () {<br>    return Product::query()-&gt;where(&#39;active&#39;, true)-&gt;get();<br>});</pre><p>The code avoids repeated work while the key exists. It does not coordinate requests when the key is missing. If 40 requests arrive just after expiration, all 40 can observe the miss, run the callback, query the same tables, and attempt to write the same key. The cache is working, but the application has created a cache stampede.</p><p>This article targets Laravel 13 and PHP 8.3 or later. The examples use a cache store that supports Laravel atomic locks and is shared by every application server. Laravel documents lock support for Memcached, Redis, DynamoDB, database, file, and array stores, but distributed coordination requires all servers to use the same central store. In a multi-server deployment, a local file or array store cannot coordinate the fleet.</p><p>The practical fix is not “cache for longer.” It is to make one request responsible for rebuilding a missing value while the others wait briefly, fail explicitly, or receive a stale value according to a chosen product contract.</p><figure><img alt="Six Laravel request workers observe the same missing cache key and concurrently repeat one expensive database rebuild." src="https://cdn-images-1.medium.com/max/1024/1*N-wFF6oAuyNaN468p1UzbA.png" /><figcaption>One missing key can turn every concurrent request into the same expensive rebuild.</figcaption></figure><h3>Why remember is not a mutex</h3><p>Cache::remember performs a read, calls the closure on a miss, and stores the result. Those steps are useful, but they are not one atomic operation across requests. Two workers can both read before either has written.</p><p>Here is a complete service with the common mistake:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Catalog;<br><br>use App\Models\Product;<br>use Illuminate\Support\Facades\Cache;<br><br>final class ProductCatalog<br>{<br>    private const KEY = &#39;catalog:active:v1&#39;;<br><br>    /**<br>     * @return array&lt;int, array{id: int, name: string, price_cents: int}&gt;<br>     */<br>    public function all(): array<br>    {<br>        return Cache::remember(self::KEY, 600, function (): array {<br>            return Product::query()<br>                -&gt;where(&#39;active&#39;, true)<br>                -&gt;orderBy(&#39;id&#39;)<br>                -&gt;get([&#39;id&#39;, &#39;name&#39;, &#39;price_cents&#39;])<br>                -&gt;map(fn (Product $product): array =&gt; [<br>                    &#39;id&#39; =&gt; $product-&gt;id,<br>                    &#39;name&#39; =&gt; $product-&gt;name,<br>                    &#39;price_cents&#39; =&gt; $product-&gt;price_cents,<br>                ])<br>                -&gt;all();<br>        });<br>    }<br>}</pre><p>The key is versioned, the query has a deterministic order, and the cached value is a plain array rather than a collection of live Eloquent models. Those are good properties. None of them prevents concurrent callbacks.</p><p>A stampede is most visible after expiration, but expiration is not the only trigger. A deploy can change the key version. An operator can flush the store. Redis can evict a key under memory pressure. A regional restart can create a cold cache. The dangerous moment is any transition from “many readers have a value” to “many readers see no value.”</p><p>Longer time-to-live values only move that moment. Random TTL jitter can spread expiration across many different keys, which is useful for a large cache population, but it cannot separate requests competing for the same key.</p><h3>Put an atomic lock around the rebuild</h3><p>Laravel 13 provides Cache::withoutOverlapping, which executes a closure while holding an atomic cache lock. The important detail is the second cache read after the lock is acquired.</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Catalog;<br><br>use App\Models\Product;<br>use Illuminate\Support\Facades\Cache;<br><br>final class ProductCatalog<br>{<br>    private const KEY = &#39;catalog:active:v1&#39;;<br>    private const LOCK = &#39;catalog:active:v1:rebuild&#39;;<br><br>    /**<br>     * @return array&lt;int, array{id: int, name: string, price_cents: int}&gt;<br>     */<br>    public function all(): array<br>    {<br>        $cached = Cache::get(self::KEY);<br><br>        if (is_array($cached)) {<br>            return $cached;<br>        }<br><br>        return Cache::withoutOverlapping(<br>            self::LOCK,<br>            function (): array {<br>                $cached = Cache::get(self::KEY);<br><br>                if (is_array($cached)) {<br>                    return $cached;<br>                }<br><br>                $catalog = $this-&gt;build();<br><br>                Cache::put(self::KEY, $catalog, now()-&gt;addMinutes(10));<br><br>                return $catalog;<br>            },<br>            lockFor: 45,<br>            waitFor: 5,<br>        );<br>    }<br><br>    /**<br>     * @return array&lt;int, array{id: int, name: string, price_cents: int}&gt;<br>     */<br>    private function build(): array<br>    {<br>        return Product::query()<br>            -&gt;where(&#39;active&#39;, true)<br>            -&gt;orderBy(&#39;id&#39;)<br>            -&gt;get([&#39;id&#39;, &#39;name&#39;, &#39;price_cents&#39;])<br>            -&gt;map(fn (Product $product): array =&gt; [<br>                &#39;id&#39; =&gt; $product-&gt;id,<br>                &#39;name&#39; =&gt; $product-&gt;name,<br>                &#39;price_cents&#39; =&gt; $product-&gt;price_cents,<br>            ])<br>            -&gt;all();<br>    }<br>}</pre><p>The first read is the fast path. A warm request does not acquire a lock. On a miss, requests compete for the rebuild lock. The winner enters the closure and reads the cache again. That second read matters because another request may have filled the cache while this request was waiting. Without it, every waiter would rebuild in sequence, which changes a concurrent stampede into a slower queue of duplicate work.</p><p>lockFor is the lock lease, not the maximum time a request waits. It must exceed the longest credible rebuild duration, including the query and serialization. If the lease expires while the winner is still building, another request can acquire the lock and start a second rebuild. Laravel 13 also supports refreshing an owned lock, but a cache fill that needs periodic lease extension is often too large for a synchronous request. Consider precomputation or a queued materialization job instead.</p><p>waitFor is how long a loser waits to acquire the lock. Laravel throws Illuminate\Contracts\Cache\LockTimeoutException if it cannot acquire the lock in that period. Do not catch that exception and silently run the expensive query outside the lock. That recreates the stampede precisely when the system is already slow. Map it to an explicit temporary failure, use a known fallback, or serve stale data if the product permits it.</p><figure><img alt="A cache hit returns directly, while a miss enters one atomic lock, checks again, and allows only one request to rebuild." src="https://cdn-images-1.medium.com/max/1024/1*O9yy6j1C4ruD2q7yNdQxNg.png" /><figcaption>A hit returns without a lock. A miss acquires one rebuild lock, checks again, and builds only if still empty.</figcaption></figure><h3>Choose the timeout behavior deliberately</h3><p>A lock is a concurrency policy, not only a code primitive. The application still needs a response policy for requests that cannot become the rebuilder.</p><p>For an internal API where stale catalog data would be misleading, a short 503 response may be safer than bypassing the lock:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Http\Controllers;<br><br>use App\Catalog\ProductCatalog;<br>use Illuminate\Contracts\Cache\LockTimeoutException;<br>use Illuminate\Http\JsonResponse;<br><br>final class CatalogController<br>{<br>    public function __invoke(ProductCatalog $catalog): JsonResponse<br>    {<br>        try {<br>            return response()-&gt;json([&#39;data&#39; =&gt; $catalog-&gt;all()]);<br>        } catch (LockTimeoutException) {<br>            return response()-&gt;json(<br>                [<br>                    &#39;message&#39; =&gt; &#39;The catalog is being refreshed. Retry shortly.&#39;,<br>                ],<br>                503,<br>                [&#39;Retry-After&#39; =&gt; &#39;2&#39;],<br>            );<br>        }<br>    }<br>}</pre><p>This controller keeps the failure boundary visible. A client receives a temporary response and a retry hint instead of unknowingly adding load to the database. The value 2 is an example, not a measured universal setting. It should match the service&#39;s retry policy and expected rebuild time.</p><p>A 503 is not appropriate for every page. If the previous catalog remains safe to show, stale-while-revalidate gives a better reading experience. If the value controls authorization, inventory allocation, a financial decision, or another state that must be current, a stale response may be wrong even when it is fast. Cache policy starts with data semantics.</p><h3>Use flexible when stale data is safe</h3><p>Laravel’s Cache::flexible divides a cached value into fresh and stale periods. During the fresh period, it returns immediately. During the stale period, it returns the existing value and registers a deferred refresh after the response. After the stale period, the request rebuilds synchronously.</p><p>Laravel 13’s repository API also accepts lock options for the deferred refresh. The framework source uses an atomic lock and rechecks the cached creation timestamp before running the refresh callback. That prevents several stale requests from all performing the same deferred rebuild.</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Catalog;<br><br>use App\Models\Product;<br>use Illuminate\Support\Facades\Cache;<br><br>final class StaleProductCatalog<br>{<br>    /**<br>     * @return array&lt;int, array{id: int, name: string, price_cents: int}&gt;<br>     */<br>    public function all(): array<br>    {<br>        return Cache::flexible(<br>            &#39;catalog:active:v1&#39;,<br>            [60, 300],<br>            function (): array {<br>                return Product::query()<br>                    -&gt;where(&#39;active&#39;, true)<br>                    -&gt;orderBy(&#39;id&#39;)<br>                    -&gt;get([&#39;id&#39;, &#39;name&#39;, &#39;price_cents&#39;])<br>                    -&gt;map(fn (Product $product): array =&gt; [<br>                        &#39;id&#39; =&gt; $product-&gt;id,<br>                        &#39;name&#39; =&gt; $product-&gt;name,<br>                        &#39;price_cents&#39; =&gt; $product-&gt;price_cents,<br>                    ])<br>                    -&gt;all();<br>            },<br>            lock: [&#39;seconds&#39; =&gt; 45],<br>        );<br>    }<br>}</pre><p>For the first 60 seconds, the value is fresh. From 60 to 300 seconds, Laravel may return it as stale while one deferred callback refreshes it. After 300 seconds, the value is expired and the requesting process must calculate it before returning.</p><p>That last case is important. Cache::flexible improves the stale window, but a completely cold key still has no value to serve. The Laravel 13 source calculates a missing value immediately. If simultaneous cold starts matter, prewarm the key during deployment or use the double-checked lock pattern for the cold path. Do not describe stale-while-revalidate as a universal stampede solution when there is no stale value.</p><p>Deferred execution also still consumes application work after the response is sent. It is not a durable queue. A process failure can lose that refresh, and a long callback still occupies resources. Use a queued materialization workflow when refresh must survive process termination, needs retries, or takes longer than a request worker should own.</p><figure><img alt="Cache flexible returns fresh data first, then stale data while refreshing, and rebuilds synchronously after expiration or on a cold start." src="https://cdn-images-1.medium.com/max/1024/1*3L_XV7RBk3XNAXSlwYsV0w.png" /><figcaption>Fresh returns immediately, stale returns while one locked refresh runs, and expired or cold paths rebuild before the response.</figcaption></figure><h3>Invalidate after the write commits</h3><p>A rebuild lock prevents duplicate reads. It does not make invalidation correct. If a database transaction forgets the cache before it commits, another request can rebuild from the old committed rows and cache stale data immediately.</p><p>Invalidate after a successful commit:</p><pre>&lt;?php<br><br>declare(strict_types=1);<br><br>namespace App\Catalog;<br><br>use App\Models\Product;<br>use Illuminate\Support\Facades\Cache;<br>use Illuminate\Support\Facades\DB;<br><br>final class ChangeProductPrice<br>{<br>    public function execute(Product $product, int $priceCents): void<br>    {<br>        DB::transaction(function () use ($product, $priceCents): void {<br>            $product-&gt;forceFill([<br>                &#39;price_cents&#39; =&gt; $priceCents,<br>            ])-&gt;save();<br><br>            DB::afterCommit(function (): void {<br>                Cache::forget(&#39;catalog:active:v1&#39;);<br>            });<br>        });<br>    }<br>}</pre><p>The transaction owns the database change. DB::afterCommit schedules invalidation only after that transaction commits. If it rolls back, the existing cache remains valid for the unchanged database state.</p><p>There is still a race in a cache-aside design: a rebuild that started before the commit can finish after invalidation and write an older snapshot. Solving that stronger consistency problem requires more than moving forget. Options include versioned keys tied to a durable revision, rebuilding under a domain write lock shared with mutations, or accepting bounded staleness as part of the read contract. Choose based on the consequence of stale data. A cache should not be the source of truth for rules that cannot tolerate that race.</p><h3>Test the contract you can prove</h3><p>A normal feature test can prove fast-path reuse, invalidation, and failure mapping. It cannot prove multi-process exclusion when it runs one PHP process against the array cache. Add an integration test with the production cache driver when the lock is operationally important.</p><p>For the service above, useful tests should assert that two sequential reads perform one build, a committed price change invalidates the key, and a lock timeout does not fall through to an unlocked query. Then run a concurrency probe against Redis with several worker processes and an intentionally slow builder. The durable evidence is that one process builds while the others either receive the filled value or hit the documented timeout path.</p><p>Do not use Cache::flush() in a focused test suite unless the cache store is isolated for that test. Laravel documents that flush ignores the configured cache prefix and removes every item in the store. Prefer deleting the exact key and lock key, or give the test process a dedicated cache database.</p><h3>A practical decision checklist</h3><p>Use a double-checked atomic lock when a cold miss is expensive, one request can rebuild synchronously, and callers may wait for a bounded time. Use Cache::flexible when serving a stale value is acceptable and reducing refresh latency matters. Use a queued materialization process when rebuilding must be durable, retryable, observable, or longer than a web request should own.</p><p>Before shipping the cache path, verify:</p><ul><li>Every application server coordinates through the same lock-capable cache store.</li><li>The cache key and rebuild lock name identify the same logical value.</li><li>The winner reads the cache again after acquiring the lock.</li><li>The lock lease exceeds the measured worst credible rebuild, with a deliberate margin.</li><li>Lock timeout has an explicit response or fallback policy and never bypasses the lock accidentally.</li><li>Stale data is used only where the product can tolerate it.</li><li>Invalidation happens after a successful database commit.</li><li>Tests separate single-process functional checks from real multi-process lock verification.</li><li>Cache misses, rebuild duration, lock waits, lock timeouts, and callback failures are observable.</li></ul><p>A cache hit is easy. The engineering work is defining what happens at the boundary where the value disappears. Treat rebuild ownership and stale-data policy as part of the application contract, and expiration stops being an uncontrolled load event.</p><h3>Sources</h3><ul><li><a href="https://laravel.com/docs/13.x/cache">Laravel 13 cache documentation: retrieval, stale-while-revalidate, atomic locks, lock refreshing, and concurrency limiting</a></li><li><a href="https://api.laravel.com/docs/13.x/Illuminate/Cache/Repository.html">Laravel 13 Repository API: </a><a href="https://api.laravel.com/docs/13.x/Illuminate/Cache/Repository.html">flexible and </a><a href="https://api.laravel.com/docs/13.x/Illuminate/Cache/Repository.html">withoutOverlapping</a></li><li><a href="https://github.com/laravel/framework/blob/13.x/src/Illuminate/Cache/Repository.php">Laravel 13 cache repository source</a></li><li><a href="https://laravel.com/docs/13.x/database">Laravel 13 database documentation: transactions and after-commit callbacks</a></li><li><a href="https://laravel.com/docs/13.x/releases">Laravel 13 release notes: PHP version support</a></li></ul><p><em>Udit Rawat is a Senior Software Engineer with more than 10 years of experience and co-founder of Moosky AI.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ee37fefe8d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AI Content Disclosure Belongs in Production, Not at the Publish Screen]]></title>
            <link>https://medium.com/@udit.rawat03/ai-content-disclosure-belongs-in-production-not-at-the-publish-screen-2e82a4d141f1?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e82a4d141f1</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[digital-media]]></category>
            <category><![CDATA[creative-workflow]]></category>
            <category><![CDATA[content-creation]]></category>
            <category><![CDATA[video-production]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Fri, 06 Nov 2026 08:48:50 GMT</pubDate>
            <atom:updated>2026-11-06T08:48:50.751Z</atom:updated>
            <content:encoded><![CDATA[<p>Track origin, transformations, approval, and destination requirements while the work is being made, not after the final export.</p><figure><img alt="An abstract film-and-audio ribbon passes through four production archways while a layered provenance record remains attached in the foreground." src="https://cdn-images-1.medium.com/max/1024/1*JAHVGOITVqKYnjQyw2Dw9g.png" /><figcaption>AI-generated image created for this article.</figcaption></figure><p>A disclosure question often appears at the worst possible moment: the work is finished, the deadline is close, and someone uploading it sees a field asking whether AI was used. By then, the answer may be scattered across prompts, edits, generated elements, and decisions made by several people.</p><p>Disclosure: This article was created with AI assistance.</p><p>Treating that question as a final publishing checkbox creates avoidable guesswork. The person uploading a deliverable may know that AI was involved without knowing where, how substantially, or whether a realistic element could mislead a viewer. The practical fix is to make disclosure part of production. Each deliverable should carry a small record of its origin, meaningful transformations, approval, and destination-specific disclosure decision.</p><h3>The publish screen cannot reconstruct the work</h3><p>Consider a small agency producing a vertical recipe video for a kitchen brand. The host and food are filmed. A generative tool extends the kitchen background in two shots. Another tool repairs noisy audio. An artificial voice is used for a translated version. The soundtrack is generated. A writer then uses AI-generated text in the campaign recap.</p><p>“Was AI used?” is too blunt to describe this production. It does not distinguish a repaired recording from a realistic scene that never occurred. It does not say whether the generated music is central to the video. It does not tell the person preparing the Medium recap that generated text needs a written disclosure near the beginning.</p><p>The platforms also ask different questions. <a href="https://support.google.com/youtube/answer/14328491?hl=en">YouTube’s guidance</a> requires creators to disclose generated or meaningfully altered content when it appears realistic, and it gives examples that need disclosure as well as minor or non-realistic uses that do not. It separately identifies AI-generated music that is the main focus of a video. <a href="https://help.medium.com/hc/en-us/articles/22576852947223-Artificial-Intelligence-AI-content-policy">Medium’s AI content policy</a> requires a clear label when a story incorporates AI-generated text or images, while its FAQ says grammar checking, spell checking, outlining, and fact verification do not by themselves require disclosure.</p><p>Those are not interchangeable rules. A truthful answer for one destination is not automatically a complete answer for another. The production record needs enough detail to support the decision at each destination.</p><h3>Record four things with every deliverable</h3><p>A useful record does not need to become a lengthy compliance document. It needs four concrete fields.</p><h3>1. Origin</h3><p>Start with what the deliverable contains and where those elements came from. Mark each material element as captured, written or performed by a person, generated, licensed, supplied by the client, or derived from another source.</p><p>For the recipe video, that might read: filmed host performance, filmed food close-ups, generated background extensions in shots three and seven, generated music, and a translated synthetic voice based on an approved script. This is more useful than a general “AI assisted” tag because it identifies the parts a reviewer must assess.</p><p>Origin is also where rights and consent questions become visible. A disclosure label does not resolve whether a team had permission to use a voice, likeness, source image, or recording. Those decisions still need their own checks.</p><h3>2. Transformations</h3><p>Record the changes that matter to meaning or viewer understanding. Do not log every crop or color adjustment. Note transformations such as generating a realistic event, changing what a person appears to say, replacing a voice, inserting a synthetic product view, or composing a scene from generated and captured material.</p><p>This distinction follows the practical shape of YouTube’s guidance. Minor aesthetic edits and production assistance are treated differently from realistic generated content or meaningful alterations. A transformation note gives the uploader the facts needed to apply that distinction rather than forcing them to infer it from the finished video.</p><p>The note should use plain language. “Background extended with a generative tool; no people or product behavior changed” is clearer than a model name and a prompt. “Synthetic translation uses the host’s approved voice replica” explains more than “AI audio.” Tool details may be useful elsewhere, but they are not a substitute for describing what changed.</p><h3>3. Approval</h3><p>Name the person responsible for reviewing the disclosure facts and record the decision they made. Approval should cover both the creative change and the accuracy of the description.</p><p>A reviewer might approve the background extension as non-misleading, confirm that the translated script matches the source, and decide that the generated soundtrack needs to be considered under YouTube’s current guidance. If the campaign recap contains generated sentences, the editor should confirm that the disclosure appears where Medium expects it, within the first two paragraphs.</p><p>This step matters because tools cannot decide every contextual question. Whether an alteration is realistic, meaningful, or likely to mislead can depend on the content and presentation. The record should preserve a human decision, not pretend the question disappeared because a tool supplied metadata.</p><h3>4. Destination</h3><p>Complete a disclosure decision for each planned destination. The decision should include the applicable rule, the answer to the platform’s field or labeling requirement, and any visible wording or caption that must accompany the deliverable.</p><p>For example:</p><ul><li><strong>YouTube:</strong> Review the realistic generated background, synthetic translated voice, and generated music against the current upload guidance. Record whether the “AI use” setting is Yes or No and why.</li><li><strong>TikTok:</strong> Check the current posting controls and retain the production facts even if the platform applies a label through another signal.</li><li><strong>Medium:</strong> Put the text disclosure within the first two paragraphs when generated text is included, and caption any generated or AI-assisted image as required by the current policy.</li></ul><p>This destination check should happen before delivery, not for the first time during upload. Platform guidance can change, so the production facts should remain stable while the destination decision can be reviewed against the current rule.</p><h3>Credentials help, but they do not finish the job</h3><p>The <a href="https://spec.c2pa.org/specifications/specifications/2.2/specs/C2PA_Specification.html">C2PA 2.2 specification</a> defines a technical structure for provenance information. Its model can describe assertions about creation and edit actions, bind claims to content, and use signatures to make claims tamper-evident. The specification also distinguishes provenance signals from value judgments. Validation can show that assertions are associated with an asset and have not been altered; it does not decide whether the content is honest, safe, or properly disclosed for a particular platform.</p><p>That boundary is important. Content Credentials can support a production record, but credentials do not replace a platform disclosure or a human review. Support also varies by tool, format, and destination, so it is not safe to assume that every stage will preserve or display the same information.</p><p>Metadata can be removed, and manual records still matter. C2PA 2.2 itself describes the possibility that an asset becomes separated from its manifest through metadata removal or corruption. Durable approaches such as soft bindings can help recovery in supporting systems, but they do not justify discarding the team’s own record.</p><p>TikTok’s <a href="https://newsroom.tiktok.com/helping-people-spot-and-understand-aigc-on-tiktok?lang=en-150">July 2026 transparency update</a> makes the same operational point from another angle. TikTok describes using a combination of Content Credentials, creator labeling tools, and invisible watermarking rather than relying on one signal. That does not establish universal interoperability. It shows why creators should be ready to supply an accurate disclosure even when technical signals are also present.</p><h3>A lightweight release gate</h3><p>Before a deliverable leaves production, ask five questions:</p><ol><li>What was captured, written, performed, generated, or supplied?</li><li>Which transformations could change meaning or a viewer’s understanding of what happened?</li><li>Who reviewed those facts and approved the disclosure decision?</li><li>What does each destination currently require?</li><li>If embedded provenance is missing later, can the team still explain the work accurately?</li></ol><p>The record can be short. For a simple social graphic, it might be four lines. For a mixed-media campaign, it may need separate entries for the video, soundtrack, voice version, article, and images. The goal is not to collect every technical event. It is to keep enough evidence for an honest, specific decision.</p><p>This is also a production-orchestration problem. <a href="https://moosky.ai/">Moosky AI</a> is building workflows that coordinate specialized agents and models across editable, reviewable production while users control approvals and revisions. In that kind of workflow, disclosure facts should travel with the deliverable through creation and review instead of being reconstructed at the end.</p><p>A publishing checkbox still has a purpose. It is where the platform receives the final answer. But the answer should already exist, supported by a clear account of origin, transformation, approval, and destination. That turns disclosure from a last-minute memory test into an ordinary part of responsible production.</p><p><em>Moosky AI is building a more coordinated approach to AI-assisted creative production, with editable workflows and human review. Learn more at </em><a href="https://moosky.ai/"><em>Moosky AI</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e82a4d141f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop Rewriting State Files In Place: Use Atomic Replacement in Python]]></title>
            <link>https://medium.com/@udit.rawat03/stop-rewriting-state-files-in-place-use-atomic-replacement-in-python-469fc7b5254d?source=rss-50b0e74af3fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/469fc7b5254d</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[filesystem]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[reliability]]></category>
            <dc:creator><![CDATA[Udit Rawat]]></dc:creator>
            <pubDate>Thu, 05 Nov 2026 04:13:16 GMT</pubDate>
            <atom:updated>2026-11-05T04:13:16.868Z</atom:updated>
            <content:encoded><![CDATA[<p>Build complete snapshots beside the target, publish them with os.replace(), and keep atomic visibility separate from durability.</p><figure><img alt="Three document cards illustrate an old JSON state file, a completed temporary file, and a single replacement step that publishes the new state." src="https://cdn-images-1.medium.com/max/1024/1*IS0qbRSCls3ZS9RWYkaoow.png" /><figcaption>AI-generated illustration of staged file construction followed by atomic pathname replacement.</figcaption></figure><p>A small JSON state file often begins with one convenient line: path.write_text(...). That is reasonable until the file becomes the record a CLI, worker, or local service needs after a restart. An in-place rewrite truncates the current file before the replacement content is complete. If serialization or writing fails, the valid old state is already gone.</p><p>The safer pattern is to construct the next snapshot in a separate file, close it, and publish it by replacing the destination path. Python’s standard library provides the pieces: tempfile.NamedTemporaryFile(), flush(), os.fsync(), and os.replace().</p><p>This article targets CPython 3.14.7 and uses only the standard library. The important distinction is scope. Atomic replacement prevents publication of a partially written snapshot under the destination path. It does not automatically provide power-loss durability, coordinate competing writers, preserve every piece of metadata, or turn several files into one transaction.</p><h3>An in-place rewrite destroys the fallback first</h3><p>The failure window is easy to reproduce without pretending to simulate an operating-system crash:</p><pre>import json<br>import tempfile<br>from pathlib import Path<br></pre><pre>def main() -&gt; None:<br>    with tempfile.TemporaryDirectory() as directory:<br>        state_path = Path(directory) / &quot;state.json&quot;<br>        state_path.write_text(<br>            &#39;{&quot;generation&quot;: 1, &quot;status&quot;: &quot;ready&quot;}\n&#39;,<br>            encoding=&quot;utf-8&quot;,<br>        )</pre><pre>        try:<br>            with state_path.open(&quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<br>                file.write(&#39;{&quot;generation&quot;: 2,&#39;)<br>                raise RuntimeError(&quot;simulated serialization failure&quot;)<br>        except RuntimeError as error:<br>            print(error)</pre><pre>        damaged = state_path.read_text(encoding=&quot;utf-8&quot;)<br>        print(&quot;File now contains:&quot;, repr(damaged))</pre><pre>        try:<br>            json.loads(damaged)<br>        except json.JSONDecodeError as error:<br>            print(&quot;State is no longer valid JSON:&quot;, error)<br></pre><pre>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>Opening with mode &quot;w&quot; truncates the existing file. The controlled exception then leaves a partial document behind. The example models an application failure during serialization or writing, not a sudden power loss, but it exposes the ordering problem: the code invalidates the known-good snapshot before it has a complete replacement.</p><p>Writing the bytes faster does not repair that ordering. Neither does catching the exception after the destination has been truncated. The old state must remain untouched until the new state is complete.</p><figure><img alt="Two update flows compare an in-place rewrite that leaves partial JSON after failure with a staged replacement that keeps the old file until the new file is complete." src="https://cdn-images-1.medium.com/max/1024/1*Anl_0iIf9_wii_QMmNBEYw.png" /><figcaption>The in-place path destroys the fallback first. The staged path keeps it until the replacement is complete.</figcaption></figure><h3>Build privately, then publish once</h3><p>A replacement helper should create its temporary file in the destination directory. os.replace() can fail when source and destination are on different filesystems, so the default system temporary directory is the wrong place for this job. A sibling temporary file also keeps the publication operation within one directory and, normally, one filesystem.</p><p>Here is a complete cross-platform implementation:</p><pre>import json<br>import os<br>import tempfile<br>from pathlib import Path<br>from typing import Any<br></pre><pre>def atomic_write_json(path: Path, value: Any) -&gt; None:<br>    path = Path(path)<br>    path.parent.mkdir(parents=True, exist_ok=True)<br>    temporary_path: Path | None = None</pre><pre>    try:<br>        with tempfile.NamedTemporaryFile(<br>            mode=&quot;w&quot;,<br>            encoding=&quot;utf-8&quot;,<br>            newline=&quot;\n&quot;,<br>            dir=path.parent,<br>            prefix=f&quot;.{path.name}.&quot;,<br>            suffix=&quot;.tmp&quot;,<br>            delete=False,<br>        ) as temporary_file:<br>            temporary_path = Path(temporary_file.name)<br>            json.dump(<br>                value,<br>                temporary_file,<br>                ensure_ascii=False,<br>                indent=2,<br>                sort_keys=True,<br>                allow_nan=False,<br>            )<br>            temporary_file.write(&quot;\n&quot;)<br>            temporary_file.flush()<br>            os.fsync(temporary_file.fileno())</pre><pre>        os.replace(temporary_path, path)<br>        temporary_path = None<br>    finally:<br>        if temporary_path is not None:<br>            try:<br>                temporary_path.unlink()<br>            except FileNotFoundError:<br>                pass<br></pre><pre>def main() -&gt; None:<br>    with tempfile.TemporaryDirectory() as directory:<br>        state_path = Path(directory) / &quot;state.json&quot;<br>        atomic_write_json(<br>            state_path,<br>            {<br>                &quot;generation&quot;: 2,<br>                &quot;status&quot;: &quot;ready&quot;,<br>                &quot;workers&quot;: [&quot;alpha&quot;, &quot;beta&quot;],<br>            },<br>        )<br>        print(state_path.read_text(encoding=&quot;utf-8&quot;))<br></pre><pre>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>NamedTemporaryFile() creates a unique named file rather than relying on a predictable filename. dir=path.parent keeps it beside the destination. delete=False lets the with block close the file before os.replace() runs, which is important on Windows because replacing an open file can be blocked by sharing rules.</p><p>Serialization happens before publication. allow_nan=False is not part of atomicity, but it is a useful state-file policy because it rejects NaN and infinity instead of writing JavaScript-style values outside the JSON specification. If json.dump(), flush(), or fsync() raises, the destination is unchanged and the finally block attempts to remove only the unpublished temporary file.</p><p>The order of temporary_path = None also matters. It happens only after a successful replacement. Once replacement succeeds, that temporary pathname no longer names a file, and cleanup must not treat the published destination as disposable.</p><p>Do not add a fallback that opens the destination and writes in place when os.replace() fails. A sharing violation, permission error, or unsupported filesystem behavior should remain visible to the caller. Falling back would silently discard the property this helper exists to provide.</p><h3>Flush, sync, close, replace</h3><p>These operations solve different problems:</p><ol><li>json.dump() writes through a buffered text stream.</li><li>temporary_file.flush() moves buffered Python data to the operating system.</li><li>os.fsync(temporary_file.fileno()) asks the operating system to synchronize the file represented by that descriptor. Python documents that it maps to fsync() on Unix and _commit() on Windows.</li><li>Leaving the with block closes the temporary file.</li><li>os.replace() changes which file the destination pathname names.</li></ol><p>Calling flush() without fsync() is not the same as requesting storage synchronization. Calling fsync() before flushing can miss bytes still held in Python&#39;s buffer. The Python documentation explicitly directs buffered-file users to flush first and then call os.fsync().</p><figure><img alt="A sequence diagram shows JSON serialization, buffer flush, file synchronization, close, and a final os.replace publication step." src="https://cdn-images-1.medium.com/max/1024/1*wkIMen8njmDqNkpczD3sDA.png" /><figcaption>Everything above the boundary is private construction. Only os.replace() publishes.</figcaption></figure><p>The helper provides a stronger update boundary, but its name deserves care. “Atomic write” is common shorthand, yet “atomic pathname replacement” is more precise. The data is written over many operations. The publication step is the replacement.</p><p>Python documents that os.replace() silently replaces an existing file when permissions allow, may fail across filesystems, and gives atomic rename semantics as a POSIX requirement. That is not a universal promise about every network mount, user-space filesystem, synchronization service, or storage appliance. If a filesystem is outside your control, test its documented behavior rather than extending local-filesystem assumptions to it.</p><h3>Verify the failure path, not only the round trip</h3><p>A useful test proves that a failed serialization leaves the old destination unchanged and removes the unpublished temporary file. The serializer is injected here so the test can fail at a deterministic point:</p><pre>import os<br>import tempfile<br>import unittest<br>from collections.abc import Callable<br>from pathlib import Path<br>from typing import TextIO<br></pre><pre>Serializer = Callable[[TextIO], None]<br></pre><pre>def replace_text(path: Path, serialize: Serializer) -&gt; None:<br>    temporary_path: Path | None = None</pre><pre>    try:<br>        with tempfile.NamedTemporaryFile(<br>            mode=&quot;w&quot;,<br>            encoding=&quot;utf-8&quot;,<br>            newline=&quot;\n&quot;,<br>            dir=path.parent,<br>            prefix=f&quot;.{path.name}.&quot;,<br>            suffix=&quot;.tmp&quot;,<br>            delete=False,<br>        ) as temporary_file:<br>            temporary_path = Path(temporary_file.name)<br>            serialize(temporary_file)<br>            temporary_file.flush()<br>            os.fsync(temporary_file.fileno())</pre><pre>        os.replace(temporary_path, path)<br>        temporary_path = None<br>    finally:<br>        if temporary_path is not None:<br>            try:<br>                temporary_path.unlink()<br>            except FileNotFoundError:<br>                pass<br></pre><pre>class ReplacementTests(unittest.TestCase):<br>    def test_failed_serialization_preserves_old_state(self) -&gt; None:<br>        with tempfile.TemporaryDirectory() as directory:<br>            state_path = Path(directory) / &quot;state.txt&quot;<br>            state_path.write_text(&quot;known-good\n&quot;, encoding=&quot;utf-8&quot;)</pre><pre>            def fail_after_partial_write(file: TextIO) -&gt; None:<br>                file.write(&quot;incomplete&quot;)<br>                raise ValueError(&quot;cannot serialize value&quot;)</pre><pre>            with self.assertRaisesRegex(ValueError, &quot;cannot serialize&quot;):<br>                replace_text(state_path, fail_after_partial_write)</pre><pre>            self.assertEqual(<br>                state_path.read_text(encoding=&quot;utf-8&quot;),<br>                &quot;known-good\n&quot;,<br>            )<br>            self.assertEqual(<br>                list(Path(directory).glob(&quot;.state.txt.*.tmp&quot;)),<br>                [],<br>            )<br></pre><pre>if __name__ == &quot;__main__&quot;:<br>    unittest.main()</pre><p>The test checks three caller-visible outcomes: the original exception propagates, the old content remains intact, and no temporary file is left behind. It does not inspect the helper’s private control flow. A second test should cover successful replacement, but a round-trip assertion alone would miss the reason for using this pattern.</p><p>Cleanup has its own tradeoff. This implementation ignores only FileNotFoundError. Another unlink error can replace the original serialization exception in the finally block. A library-quality helper may attach cleanup failure as a note, log it, or preserve both errors with a documented policy. Do not broadly suppress every OSError, because abandoned temporary files are operational evidence worth seeing.</p><h3>Atomic visibility is not durable commit</h3><p>File synchronization and directory-entry synchronization are separate concerns. On POSIX systems, applications with a strict post-crash durability requirement commonly open and synchronize the parent directory after replacement. Python exposes the low-level operations, but directory descriptors and their synchronization behavior are not portable to every platform or filesystem.</p><p>The following example makes that boundary explicit instead of pretending it is cross-platform:</p><pre>import json<br>import os<br>import tempfile<br>from pathlib import Path<br>from typing import Any<br></pre><pre>def fsync_directory(directory: Path) -&gt; None:<br>    if os.name != &quot;posix&quot;:<br>        raise NotImplementedError(&quot;directory fsync example is POSIX-only&quot;)</pre><pre>    flags = os.O_RDONLY | getattr(os, &quot;O_DIRECTORY&quot;, 0)<br>    descriptor = os.open(directory, flags)<br>    try:<br>        os.fsync(descriptor)<br>    finally:<br>        os.close(descriptor)<br></pre><pre>def durable_replace_json(path: Path, value: Any) -&gt; None:<br>    temporary_path: Path | None = None</pre><pre>    try:<br>        with tempfile.NamedTemporaryFile(<br>            mode=&quot;w&quot;,<br>            encoding=&quot;utf-8&quot;,<br>            dir=path.parent,<br>            prefix=f&quot;.{path.name}.&quot;,<br>            suffix=&quot;.tmp&quot;,<br>            delete=False,<br>        ) as temporary_file:<br>            temporary_path = Path(temporary_file.name)<br>            json.dump(value, temporary_file, allow_nan=False)<br>            temporary_file.write(&quot;\n&quot;)<br>            temporary_file.flush()<br>            os.fsync(temporary_file.fileno())</pre><pre>        os.replace(temporary_path, path)<br>        temporary_path = None<br>        fsync_directory(path.parent)<br>    finally:<br>        if temporary_path is not None:<br>            try:<br>                temporary_path.unlink()<br>            except FileNotFoundError:<br>                pass<br></pre><pre>def main() -&gt; None:<br>    if os.name != &quot;posix&quot;:<br>        print(&quot;Skipped: directory synchronization example is POSIX-only&quot;)<br>        return</pre><pre>    with tempfile.TemporaryDirectory() as directory:<br>        state_path = Path(directory) / &quot;state.json&quot;<br>        durable_replace_json(state_path, {&quot;generation&quot;: 3})<br>        print(state_path.read_text(encoding=&quot;utf-8&quot;))<br></pre><pre>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>This version synchronizes the staged file before replacement and then synchronizes the containing directory. It propagates failures rather than claiming a durable commit when the filesystem rejects directory synchronization. It still is not a promise against every device, controller, filesystem, or machine failure.</p><p>On Windows, the core helper remains useful for complete-snapshot publication, and os.fsync() is available for the temporary file. The POSIX directory technique is intentionally not presented as portable Windows code. If your requirement is a precisely documented durable transaction across platforms, a database or platform-specific storage layer is usually a better boundary than a growing collection of filesystem assumptions.</p><h3>Replacement does not coordinate writers</h3><p>Imagine two processes reading generation 10. Both calculate generation 11 in private temporary files. Each replacement can publish a complete document, but the last successful replacement wins. One update disappears without any partial JSON appearing.</p><p>That is a lost update, and atomic replacement does not prevent it. Add a real coordination protocol when multiple writers are possible: an operating-system lock with documented platform behavior, a version check under a lock, or a transactional store such as SQLite. Comparing the generation before calling os.replace() is not sufficient by itself because another writer can replace the file between the comparison and publication.</p><figure><img alt="Two writers read the same generation and each publish a complete replacement, but the later replacement overwrites the earlier update." src="https://cdn-images-1.medium.com/max/1024/1*tFZT93KMTxYMo7FO3Z-60A.png" /><figcaption>Every visible file is complete, and an update is still lost.</figcaption></figure><p>Replacement is also a one-path operation. Updating state.json and index.json with two calls creates a moment when one file is new and the other is old. If readers require a consistent set, publish one manifest that points to versioned immutable files, or use storage with multi-record transactions.</p><p>Existing open handles add another boundary. Replacement changes the directory entry. A reader that opened the old file before replacement may continue reading the old file object, while a later opener reads the new one. That behavior is often useful for snapshot reads, but it is not a broadcast that changes already-open streams.</p><p>Metadata needs an explicit policy too. The published file is the temporary file, not the old destination modified in place. Do not assume the old file’s permissions, ownership, access-control entries, timestamps, or extended attributes automatically become properties of the new file. If specific metadata matters, define and test how it is applied before publication, including the security implications.</p><h3>When this pattern is the right size</h3><p>Use same-directory staged replacement when all of these are true:</p><ul><li>The state fits naturally in one independently replaceable file.</li><li>Readers can treat each file version as an immutable snapshot.</li><li>One writer owns updates, or writer coordination exists elsewhere.</li><li>A failed update should leave the previous snapshot available.</li><li>The filesystem’s replacement semantics are known for the deployment environment.</li></ul><p>Choose SQLite or another transactional store when updates span records or files, several processes perform read-modify-write operations, history matters, or the durability contract must be stronger and easier to state. Use an append-only log when every event must be retained rather than replacing the latest snapshot.</p><p>For a file-based implementation, review this sequence:</p><ol><li>Create a unique temporary file in the destination directory.</li><li>Serialize the entire next state and reject values the format cannot represent safely.</li><li>Flush the Python stream, then synchronize the file when required.</li><li>Close the temporary file before replacement.</li><li>Publish with os.replace() and surface replacement errors.</li><li>Clean up unpublished temporary files without deleting the destination.</li><li>Test serialization failure, replacement failure, and successful publication.</li><li>Add directory synchronization only with a documented platform policy.</li><li>Add locking or move to transactional storage when writers compete.</li></ol><p>The key design change is ordering. Keep the known-good state in place while building the next snapshot. Publish only after the replacement is complete, and describe the guarantee narrowly enough that operations and callers can rely on what the code actually does.</p><h3>Sources</h3><ul><li><a href="https://docs.python.org/3.14/library/os.html#os.replace">Python 3.14 </a><a href="https://docs.python.org/3.14/library/os.html#os.replace">os.replace() documentation</a></li><li><a href="https://docs.python.org/3.14/library/os.html#os.fsync">Python 3.14 </a><a href="https://docs.python.org/3.14/library/os.html#os.fsync">os.fsync() documentation</a></li><li><a href="https://docs.python.org/3.14/library/tempfile.html#tempfile.NamedTemporaryFile">Python 3.14 </a><a href="https://docs.python.org/3.14/library/tempfile.html#tempfile.NamedTemporaryFile">tempfile.NamedTemporaryFile() documentation</a></li><li><a href="https://docs.python.org/3.14/library/json.html">Python 3.14 </a><a href="https://docs.python.org/3.14/library/json.html">json documentation</a></li><li><a href="https://pubs.opengroup.org/onlinepubs/9799919799/functions/fsync.html">POSIX </a><a href="https://pubs.opengroup.org/onlinepubs/9799919799/functions/fsync.html">fsync() specification</a></li></ul><p><em>Udit Rawat is a Senior Software Engineer with more than 10 years of experience and co-founder of Moosky AI.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=469fc7b5254d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>