ReMe/tests/integration/test_auto_dream.py
imrewce 354837f9af
feat(proactive): separate proactive refresh from auto dream (#488)
* refractor(proactive): upgrade proactive feature with disentangled job and steps

* refactor(proactive): apply audit fixes

- rename read-side job 'proactive' -> 'proactive_read' (less confusing vs the refresh pipeline)
- drop dedicated agent_wrapper.proactive; extraction reuses the default wrapper
- simplify schema: remove unused ProactiveExtractOutput/TopicUpdate, drop resource_paths
- extract no longer scans resource/ directly (daily notes already carry resource content)
- update tests and docs accordingly

* feat(proactive): strict extract-output gate and prompt total budget

- parse_extract_reply now requires a contract section (follow_ups/extends/updates
  as a list); non-empty replies with misspelled section names trigger the
  existing one-shot retry instead of silently checkpointing changed files
- pack_paths gains max_total_chars; extract packs newest daily material first,
  keeps the first file on overflow, and records omitted files in a trailer
  (default budget 300000 chars, configurable via max_total_chars)
- tests: schema gate unit, schema-error retry e2e, budget unit + e2e

* feat(proactive): add scenario-card plan step and generative agenda step

* feat(proactive): digest-personal profile personalization and leaner LLM contract

- extract/plan/agenda now draw a user profile block from <digest_dir>/personal/*.md
  (frontmatter description + body excerpt, per-file budget, profile.md fallback)
- all daily access honours the configured daily_dir (prompt paths parameterized,
  config-driven fallbacks) so workspaces using e.g. memory/ work unchanged
- schema trim: drop dead fields errors/material_paths, carry_forward_all -> count
- shrink LLM output contract: new topics emit title/reason/confidence/paths only;
  keywords removed end-to-end, evidence derived from paths[0] (updates keep it)

* fix(proactive): skip checkpoint when extract reply stays unusable after retry

Two consecutive unparseable replies now short-circuit the round without
checkpointing, so the same material is retried next round instead of being
silently consumed (closes the residual audit #1 gap: the structural gate
detected schema-wrong output but a double failure still checkpointed).

* fix(proactive): replace running bool with reference-counted job activity tracker for the idle gate

* refactor(proactive): remove job activity tracking and idle gate, restore job tree to upstream

* fix(proactive): address second audit round (readonly reader, mtime checkpoint, wider fallbacks, profile containment, horizon content, expiry boundary)

* refactor(dream): strip interests.yaml ownership from dream, proactive is now the sole writer

* refactor(dream): separate proactive topic generation

* ci: update renamed auto dream smoke test

* fix(proactive): complete refresh migration and docs

---------

Co-authored-by: jinli.yl <jinli.yl@alibaba-inc.com>
2026-09-07 17:23:37 +08:00

221 lines
9.4 KiB
Python

"""Integration test for the three-step auto_dream job.
Runs against a real LLM. The test seeds a dream workspace, runs ``auto_dream`` for
2026-05-28, and verifies digest/catalog effects without producing proactive output.
Agent messages and generated markdown/jsonl artifacts are copied to
``tests/integration/logs/auto_dream_latest/`` for manual inspection.
"""
import asyncio
import json
import shutil
import sys
from pathlib import Path
INTEGRATION_DIR = Path(__file__).resolve().parent
ARTIFACT_DIR = INTEGRATION_DIR / "logs" / "auto_dream_latest"
sys.path.insert(0, str(INTEGRATION_DIR))
# pylint: disable=wrong-import-position
from _workspace_fixture import DREAM_INPUT_PATH, workspace_env # noqa: E402
from reme.utils.jsonl_zst import read_jsonl_zst # noqa: E402
DREAM_DATE = "2026-05-28"
def _read_text(path: Path) -> str:
return path.read_text(encoding="utf-8")
def _print_text_file(label: str, path: Path) -> str:
text = _read_text(path)
print("\n" + "=" * 70)
print(f"[{label}] {path} ({len(text)} bytes)")
print(text)
print("=" * 70)
return text
def _reset_artifacts() -> None:
if ARTIFACT_DIR.exists():
shutil.rmtree(ARTIFACT_DIR)
ARTIFACT_DIR.mkdir(parents=True, exist_ok=True)
def _copy_artifact(src: Path, label: str) -> Path | None:
if not src.exists():
return None
rel = Path(label) / src.name if src.is_file() else Path(label)
dst = ARTIFACT_DIR / rel
dst.parent.mkdir(parents=True, exist_ok=True)
if src.is_dir():
shutil.copytree(src, dst, dirs_exist_ok=True)
else:
shutil.copy2(src, dst)
return dst
def _copy_outputs(env, message_files: list[Path]) -> list[Path]:
copied: list[Path] = []
for path in message_files:
if dst := _copy_artifact(path, "messages"):
copied.append(dst)
for root in ("daily", "digest", "metadata", "session", "agent_logs"):
if dst := _copy_artifact(env.workspace_dir / root, root):
copied.append(dst)
return copied
def _print_message_files(paths: list[Path]) -> None:
for idx, path in enumerate(paths, 1):
text = _read_text(path)
print("\n" + "=" * 70)
print(f"[messages] {idx}: {path} ({len(text)} bytes)")
print(text[:6000])
if len(text) > 6000:
print("\n[truncated]")
print("=" * 70)
def _all_digest_text(env) -> str:
return "\n\n".join(_read_text(p) for p in env.digest_files())
def _file_graph_links(env) -> dict[str, list[dict]]:
"""Map ``path -> links`` from every ``metadata/file_graph/*.jsonl.zst``."""
out: dict[str, list[dict]] = {}
graph_dir = env.workspace_dir / "metadata" / "file_graph"
if not graph_dir.is_dir():
return out
for graph_path in sorted(graph_dir.glob("*.jsonl.zst")):
for line in read_jsonl_zst(graph_path):
if not line.strip():
continue
node = json.loads(line)
out[node.get("path", "")] = node.get("links") or []
return out
def test_auto_dream():
"""Run auto_dream end to end and verify that it does not produce proactive interests."""
async def run():
_reset_artifacts()
with workspace_env() as env:
seeded = env.seed_dream_workspace()
app = await env.make_app()
message_files: list[Path] = []
try:
print("\n" + "=" * 70)
print("[setup] workspace_root =", env.workspace_dir)
print("[setup] date =", DREAM_DATE)
print("[setup] seeded =", json.dumps(seeded, ensure_ascii=False, indent=2))
print("=" * 70)
before_digest = _all_digest_text(env)
with env.record_agents(prefix="agent_dream") as recorder:
response = await app.run_job(
"auto_dream",
date=DREAM_DATE,
hint="Integration test: preserve SOC2, JWT kid, Redis current_kid, and small-PR facts.",
)
dumped = await recorder.dump()
session_jsonl = sorted((env.workspace_dir / "mem_session" / "agentscope").glob("*.jsonl"))
message_files = [*dumped, *session_jsonl]
_print_message_files(message_files)
assert response.success is True, f"auto_dream failed: {response.answer!r}\n{response.metadata!r}"
dream = (response.metadata or {}).get("dream") or {}
assert dream.get("date") == DREAM_DATE
assert dream.get("files_changed", 0) >= 1, dream
assert dream.get("units"), f"extract produced no units: {dream!r}"
assert dream.get("integrate_results"), f"integrate produced no results: {dream!r}"
assert dream.get("checkpoint_paths"), f"finish did not checkpoint changed paths: {dream!r}"
day_index = env.workspace_dir / "daily" / f"{DREAM_DATE}.md"
changed_note = env.workspace_dir / DREAM_INPUT_PATH
interests = env.workspace_dir / "daily" / DREAM_DATE / "interests.yaml"
catalog = env.workspace_dir / "metadata" / "file_catalog" / "dream.jsonl.zst"
assert changed_note.is_file(), f"changed note missing: {changed_note}"
assert not interests.exists(), f"auto_dream unexpectedly wrote interests.yaml: {interests}"
assert catalog.is_file(), f"dream catalog missing: {catalog}"
after_digest = _all_digest_text(env)
new_signal = [
needle
for needle in ("SOC2", "24", "current_kid", "Redis", "300", "next steps", "kid")
if needle in after_digest and needle not in before_digest
]
print(f"[dream] new digest signals: {new_signal}")
assert len(new_signal) >= 2, f"digest missed expected new signal\n--- digest ---\n{after_digest}"
# wikilink: the seeded daily note cites existing digest nodes, so it
# should have outbound wikilink edges in the file_graph, and the dream
# integrate agents should emit provenance and digest↔digest wikilinks
# in the markdown they just created or updated.
graph_links = _file_graph_links(env)
note_links = [
link.get("target_path") for link in graph_links.get(DREAM_INPUT_PATH, []) if isinstance(link, dict)
]
print(f"[wikilink] {DREAM_INPUT_PATH} -> {note_links}")
assert note_links, (
f"seeded daily note produced no wikilink out-edges in file_graph\n"
f"file_graph links: {graph_links}"
)
assert any(
str(target).lstrip("!").startswith("digest/") for target in note_links
), f"daily note did not link out to any digest node: {note_links}"
target_paths = [
str(result.get("target_path") or "")
for result in dream.get("integrate_results", [])
if result.get("target_path")
]
target_texts = {
rel: _read_text(env.workspace_dir / rel)
for rel in target_paths
if (env.workspace_dir / rel).is_file()
}
digest_wikilinks = [rel for rel, text in target_texts.items() if "[[digest/" in text]
source_links = [rel for rel, text in target_texts.items() if f"- [[{DREAM_INPUT_PATH}]]" in text]
print(f"[wikilink] integrated targets: {target_paths}")
print(f"[wikilink] integrated targets with [[digest/...]] links: {digest_wikilinks}")
print(f"[wikilink] integrated targets with source links: {source_links}")
assert target_texts, f"no integrated target files found: {target_paths}"
assert source_links, (
"no source wikilink back to the changed daily note in integrated targets\n"
f"targets: {target_paths}"
)
assert digest_wikilinks, (
"no digest↔digest wikilink found in integrated target markdown\n" f"targets: {target_paths}"
)
if day_index.is_file():
_print_text_file("day_index.md", day_index)
else:
print(f"[day_index] skipped: no direct daily notes, so {day_index} was not created")
_print_text_file("changed input.md", changed_note)
for path in env.digest_files():
_print_text_file(f"digest {path.relative_to(env.workspace_dir)}", path)
catalog_lines = list(read_jsonl_zst(catalog))
print("\n" + "=" * 70)
print(f"[dream catalog.jsonl.zst] {catalog} ({len(catalog_lines)} node(s))")
print("".join(catalog_lines))
print("=" * 70)
finally:
copied = _copy_outputs(env, message_files)
print("\n" + "=" * 70)
print(f"[artifacts] copied {len(copied)} item(s) to {ARTIFACT_DIR}")
for path in copied:
print(f"[artifacts] {path}")
print("=" * 70)
await env.close_all()
asyncio.run(run())
if __name__ == "__main__":
print("=== auto_dream integration test ===")
test_auto_dream()
print("\nIntegration test passed!")