Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(agent): force target-file writes before the final iteration

Will Liao44de07fa92f1
diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.py
index 0d31eac4c..2e1f5d8d9 100644
--- a/agent/src/agent/loop.py
+++ b/agent/src/agent/loop.py
@@ -604,6 +604,44 @@ def _looks_like_tool_call_syntax(content: str) -> bool:
)
+
+
+# Task target-file detection: a user message usually names the file to
+# create or update ("update C:\\...\\plan.md"). If a run approaches its
+# iteration cap without having written that file, the loop must remind the
+# model instead of ending "answered but incomplete".
+_TARGET_PATH_RE = re.compile(
+ r"[A-Za-z]:\\[^\s\x22\x27<>|?*]+\.md\b"
+ r"|\b[\w./\\-]+\.md\b"
+)
+_TARGET_ACTION_RE = re.compile(
+ r"update|create|write|add|make|edit|generate|overwrite|append"
+ r"|更新|创建|写|添加|修改|生成|建立|编制",
+ re.IGNORECASE,
+)
+
+
+def _named_target_paths(text: str) -> list[Path]:
+ """Return the .md file paths named in a user message (deduped).
+
+ Both absolute Windows paths and bare or relative filenames are matched.
+ """
+ seen: set[str] = set()
+ paths: list[Path] = []
+ for match in _TARGET_PATH_RE.finditer(text or ""):
+ raw = match.group(0).strip().strip("\x22\x27")
+ try:
+ p = Path(raw)
+ except (ValueError, OSError):
+ continue
+ if p.suffix != ".md":
+ continue
+ key = str(p).casefold()
+ if key in seen:
+ continue
+ seen.add(key)
+ paths.append(p)
+ return paths
def _normalize_tool_run_dir(args: dict[str, Any], memory_run_dir: str | None) -> dict[str, Any]:
"""Normalize ``run_dir`` in tool args to an absolute path when possible.
@@ -741,6 +779,7 @@ class AgentLoop:
self._has_run = False
self._grounding: GroundingLedger | None = None
self._released_fallback = False
+ self._written_files: set[str] = set()
def cancel(self) -> None:
"""Cancel the current loop.
@@ -829,6 +868,8 @@ class AgentLoop:
self._called_ok = set()
self._previous_summary = ""
self._released_fallback = False
+ self._written_files = set()
+ run_started_wall = _time.time()
state_store = RunStateStore()
RUNS_DIR.mkdir(parents=True, exist_ok=True)
@@ -945,18 +986,33 @@ class AgentLoop:
# context as the most recent user message.
if iteration == wrap_up_at and 1 < iteration < self.max_iterations:
remaining = self.max_iterations - iteration
- messages.append({
- "role": "user",
- "content": (
- f"[SYSTEM] You have {remaining} iterations remaining out of "
- f"{self.max_iterations}. Wrap up your work now: finish any "
- "outstanding file writes or data updates that are part of your "
- "task FIRST, while tools are still available for a few more "
- "iterations. Do not start new analysis or re-verify data you "
- "already hold. Then provide your final answer as plain text; "
- "if your task was to update a file, state that it is done and where."
- ),
- })
+ wrap_content = (
+ f"[SYSTEM] You have {remaining} iterations remaining out of "
+ f"{self.max_iterations}. Wrap up your work now: finish any "
+ "outstanding file writes or data updates that are part of your "
+ "task FIRST, while tools are still available for a few more "
+ "iterations. Do not start new analysis or re-verify data you "
+ "already hold. Then provide your final answer as plain text; "
+ "if your task was to update a file, state that it is done and where."
+ )
+ pending_directive = self._pending_write_directive(
+ user_message, run_started_wall
+ )
+ if pending_directive:
+ wrap_content += "\n\n" + pending_directive
+ messages.append({"role": "user", "content": wrap_content})
+
+ # Safety net: on the second-to-last iteration tools are still
+ # available, but the final iteration is text-only and cannot
+ # call tools. If the task names a target file that has not been
+ # written, force the write now so the run does not end
+ # "answered but incomplete".
+ if iteration == self.max_iterations - 1:
+ pending_directive = self._pending_write_directive(
+ user_message, run_started_wall
+ )
+ if pending_directive:
+ messages.append({"role": "system", "content": pending_directive})
# Streaming output + collect thinking text
thinking_chunks: List[str] = []
@@ -2010,6 +2066,59 @@ class AgentLoop:
return False
return bool(tool_def and getattr(tool_def, "is_readonly", False))
+ def _record_written_target(self, arguments: Mapping[str, Any]) -> None:
+ """Remember a file written by write_file/edit_file for completion checks."""
+ raw = arguments.get("path") or arguments.get("file_path")
+ if not raw:
+ return
+ p = Path(str(raw))
+ if not p.is_absolute() and self.memory.run_dir:
+ p = Path(self.memory.run_dir) / p
+ try:
+ self._written_files.add(str(p.resolve()).casefold())
+ except (OSError, ValueError):
+ self._written_files.add(str(p).casefold())
+
+ def _pending_write_directive(
+ self, user_message: str, run_started_wall: float
+ ) -> str:
+ """Return a directive naming task target files not yet written this run.
+
+ Empty when the message names no .md targets, carries no create/update
+ intent, or every named target has been written (directly via
+ write_file/edit_file, or by any process whose mtime is newer than the
+ run start - which covers the bash workaround).
+ """
+ if not _TARGET_ACTION_RE.search(user_message or ""):
+ return ""
+ targets = _named_target_paths(user_message)
+ if not targets:
+ return ""
+ pending: list[str] = []
+ for p in targets:
+ try:
+ key = str(p.resolve()).casefold()
+ except (OSError, ValueError):
+ key = str(p).casefold()
+ written = key in self._written_files
+ if not written:
+ try:
+ if p.exists() and p.stat().st_mtime >= run_started_wall:
+ written = True
+ except OSError:
+ pass
+ if not written:
+ pending.append(str(p))
+ if not pending:
+ return ""
+ return (
+ "[SYSTEM] The following task target file(s) have NOT been written "
+ "yet in this run: " + ", ".join(pending) + ". Write them NOW using "
+ "write_file/edit_file (or bash for file operations). If your task "
+ "was to create or update a file, a plain-text answer without the "
+ "file write is a failure."
+ )
+
def _finalize_tool_result(
self,
tc: Any,
@@ -2047,6 +2156,8 @@ class AgentLoop:
_archive_backtest_result(result, self.memory.run_dir)
except OSError as exc:
logger.warning("Could not archive backtest output into active run: %s", exc)
+ if tc.name in {"write_file", "edit_file"}:
+ self._record_written_target(tc.arguments)
if self._grounding is not None:
self._grounding.ingest_tool_result(
diff --git a/agent/tests/_bash_written.md b/agent/tests/_bash_written.md
new file mode 100644
index 000000000..c1b0730e0
--- /dev/null
+++ b/agent/tests/_bash_written.md
@@ -0,0 +1 @@
+x
\ No newline at end of file
diff --git a/agent/tests/_iso1.txt b/agent/tests/_iso1.txt
new file mode 100644
index 000000000..5c0f4d672
--- /dev/null
+++ b/agent/tests/_iso1.txt
@@ -0,0 +1,4 @@
+x
+ r"更新|创建|写|添加",
+def f(text: str) -> list[str]:
+ return []
\ No newline at end of file
diff --git a/agent/tests/_iso2.txt b/agent/tests/_iso2.txt
new file mode 100644
index 000000000..c8d1db929
--- /dev/null
+++ b/agent/tests/_iso2.txt
@@ -0,0 +1,9 @@
+_TARGET_PATH_RE = re.compile(
+ r"[A-Za-z]:\\[^\s\x22\x27<>|?*]+\.md\b"
+ r"|\b[\w./\\-]+\.md\b"
+)
+def _named_target_paths(text: str) -> list[Path]:
+ """Docstring with "quotes" and \\ backslashes."""
+ for match in _TARGET_PATH_RE.finditer(text or ""):
+ raw = match.group(0).strip().strip("\x22\x27")
+ return []
\ No newline at end of file
diff --git a/agent/tests/_iso4.txt b/agent/tests/_iso4.txt
new file mode 100644
index 000000000..4ecb66362
--- /dev/null
+++ b/agent/tests/_iso4.txt
@@ -0,0 +1,4 @@
+# Task target-file detection: a user message usually names the file to
+# create or update ("update C:\\...\\plan.md"). If a run approaches its
+# iteration cap without having written that file, the loop must remind the
+# model instead of ending "answered but incomplete".
\ No newline at end of file
diff --git a/agent/tests/_iso5.txt b/agent/tests/_iso5.txt
new file mode 100644
index 000000000..af274ec9f
--- /dev/null
+++ b/agent/tests/_iso5.txt
@@ -0,0 +1,5 @@
+_TARGET_ACTION_RE = re.compile(
+ r"update|create|write|add|make|edit|generate|overwrite|append",
+ r"更新|创建|写|添加|修改|生成|建立|编制",
+ re.IGNORECASE,
+)
\ No newline at end of file
diff --git a/agent/tests/_iso6.txt b/agent/tests/_iso6.txt
new file mode 100644
index 000000000..a3ecd3f21
--- /dev/null
+++ b/agent/tests/_iso6.txt
@@ -0,0 +1,5 @@
+_TARGET_ACTION_RE = re.compile(
+ r"update|create|write|add|make|edit|generate|overwrite|append"
+ r"|更新|创建|写|添加|修改|生成|建立|编制",
+ re.IGNORECASE,
+)
\ No newline at end of file
diff --git a/agent/tests/test_loop_helpers.py b/agent/tests/test_loop_helpers.py
index 170da3e0f..191e1f022 100644
--- a/agent/tests/test_loop_helpers.py
+++ b/agent/tests/test_loop_helpers.py
@@ -404,3 +404,50 @@ def test_llm_timeout_seconds_default_and_override(monkeypatch) -> None:
assert _llm_timeout_seconds() == 42.0
monkeypatch.delattr(loop_module, "LLM_TIMEOUT_SECONDS", raising=False)
assert _llm_timeout_seconds() > 0
+
+def test_pending_write_directive_tracks_written_targets(tmp_path: Path) -> None:
+ """A named target file that was not written yields a write directive.
+
+ Regression for runs that ended "success" without delivering a file
+ (2026-08-15 mutual-fund update): the loop must remind the model to write
+ the task target before the forced-text final iteration. The directive
+ clears once the file is written directly (write_file/edit_file) or by
+ any process with a newer mtime (the bash workaround), and only fires for
+ messages with create/update intent.
+ """
+ import os
+ import time
+
+ import src.agent.loop as loop_module
+ from src.agent.loop import AgentLoop
+ from src.agent.tools import ToolRegistry
+
+ agent = AgentLoop(registry=ToolRegistry(), llm=None)
+ now = time.time()
+ target = str(tmp_path / "plan.md")
+ msg = f"please update {target}"
+
+ assert loop_module._TARGET_ACTION_RE.search(msg)
+ assert not loop_module._TARGET_ACTION_RE.search("what is the weather?")
+
+ unwritten = agent._pending_write_directive(msg, now)
+ assert "NOT been written" in unwritten and "plan.md" in unwritten
+
+ agent._record_written_target({"path": target})
+ assert agent._pending_write_directive(msg, now) == ""
+
+ # A bash-style write (mtime newer than run start) also counts.
+ agent2 = AgentLoop(registry=ToolRegistry(), llm=None)
+ p = tmp_path / "bash_plan.md"
+ p.write_text("x", encoding="utf-8")
+ os.utime(p, (now, now))
+ assert agent2._pending_write_directive(
+ f"please update {p}", now - 10,
+ ) == ""
+ # An old file not written this run still fires.
+ old = tmp_path / "old_plan.md"
+ old.write_text("x", encoding="utf-8")
+ os.utime(old, (now - 100, now - 100))
+ assert "old_plan.md" in agent2._pending_write_directive(
+ f"please update {old}", now - 10,
+ )