Skip to content

The agent that grows with you

fix(cron): inject the previous run's answer, not its prompt

liuhao1024387e8e1e1660
diff --git a/cron/scheduler_prompt.py b/cron/scheduler_prompt.py
index bdde36e6fbe..9db47a261da 100644
--- a/cron/scheduler_prompt.py
+++ b/cron/scheduler_prompt.py
@@ -63,6 +63,31 @@ _UPSTREAM_CONTEXT_INTRO = (
)
+def _archive_answer(archive: str) -> str | None:
+ """The reusable answer of a stored run: the text after ``## Response``.
+
+ Archives without the heading (script-mode runs) stay whole-document.
+ ``None`` marks "no usable answer" — a ``[SILENT]`` or blank response — so
+ the caller falls through to an older archive instead of injecting prompt
+ noise the job already has.
+ """
+ if "## Response" not in archive:
+ return archive
+ answer = archive.partition("## Response")[2].strip()
+ if answer in ("", "[SILENT]"):
+ return None
+ return answer
+
+
+def _clip_to_context_budget(text: str) -> str:
+ """Clip oversized context head+tail; conclusions and summaries sit at the end."""
+ if len(text) <= _MAX_CONTEXT_CHARS:
+ return text
+ keep = _MAX_CONTEXT_CHARS // 2
+ omitted = len(text) - 2 * keep
+ return f"{text[:keep]}\n\n[... {omitted} chars omitted ...]\n\n{text[-keep:]}"
+
+
def _inject_context_from(job: dict, prompt: str) -> tuple[str, bool]:
"""Prepend the latest output of each ``context_from`` job; returns ``(prompt, injected)``."""
context_from = job.get("context_from")
@@ -102,14 +127,16 @@ def _inject_context_from(job: dict, prompt: str) -> tuple[str, bool]:
"Script gate returned `wakeAgent=false`"))
for line in header.splitlines()
)
- if candidate and not silent_audit:
- latest_output = candidate
- break
- if len(latest_output) > _MAX_CONTEXT_CHARS:
- latest_output = (
- latest_output[:_MAX_CONTEXT_CHARS] + "\n\n[... output truncated ...]")
+ if not candidate or silent_audit:
+ continue
+ answer = _archive_answer(candidate)
+ if answer is None:
+ continue # [SILENT]/blank response — try an older archive
+ latest_output = answer
+ break
if not latest_output:
- continue # silent skip — empty output
+ continue # silent skip — no archive with a usable answer
+ latest_output = _clip_to_context_budget(latest_output)
if is_self:
prompt = _prepend_context_block(
prompt, "Your previous run's output", _SELF_CONTEXT_INTRO, latest_output)
diff --git a/tests/cron/test_context_from_response_extraction.py b/tests/cron/test_context_from_response_extraction.py
new file mode 100644
index 00000000000..53755a0c6ef
--- /dev/null
+++ b/tests/cron/test_context_from_response_extraction.py
@@ -0,0 +1,177 @@
+"""context_from must inject the previous run's answer, not its prompt (#117290).
+
+Stored agent-run archives are ``# Cron Job`` / ``## Prompt`` / ``## Response``
+documents; skill-bearing prompts routinely exceed the 8000-char injection
+budget, so head-truncation amputated the ``## Response`` section and
+self-continuity silently became a no-op.
+"""
+
+import sys
+from pathlib import Path
+
+import pytest
+
+sys.path.insert(0, str(Path(__file__).parent.parent.parent))
+
+
+def cron_env(tmp_path, monkeypatch):
+ """Isolated cron environment with temp HERMES_HOME."""
+ hermes_home = tmp_path / ".hermes"
+ hermes_home.mkdir()
+ (hermes_home / "cron").mkdir()
+ (hermes_home / "cron" / "output").mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+
+ import cron.jobs as jobs_mod
+ monkeypatch.setattr(jobs_mod, "HERMES_DIR", hermes_home)
+ monkeypatch.setattr(jobs_mod, "CRON_DIR", hermes_home / "cron")
+ monkeypatch.setattr(jobs_mod, "JOBS_FILE", hermes_home / "cron" / "jobs.json")
+ monkeypatch.setattr(jobs_mod, "OUTPUT_DIR", hermes_home / "cron" / "output")
+
+ return hermes_home
+
+
+def _write_archive(cron_env, job_id: str, filename: str, body: str) -> None:
+ from cron.jobs import OUTPUT_DIR
+
+ out_dir = OUTPUT_DIR / job_id
+ out_dir.mkdir(parents=True, exist_ok=True)
+ (out_dir / filename).write_text(body, encoding="utf-8")
+
+
+class TestResponseSurvivesLongPrompt:
+ """The answer, not the prompt, is the part continuity needs."""
+
+ def test_long_prompt_archive_keeps_response(self, cron_env):
+ from cron.jobs import create_job
+ from cron.scheduler import _build_job_prompt
+
+ job = create_job(prompt="Run the daily check", schedule="0 8 * * *", context_from="self")
+ _write_archive(
+ cron_env, job["id"], "2026-09-19_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\n" + "SKILL LINE\n" * 1800 +
+ "\n\n## Response\n\nCONCLUSION-MARKER-42\n",
+ )
+
+ prompt = _build_job_prompt(job)
+
+ assert "CONCLUSION-MARKER-42" in prompt
+ assert "SKILL LINE" not in prompt # the prompt half is dropped, not the answer
+
+
+class TestUnusableAnswersFallThrough:
+ """A [SILENT] or blank response is not usable continuity."""
+
+ def test_silent_response_falls_through_to_older_archive(self, cron_env):
+ from cron.jobs import create_job
+ from cron.scheduler_prompt import _inject_context_from
+
+ job = create_job(prompt="Report", schedule="0 8 * * *", context_from="self")
+ _write_archive(
+ cron_env, job["id"], "2026-09-18_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\ndo the thing\n\n## Response\n\nOLDER-REAL-ANSWER\n",
+ )
+ _write_archive(
+ cron_env, job["id"], "2026-09-19_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\ndo the thing\n\n## Response\n\n[SILENT]\n",
+ )
+
+ prompt, injected = _inject_context_from(job, "Report")
+
+ assert injected is True
+ assert "OLDER-REAL-ANSWER" in prompt
+ assert "[SILENT]" not in prompt
+
+ def test_blank_response_falls_through_to_older_archive(self, cron_env):
+ from cron.jobs import create_job
+ from cron.scheduler_prompt import _inject_context_from
+
+ job = create_job(prompt="Report", schedule="0 8 * * *", context_from="self")
+ _write_archive(
+ cron_env, job["id"], "2026-09-18_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\ndo the thing\n\n## Response\n\nOLDER-REAL-ANSWER\n",
+ )
+ _write_archive(
+ cron_env, job["id"], "2026-09-19_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\ndo the thing\n\n## Response\n\n",
+ )
+
+ prompt, injected = _inject_context_from(job, "Report")
+
+ assert injected is True
+ assert "OLDER-REAL-ANSWER" in prompt
+
+ def test_all_unusable_archives_inject_nothing(self, cron_env):
+ from cron.jobs import create_job
+ from cron.scheduler_prompt import _inject_context_from
+
+ job = create_job(prompt="Report", schedule="0 8 * * *", context_from="self")
+ _write_archive(
+ cron_env, job["id"], "2026-09-19_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\ndo the thing\n\n## Response\n\n[SILENT]\n",
+ )
+
+ prompt, injected = _inject_context_from(job, "Report")
+
+ assert injected is False
+ assert prompt == "Report"
+
+
+class TestScriptModeArchives:
+ """Archives without a ## Response heading (script-mode) stay whole-document."""
+
+ def test_headingless_archive_injects_whole_document(self, cron_env):
+ from cron.jobs import create_job
+ from cron.scheduler_prompt import _inject_context_from
+
+ job = create_job(prompt="Report", schedule="0 8 * * *", context_from="self")
+ _write_archive(cron_env, job["id"], "2026-09-19_08-00-00.md", "plain script payload\nline two")
+
+ prompt, injected = _inject_context_from(job, "Report")
+
+ assert injected is True
+ assert "plain script payload" in prompt
+ assert "line two" in prompt
+
+
+class TestBudgetClipping:
+ """An oversized answer clips head+tail — conclusions sit at the end."""
+
+ def test_oversized_answer_clips_head_and_tail(self, cron_env):
+ from cron.jobs import create_job
+ from cron.scheduler_prompt import _inject_context_from, _MAX_CONTEXT_CHARS
+
+ job = create_job(prompt="Report", schedule="0 8 * * *", context_from="self")
+ long_answer = "HEAD-" + "filler line\n" * 1200 + "TAIL-CONCLUSION"
+ assert len(long_answer) > _MAX_CONTEXT_CHARS
+ _write_archive(
+ cron_env, job["id"], "2026-09-19_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\nshort\n\n## Response\n\n" + long_answer,
+ )
+
+ prompt, injected = _inject_context_from(job, "Report")
+
+ assert injected is True
+ assert "HEAD-" in prompt
+ assert "TAIL-CONCLUSION" in prompt
+ assert "chars omitted" in prompt
+
+ block = prompt.split("## Your previous run's output", 1)[-1] if "## Your previous run's output" in prompt else prompt
+ assert len(block) < _MAX_CONTEXT_CHARS + 400 # clipped, not whole
+
+ def test_short_answer_injects_verbatim(self, cron_env):
+ from cron.jobs import create_job
+ from cron.scheduler_prompt import _inject_context_from
+
+ job = create_job(prompt="Report", schedule="0 8 * * *", context_from="self")
+ _write_archive(
+ cron_env, job["id"], "2026-09-19_08-00-00.md",
+ "# Cron Job: probe\n\n## Prompt\n\ndo the thing\n\n## Response\n\nAll quiet.\n",
+ )
+
+ prompt, injected = _inject_context_from(job, "Report")
+
+ assert injected is True
+ assert "All quiet." in prompt
+ assert "do the thing" not in prompt # only the answer is injected
diff --git a/tests/cron/test_cron_context_from.py b/tests/cron/test_cron_context_from.py
index 46c5ccff8fd..6c8dfb45d45 100644
--- a/tests/cron/test_cron_context_from.py
+++ b/tests/cron/test_cron_context_from.py
@@ -158,22 +158,24 @@ class TestBuildJobPromptContextFrom:
assert context_pos < prompt_pos
def test_output_truncated_at_8k_chars(self, cron_env):
- """Output longer than 8000 chars should be truncated."""
+ """Output longer than the 8000-char budget is clipped head+tail (#117290)."""
from cron.jobs import create_job, OUTPUT_DIR
from cron.scheduler import _build_job_prompt
job_a = create_job(prompt="Find data", schedule="every 1h")
out_dir = OUTPUT_DIR / job_a["id"]
out_dir.mkdir(parents=True, exist_ok=True)
- big_output = "x" * 10000
+ big_output = "HEAD" + "x" * 9992 + "TAIL"
(out_dir / "2026-04-22_10-00-00.md").write_text(big_output, encoding="utf-8")
job_b = create_job(
prompt="Process", schedule="every 2h", context_from=job_a["id"]
)
prompt = _build_job_prompt(job_b)
- assert "truncated" in prompt
- assert "x" * 10000 not in prompt
+ assert "chars omitted" in prompt
+ assert "x" * 9992 not in prompt
+ assert "HEAD" in prompt # head+tail clip keeps both ends
+ assert "TAIL" in prompt
def test_invalid_job_id_skipped(self, cron_env):