-
Notifications
You must be signed in to change notification settings - Fork 44
feat(storage): preserve retention evidence for later evaluation #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3498d97
feat(storage): archive retained evidence before deletion
wenchanghan 6579403
fix(storage): make retention archiving atomic
wenchanghan 241129c
fix(storage): bound and harden retention archives
wenchanghan a3dbda0
fix(storage): warn without stopping retention
wenchanghan b23091d
feat(evaluation): preserve injected learning snapshots
wenchanghan f02f21d
fix(storage): bound archive retention work
wenchanghan 31499fe
fix(storage): keep retention live-first at archive ceiling
wenchanghan 9e61f5d
fix(storage): retain newest evidence within archive ceiling
wenchanghan 8d2196b
refactor(storage): make FIFO order explicit
wenchanghan d3ad038
fix(storage): bound abandoned archive writes
wenchanghan 323ddea
refactor(storage): clarify archive interfaces
wenchanghan abe14a4
fix(storage): split oversized archive batches
wenchanghan dbac0ed
chore(ci): remove feature-specific archive workflow
wenchanghan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| """Bounded FIFO JSONL archive for rows removed by row-count retention.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| import time | ||
| from collections.abc import Mapping | ||
| from pathlib import Path | ||
| from typing import Any | ||
| from uuid import uuid4 | ||
|
|
||
| DEFAULT_RETENTION_ARCHIVE_MAX_BYTES = 10 * 1024**3 | ||
| RETENTION_ARCHIVE_DELETE_BATCH = 1_000 | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def retention_archive_enabled() -> bool: | ||
| """Return whether archive-before-delete retention is enabled.""" | ||
| return os.environ.get("REFLEXIO_RETENTION_ARCHIVE", "").lower() in { | ||
| "1", | ||
| "true", | ||
| } | ||
|
|
||
|
|
||
| def resolve_archive_directory(database_path: str) -> Path: | ||
| """Return the configured archive directory or the SQLite-local default.""" | ||
| override = os.environ.get("REFLEXIO_RETENTION_ARCHIVE_DIR") | ||
| if override: | ||
| return Path(override).expanduser() | ||
| return Path(database_path).expanduser().parent / "archive" | ||
|
|
||
|
|
||
| def retention_archive_max_bytes() -> int: | ||
| """Return the positive archive ceiling, defaulting to 10 GiB.""" | ||
| raw = os.environ.get("REFLEXIO_RETENTION_ARCHIVE_MAX_BYTES") | ||
| if raw is None or not raw.strip(): | ||
| return DEFAULT_RETENTION_ARCHIVE_MAX_BYTES | ||
| try: | ||
| value = int(raw) | ||
| except ValueError: | ||
| value = 0 | ||
| if value <= 0: | ||
| logger.error( | ||
| "Invalid REFLEXIO_RETENTION_ARCHIVE_MAX_BYTES=%r; using %d", | ||
| raw, | ||
| DEFAULT_RETENTION_ARCHIVE_MAX_BYTES, | ||
| ) | ||
| return DEFAULT_RETENTION_ARCHIVE_MAX_BYTES | ||
| return value | ||
|
|
||
|
|
||
| def _encode_records(rows_by_table: Mapping[str, list[dict[str, Any]]]) -> bytes: | ||
| archived_at = int(time.time()) | ||
| lines = [] | ||
| for table_name, rows in rows_by_table.items(): | ||
| for row in rows: | ||
| record = { | ||
| "table": table_name, | ||
| "archived_at": archived_at, | ||
| "row": {key: value for key, value in row.items() if key != "embedding"}, | ||
| } | ||
| lines.append(json.dumps(record, default=repr, separators=(",", ":"))) | ||
| return ("\n".join(lines) + "\n").encode("utf-8") if lines else b"" | ||
|
|
||
|
|
||
| def append_archive_batch( | ||
| archive_dir: Path, rows_by_table: Mapping[str, list[dict[str, Any]]] | ||
| ) -> bool: | ||
| """Append one retention batch and evict oldest segments to stay bounded. | ||
|
|
||
| One segment contains the target rows and any cascade-deleted rows, so FIFO | ||
| eviction never splits a retention batch. The completed newest segment is | ||
| installed before old segments are removed; a crash can temporarily exceed | ||
| the ceiling but cannot replace newest evidence with older evidence. | ||
|
|
||
| Returns: | ||
| True when the batch was archived. False only when one batch is itself | ||
| larger than the configured ceiling. | ||
| """ | ||
| encoded = _encode_records(rows_by_table) | ||
| if not encoded: | ||
| return True | ||
| max_bytes = retention_archive_max_bytes() | ||
| row_count = sum(len(rows) for rows in rows_by_table.values()) | ||
| if len(encoded) > max_bytes: | ||
| logger.error( | ||
| "Retention archive batch exceeds the archive ceiling; skipping evidence " | ||
| "while live-row retention continues: rows_skipped=%d batch_bytes=%d " | ||
| "ceiling_bytes=%d", | ||
| row_count, | ||
| len(encoded), | ||
| max_bytes, | ||
| ) | ||
| return False | ||
|
|
||
| archive_dir.mkdir(parents=True, exist_ok=True) | ||
| segment = archive_dir / f"{time.time_ns():020d}-{uuid4().hex}.jsonl" | ||
| temporary = segment.with_suffix(".tmp") | ||
| try: | ||
| temporary.write_bytes(encoded) | ||
| temporary.replace(segment) | ||
| finally: | ||
| temporary.unlink(missing_ok=True) | ||
|
|
||
| segments = sorted( | ||
| archive_dir.glob("*.jsonl"), | ||
| key=lambda path: (path.stat().st_mtime_ns, path.name), | ||
| ) | ||
| total_bytes = sum(path.stat().st_size for path in segments) | ||
| evicted_files = 0 | ||
| evicted_bytes = 0 | ||
| for oldest in segments: | ||
| if total_bytes <= max_bytes: | ||
| break | ||
| if oldest == segment: | ||
| continue | ||
| size = oldest.stat().st_size | ||
| oldest.unlink() | ||
| total_bytes -= size | ||
| evicted_files += 1 | ||
| evicted_bytes += size | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| if evicted_files: | ||
| logger.info( | ||
| "Retention archive FIFO evicted oldest evidence: files=%d bytes=%d " | ||
| "size_bytes=%d ceiling_bytes=%d", | ||
| evicted_files, | ||
| evicted_bytes, | ||
| total_bytes, | ||
| max_bytes, | ||
| ) | ||
| return True | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.