Files
ai-agent/backend/test_agy_tasks.py
gabrielvidalandClaude Fable 5.2 b32458e821 feat(trackers): tasks leave the core for the tasks plugin — tool trackers, checklists
Generic points the plugin stands on (docs/plugins.md → "Trackers" → Tool
trackers, "Checklists"):
- trackers[]: tool-call rules — `tool` (a neutral tool name) + `ops` (create /
  update by id / whole-list snapshot, told apart by the input's field paths) +
  `statuses`; items gain `status` and `startedAt`, keep the list's order; the
  re-derive pass finds the transcripts by the tool's quoted names
  (toolnames.byte_hints), no PARSER_VERSION bump (48: the unshipped v49 goes)
- provides tasks.list: the completion scaffold reads the task list through
  trackers.provided; an older row's `tasks` is carried over until re-read
- ui.card[].list.style "checklist" (the ✓ done/total pill and its card),
  ui.panels[] (the floating checklist over the thread) and ui.toolCards[]
  naming a tracker (the calls' rows) — declared by the plugin, drawn by the
  app: Checklist.tsx (the old TaskPanel, moved), PluginPanels, trackerCards;
  the tool-card registry has an epoch so mounted rows follow a declared card

Removed from core: todo_op / _task_times and the summary's `tasks`, the card
field, the Task schema, the registered `todo` card (its bucket stays), the
taskPanel switch (a hidden one carries over to plugin:tasks:panel), their
i18n keys.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-02 22:30:48 +01:00

200 lines
8.1 KiB
Python

"""agy's background tasks and plan steps, read off a v1 mirror the way
Claude Code's are (docs/transcript-format.md → "Tool names", ``data.taskId``):
- a ``tool_result`` with ``data.taskId`` started a background task → its card
carries ``taskId`` / ``taskState`` like a ``run_in_background`` Bash call;
- the mirror's ``task_done`` notes (``taskIds`` + ``toolUseId``) end it;
- an already-mirrored ``agy:update_steps {steps}`` folds as a todo snapshot."""
import json
import conversations as c
from fixtures.tracker_plugins import kit
CONV = "37914220-cbc8-4f69-a195-dc95483599fb"
TASK = f"{CONV}/task-4"
def _ev(t, at, **kw):
return {"t": t, "at": at, **kw}
HEAD = [_ev("session", "2026-12-01T10:00:00Z", v=1, id=CONV, harness="agy", cwd="/tmp/x"),
_ev("prompt", "2026-12-01T10:00:00Z", text="build it", origin="user")]
def _call(i, tool, inp, native=None, at="2026-12-01T10:00:01Z"):
ev = {"tool": tool, "id": f"toolu_{i}", "call": f"msg_{i}", "input": inp}
if native:
ev["native"] = native
return [_ev("call", at, id=f"msg_{i}", model="gemini-4-neon"),
_ev("tool_call", at, **ev)]
def _result(i, text, at="2026-12-01T10:00:02Z", ok=True, **data):
ev = _ev("tool_result", at, toolCall=f"toolu_{i}", ok=ok, text=text)
if data:
ev["data"] = data
return ev
def _done(at, *, status="completed", summary="", event=None, exit_code=None,
task=TASK, tool_use="toolu_1"):
note = {"taskIds": [task], "toolUseId": tool_use, "status": status,
"summary": summary, "event": event, "exitCode": exit_code}
return _ev("inject", at, kind="task_done", notes=[note])
def _parse(tmp_path, evs, full=True):
p = tmp_path / f"{CONV}.jsonl"
end = [_ev("turn_end", "2026-12-01T11:00:00Z", reason="done")]
p.write_text("".join(json.dumps(e) + "\n" for e in HEAD + evs + end))
# the task list is the tasks plugin's tool tracker (plugins/trackers.py)
with kit.bound(kit.tasks_store()):
st = c.ParserState(p, full=full)
for line in p.read_text().splitlines():
st.feed(json.loads(line))
return st
def _card(st, i):
return next(it for it in st.thread
if it["kind"] == "tool_use" and it.get("toolUseId") == f"toolu_{i}")
RUNNING = (f"Tool is running as a background task with task id: {TASK}\n"
"Task Description: npm run build\n"
f"Task logs are available at: file:///tmp/{CONV}/task-4.log")
def test_data_task_id_starts_a_background_task_on_the_card(tmp_path):
st = _parse(tmp_path, [
*_call(1, "shell", {"command": "npm run build"}, native="run_command"),
_result(1, RUNNING, taskId=TASK),
])
card = _card(st, 1)
assert card["taskId"] == TASK
assert card["taskState"] == "running"
assert st.task_items[TASK] is card
def test_no_task_id_without_the_field(tmp_path):
st = _parse(tmp_path, [
*_call(1, "shell", {"command": "npm run build"}, native="run_command"),
_result(1, RUNNING), # the text alone: agy's wording isn't Claude's
])
assert "taskId" not in _card(st, 1)
def test_an_error_result_starts_nothing(tmp_path):
st = _parse(tmp_path, [
*_call(1, "shell", {"command": "npm run build"}, native="run_command"),
_result(1, "boom", ok=False, taskId=TASK),
])
assert "taskId" not in _card(st, 1)
def _bg(evs=()):
return [*_call(1, "shell", {"command": "npm run build"}, native="run_command"),
_result(1, RUNNING, taskId=TASK), *evs]
def test_a_finished_note_ends_it_with_its_exit_code(tmp_path):
st = _parse(tmp_path, _bg([
_done("2026-12-01T10:05:00Z", summary="Command exited with code 0.", exit_code=0)]))
card = _card(st, 1)
assert card["taskState"] == "completed"
assert card["taskExitCode"] == 0
assert card["taskEvents"][-1] == {"ts": "2026-12-01T10:05:00Z", "kind": "ended",
"text": "Command exited with code 0."}
def test_a_failed_note_by_task_id_alone(tmp_path):
st = _parse(tmp_path, _bg([
_done("2026-12-01T10:05:00Z", status="failed", summary="Command exited with code 2.",
exit_code=2, tool_use=None)]))
card = _card(st, 1)
assert (card["taskState"], card["taskExitCode"]) == ("failed", 2)
def test_still_running_is_an_event_then_the_notice_stops_it(tmp_path):
st = _parse(tmp_path, _bg([
_done("2026-12-01T10:10:00Z", status=None,
event="Background command is still running after 600s: npm run build"),
_done("2026-12-01T10:20:00Z", status="stopped",
summary="All your subagents and background tasks have been stopped"),
]))
card = _card(st, 1)
assert [e["kind"] for e in card["taskEvents"]] == ["event", "stopped"]
assert card["taskState"] == "stopped"
def test_the_stop_notice_leaves_a_finished_task_alone(tmp_path):
st = _parse(tmp_path, _bg([
_done("2026-12-01T10:05:00Z", summary="Command exited with code 0.", exit_code=0),
_done("2026-12-01T10:20:00Z", status="stopped", summary="stopped"),
]))
assert _card(st, 1)["taskState"] == "completed"
def test_manage_task_status_and_stop_point_at_the_card(tmp_path):
st = _parse(tmp_path, _bg([
*_call(2, "shell_output", {"task_id": TASK, "Action": "status", "TaskId": TASK},
native="manage_task", at="2026-12-01T10:01:00Z"),
_result(2, f"Task: {TASK}\nStatus: RUNNING", at="2026-12-01T10:01:01Z"),
*_call(3, "shell_kill", {"task_id": TASK, "Action": "kill", "TaskId": TASK},
native="manage_task", at="2026-12-01T10:02:00Z"),
_result(3, "ok", at="2026-12-01T10:02:01Z"),
]))
for i in (2, 3):
assert _card(st, i)["taskRef"]["toolUseId"] == "toolu_1"
assert _card(st, 1)["taskState"] == "stopped"
def test_a_schedule_timer_fires(tmp_path):
st = _parse(tmp_path, [
*_call(1, "agy:schedule", {"delaySeconds": 300, "prompt": "check the deploy",
"DurationSeconds": 300, "Prompt": "check the deploy"},
native="schedule"),
_result(1, f"Tool is running as a background task with task id: {TASK}", taskId=TASK),
_done("2026-12-01T10:05:00Z", summary="fired"),
])
card = _card(st, 1)
assert (card["taskId"], card["taskState"]) == (TASK, "completed")
STEPS = [{"status": "done", "title": "Create worktrees"},
{"status": "in_progress", "title": "Implement backend"},
{"status": "pending", "title": "Run tests"},
{"status": "skipped", "title": "Old idea"},
{"status": "canceled", "title": "Other idea"},
{"status": "blocked", "title": "Odd one"}]
def test_an_older_mirrors_update_steps_fills_the_task_list(tmp_path):
for full in (True, False):
st = _parse(tmp_path, [
*_call(1, "agy:update_steps", {"steps": STEPS[:2]}, native="update_steps"),
*_call(2, "agy:update_steps", {"steps": STEPS}, native="update_steps",
at="2026-12-01T10:03:00Z"),
], full=full)
tasks = kit.task_list(st.summary())
assert [(t["name"], t["status"]) for t in tasks] == [
("Create worktrees", "completed"), ("Implement backend", "in_progress"),
("Run tests", "pending"), ("Old idea", "cancelled"),
("Other idea", "cancelled"), ("Odd one", "pending")]
st = _parse(tmp_path, [*_call(1, "agy:update_steps", {"steps": STEPS}, native="update_steps")])
card = _card(st, 1)
assert card["name"] == "todo" and card["native"] == "update_steps"
assert card["input"]["todos"][0] == {"content": "Create worktrees",
"activeForm": "Create worktrees", "status": "completed"}
def test_the_new_mirrors_todo_shape_reads_the_same(tmp_path):
todos = [{"content": "A", "activeForm": "A", "status": "completed"},
{"content": "B", "activeForm": "B", "status": "in_progress"}]
st = _parse(tmp_path, [*_call(1, "todo", {"todos": todos}, native="update_steps")])
assert [(t["name"], t["status"]) for t in kit.task_list(st.summary())] == [
("A", "completed"), ("B", "in_progress")]
assert _card(st, 1)["native"] == "update_steps"