Skip to content

Automate browser based workflows with AI

fix(SKY-12196): stop gating block-status polling on the narration LLM floor (#7275)

Celal Zamanoğlu407f1ce5f31b
diff --git a/skyvern/forge/sdk/copilot/narration.py b/skyvern/forge/sdk/copilot/narration.py
index 81a8994a0..850548e17 100644
--- a/skyvern/forge/sdk/copilot/narration.py
+++ b/skyvern/forge/sdk/copilot/narration.py
@@ -47,6 +47,13 @@ LOG = structlog.get_logger()
# from producing back-to-back emissions.
MIN_NARRATION_GAP_SECONDS = 10.0
+# Floor on how often narrator_poll_tick re-fetches block statuses from the DB.
+# That fetch is a free read, not an LLM call, so it must not share the
+# narration floor above -- it only needs a small burst guard. Well below the
+# caller's RUN_BLOCKS_POLL_INTERVAL_SECONDS (5.0 in run_execution.py), so it
+# never actually binds at today's cadence -- it's a ceiling, not a throttle.
+MIN_BLOCK_STATUS_POLL_GAP_SECONDS = 1.0
+
# Cap on how many tool round-trips we hand to the narrator LLM. The narrator
# only needs recent context; keeping this small caps prompt cost.
MAX_TOOL_ACTIVITY_BUFFER = 8
@@ -818,12 +825,12 @@ async def narrator_poll_tick(
"""
now = time.monotonic()
block_changed = current_block_ts != prior_block_ts
- gate_open = (now - last_block_fetch_monotonic) >= MIN_NARRATION_GAP_SECONDS
+ fetch_gate_open = (now - last_block_fetch_monotonic) >= MIN_BLOCK_STATUS_POLL_GAP_SECONDS
next_prior_block_ts = prior_block_ts
next_last_fetch = last_block_fetch_monotonic
- if block_changed and gate_open:
+ if block_changed and fetch_gate_open:
next_last_fetch = now
try:
blocks = await fetch_block_statuses()
diff --git a/tests/unit/test_copilot_narration.py b/tests/unit/test_copilot_narration.py
index 2f0b79873..c4851d093 100644
--- a/tests/unit/test_copilot_narration.py
+++ b/tests/unit/test_copilot_narration.py
@@ -17,6 +17,7 @@ import pytest
from skyvern.forge.sdk.copilot import narration
from skyvern.forge.sdk.copilot.narration import (
+ MIN_BLOCK_STATUS_POLL_GAP_SECONDS,
MIN_NARRATION_GAP_SECONDS,
NarratorState,
TransitionKind,
@@ -924,6 +925,42 @@ async def test_poll_tick_no_change_still_calls_schedule_narration() -> None:
pass
+async def test_poll_tick_refetches_block_status_within_narration_gap_but_past_poll_gap() -> None:
+ """Block-status re-fetch must not be held to the 10s narration floor (SKY-12196):
+ a change 2s after the last fetch -- inside MIN_NARRATION_GAP_SECONDS but past
+ MIN_BLOCK_STATUS_POLL_GAP_SECONDS -- must still surface immediately."""
+ assert 0.0 < MIN_BLOCK_STATUS_POLL_GAP_SECONDS < 2.0 < MIN_NARRATION_GAP_SECONDS
+ state = NarratorState()
+ stream = _StubStream()
+ seen: dict[str, str] = {}
+ fetch_calls = 0
+
+ async def fetch() -> list[_StubBlock]:
+ nonlocal fetch_calls
+ fetch_calls += 1
+ return [_StubBlock("b0", "running")]
+
+ await narrator_poll_tick(
+ state,
+ current_block_ts=_ts(2.0),
+ prior_block_ts=_ts(1.0),
+ last_block_fetch_monotonic=time.monotonic() - 2.0,
+ seen_block_states=seen,
+ fetch_block_statuses=fetch,
+ stream=stream, # type: ignore[arg-type]
+ )
+
+ assert fetch_calls == 1
+ assert seen == {"b0": "running"}
+ if state.in_flight_task is not None:
+ state.in_flight_task.cancel()
+ try:
+ await state.in_flight_task
+ except (asyncio.CancelledError, Exception):
+ pass
+
+
@pytest.mark.asyncio
async def test_poll_tick_rate_limited_block_change_retries_on_next_tick() -> None:
"""A block_ts change inside the rate-limit window must not advance