Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(memory): add opt-in scheduled memory consolidation job

shadowinlife34853aea2e4a
diff --git a/agent/.env.example b/agent/.env.example
index 64b927d0b..a55e7b350 100644
--- a/agent/.env.example
+++ b/agent/.env.example
@@ -380,6 +380,11 @@ TUSHARE_TOKEN=your-tushare-token
# VT_MEMORY_COMPRESSION=true
# VT_MEMORY_FTS_INDEX=true
+# Scheduled memory-consolidation system job (daily 04:00 UTC, archive-only GC).
+# Opt-in on its own flag: VT_MEMORY=on/full deliberately does NOT enable it.
+# The job is an honest no-op unless VT_MEMORY_GC is enabled too.
+# VT_MEMORY_CONSOLIDATION=false
+
# ============================================================================
# TAP Mode — credential isolation for broker orders (optional, OFF by default)
# ============================================================================
diff --git a/agent/src/config/env_schema.py b/agent/src/config/env_schema.py
index 27a70dba8..571a138d5 100644
--- a/agent/src/config/env_schema.py
+++ b/agent/src/config/env_schema.py
@@ -665,6 +665,17 @@ class MemoryConfig(_EnvBase):
description="Enable SQLite FTS5 search index",
)
+ # Scheduled consolidation system job (scheduler-unification T8).
+ # Deliberately NOT listed in the preset flag map or the memory flag alias
+ # map above: the preset machinery only touches fields in the alias map, so
+ # VT_MEMORY=on/full can never silently enable a background timer. This
+ # flag is opt-in on its own; the job it gates also no-ops honestly unless
+ # VT_MEMORY_GC is enabled (see src/memory/consolidation.py).
+ consolidation_enabled: EnvBool = Field(
+ default=False, alias="VT_MEMORY_CONSOLIDATION",
+ description="Enable the daily memory-consolidation system job",
+ )
+
@model_validator(mode="before")
@classmethod
def _apply_preset(cls, data: Any) -> Any:
diff --git a/agent/src/memory/consolidation.py b/agent/src/memory/consolidation.py
new file mode 100644
index 000000000..369b94268
--- /dev/null
+++ b/agent/src/memory/consolidation.py
@@ -0,0 +1,116 @@
+"""Scheduled memory consolidation (scheduler-unification T8).
+
+Thin orchestration surface consumed by the scheduler; it adds NO semantics of
+its own — every behavior lives in the existing memory modules and this module
+only decides *whether* and *how* to call them:
+
+* :func:`consolidation_enabled` is the ``enabled_predicate`` of the
+ ``system:memory-consolidation`` spec in :mod:`src.scheduling.system_jobs`.
+ It reads ``VT_MEMORY_CONSOLIDATION`` (``MemoryConfig.consolidation_enabled``)
+ through :func:`src.config.accessor.get_env_config` — the sanctioned config
+ path; the flag is deliberately NOT part of the ``VT_MEMORY`` presets, so
+ ``VT_MEMORY=full`` never silently starts a background timer.
+* :func:`consolidate` is the callable registered under the literal key
+ ``"memory.consolidate"`` in :mod:`src.scheduling.registry`. It is SYNC by
+ design: the executor's internal-run supervisor offloads sync callables via
+ ``asyncio.to_thread``, so the blocking GC/compression pass never runs on the
+ event loop.
+
+Honest no-op contract (plan T8 ②b(ii)): archive GC is gated by
+``VT_MEMORY_GC`` and compression is only ever invoked through GC's
+archive→compress handoff (``lifecycle.run_gc`` is the sole production caller
+of ``CompressionPipeline``), so with GC off there is nothing this job can
+truthfully do — it logs the reason and returns WITHOUT touching any file,
+rather than faking work. ``VT_MEMORY_DECAY`` off degrades importance to raw
+quality scores (existing ``compute_importance`` behavior); that is noted in
+the log but still a real consolidation pass.
+
+Concurrency contract (plan T8 ②b(iii)): writes are serialized by the memory
+layer's own ``memory_lock`` (``fcntl.flock`` on ``<memory_dir>/.lock``) taken
+PER ACTION inside ``lifecycle`` — this module must NOT hold that lock around
+``run_gc`` because the inner per-action acquisitions would then conflict with
+the outer one and every action would time out. A lock timeout yields
+``False`` and the action is skipped best-effort (the daily cadence re-fires
+the next day). Known pre-existing limitation, unchanged by the scheduler:
+on win32 ``memory_lock`` yields ``True`` WITHOUT any lock, so scheduled
+consolidation and session writes have NO mutual exclusion on Windows — the
+timer raises the race frequency but not the semantic. No failure path here
+leaves half-written state: the underlying writers are atomic (tmp +
+``os.replace``) and archive-only (``ENABLE_DELETE = False`` — low-importance
+memories are moved to ``<memory_dir>/archive/``, never deleted).
+"""
+
+from __future__ import annotations
+
+import logging
+from pathlib import Path
+from typing import Optional
+
+logger = logging.getLogger(__name__)
+
+
+def consolidation_enabled() -> bool:
+ """Whether the scheduled consolidation system job is enabled.
+
+ Reads ``VT_MEMORY_CONSOLIDATION`` via the cached ``EnvConfig`` singleton
+ (default ``False``; presets never raise it). Bound as the
+ ``enabled_predicate`` of the ``system:memory-consolidation`` spec.
+ """
+ from src.config.accessor import get_env_config
+
+ return get_env_config().memory.consolidation_enabled
+
+
+def consolidate(memory_dir: Optional[Path] = None) -> None:
+ """Run one consolidation pass: archive-only GC (+ its compression handoff).
+
+ Invoked with no arguments by the internal-run supervisor (``memory_dir``
+ then defaults to ``MEMORY_BASE``); the optional parameter is a test seam.
+ Safe to re-run (supervisor shutdown semantics discard in-flight results
+ and leave the job for stale-recovery).
+
+ Args:
+ memory_dir: Memory directory to consolidate; ``None`` uses the
+ process-wide ``MEMORY_BASE`` resolved at call time.
+ """
+ from src.config.accessor import get_env_config
+
+ cfg = get_env_config().memory
+ if not cfg.consolidation_enabled:
+ # Defense in depth: normally the row is not seeded (or is halted) when
+ # the flag is off; this covers the flag flipping off between startup
+ # and firing. Honest no-op — nothing is read or written.
+ logger.info("memory consolidation skipped: VT_MEMORY_CONSOLIDATION is off")
+ return
+ if not cfg.gc_enabled:
+ # Plan ②b(ii): GC gates BOTH the archive pass and the archive→compress
+ # handoff, so without it there is nothing to truthfully consolidate.
+ logger.info(
+ "memory consolidation no-op: VT_MEMORY_GC is off — archive GC and the "
+ "archive→compress handoff are both gated by it; no files were touched"
+ )
+ return
+ if not cfg.decay_enabled:
+ logger.info(
+ "memory consolidation: VT_MEMORY_DECAY is off — importance falls back "
+ "to raw quality scores for this pass"
+ )
+
+ # Imported lazily: keeps this module's import footprint stdlib-only so the
+ # scheduling package can import the predicate without pulling in the memory
+ # stack, and mirrors the lazy-import style of the memory modules themselves.
+ from src.memory.lifecycle import MemoryLifecycle
+ from src.memory.persistent import PersistentMemory
+
+ memory = PersistentMemory(memory_dir=memory_dir)
+ lifecycle = MemoryLifecycle(memory)
+ # Plan ②b(i): run_gc's default is dry_run=True (audit-only). The scheduled
+ # pass MUST archive for real, so dry_run=False is passed explicitly.
+ actions = lifecycle.run_gc(dry_run=False)
+ archived = sum(1 for a in actions if a.get("action") == "archive")
+ logger.info(
+ "memory consolidation finished: %d action(s), %d archived (archive-only, "
+ "no deletions); lock-timeout skips, if any, are retried by the next daily run",
+ len(actions),
+ archived,
+ )
diff --git a/agent/src/scheduling/registry.py b/agent/src/scheduling/registry.py
index a9da579cf..5d46dd54a 100644
--- a/agent/src/scheduling/registry.py
+++ b/agent/src/scheduling/registry.py
@@ -30,6 +30,8 @@ from __future__ import annotations
import re
from typing import Awaitable, Callable
+from src.memory.consolidation import consolidate as _memory_consolidate
+
#: A registered internal callable: invoked with no arguments, either sync
#: (return value discarded) or async. ``...`` tolerates defaulted/optional
#: parameters; the run contract above is what the executor guarantees.
@@ -137,8 +139,11 @@ def is_registered(name: object) -> bool:
#
# Rules: literal keys only; values are imported callable objects, never
# import-path strings; nothing populates this dict from runtime input.
-# Seeded EMPTY by T5 — until T6+ registers system jobs, every internal
-# firing fail-closes into the executor's dispatch-failure path
-# (``failure_kind="dispatch"``), which is the intended behavior.
+# An internal firing whose key is missing fail-closes into the executor's
+# dispatch-failure path (``failure_kind="dispatch"``).
# ---------------------------------------------------------------------------
-_INTERNAL_CALLABLES: dict[str, InternalRunnable] = {}
+_INTERNAL_CALLABLES: dict[str, InternalRunnable] = {
+ # T8: daily archive-only memory consolidation (opt-in via
+ # VT_MEMORY_CONSOLIDATION; honest no-op unless VT_MEMORY_GC is on too).
+ "memory.consolidate": _memory_consolidate,
+}
diff --git a/agent/src/scheduling/system_jobs.py b/agent/src/scheduling/system_jobs.py
index ae0e6104c..81e4dc0e4 100644
--- a/agent/src/scheduling/system_jobs.py
+++ b/agent/src/scheduling/system_jobs.py
@@ -81,6 +81,7 @@ import time
from dataclasses import dataclass, field
from typing import Callable, Optional, Sequence
+from src.memory.consolidation import consolidation_enabled as _memory_consolidation_enabled
from src.scheduled_research.models import (
JobStatus,
ScheduledResearchJob,
@@ -182,23 +183,24 @@ class SystemJobSpec:
# Production spec surface
# ---------------------------------------------------------------------------
-#: The production specs. EMPTY by design in T6 — the framework ships first;
-#: T8 (memory consolidation) and T9 (br_data sync) each append ONE literal
-#: here plus ONE registry entry in ``registry.py``'s ``_INTERNAL_CALLABLES``
-#: block, e.g.:
-#:
-#: SystemJobSpec(
-#: id="system:memory-consolidation",
-#: schedule="0 3 * * *",
-#: timezone="UTC",
-#: internal_callable="memory.consolidate",
-#: title="Memory consolidation",
-#: enabled_predicate=_memory_consolidation_enabled, # reads T8's flag
-#: ),
-#:
-#: Nothing else changes: ensure_system_jobs, the guards (T7) and the lifespan
-#: wiring (T12) all consume this tuple generically.
-_SYSTEM_JOB_SPECS: tuple[SystemJobSpec, ...] = ()
+#: The production specs. Each job is ONE literal here plus ONE registry entry
+#: in ``registry.py``'s ``_INTERNAL_CALLABLES`` block (T8 landed memory
+#: consolidation; T9 appends br_data sync the same way). Nothing else changes:
+#: ensure_system_jobs, the guards (T7) and the lifespan wiring (T12) all
+#: consume this tuple generically. The predicate lives in the memory domain
+#: (``src.memory.consolidation``) so THIS module keeps reading zero env.
+#: Cadence: daily at 04:00 UTC (plan T8 ①; a valid edited schedule/timezone
+#: on the row is trusted per (β) — e.g. an IANA key for local-wall-time runs).
+_SYSTEM_JOB_SPECS: tuple[SystemJobSpec, ...] = (
+ SystemJobSpec(
+ id="system:memory-consolidation",
+ schedule="0 4 * * *",
+ timezone="UTC",
+ internal_callable="memory.consolidate",
+ title="Memory consolidation",
+ enabled_predicate=_memory_consolidation_enabled,
+ ),
+)
def system_job_specs() -> tuple[SystemJobSpec, ...]:
diff --git a/agent/tests/scheduling/test_memory_consolidation_job.py b/agent/tests/scheduling/test_memory_consolidation_job.py
new file mode 100644
index 000000000..68c632d97
--- /dev/null
+++ b/agent/tests/scheduling/test_memory_consolidation_job.py
@@ -0,0 +1,356 @@
+"""Scheduled memory-consolidation system job (scheduler-unification T8).
+
+Covers the plan's acceptance criteria and the three Metis traps mechanically:
+
+* (i) ``run_gc`` defaults to ``dry_run=True`` — the due-tick test asserts a
+ REAL archive artifact on disk (file moved into ``archive/``), never a
+ "function was called" mock; the break-probe for this is flipping the
+ explicit ``dry_run=False`` in ``consolidation.py``;
+* (ii) honest no-op — with ``VT_MEMORY_GC`` off the callable logs the reason
+ and touches NOTHING (no archive dir, no gc.log, entry file untouched);
+* (iii) lock semantics — an ``.lock`` held by another descriptor makes the
+ per-action ``memory_lock`` time out: no crash, no ``.tmp`` residue, no
+ orphan ``.relations.json`` sidecar, entry left in place for the next run;
+* default-off + preset isolation — ``VT_MEMORY_CONSOLIDATION`` defaults
+ False, is absent from ``_PRESET_FLAGS`` / ``_MEMORY_FLAG_ALIASES``, and
+ ``VT_MEMORY=full`` leaves it False with no job seeded;
+* end-to-end through the T5 supervisor with the PRODUCTION registry entry
+ (``memory.consolidate``) and the production spec tuple.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import logging
+import sys
+from pathlib import Path
+
+import pytest
+from src.config.accessor import get_env_config, reset_env_config
+from src.config.env_schema import _MEMORY_FLAG_ALIASES, _PRESET_FLAGS, MemoryConfig
+from src.memory import persistent as persistent_mod
+from src.memory.consolidation import consolidate, consolidation_enabled
+from src.scheduled_research.executor import ScheduledResearchExecutor
+from src.scheduled_research.models import JobStatus, ScheduledResearchJob
+from src.scheduled_research.store import ScheduledResearchJobStore
+from src.scheduling.registry import is_registered, resolve_internal
+from src.scheduling.system_jobs import SystemJobSpec, ensure_system_jobs, system_job_specs
+from src.scheduling.triggers import next_due
+
+JOB_ID = "system:memory-consolidation"
+
+
[email protected](autouse=True)
+def _reset_config_cache():
+ """Same convention as test_memory_gc.py: setenv must reach the singleton."""
+ reset_env_config()
+ yield
+ reset_env_config()
+
+
+def _set_env(monkeypatch: pytest.MonkeyPatch, **vars: str) -> None:
+ for key, value in vars.items():
+ monkeypatch.setenv(key, value)
+ reset_env_config()
+
+
+def _store(tmp_path: Path) -> ScheduledResearchJobStore:
+ return ScheduledResearchJobStore(path=tmp_path / "jobs.json")
+
+
+def _create_memory_file(
+ tmp_path: Path,
+ name: str,
+ *,
+ quality_score: float = 0.01,
+ created_at: str = "2024-01-01T00:00:00",
+) -> Path:
+ """Old, low-quality entry — qualified for archival (age > 7d, imp < 0.15).
+
+ Frontmatter shape mirrors test_memory_gc.py's helper.
+ """
+ slug = name.lower().replace(" ", "_")
+ path = tmp_path / f"project_{slug}.md"
+ path.write_text(
+ f"---\n"
+ f"name: {name}\n"
+ f"description: {name}\n"
+ f"type: project\n"
+ f"id: ab12cd\n"
+ f"created_at: {created_at}\n"
+ f"updated_at: {created_at}\n"
+ f"keywords: []\n"
+ f"quality_score: {quality_score}\n"
+ f"access_count: 0\n"
+ f"last_accessed: {created_at}\n"
+ f"importance: {quality_score}\n"
+ f"related_memories: []\n"
+ f"---\n\n"
+ f"stale body for {name}\n",
+ encoding="utf-8",
+ )
+ return path
+
+
+def _resolver(job: ScheduledResearchJob):
+ return resolve_internal(job.internal_callable)
+
+
+async def _drain(executor: ScheduledResearchExecutor) -> None:
+ tasks = list(executor._internal._tasks)
+ if tasks:
+ await asyncio.wait_for(asyncio.gather(*tasks, return_exceptions=True), timeout=30)
+
+
+def _production_spec() -> SystemJobSpec:
+ matches = [s for s in system_job_specs() if s.id == JOB_ID]
+ assert len(matches) == 1, f"expected exactly one {JOB_ID} spec, got {matches}"
+ return matches[0]
+
+
+# ---------------------------------------------------------------------------
+# Env flag: default off, preset isolation, explicit env wins
+# ---------------------------------------------------------------------------
+
+
+def test_consolidation_flag_defaults_false(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.delenv("VT_MEMORY_CONSOLIDATION", raising=False)
+ assert get_env_config().memory.consolidation_enabled is False
+ assert consolidation_enabled() is False
+
+
+def test_consolidation_flag_reads_env(monkeypatch: pytest.MonkeyPatch) -> None:
+ _set_env(monkeypatch, VT_MEMORY_CONSOLIDATION="1")
+ assert get_env_config().memory.consolidation_enabled is True
+ assert consolidation_enabled() is True
+
+
+def test_flag_absent_from_preset_maps() -> None:
+ """Mechanical proof the preset machinery can never touch this field."""
+ assert "consolidation_enabled" not in _MEMORY_FLAG_ALIASES
+ for preset_name, flags in _PRESET_FLAGS.items():
+ assert "consolidation_enabled" not in flags, preset_name
+
+
+def test_vt_memory_full_leaves_consolidation_false(monkeypatch: pytest.MonkeyPatch) -> None:
+ _set_env(monkeypatch, VT_MEMORY="full")
+ cfg = get_env_config().memory
+ assert cfg.gc_enabled is True # preset itself works
+ assert cfg.consolidation_enabled is False # ...but never raises this flag
+ assert consolidation_enabled() is False
+ assert MemoryConfig().consolidation_enabled is False
+
+
+def test_explicit_env_wins_over_preset(monkeypatch: pytest.MonkeyPatch) -> None:
+ _set_env(monkeypatch, VT_MEMORY="full", VT_MEMORY_CONSOLIDATION="1")
+ assert get_env_config().memory.consolidation_enabled is True
+
+
+# ---------------------------------------------------------------------------
+# Production spec + registry surfaces
+# ---------------------------------------------------------------------------
+
+
+def test_production_spec_shape() -> None:
+ spec = _production_spec()
+ assert spec.schedule == "0 4 * * *"
+ assert spec.timezone == "UTC"
+ assert spec.internal_callable == "memory.consolidate"
+ assert spec.source_type == "system"
+ assert spec.executor_kind == "internal"
+ assert spec.prompt == ""
+ assert spec.enabled_predicate is consolidation_enabled
+
+
+def test_registry_production_entry_resolves_to_consolidate() -> None:
+ assert is_registered("memory.consolidate") is True
+ assert resolve_internal("memory.consolidate") is consolidate
+
+
+# ---------------------------------------------------------------------------
+# ensure_system_jobs: default off / flag on / VT_MEMORY=full
+# ---------------------------------------------------------------------------
+
+
+def test_default_off_seeds_nothing(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ """QA (a): default flag off → no row, store file never created."""
+ monkeypatch.delenv("VT_MEMORY_CONSOLIDATION", raising=False)
+ store = _store(tmp_path)
+ report = ensure_system_jobs(store, now_fn=lambda: 1000)
+ assert report.skipped_disabled == (JOB_ID,)
+ assert report.seeded == ()
+ assert store.get(JOB_ID) is None
+ assert not store.path.exists()
+
+
+def test_flag_on_seeds_row_with_spec_fields(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ _set_env(monkeypatch, VT_MEMORY_CONSOLIDATION="1")
+ store = _store(tmp_path)
+ report = ensure_system_jobs(store, now_fn=lambda: 1000)
+ assert report.seeded == (JOB_ID,)
+ job = store.get(JOB_ID)
+ assert job is not None
+ spec = _production_spec()
+ assert job.source_type == "system"
+ assert job.executor_kind == "internal"
+ assert job.internal_callable == "memory.consolidate"
+ assert job.schedule == spec.schedule
+ assert job.timezone == "UTC"
+ assert job.status == JobStatus.PENDING
+ # cron + timezone seeds the NEXT 04:00 UTC slot, not "now"
+ assert job.next_run_at == next_due(spec.schedule, 1000, "UTC")
+ assert job.next_run_at > 1000
+
+
+def test_vt_memory_full_seeds_no_job(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ _set_env(monkeypatch, VT_MEMORY="full")
+ store = _store(tmp_path)
+ report = ensure_system_jobs(store, now_fn=lambda: 1000)
+ assert report.skipped_disabled == (JOB_ID,)
+ assert store.get(JOB_ID) is None
+
+
+# ---------------------------------------------------------------------------
+# consolidate(): honest no-op (trap ii)
+# ---------------------------------------------------------------------------
+
+
+def test_consolidate_noop_when_gc_off(tmp_path: Path, monkeypatch, caplog) -> None:
+ """VT_MEMORY_GC off → log the reason, touch NOTHING (no fake work)."""
+ _set_env(monkeypatch, VT_MEMORY_CONSOLIDATION="1")
+ monkeypatch.delenv("VT_MEMORY_GC", raising=False)
+ entry = _create_memory_file(tmp_path, "stale-note")
+
+ with caplog.at_level(logging.INFO, logger="src.memory.consolidation"):
+ consolidate(memory_dir=tmp_path)
+
+ assert entry.exists() # untouched, not archived, not deleted
+ assert not (tmp_path / "archive").exists()
+ assert not (tmp_path / "gc.log").exists()
+ assert "VT_MEMORY_GC is off" in caplog.text
+ assert "no files were touched" in caplog.text
+
+
+def test_consolidate_noop_when_own_flag_off(tmp_path: Path, monkeypatch, caplog) -> None:
+ """Defense in depth: flag flipped off between startup and firing."""
+ monkeypatch.delenv("VT_MEMORY_CONSOLIDATION", raising=False)
+ _set_env(monkeypatch, VT_MEMORY_GC="1")
+ entry = _create_memory_file(tmp_path, "stale-note")
+
+ with caplog.at_level(logging.INFO, logger="src.memory.consolidation"):
+ consolidate(memory_dir=tmp_path)
+
+ assert entry.exists()
+ assert not (tmp_path / "archive").exists()
+ assert "VT_MEMORY_CONSOLIDATION is off" in caplog.text
+
+
+# ---------------------------------------------------------------------------
+# consolidate(): real archival (trap i) + archive-only (QA d)
+# ---------------------------------------------------------------------------
+
+
+def test_consolidate_really_archives_not_dry_run(tmp_path: Path, monkeypatch) -> None:
+ """dry_run=False is explicit: a qualified entry MOVES into archive/.
+
+ Archive-only semantics (ENABLE_DELETE=False): the file must exist under
+ archive/ — never vanish. Break-probe target: passing dry_run=True in
+ consolidation.py reddens this test.
+ """
+ _set_env(monkeypatch, VT_MEMORY_CONSOLIDATION="1", VT_MEMORY_GC="1", VT_MEMORY_DECAY="1")
+ entry = _create_memory_file(tmp_path, "garbage")
+
+ consolidate(memory_dir=tmp_path)
+
+ archived = tmp_path / "archive" / entry.name
+ assert archived.is_file(), "real archive artifact must exist (not a dry run)"
+ assert not entry.exists(), "entry must have MOVED into archive/"
+ assert "stale body" in archived.read_text(encoding="utf-8")
+ gc_log = (tmp_path / "gc.log").read_text(encoding="utf-8")
+ assert "mode=execute" in gc_log # not mode=dry_run
+
+
+# ---------------------------------------------------------------------------
+# consolidate(): lock conflict (trap iii)
+# ---------------------------------------------------------------------------
+
+
[email protected](sys.platform == "win32", reason="fcntl is unix-only; win32 memory_lock is a documented no-op")
+def test_consolidate_survives_held_memory_lock(tmp_path: Path, monkeypatch) -> None:
+ """Another holder owns <dir>/.lock: best-effort skip, no crash, no residue.
+
+ The per-action memory_lock times out and yields False; the entry stays in
+ place for the next daily run. No .tmp files, no orphan .relations.json.
+ """
+ import fcntl
+
+ _set_env(monkeypatch, VT_MEMORY_CONSOLIDATION="1", VT_MEMORY_GC="1", VT_MEMORY_DECAY="1")
+ monkeypatch.setattr(persistent_mod, "_LOCK_TIMEOUT_S", 0.3)
+ entry = _create_memory_file(tmp_path, "contended")
+ lock_path = tmp_path / ".lock"
+ lock_path.touch(exist_ok=True)
+
+ holder = open(lock_path, "w") # noqa: SIM115 — second open file description
+ try:
+ fcntl.flock(holder, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ consolidate(memory_dir=tmp_path) # must not raise
+ finally:
+ fcntl.flock(holder, fcntl.LOCK_UN)
+ holder.close()
+
+ assert entry.exists(), "lock-timeout skip leaves the entry for the next run"
+ assert not (tmp_path / "archive" / entry.name).exists()
+ residue = [p.name for p in tmp_path.rglob("*") if p.name.endswith(".tmp")]
+ assert residue == []
+ sidecars = [p.name for p in tmp_path.rglob("*.relations.json")]
+ assert sidecars == []
+
+
+# ---------------------------------------------------------------------------
+# End-to-end: flag on → seeded → due tick → supervisor → real consolidation
+# ---------------------------------------------------------------------------
+
+
+def test_due_tick_runs_consolidation_once_and_archives(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, clean_registry
+) -> None:
+ """QA happy, end-to-end through the PRODUCTION spec + registry entry and
+ the T5 supervisor: the due internal job archives a qualified memory file
+ exactly once; prompt dispatch is never called for it."""
+ _set_env(monkeypatch, VT_MEMORY_CONSOLIDATION="1", VT_MEMORY_GC="1", VT_MEMORY_DECAY="1")
+ memory_dir = tmp_path / "memory"
+ memory_dir.mkdir()
+ monkeypatch.setattr(persistent_mod, "MEMORY_BASE", memory_dir)
+ entry = _create_memory_file(memory_dir, "e2e-garbage")
+
+ store = _store(tmp_path)
+ report = ensure_system_jobs(store, now_fn=lambda: 1000)
+ assert report.seeded == (JOB_ID,)
+ jobs = store.load()
+ jobs[JOB_ID].next_run_at = 1200 # make it due for the tick below
+ store.save(jobs)
+
+ prompt_calls: list[str] = []
+
+ async def dispatch(job: ScheduledResearchJob) -> None:
+ prompt_calls.append(job.id)
+
+ async def scenario() -> None:
+ executor = ScheduledResearchExecutor(
+ store, dispatch, internal_dispatcher=_resolver, now_fn=lambda: 1500
+ )
+ await executor.tick(1500)
+ await _drain(executor)
+
+ asyncio.run(scenario())
+
+ assert (memory_dir / "archive" / entry.name).is_file() # REAL artifact
+ assert not entry.exists()
+ assert prompt_calls == []
+ saved = store.get(JOB_ID)
+ assert saved is not None
+ assert saved.status == JobStatus.COMPLETED
+ assert saved.failure_kind is None
+ assert saved.last_run_at == 1500
+ # daily cadence: next fire is the following 04:00 UTC slot after completion
+ assert saved.next_run_at == next_due("0 4 * * *", 1500, "UTC")