fix: background tasks now fetch real page content; add task-status tools
Rebuild & Deploy / rebuild (push) Successful in 58s

Background research tasks were producing garbage or failing because the
self-hosted Firecrawl /v1/scrape defaults to a ~30s timeout that's too short
for heavy news/research sites, and the small LLM won't reliably chain a
second scrape call after a search that only returns snippets.

task_worker.py:
- web_search now auto-fetches the top results directly from the container
  (which has working outbound internet) and returns their real text, so the
  model gets actual page content in one call instead of snippet echoes.
- web_scrape requests timeout: 90000 (Firecrawl renders heavy sites but needs
  >30s), and falls back to a direct container fetch with tag-stripping if
  Firecrawl times out or returns nothing.

task_registry.py / dispatch_mcp.py:
- Add task_status(id) and list_tasks() tools so Hope can check the live state
  of any dispatched task (pending / running / completed / failed / STALLED).
- Track last_activity per task and surface a stall heuristic.
- Move the status summary into task_registry (no mcp dependency) so it's
  unit-testable.

agent.py: prompt tells Hope about task_status / list_tasks.
tests: add task-status summary and last-activity regression tests.
This commit is contained in:
snowspeeder committed 2026-08-30 06:54:57 -04:00
1 parent df8aa64504
commit d4ea4fc984
5 files changed
+217 -30

No files matched your search

+47
View File
@@ -88,6 +88,53 @@ def test_task_event_directories_are_isolated(tmp_path):
assert second_events == []
def test_task_summary_reports_live_status(tmp_path):
"""task_status/list_tasks summarise live state including a stall hint."""
import time as _time
sys.path.insert(0, str(Path(__file__).parents[1] / "agent"))
from task_registry import status_summary # noqa: E402
import task_registry as tr_mod # noqa: E402
# Isolate: status_summary reads the module-level registry.
held = tr_mod.registry
iso = tr_mod.TaskRegistry(tmp_path)
tr_mod.registry = iso
try:
task = iso.create("test task")
task_id = task.id
iso.add_step(task_id, "thinking", "computing")
summary = status_summary(task_id, stalled_s=45.0)
assert "RUNNING" in summary, summary
assert "step(s)" in summary, summary
# Stalled: force last activity far in the past.
task.last_activity = _time.time() - 100
iso._save_task(task)
assert "STALLED" in status_summary(task_id, stalled_s=45.0)
# Completed with result.
iso.complete(task_id, "the answer")
done = status_summary(task_id, stalled_s=45.0)
assert "COMPLETED" in done, done
assert "the answer" in done, done
# Unknown id.
assert "was not found" in status_summary("nope", stalled_s=45.0)
finally:
tr_mod.registry = held
def test_registry_tracks_last_activity(tmp_path):
reg = TaskRegistry(tmp_path)
task = reg.create("tracked")
before = task.last_activity
task.last_activity = before - 10
reg.add_step(task.id, "thinking", "step")
data = reg.get(task.id)
assert data["last_activity"] > before - 10, data["last_activity"]
@pytest.mark.slow
def test_dispatch_mcp_task_reaches_terminal_event():
"""The MCP request returns immediately while its child task stays alive."""