fix: backfill missing 1.1.4 fixes into main (#366)

* fix(knowledge): contain original-file write path (CWE-22)

The multipart upload filename was joined into ``_original/`` verbatim.
An attacker-controlled filename such as ``/tmp/pwned`` or
``../../.bashrc`` would let ``POST /knowledge/documents`` write outside
the document directory (the ``/`` operator discards the left operand
for absolute paths; ``..`` walks upward). The read side had the
symmetric issue.

Fix, mirroring the sender_id containment shipped in 1.0.1
(GHSA-c795-2g9c-j48m):

- Add ``_safe_original_filename`` reducing the untrusted filename to a
  single POSIX/Windows-basename component; reject degenerate residuals
  (``""``, ``"."``, ``".."``) with PathTraversalError.
- ``_write_original_file`` asserts ``target.resolve()`` stays inside
  ``original_dir.resolve()`` before any filesystem touch (mkdir/write).
- ``_resolve_original_file_path`` sanitises symmetrically so a stored
  provenance label can never resolve to an out-of-directory file.

Four SEC regression tests cover: absolute filename, ``..`` traversal,
degenerate filename rejection, and read-side sanitisation.

Backport from GitLab release/v1.1.4 (commit 40f19de) — 1.1.4 shipped
this fix; the GitLab -> GitHub sync stopped at 1.1.3, so 1.2.0
regressed the containment. This commit alone restores the fix; the
1.2.1 release PR ships it to PyPI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(cascade): retry classification, budget, and reconcile races

Backport the cascade reliability work that shipped in GitLab 1.1.4
(MR !49 / commit 95db2f5) — six interlocking changes the reviewer
should read in order:

1. Worker retry classification uses ExternalServiceError (embedding /
   LLM / rerank transient failures) as the "retry inline" signal.
   The legacy RecoverableError hierarchy under cascade/errors.py is
   removed; the retry contract now lives in the domain error tree
   (core/errors.py). Docstrings in handlers/base.py and
   sqlite/tables/md_change_state.py updated to match.

2. Cross-cycle retry budget: _MAX_TOTAL_RETRIES = 12. Once total
   attempts across scanner cycles exhaust the budget, the worker
   marks retryable=False in place instead of looping forever on a
   sustained upstream outage.

3. md_change_state upsert preserves retry_count on scanner
   re-enqueue when mtime is unchanged (previously reset to 0 every
   sweep, defeating the budget). mtime change (user edit) still
   resets the counter.

4. Reconciler no longer re-enqueues pending / processing rows on
   stable mtime — that was overwriting the worker's mark_done. It
   also skips failed rows with retryable=False on stable mtime so
   the entry-check demote path is stable.

5. mtime tolerance (10 ms, MTIME_TOLERANCE_SECONDS) absorbs the
   SQLite REAL float precision loss that previously flapped the
   reconcile decision when the same md was rewritten without a real
   content change. The constant is defined once in the sqlite repo
   and imported by the reconciler so both sides use the same tol.

6. Worker _run_rebuild_once carries an explicit
   `state.task is not asyncio.current_task()` guard before awaiting
   the optimize task — the previous contextlib.suppress was silently
   swallowing self-await RuntimeError.

Kept intact from the GitHub 1.2.0 baseline:
- The `except FileNotFoundError → handle_deleted` branch in the
  worker (delete/modify race — see
  test_modified_event_for_vanished_file_is_processed_as_delete).

Test coverage added:
- test_retry_budget_exhausted_marks_unrecoverable
- test_external_service_error_at_budget_edge_demotes_in_place
- test_upsert_preserves_retry_count_for_failed_stable_mtime
- test_upsert_resets_retry_count_on_mtime_change
- test_optimize_fallback_rebuild_on_sustained_failure
- reconciler mtime-tolerance / stable-mtime skip suite

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(embedding): raise on empty API data; forward MRL dimensions

Backport from GitLab 1.1.4 (MR !49 / commit 95db2f5).

Two behavioural changes on ``OpenAIEmbeddingProvider._embed_chunk``:

1. ``response.data == []`` now raises ``EmbeddingServiceError`` instead
   of returning an empty list. Some upstream providers (observed on
   DeepInfra under load) return HTTP 200 with an empty ``data`` array;
   the silent zero-vector path was corrupting search indexes without
   any signal.

2. When ``[embedding] dimensions = N`` is set in ``everos.toml``, the
   parameter is forwarded to the API so MRL-capable models
   (OpenAI text-embedding-3-*, Qwen3-Embedding, ...) do server-side
   truncation with proper re-normalization. Client-side truncation to
   ``dim`` remains as a fallback for backends that ignore the param.
   ``openai.NOT_GIVEN`` is used as the sentinel so the request omits
   the field when the setting is left at the default ``None``.

Config plumbing:
- ``EmbeddingSettings.dimensions: int | None = None``
- factory forwards ``dimensions=settings.dimensions`` to the provider

The provider stays inside the existing ``memory_span`` OTel wrapper
and continues to report input-only tokens via ``set_generation_usage``
- both are GitHub 1.2.0 native tracing behaviours preserved intact.

Test coverage:
- test_empty_response_data_raises_embedding_error (new)
- test_usage_span._FakeEmbeddings.create signature updated to accept
  ``dimensions`` kwarg so the OTel token-recording tests still exercise
  the same call path

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(extract): retry episode extraction on malformed LLM output

Backport from GitLab 1.1.4 (MR !49 / commit 95db2f5).

The ``/flush`` synchronous path called ``EpisodeExtractor.aextract``
exactly once. everalgo raises ``ValueError`` when the LLM returns
malformed JSON (observed with OpenRouter partial responses where
finish_reason=stop but the body is truncated) — the caller was
surfaced a 500 for a transient upstream hiccup.

``_extract_with_retry`` wraps the call with two extra attempts at 1s
and 2s backoff (final attempt propagates untouched), typed as
``AlgoEpisode`` so the caller path stays annotated. Retry stays inside
the existing GitHub 1.2.0 ``memory_span("everos.extract", ...)`` OTel
wrapper — the OTel token capture and the retry loop are orthogonal.

TODO in the code notes we should catch a typed everalgo
``ExtractionError`` once that type is introduced (currently ValueError
is broad).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: log non-stop finish_reason; bump everalgo-user-memory 0.3.2

Two loosely coupled changes from GitLab 1.1.4 (MR !49 / commit 95db2f5)
that arrive together as a housekeeping commit.

1. ``_LoggingLLMClient`` diagnostic wrapper (new, file-private).

   Wraps the raw everalgo LLM client and, on every ``chat()``, warns
   when ``resp.finish_reason != "stop"`` — logging the reason,
   ``content_len``, the last 200 chars of ``content``, and ``model``.
   Aims at OpenRouter/DeepSeek truncation triage where the provider
   silently caps output length and returns finish_reason=length /
   filter / etc. Non-invasive: one branch per call, no config gate.

   Wrapper stack in ``get_llm_client``:

       LoggingLLMClient(UsageRecordingClient(build_client(...)))
       LoggingLLMClient(build_client(...))    # observability off

   ``UsageRecordingClient`` (GitHub 1.2.0 native OTel token capture)
   stays gated by ``settings.observability.enabled`` — this commit
   preserves that. LoggingLLMClient is always outermost so the reason
   it observes is exactly the reason the underlying provider reported.

2. ``everalgo-user-memory`` 0.3.1 -> 0.3.2 (pyproject + uv.lock).

   Same bump the GitLab 1.1.4 release lane took; unblocks the
   episode-extract retry work in commit 4 seeing the upstream
   improvements. Verified via ``uv sync``.

No functional API changes.

Test coverage:
- test_returns_singleton_when_configured now asserts the outer
  LoggingLLMClient wrapper.
- test_wraps_client_when_observability_enabled asserts the two-layer
  Logging(UsageRecording(...)) stack.
- test_does_not_wrap_client_when_observability_disabled asserts
  Logging still wraps when tracing is off.
- test_logging_wrapper_warns_on_non_stop_finish_reason (new).
- test_logging_wrapper_silent_on_stop_finish_reason (new).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs(changelog): restore [1.1.4] section to match published sdist

The 1.1.4 changelog entry on this branch previously listed three
items (Langfuse example, delete/modify race, live-server telemetry).
The 1.1.4 sdist on PyPI, however, was built from the internal
release lane and includes the CWE-22 containment, the cascade
retry-budget / mtime-tolerance / reconcile-guard work, the embedding
empty-data raise, the episode-extract retry, MRL dimensions, and the
LLM finish_reason diagnostic — none of which were represented here
when the tag was cut.

Rewrite the [1.1.4] section so it matches the wheel a user actually
installs from PyPI:

- Add a header note explaining the retroactive restoration.
- Fixed: CWE-22, cascade reliability bundle, delete/modify race
  (unchanged wording), embedding empty-data, episode extract retry,
  Langfuse live-server telemetry (unchanged wording).
- Added: MRL dimensions, LLM finish_reason diagnostic, Langfuse
  example (unchanged wording).
- Changed: everalgo-user-memory 0.3.1 -> 0.3.2.

The GitLab-side `.gitlab-ci.yml` in-house-runner entry is dropped —
open-source CI runs on GitHub Actions and the internal runner switch
is not visible to public users.

Date stays 2026-07-20 (the GitHub v1.1.4 tag date / PyPI upload
timestamp) rather than the internal 2026-07-23 code-freeze date, so
the timeline of what shipped where remains internally consistent.

The corresponding code fixes are all backported by earlier commits
in this PR; this commit only aligns the changelog surface.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kendrick-Song
2026-07-28 14:57:53 +08:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 6dcd3ebda2
commit 42629dfd4d
27 changed files with 818 additions and 147 deletions
@@ -49,6 +49,7 @@ def build_embedding_provider(
api_key=settings.api_key.get_secret_value(),
base_url=settings.base_url,
dim=dim,
dimensions=settings.dimensions,
timeout=settings.timeout_seconds,
max_retries=settings.max_retries,
batch_size=settings.batch_size,
@@ -2,10 +2,11 @@
Wraps :class:`openai.AsyncOpenAI` so any OpenAI-protocol endpoint
(DeepInfra, OpenAI, Together, Fireworks, …) works without per-provider
forks. Self-hosted vLLM also exposes the same shape; the only quirk it
imposes is that the ``dimensions`` request parameter is ignored — we
truncate client-side to ``dim`` so callers always see the declared
shape regardless of backend.
forks. When ``dimensions`` is set, the parameter is forwarded to the
API so MRL-capable models (OpenAI text-embedding-3-*, Qwen3-Embedding,
…) can do server-side truncation with proper re-normalization.
Client-side truncation to ``dim`` is always applied as a fallback for
backends that ignore or don't support the parameter.
Concurrency model:
@@ -37,8 +38,11 @@ class OpenAIEmbeddingProvider:
base_url: OpenAI-protocol endpoint
(e.g. ``"https://api.deepinfra.com/v1/openai"``).
dim: Target vector dimension. Vectors longer than this are
truncated client-side (matches the LanceDB column shape —
see ``17_lancedb_tables_design.md``).
truncated client-side (matches the LanceDB column shape).
dimensions: Optional API-level ``dimensions`` parameter for
MRL-capable models. When set, forwarded to the embedding
API for server-side truncation with re-normalization.
When ``None``, the parameter is omitted from the request.
timeout: Per-request timeout, seconds.
max_retries: Retry budget exposed via the openai SDK.
batch_size: How many inputs per ``/embeddings`` call.
@@ -52,12 +56,14 @@ class OpenAIEmbeddingProvider:
api_key: str,
base_url: str,
dim: int = 1024,
dimensions: int | None = None,
timeout: float = 30.0,
max_retries: int = 3,
batch_size: int = 10,
max_concurrent: int = 5,
) -> None:
self.dim = dim
self._dimensions = dimensions
self._model = model
self._batch_size = batch_size
self._semaphore = asyncio.Semaphore(max_concurrent)
@@ -100,14 +106,21 @@ class OpenAIEmbeddingProvider:
response = await self._client.embeddings.create(
model=self._model,
input=chunk,
dimensions=self._dimensions
if self._dimensions is not None
else openai.NOT_GIVEN,
)
except openai.OpenAIError as exc:
raise EmbeddingServiceError(str(exc)) from exc
if not response.data:
raise EmbeddingServiceError(
f"Embedding API returned empty data for {len(chunk)} inputs"
)
# Embeddings report only input (prompt) tokens.
usage = getattr(response, "usage", None)
set_generation_usage(
model=self._model,
input_tokens=usage.prompt_tokens if usage else None,
)
# OpenAI returns ``data`` indexed by request order; truncate to ``dim``.
# Client-side truncation — always applied as fallback.
return [list(item.embedding[: self.dim]) for item in response.data]
+51 -1
View File
@@ -9,9 +9,13 @@ provider) instead of silently failing per-request downstream.
from __future__ import annotations
from typing import Any
from everalgo.llm import build_client
from everalgo.llm.config import LLMConfig
from everalgo.llm.protocols import LLMClient
from everalgo.llm.types import ChatMessage, ChatResponse
from pydantic import BaseModel
from everos.config import load_settings
from everos.core.observability.logging import get_logger
@@ -21,6 +25,48 @@ from ._usage_client import UsageRecordingClient
logger = get_logger(__name__)
class _LoggingLLMClient:
"""Wrapper that logs non-stop ``finish_reason`` for diagnostics.
Always active — cost is one branch per chat() call. OpenRouter and
a few compatible providers occasionally return HTTP 200 with a
truncated body and ``finish_reason != "stop"`` (length cap, filter
trigger). Recording the reason plus the tail of the content lets
us triage those without needing a repro from the caller.
"""
def __init__(self, inner: LLMClient) -> None:
self._inner = inner
async def chat(
self,
messages: list[ChatMessage],
*,
model: str | None = None,
temperature: float | None = None,
max_tokens: int | None = None,
response_format: type[BaseModel] | None = None,
**extra: Any,
) -> ChatResponse:
resp = await self._inner.chat(
messages,
model=model,
temperature=temperature,
max_tokens=max_tokens,
response_format=response_format,
**extra,
)
if resp.finish_reason and resp.finish_reason != "stop":
logger.warning(
"llm_non_stop_finish",
finish_reason=resp.finish_reason,
content_len=len(resp.content),
content_tail=resp.content[-200:] if resp.content else "",
model=resp.model,
)
return resp
class LLMNotConfiguredError(RuntimeError):
"""Raised when ``settings.llm`` is missing ``api_key`` or ``base_url``."""
@@ -60,7 +106,11 @@ def get_llm_client() -> LLMClient:
# disabled path (the default) allocation- and overhead-free.
if settings.observability.enabled:
client = UsageRecordingClient(client)
_llm_client = client
# Finish-reason diagnostic wrapper is always outermost: it must see
# the response even when tracing is off, and it must observe the
# exact reason the underlying provider reported (not one synthesised
# by an inner wrapper).
_llm_client = _LoggingLLMClient(client)
logger.info("llm_client_built", model=llm_cfg.model)
return _llm_client
+7
View File
@@ -176,6 +176,7 @@ class EmbeddingSettings(BaseModel):
EVEROS_EMBEDDING__MODEL
EVEROS_EMBEDDING__API_KEY
EVEROS_EMBEDDING__BASE_URL
EVEROS_EMBEDDING__DIMENSIONS
EVEROS_EMBEDDING__TIMEOUT_SECONDS
EVEROS_EMBEDDING__MAX_RETRIES
EVEROS_EMBEDDING__BATCH_SIZE
@@ -185,6 +186,12 @@ class EmbeddingSettings(BaseModel):
model: str | None = None
api_key: SecretStr | None = None
base_url: str | None = None
dimensions: int | None = None
"""API-level ``dimensions`` parameter for MRL-capable models.
When set, passed to the embedding API so the server truncates
with proper re-normalization. When ``None`` (default), the
parameter is omitted and client-side truncation to ``dim``
handles dimension alignment."""
timeout_seconds: float = Field(default=30.0, gt=0)
max_retries: int = Field(default=3, ge=0)
batch_size: int = Field(default=10, ge=1)
@@ -31,6 +31,7 @@ not need to manage either.
# ``SQLModel.metadata`` so ``SqliteLifespanProvider.startup`` can
# ``create_all`` without callers having to import each model module.
from . import tables as tables
from .repos import MTIME_TOLERANCE_SECONDS as MTIME_TOLERANCE_SECONDS
from .repos import DocumentListPage as DocumentListPage
from .repos import DocumentUpsertPayload as DocumentUpsertPayload
from .repos import QueueSummary as QueueSummary
@@ -58,6 +59,7 @@ from .tables import ReflectionReport as ReflectionReport
from .tables import UnprocessedBuffer as UnprocessedBuffer
__all__ = [
"MTIME_TOLERANCE_SECONDS",
"Cluster",
"ClusterMember",
"ConversationStatus",
@@ -12,6 +12,7 @@ from .knowledge import DocumentUpsertPayload as DocumentUpsertPayload
from .knowledge import TopicUpsertPayload as TopicUpsertPayload
from .knowledge import knowledge_document_repo as knowledge_document_repo
from .knowledge import knowledge_topic_sqlite_repo as knowledge_topic_sqlite_repo
from .md_change_state import MTIME_TOLERANCE_SECONDS as MTIME_TOLERANCE_SECONDS
from .md_change_state import QueueSummary as QueueSummary
from .md_change_state import md_change_state_repo as md_change_state_repo
from .memcell import memcell_repo as memcell_repo
@@ -19,6 +20,7 @@ from .reflection_report import reflection_report_repo as reflection_report_repo
from .unprocessed_buffer import unprocessed_buffer_repo as unprocessed_buffer_repo
__all__ = [
"MTIME_TOLERANCE_SECONDS",
"DocumentListPage",
"DocumentUpsertPayload",
"QueueSummary",
@@ -29,7 +29,7 @@ from __future__ import annotations
import dataclasses
from sqlalchemy import func, select, update
from sqlalchemy import func, select, text, update
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
@@ -39,6 +39,20 @@ from everos.core.persistence.sqlite import RepoBase, session_scope
from ..sqlite_manager import get_session_factory
from ..tables import MdChangeState
MTIME_TOLERANCE_SECONDS = 0.01
"""Absolute mtime delta (seconds) treated as "unchanged".
Used both by the upsert's retry_count carry-over CASE (below) and by the
cascade reconciler's stable-mtime check
(:func:`everos.memory.cascade.reconciler.reconcile`). The two comparisons
share this single source of truth so their notions of "same file" stay
in lock-step; drift would leave upsert preserving ``retry_count`` while
the reconciler skips re-enqueue (or vice versa), corrupting the retry
budget.
Sized to absorb SQLite ``REAL`` round-trip loss (~5 µs) so a stable
mtime does not oscillate the reconcile decision every scanner tick."""
@dataclasses.dataclass(frozen=True)
class QueueSummary:
@@ -94,9 +108,13 @@ class _MdChangeStateRepo(RepoBase[MdChangeState]):
``lsn = MAX(lsn) + 1``.
- **Existing row** → bump ``last_changed_at``, refresh
``kind`` / ``change_type`` / ``mtime``, reset status back to
``pending``, zero ``retry_count`` / ``error`` / ``retryable``,
and assign a fresh ``MAX(lsn) + 1`` so the worker re-processes
this path *after* anything queued in between.
``pending``, clear ``error`` / ``retryable``, and assign a
fresh ``MAX(lsn) + 1`` so the worker re-processes this path
*after* anything queued in between. ``retry_count`` is
conditionally preserved: if the row was ``failed`` and the
mtime is unchanged (scanner auto-retry), ``retry_count``
carries over so the worker can enforce a total retry budget;
otherwise it resets to 0 (new content deserves a fresh start).
The fresh LSN on re-enqueue is the property that lets the worker
rely on ``ORDER BY lsn`` for ordering without losing fairness
@@ -134,8 +152,16 @@ class _MdChangeStateRepo(RepoBase[MdChangeState]):
"status": "pending",
"retryable": None,
"last_attempt_at": None,
"retry_count": 0,
"error": None,
"retry_count": text(
"CASE"
" WHEN md_change_state.status = 'failed'"
" AND ABS(md_change_state.mtime - :new_mtime)"
f" < {MTIME_TOLERANCE_SECONDS}"
" THEN md_change_state.retry_count"
" ELSE 0"
" END"
).bindparams(new_mtime=mtime),
},
)
)
@@ -99,10 +99,10 @@ class MdChangeState(BaseTable, table=True):
retryable: bool | None = Field(default=None)
"""Meaningful only when ``status='failed'``.
- ``TRUE`` — RecoverableError exhausted MAX_RETRY; ``cascade fix
- ``TRUE`` — ExternalServiceError exhausted MAX_RETRY; ``cascade fix
--apply`` will re-enqueue this row (pending, retry_count reset).
- ``FALSE`` — UnrecoverableError (malformed YAML, schema error
etc.); requires editing the md and re-saving.
- ``FALSE`` — unrecoverable error (malformed YAML, schema error,
retry budget exhausted); requires editing the md and re-saving.
- ``NULL`` — not a failed row (pending / processing / done).
"""
-8
View File
@@ -9,15 +9,10 @@ Public surface — what lifespan providers / CLI commands import:
- :class:`CascadeOrchestrator` — composite owner; start / stop / sync.
- :class:`CascadeConfig` — construction-time tuning knobs.
- :class:`RecoverableError` / :class:`UnrecoverableError` — handler
contract for retry classification.
- :data:`KIND_REGISTRY` / :func:`match_kind` — kind dispatch (also
used by CLI ``cascade sync --path`` to resolve a single file's kind).
"""
from .errors import CascadeError as CascadeError
from .errors import RecoverableError as RecoverableError
from .errors import UnrecoverableError as UnrecoverableError
from .orchestrator import CascadeConfig as CascadeConfig
from .orchestrator import CascadeOrchestrator as CascadeOrchestrator
from .registry import KIND_REGISTRY as KIND_REGISTRY
@@ -27,10 +22,7 @@ from .registry import match_kind as match_kind
__all__ = [
"KIND_REGISTRY",
"CascadeConfig",
"CascadeError",
"CascadeOrchestrator",
"KindSpec",
"RecoverableError",
"UnrecoverableError",
"match_kind",
]
-30
View File
@@ -1,30 +0,0 @@
"""Cascade error hierarchy — drives the worker's retry classification.
The worker decides ``mark_failed(retryable=True/False)`` purely by
exception class:
- :class:`RecoverableError` → transient (HTTP 5xx, network, embedding
rate limit). Worker retries up to ``MAX_RETRY`` inline, then marks
``retryable=TRUE`` so ``cascade fix --apply`` can re-enqueue.
- :class:`UnrecoverableError` → fatal (YAML parse, missing required
field, schema mismatch). Worker stops immediately and marks
``retryable=FALSE`` — only a user edit to the md will unstick it.
- Anything else → treated as :class:`UnrecoverableError`. The worker
catches ``Exception`` defensively so an unexpected failure never
hangs the daemon, but the diagnostic message carries the original
type for triage.
"""
from __future__ import annotations
class CascadeError(Exception):
"""Root of the cascade error tree."""
class RecoverableError(CascadeError):
"""Transient failure — worker should retry then mark retryable."""
class UnrecoverableError(CascadeError):
"""Fatal failure — needs a user edit before re-running."""
+1 -1
View File
@@ -45,7 +45,7 @@ class Handler(abc.ABC):
``handle_added_or_modified`` and ``handle_deleted`` are the two
cases the worker dispatches on, derived from
:class:`MdChangeState.change_type`. Either may raise — the worker
catches and classifies (``RecoverableError`` vs unrecoverable) to
catches and classifies (``ExternalServiceError`` vs unrecoverable) to
drive the retry / failed-state lifecycle.
"""
+21 -3
View File
@@ -21,7 +21,12 @@ Three categories per 12 doc §5.3:
Paths whose prior state row is ``done`` AND the mtime matches are
skipped on the add/modify side — the reconcile output stays tight on
quiet sweeps.
quiet sweeps. The same applies to ``failed`` rows whose ``retryable``
flag is ``False`` (unrecoverable) and whose mtime is unchanged — those
must not be auto-re-enqueued; the user has to edit the md (which
changes mtime) to get another attempt. ``failed`` rows with
``retryable=True`` (transient) are re-emitted on a stable mtime so the
worker auto-retries on the next scanner sweep.
"""
from __future__ import annotations
@@ -29,6 +34,8 @@ from __future__ import annotations
import dataclasses
from collections.abc import Iterable, Mapping
from everos.infra.persistence.sqlite import MTIME_TOLERANCE_SECONDS
from .types import ReconcileDecision, ScanInput
@@ -46,6 +53,11 @@ class PriorState:
mtime: float
status: str # "pending" | "processing" | "done" | "failed"
change_type: str # "added" | "modified" | "deleted"
retryable: bool | None = None
"""``failed`` eligibility flag: ``True`` transient, ``False``
unrecoverable, ``None`` for non-``failed`` rows."""
retry_count: int = 0
"""Total retry attempts across scanner re-enqueue cycles."""
def reconcile(
@@ -81,8 +93,14 @@ def reconcile(
)
)
continue
# Skip when the row is already done and mtime hasn't moved.
if prior.status == "done" and prior.mtime == item.mtime:
mtime_stable = abs(prior.mtime - item.mtime) < MTIME_TOLERANCE_SECONDS
if mtime_stable and prior.status in (
"done",
"pending",
"processing",
):
continue
if mtime_stable and prior.status == "failed" and prior.retryable is False:
continue
decisions.append(
ReconcileDecision(
+2
View File
@@ -173,6 +173,8 @@ async def _load_state_snapshot() -> dict[str, PriorState]:
mtime=row.mtime,
status=row.status,
change_type=row.change_type,
retryable=row.retryable,
retry_count=row.retry_count,
)
for row in rows
}
+77 -11
View File
@@ -9,12 +9,23 @@ Each cycle:
:meth:`handle_added_or_modified` or :meth:`handle_deleted` based on
the row's ``change_type``.
3. On success: ``mark_done``.
4. On :class:`RecoverableError`: retry inline up to ``MAX_RETRY``; if
all attempts fail, ``mark_failed(retryable=True)``.
4. On :class:`~everos.core.errors.ExternalServiceError`: retry inline
up to ``MAX_RETRY``; if all attempts fail,
``mark_failed(retryable=True)`` — unless the cross-cycle retry
budget (:data:`_MAX_TOTAL_RETRIES`) is also exhausted, in which
case ``retryable=False`` is set here directly so the row skips
one extra scanner cycle before the entry-check would demote it.
5. On any other exception: ``mark_failed(retryable=False)`` (treated
as unrecoverable, surfaces in ``cascade fix`` for the user to
triage by editing the md).
Before step 2, a row whose ``retry_count`` has already reached
:data:`_MAX_TOTAL_RETRIES` (across scanner re-enqueue cycles, not just
this batch's inline retries) is short-circuited straight to
``mark_failed(retryable=False)`` without invoking the handler — this
bounds the total retry budget so a persistently-failing row cannot
retry forever.
Batch processing is concurrent inside a batch (``asyncio.gather``);
ordering across rows is best-effort — the LSN gives a deterministic
prefix but the handlers themselves are independent.
@@ -48,10 +59,10 @@ import datetime as dt
import time
from dataclasses import dataclass
from everos.core.errors import ExternalServiceError
from everos.core.observability.logging import get_logger
from everos.infra.persistence.sqlite import MdChangeState, md_change_state_repo
from .errors import RecoverableError
from .handlers import Handler
logger = get_logger(__name__)
@@ -70,6 +81,17 @@ escalated from ``warning`` to ``error``. A one-off failure is benign
cleanup are stuck and the index dir will grow unbounded — that must
surface to health checks / alerting rather than rot as a warning nobody
reads (the failure mode behind lance-format/lance#7653)."""
_MAX_TOTAL_RETRIES = 12
"""Total retry budget across scanner re-enqueue cycles.
Each cycle: worker retries up to ``MAX_RETRY`` (default 3) inline;
scanner re-enqueues on the next 30s sweep; retry_count accumulates.
12 ≈ 4 scanner cycles × 3 inline retries ≈ 2 minutes of retrying.
Once exhausted, ``mark_failed(retryable=False)`` so the reconciler
stops re-enqueuing. Recover via ``cascade fix --apply`` (resets
retry_count) or editing the md (mtime change resets retry_count)."""
DEFAULT_OPTIMIZE_REBUILD_INTERVAL_SECONDS = 12 * 60 * 60.0
"""How often (per kind) to do a full ``drop_index + create_index`` rebuild.
@@ -323,6 +345,21 @@ class CascadeWorker:
)
return None
if row.retry_count >= _MAX_TOTAL_RETRIES:
logger.warning(
"cascade_worker_retry_budget_exhausted",
md_path=row.md_path,
kind=row.kind,
retry_count=row.retry_count,
)
await md_change_state_repo.mark_failed(
row.md_path,
retryable=False,
error=f"retry budget exhausted after {row.retry_count} attempts",
new_retry_count=row.retry_count,
)
return None
retry_count = row.retry_count
last_error: str = ""
for attempt in range(self._max_retry + 1):
@@ -333,8 +370,13 @@ class CascadeWorker:
try:
outcome = await handler.handle_added_or_modified(row.md_path)
except FileNotFoundError:
# The md disappeared between scanner enqueue and here
# (delete/modify race — cascade delete event may not
# arrive if it fires while the row is already in
# ``processing``). Fold into a deletion so the row
# completes with ``mark_done`` instead of failing.
outcome = await handler.handle_deleted(row.md_path)
except RecoverableError as exc:
except ExternalServiceError as exc:
last_error = f"{type(exc).__name__}: {exc}"
logger.warning(
"cascade_worker_recoverable",
@@ -346,9 +388,13 @@ class CascadeWorker:
retry_count += 1
await asyncio.sleep(self._retry_backoff * (attempt + 1))
continue
# Inline attempts exhausted. Only stay retryable when
# the cross-cycle budget still has room; otherwise
# demote directly instead of taking an extra scanner
# cycle to hit the entry-check short-circuit.
await md_change_state_repo.mark_failed(
row.md_path,
retryable=True,
retryable=retry_count < _MAX_TOTAL_RETRIES,
error=last_error,
new_retry_count=retry_count,
)
@@ -513,11 +559,6 @@ class CascadeWorker:
if state is not None:
state.optimize_failures += 1
failures = state.optimize_failures
# A one-off failure is benign (next tick retries). A sustained
# streak means optimize — compaction *and* version cleanup — is
# stuck, so the index dir grows unbounded; escalate to error so
# it surfaces to health checks / alerting instead of rotting as
# a warning nobody reads (see lance-format/lance#7653).
log = (
logger.error
if failures >= _OPTIMIZE_FAILURE_ALERT_THRESHOLD
@@ -530,6 +571,20 @@ class CascadeWorker:
consecutive_failures=failures,
error=f"{type(exc).__name__}: {exc}",
)
if failures >= _OPTIMIZE_FAILURE_ALERT_THRESHOLD:
logger.info(
"cascade_lancedb_optimize_fallback_rebuild",
kind=kind,
consecutive_failures=failures,
)
await self._run_rebuild_once(kind)
# Reset even when rebuild fails: rate-limits fallback
# rebuild to at most once per threshold failures. A
# failed rebuild defers cleanup to the 12h periodic
# sweep — harmless for correctness (see
# _run_rebuild_once docstring).
if state is not None:
state.optimize_failures = 0
async def _heartbeat_loop(self) -> None:
"""Periodic safety net for the optimizer.
@@ -603,7 +658,18 @@ class CascadeWorker:
# Drain any in-flight optimize before taking the rebuild slot —
# both would commit on the same manifest version. The optimize
# runner reciprocates (it awaits ``state.rebuild_task`` on entry).
if state.task is not None and not state.task.done():
# Skip when ``state.task`` is the current task: the fallback-rebuild
# path in ``_run_optimize_once`` reaches here from *inside* the
# optimize runner itself, so awaiting ``state.task`` would be
# self-await (asyncio raises RuntimeError). Suppress catches it,
# but relying on that is fragile — the explicit check is the
# correctness contract; suppress remains only for unexpected
# optimize failures.
if (
state.task is not None
and not state.task.done()
and state.task is not asyncio.current_task()
):
with contextlib.suppress(Exception):
await state.task
rebuild_task = asyncio.create_task(
@@ -13,9 +13,11 @@ Run inside ``service.memorize`` via ``asyncio.gather`` alongside
from __future__ import annotations
import asyncio
from pathlib import Path
from typing import TYPE_CHECKING
from everalgo.types import Episode as AlgoEpisode
from everalgo.types import MemCell as AlgoMemCell
from everalgo.user_memory import EpisodeExtractor
@@ -36,6 +38,8 @@ if TYPE_CHECKING:
logger = get_logger(__name__)
_TRACK = "user_memory"
_EXTRACT_MAX_RETRIES = 2
_EXTRACT_RETRY_BACKOFF = 1.0
def _root_relative(path: str) -> str:
@@ -122,8 +126,14 @@ class UserMemoryPipeline:
) as extract_span:
# Token usage is recorded onto this span by the LLM client
# wrapper when the extractor issues its chat() call.
algo_ep = await self._ep_ext.aextract(
cell, sender_id=None, prompt=episode_prompt
#
# Retry on ValueError: everalgo raises ValueError when the
# LLM returns malformed JSON (e.g. OpenRouter partial
# response with finish_reason=stop). Transient — same input
# typically succeeds on retry. TODO: catch a typed
# ExtractionError once everalgo introduces one.
algo_ep = await _extract_with_retry(
self._ep_ext, cell, episode_prompt, memcell_id
)
# Extracted memory text (only when capture_content is on).
capture_output(extract_span, algo_ep.episode)
@@ -281,3 +291,27 @@ def _episode_to_entry_body(
sections["Summary"] = str(summary)
sections["Content"] = episode.episode
return inline, sections
async def _extract_with_retry(
extractor: EpisodeExtractor,
cell: AlgoMemCell,
prompt: str | None,
memcell_id: str,
) -> AlgoEpisode:
"""Call everalgo episode extraction with retry on malformed LLM output."""
for attempt in range(_EXTRACT_MAX_RETRIES):
try:
return await extractor.aextract(cell, sender_id=None, prompt=prompt)
except ValueError as exc:
wait = _EXTRACT_RETRY_BACKOFF * (attempt + 1)
logger.warning(
"episode_extract_retry",
memcell_id=memcell_id,
attempt=attempt,
error=str(exc)[:200],
backoff_s=wait,
)
await asyncio.sleep(wait)
# Final attempt — a ValueError here propagates to the caller.
return await extractor.aextract(cell, sender_id=None, prompt=prompt)
+59 -5
View File
@@ -21,7 +21,7 @@ import shutil
import time
from collections.abc import Sequence
from datetime import datetime
from pathlib import Path
from pathlib import Path, PurePosixPath
from typing import TYPE_CHECKING, Protocol
from uuid import uuid4
@@ -35,6 +35,7 @@ from everos.core.errors import (
DocumentNotFoundError,
DuplicateDocumentError,
ExtractionEmptyError,
PathTraversalError,
TopicNotFoundError,
)
from everos.core.observability.logging import get_logger
@@ -922,19 +923,58 @@ def _apply_category_fallback(
return patched
def _safe_original_filename(source_name: str) -> str:
"""Reduce an untrusted upload filename to a single safe path component.
The multipart ``filename`` is attacker-controlled. Used verbatim as a
path segment it enables traversal (CWE-22): ``Path(base) / "/abs/path"``
discards ``base`` because the right operand is absolute, and
``base / ".."`` walks upward — so a filename of ``/home/u/.bashrc`` or
``../../x`` would let a caller write (or read back) outside the
``_original/`` directory. Reducing to the trailing component of both
POSIX and Windows-style paths strips any directory part; the residual
``.`` / ``..`` / empty cases (which a basename can still be, e.g. a
filename of ``..``) are rejected outright.
Args:
source_name: The client-supplied multipart filename.
Returns:
A single filename component safe to join under ``_original/``.
Raises:
PathTraversalError: If no safe filename can be derived.
"""
# PurePosixPath does not split on "\\"; strip backslash segments too so a
# Windows-style ``..\\..\\x`` or ``C:\\x`` filename cannot survive.
name = PurePosixPath(source_name).name.rsplit("\\", 1)[-1]
if name in {"", ".", ".."}:
raise PathTraversalError(
f"upload filename is not a valid file component: {source_name!r}"
)
return name
async def _resolve_original_file_path(
md_path: str, source_name: str | None
) -> str | None:
"""Derive the original file path from md_path and source_name.
Returns the absolute path string if the file exists on disk,
``None`` otherwise (legacy documents or missing source_name).
``None`` otherwise (legacy documents, missing source_name, or a stored
source_name that does not reduce to a safe in-``_original/`` filename —
the read side sanitises identically to the write side so a crafted
provenance label can never resolve to an out-of-directory file).
"""
if not source_name:
return None
try:
safe_name = _safe_original_filename(source_name)
except PathTraversalError:
return None
memory_root = MemoryRoot.default()
doc_dir = memory_root.root / Path(md_path).parent
candidate = doc_dir / _ORIGINAL_DIR_NAME / source_name
candidate = doc_dir / _ORIGINAL_DIR_NAME / safe_name
if await anyio.Path(candidate).is_file():
return str(candidate)
return None
@@ -943,10 +983,24 @@ async def _resolve_original_file_path(
async def _write_original_file(
doc_dir: Path, source_name: str, file_content: bytes
) -> Path:
"""Write the uploaded binary to ``_original/`` and return its path."""
"""Write the uploaded binary to ``_original/`` and return its path.
``source_name`` is the untrusted multipart filename: it is reduced to a
safe basename, and the resolved target is asserted to stay inside
``_original/`` *before* any filesystem touch, so a crafted filename can
neither escape the document directory nor create out-of-root parents.
"""
original_dir = doc_dir / _ORIGINAL_DIR_NAME
safe_name = _safe_original_filename(source_name)
target = original_dir / safe_name
# Defense-in-depth backstop mirroring the markdown writer's
# ``_ensure_within_root``: resolve() collapses ``..``/symlinks before the
# containment check, which holds even though target does not exist yet.
if not target.resolve().is_relative_to(original_dir.resolve()):
raise PathTraversalError(
f"original file target escapes {_ORIGINAL_DIR_NAME}/: {target}"
)
await anyio.Path(original_dir).mkdir(parents=True, exist_ok=True)
target = original_dir / source_name
await anyio.Path(target).write_bytes(file_content)
return target