review(dream): harden line-age annotation per review feedback

Follow-up to #3212, fully backward compatible:

- Extract the 14-day staleness threshold as `_STALE_THRESHOLD_DAYS` module
  constant and pass it into the Phase 1 prompt template as
  `{{ stale_threshold_days }}`. The number lived in three places before
  (code threshold, prompt instruction, docstring); now there is one.
- Add `DreamConfig.annotate_line_ages` (default True = current behavior)
  and propagate it through `Dream.__init__` and the gateway wiring in
  cli/commands.py. Gives users a knob to disable the feature without a
  code patch if an LLM reacts poorly to the `← Nd` suffix.
- Harden `_annotate_with_ages` against dirty working trees: when HEAD
  blob line count disagrees with the working-tree content length, skip
  annotation entirely instead of assigning ages to the wrong lines. The
  previous `i >= len(ages)` guard only handled one direction of the
  mismatch.
- Inline-comment the `max_iterations` 10→15 bump with a pointer to
  exp002 so future blame has context.
- Add 4 regression tests: end-to-end `← 30d` reaches prompt, 14/15
  threshold boundary, `annotate_line_ages=False` bypasses git entirely
  (verified via `assert_not_called`), length-mismatch defense, and
  template-var rendering.

Made-with: Cursor
This commit is contained in:
Xubin Ren
2026-04-17 13:45:38 +08:00
committed by Xubin Ren
parent 35f3084c03
commit cc5a666d5d
5 changed files with 132 additions and 13 deletions
+43 -11
View File
@@ -552,6 +552,13 @@ class Consolidator:
# ---------------------------------------------------------------------------
# Single source of truth for the staleness threshold used in _annotate_with_ages
# *and* in the Phase 1 prompt template (passed as `stale_threshold_days`).
# Keep code and prompt aligned — if you bump this, the LLM's instruction string
# updates automatically.
_STALE_THRESHOLD_DAYS = 14
class Dream:
"""Two-phase memory processor: analyze history.jsonl, then edit files via AgentRunner.
@@ -568,6 +575,7 @@ class Dream:
max_batch_size: int = 20,
max_iterations: int = 10,
max_tool_result_chars: int = 16_000,
annotate_line_ages: bool = True,
):
self.store = store
self.provider = provider
@@ -575,6 +583,10 @@ class Dream:
self.max_batch_size = max_batch_size
self.max_iterations = max_iterations
self.max_tool_result_chars = max_tool_result_chars
# Kill switch for the git-blame-based per-line age annotation in Phase 1.
# Default True keeps the #3212 behavior; set False to feed MEMORY.md raw
# (e.g. if a specific LLM reacts poorly to the `← Nd` suffix).
self.annotate_line_ages = annotate_line_ages
self._runner = AgentRunner(provider)
self._tools = self._build_tools()
@@ -635,9 +647,12 @@ class Dream:
def _annotate_with_ages(self, content: str) -> str:
"""Append per-line age suffixes to MEMORY.md content.
Each non-blank line gets a suffix like ``← 30d`` indicating how
many days since it was last modified. Lines ≤14 days old get no
suffix. Returns the original content unchanged if git is unavailable.
Each non-blank line whose age exceeds ``_STALE_THRESHOLD_DAYS`` gets a
suffix like ``← 30d`` indicating days since last modification.
Returns the original content unchanged if git is unavailable,
annotate fails, or the line count doesn't match the age count
(which can happen with an uncommitted working-tree edit — better to
skip annotation than to tag the wrong line).
SOUL.md and USER.md are never annotated.
"""
file_path = "memory/MEMORY.md"
@@ -651,14 +666,23 @@ class Dream:
had_trailing = content.endswith("\n")
lines = content.splitlines()
# If HEAD-blob line count disagrees with the working-tree content we
# received, ages would be assigned to the wrong lines — skip entirely
# and feed the LLM un-annotated content rather than misleading data.
if len(lines) != len(ages):
logger.debug(
"line_ages length mismatch for {} (lines={}, ages={}); skipping annotation",
file_path, len(lines), len(ages),
)
return content
annotated: list[str] = []
for i, line in enumerate(lines):
if not line.strip() or i >= len(ages):
for line, age in zip(lines, ages):
if not line.strip():
annotated.append(line)
continue
d = ages[i].age_days
if d > 14:
annotated.append(f"{line} \u2190 {d}d")
if age.age_days > _STALE_THRESHOLD_DAYS:
annotated.append(f"{line} \u2190 {age.age_days}d")
else:
annotated.append(line)
result = "\n".join(annotated)
@@ -686,10 +710,14 @@ class Dream:
f"[{e['timestamp']}] {e['content']}" for e in batch
)
# Current file contents + per-line age annotations
# Current file contents + per-line age annotations (MEMORY.md only)
current_date = datetime.now().strftime("%Y-%m-%d")
raw_memory = self.store.read_memory() or "(empty)"
current_memory = self._annotate_with_ages(raw_memory)
current_memory = (
self._annotate_with_ages(raw_memory)
if self.annotate_line_ages
else raw_memory
)
current_soul = self.store.read_soul() or "(empty)"
current_user = self.store.read_user() or "(empty)"
@@ -711,7 +739,11 @@ class Dream:
messages=[
{
"role": "system",
"content": render_template("agent/dream_phase1.md", strip=True),
"content": render_template(
"agent/dream_phase1.md",
strip=True,
stale_threshold_days=_STALE_THRESHOLD_DAYS,
),
},
{"role": "user", "content": phase1_prompt},
],
+1
View File
@@ -871,6 +871,7 @@ def gateway(
agent.dream.model = dream_cfg.model_override
agent.dream.max_batch_size = dream_cfg.max_batch_size
agent.dream.max_iterations = dream_cfg.max_iterations
agent.dream.annotate_line_ages = dream_cfg.annotate_line_ages
from nanobot.cron.types import CronJob, CronPayload
cron.register_system_job(CronJob(
id="dream",
+5
View File
@@ -43,7 +43,12 @@ class DreamConfig(Base):
validation_alias=AliasChoices("modelOverride", "model", "model_override"),
) # Optional Dream-specific model override
max_batch_size: int = Field(default=20, ge=1) # Max history entries per run
# Bumped from 10 to 15 in #3212 (exp002: +30% dedup, no accuracy loss; >15 plateaus).
max_iterations: int = Field(default=15, ge=1) # Max tool calls per Phase 2
# Per-line git-blame age annotation in Phase 1 prompt (see #3212). Default
# on — set to False to feed MEMORY.md raw if a specific LLM reacts poorly
# to the `← Nd` suffix or you want deterministic, git-independent prompts.
annotate_line_ages: bool = True
def build_schedule(self, timezone: str) -> CronSchedule:
"""Build the runtime schedule, preferring the legacy cron override if present."""
+1 -1
View File
@@ -26,7 +26,7 @@ Staleness — MEMORY.md lines may have a ``← Nd`` suffix showing days since la
- Age only indicates when content was last touched, not whether it should be removed
- Use content judgment: user habits/preferences/personality traits are permanent regardless of age
- Only prune content that is objectively outdated: passed events, resolved tracking, superseded approaches
- Lines with ``← Nd`` (N>14) deserve closer review but are NOT automatically removable
- Lines with ``← Nd`` (N>{{ stale_threshold_days }}) deserve closer review but are NOT automatically removable
- When removing: prefer deleting individual items over entire sections
Skill discovery — flag [SKILL] when ALL of these are true: