DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(reading): bound extension action execution
XZH88777a442112
diff --git a/READING_EXTENSIONS.md b/READING_EXTENSIONS.mdindex 1382ba02e..979ac2040 100644--- a/READING_EXTENSIONS.md+++ b/READING_EXTENSIONS.md@@ -48,6 +48,10 @@ class ExampleExtension:`feedback`, or `browser_speech`.- Results have a 64 KB serialized ceiling. Quiz and speech payloads receiveadditional shape and length validation.+- Units larger than the protocol's 60,000-character context ceiling are+ rejected with a client error instead of invoking an extension.+- Actions have a 30-second execution timeout and return the standard+ recoverable unavailability response when exceeded.- Result data is rendered as React text. Extensions cannot send JavaScript orraw HTML to the Reader.- Discovery and execution failures are isolated. A broken optional packagediff --git a/deeptutor/api/routers/reading_extensions.py b/deeptutor/api/routers/reading_extensions.pyindex daf2f58c6..ac9e1b700 100644--- a/deeptutor/api/routers/reading_extensions.py+++ b/deeptutor/api/routers/reading_extensions.py@@ -2,12 +2,13 @@from __future__ import annotations+import asyncioimport inspectimport refrom typing import Anyfrom fastapi import APIRouter, HTTPException-from pydantic import BaseModel, Field+from pydantic import BaseModel, Field, ValidationErrorfrom deeptutor.reading import ReadingStorefrom deeptutor.reading.extensions import (@@ -17,6 +18,7 @@ from deeptutor.reading.extensions import ()router = APIRouter()+ACTION_TIMEOUT_S = 30class ActionPayload(BaseModel):@@ -60,18 +62,27 @@ async def run_extension_action(selection = _verified_selection(payload.selection, unit_text)if "selection" in declared_action.requires and not selection:raise HTTPException(status_code=400, detail="Select text from the visible unit first.")- context = ReadingContext(- material_id=material_id,- locator=payload.locator,- source_anchor=payload.source_anchor,- locale=payload.locale,- selection=selection,- visible_text=unit_text,- )try:- value = extension.run_action(action, context)+ context = ReadingContext(+ material_id=material_id,+ locator=payload.locator,+ source_anchor=payload.source_anchor,+ locale=payload.locale,+ selection=selection,+ visible_text=unit_text,+ )+ except ValidationError as exc:+ raise HTTPException(+ status_code=422,+ detail="This reading unit is too large for the extension protocol.",+ ) from exc+ try:+ value = await asyncio.wait_for(+ asyncio.to_thread(extension.run_action, action, context),+ timeout=ACTION_TIMEOUT_S,+ )if inspect.isawaitable(value):- value = await value+ value = await asyncio.wait_for(value, timeout=ACTION_TIMEOUT_S)result = (valueif isinstance(value, ReadingExtensionResult)diff --git a/tests/reading/test_extension_router.py b/tests/reading/test_extension_router.pyindex aa7b2219a..abdf6ac28 100644--- a/tests/reading/test_extension_router.py+++ b/tests/reading/test_extension_router.py@@ -1,5 +1,6 @@from __future__ import annotations+import asynciofrom pathlib import Pathfrom types import SimpleNamespace@@ -89,6 +90,23 @@ def test_selection_requirement_rejects_unverified_text(material, monkeypatch):assert response.status_code == 400+def test_oversized_unit_returns_protocol_error(material, monkeypatch):+ unit_path = ReadingStore().root / material.material_id / "units" / "0001.txt"+ unit_path.write_text("x" * 60_001, encoding="utf-8")+ client = _client(+ monkeypatch,+ _extension(lambda *_: pytest.fail("must not run")),+ )++ response = client.post(+ f"/api/v1/reading/materials/{material.material_id}/extensions/sample/actions/open",+ json={"locator": 1},+ )++ assert response.status_code == 422+ assert "too large" in response.json()["detail"]++@pytest.mark.parametrize("run_action",[@@ -104,3 +122,19 @@ def test_extension_failures_are_isolated(material, monkeypatch, run_action):)assert response.status_code == 503assert response.json()["detail"]["recoverable"] is True+++def test_hanging_extension_action_times_out(material, monkeypatch):+ async def run(*_args):+ await asyncio.sleep(1)++ monkeypatch.setattr(reading_extensions, "ACTION_TIMEOUT_S", 0.01)+ client = _client(monkeypatch, _extension(run))++ response = client.post(+ f"/api/v1/reading/materials/{material.material_id}/extensions/sample/actions/open",+ json={"locator": 1},+ )++ assert response.status_code == 503+ assert response.json()["detail"]["recoverable"] is True