mirror of
https://github.com/lfnovo/open-notebook.git
synced 2026-12-05 14:04:08 +00:00
Some checks are pending
Development Build / build-regular (push) Blocked by required conditions
Development Build / extract-version (push) Waiting to run
Development Build / changes (push) Waiting to run
Development Build / build-single (push) Blocked by required conditions
Development Build / summary (push) Blocked by required conditions
Tests / Backend Typecheck (push) Waiting to run
Tests / Frontend Tests (push) Waiting to run
Tests / Frontend Lint (push) Waiting to run
Tests / Frontend Build (push) Waiting to run
Tests / Backend Tests (push) Waiting to run
Tests / Backend Lint (push) Waiting to run
* fix(worker): fail permanently when the source was deleted before processing Source.get() raises NotFoundError instead of returning None, so the 'if not source: raise ValueError' guard in process_source_command was unreachable. NotFoundError is not in the command's stop_on list, so the job was retried as transient (max_attempts=15, exponential_jitter, wait_max=120) and every job behind it starved until the worker process was restarted. Catch NotFoundError around that call and re-raise as ValueError, which is already this command's permanent-failure channel. Fixes #1363 * chore(worker): rebase on main, cover deleted transformations, changelog - Rebased onto main (import conflict with #1425). - A transformation deleted before the job runs hits the same NotFoundError path in process_source_command; it now fails permanently too (test added). - ruff format on the new test (CI gates formatting since #1429). - CHANGELOG entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy * fix(domain): only a missing record is NotFoundError in ObjectModel.get ObjectModel.get wrapped every exception from repo_query, including the RuntimeError it re-raises for retriable SurrealDB transaction conflicts, as NotFoundError. With process_source now treating NotFoundError as permanent, a transient conflict would have failed the job for good. get() now lets NotFoundError / InvalidInputError through and raises DatabaseOperationError (chained) for anything else. The permanent-failure messages are static; the underlying error stays as the exception's cause for the worker log. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy * fix(worker): run_transformation fails permanently on a deleted source too NotFoundError (now only raised for a genuinely missing record) joins run_transformation's stop_on and permanent-failure arm. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy * docs(changelog): rebase entry onto the consolidated Unreleased section Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy * fix(worker): a source deleted mid-processing also fails permanently save_source re-reads the source after extraction; a deletion in that window raised NotFoundError from inside the graph, which process_source retried as transient. NotFoundError (now only a genuinely missing record) joins its stop_on and gets a permanent-failure arm. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy * docs(adr): ADR-013 for the ObjectModel.get error contract Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy * fix(embeddings): a record deleted before embedding is a permanent failure Completes the review on #1364: embed_* commands treat NotFoundError (now only a genuinely missing record) like their other permanent failures instead of retrying it; a DatabaseOperationError still retries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy * docs(embeddings): NotFoundError is a permanent failure too Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBYpxzSS62Y3TZkgB9NUZy --------- Co-authored-by: Luis Novo <lfnovo@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
204 lines
7.5 KiB
Python
204 lines
7.5 KiB
Python
"""A source deleted before its process_source job runs must fail permanently.
|
|
|
|
Regression for the worker-starvation bug: `Source.get()` raises `NotFoundError`
|
|
instead of returning `None`, so the `if not source: raise ValueError(...)` guard in
|
|
`process_source_command` never fired. `NotFoundError` is not in the command's
|
|
`stop_on` list, so the job was retried as a transient failure (up to 15 attempts
|
|
with exponential backoff, waits up to 120s) and every job behind it starved —
|
|
the worker stopped consuming jobs until the process was restarted.
|
|
"""
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
import commands # noqa: F401 -- import registers the @command decorators
|
|
from open_notebook.exceptions import NotFoundError
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_missing_source_raises_permanent_error():
|
|
"""NotFoundError from Source.get must surface as ValueError (terminal)."""
|
|
from commands.source_commands import SourceProcessingInput, process_source_command
|
|
|
|
input_data = SourceProcessingInput(
|
|
source_id="source:does-not-exist",
|
|
content_state={"file_path": "/tmp/whatever.md"},
|
|
notebook_ids=["notebook:whatever"],
|
|
transformations=[],
|
|
embed=True,
|
|
)
|
|
|
|
with patch(
|
|
"commands.source_commands.Source.get",
|
|
new=AsyncMock(
|
|
side_effect=NotFoundError("source with id source:does-not-exist not found")
|
|
),
|
|
):
|
|
with pytest.raises(ValueError) as excinfo:
|
|
await process_source_command(input_data)
|
|
|
|
assert "no longer exists" in str(excinfo.value)
|
|
# The retry policy keys off the exception type: ValueError is in `stop_on`,
|
|
# NotFoundError is not — that difference is the whole fix.
|
|
assert not isinstance(excinfo.value, NotFoundError)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_missing_transformation_raises_permanent_error():
|
|
"""A transformation deleted before the job runs is just as permanent."""
|
|
from commands.source_commands import SourceProcessingInput, process_source_command
|
|
|
|
input_data = SourceProcessingInput(
|
|
source_id="source:abc",
|
|
content_state={"file_path": "/tmp/whatever.md"},
|
|
notebook_ids=["notebook:whatever"],
|
|
transformations=["transformation:gone"],
|
|
embed=True,
|
|
)
|
|
|
|
with patch(
|
|
"commands.source_commands.Transformation.get",
|
|
new=AsyncMock(side_effect=NotFoundError("transformation not found")),
|
|
):
|
|
with pytest.raises(ValueError, match="no longer exists"):
|
|
await process_source_command(input_data)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_object_get_reports_db_failures_as_database_errors():
|
|
"""ObjectModel.get used to wrap every exception (e.g. a retriable SurrealDB
|
|
transaction conflict) as NotFoundError, so the fix above would have made
|
|
transient failures permanent. Only a missing record is NotFoundError."""
|
|
from open_notebook.domain.notebook import Source
|
|
from open_notebook.exceptions import DatabaseOperationError
|
|
|
|
with patch(
|
|
"open_notebook.domain.base.repo_query",
|
|
new=AsyncMock(side_effect=RuntimeError("Transaction conflict: retry")),
|
|
):
|
|
with pytest.raises(DatabaseOperationError):
|
|
await Source.get("source:abc")
|
|
|
|
with patch("open_notebook.domain.base.repo_query", new=AsyncMock(return_value=[])):
|
|
with pytest.raises(NotFoundError):
|
|
await Source.get("source:abc")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_transient_db_failure_stays_retryable():
|
|
"""A database failure while loading the source is re-raised as is (not
|
|
ValueError), so the worker's retry policy still applies."""
|
|
from commands.source_commands import SourceProcessingInput, process_source_command
|
|
from open_notebook.exceptions import DatabaseOperationError
|
|
|
|
input_data = SourceProcessingInput(
|
|
source_id="source:abc",
|
|
content_state={"file_path": "/tmp/whatever.md"},
|
|
notebook_ids=["notebook:whatever"],
|
|
transformations=[],
|
|
embed=True,
|
|
)
|
|
|
|
with patch(
|
|
"commands.source_commands.Source.get",
|
|
new=AsyncMock(side_effect=DatabaseOperationError("Failed to fetch")),
|
|
):
|
|
with pytest.raises(DatabaseOperationError):
|
|
await process_source_command(input_data)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_transformation_fails_permanently_when_source_is_gone():
|
|
"""run_transformation had the same deletion-before-processing gap."""
|
|
from surreal_commands import registry
|
|
|
|
from commands.source_commands import (
|
|
RunTransformationInput,
|
|
run_transformation_command,
|
|
)
|
|
|
|
with patch(
|
|
"commands.source_commands.Source.get",
|
|
new=AsyncMock(side_effect=NotFoundError("source not found")),
|
|
):
|
|
with pytest.raises(NotFoundError):
|
|
await run_transformation_command(
|
|
RunTransformationInput(
|
|
source_id="source:gone", transformation_id="transformation:t1"
|
|
)
|
|
)
|
|
|
|
command = registry.get_command("open_notebook", "run_transformation")
|
|
assert command is not None
|
|
assert NotFoundError in command.retry_config.stop_on
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_source_deleted_during_processing_fails_permanently():
|
|
"""save_source re-reads the source after extraction; a deletion in that
|
|
window raises NotFoundError from inside the graph, which must not be
|
|
retried either."""
|
|
from types import SimpleNamespace
|
|
|
|
from surreal_commands import registry
|
|
|
|
from commands.source_commands import SourceProcessingInput, process_source_command
|
|
|
|
source = SimpleNamespace(id="source:abc", command=None, save=AsyncMock())
|
|
with (
|
|
patch(
|
|
"commands.source_commands.Source.get", new=AsyncMock(return_value=source)
|
|
),
|
|
patch(
|
|
"commands.source_commands.source_graph.ainvoke",
|
|
new=AsyncMock(
|
|
side_effect=NotFoundError("source with id source:abc not found")
|
|
),
|
|
),
|
|
):
|
|
with pytest.raises(NotFoundError):
|
|
await process_source_command(
|
|
SourceProcessingInput(
|
|
source_id="source:abc",
|
|
content_state={"file_path": "/tmp/x.md"},
|
|
notebook_ids=[],
|
|
transformations=[],
|
|
embed=False,
|
|
)
|
|
)
|
|
|
|
command = registry.get_command("open_notebook", "process_source")
|
|
assert command is not None
|
|
assert NotFoundError in command.retry_config.stop_on
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_embedding_a_deleted_source_fails_permanently():
|
|
"""embed_source had the same gap (lfnovo's review on #1364): a record
|
|
deleted before the job ran is a permanent failure, not a retry."""
|
|
from commands.embedding_commands import EmbedSourceInput, embed_source_command
|
|
|
|
with patch(
|
|
"commands.embedding_commands.Source.get",
|
|
new=AsyncMock(
|
|
side_effect=NotFoundError("source with id source:gone not found")
|
|
),
|
|
):
|
|
result = await embed_source_command(EmbedSourceInput(source_id="source:gone"))
|
|
|
|
assert result.success is False
|
|
assert "not found" in (result.error_message or "")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_embedding_db_failure_stays_retryable():
|
|
from commands.embedding_commands import EmbedSourceInput, embed_source_command
|
|
from open_notebook.exceptions import DatabaseOperationError
|
|
|
|
with patch(
|
|
"commands.embedding_commands.Source.get",
|
|
new=AsyncMock(side_effect=DatabaseOperationError("Failed to fetch")),
|
|
):
|
|
with pytest.raises(DatabaseOperationError):
|
|
await embed_source_command(EmbedSourceInput(source_id="source:abc"))
|