delegate_tool_tasks.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """delegate_task input validation: tasks=[...] / legacy goal normalisation, per-task output schemas and images."""
  2. from __future__ import annotations
  3. import json
  4. import re
  5. from typing import Any, Dict, List, Optional
  6. # Placeholder shapes for batch goal validation: bare 'TODO' / 'task N' labels, or unexpanded template markers. The
  7. # marker regex is deliberately NARROW — only snake_case / space-separated placeholder identifiers (`<feature_name>`,
  8. # `{file path}`, `<FEATURE-NAME>`), the shape LLM templates leave behind. Bare single-word brackets must never be
  9. # rejected: legitimate goals are full of generics (`Vec<T>`), HTML tags (`<div>`), dict snippets (`{"key": 1}`), glob
  10. # braces (`{a,b}`) and f-string style (`{i}`).
  11. # See #81141.
  12. _PLACEHOLDER_GOAL_RE = re.compile(r"^(todo|task\s*\d+)$", re.IGNORECASE)
  13. _TEMPLATE_MARKER_RE = re.compile(
  14. r"<[A-Za-z][A-Za-z0-9]*(?:[ _-][A-Za-z0-9]+)+>|\{[A-Za-z][A-Za-z0-9]*(?:[ _-][A-Za-z0-9]+)+\}"
  15. )
  16. _MIN_BATCH_GOAL_LEN = 10
  17. def _recover_tasks_from_json_string(tasks: Any) -> tuple[Optional[list[dict[str, Any]]], Optional[str]]:
  18. """``(parsed_list, None)`` for a JSON-array string, ``(None, error)`` for a bad string, ``(None, None)`` otherwise."""
  19. if not isinstance(tasks, str):
  20. return None, None
  21. raw = tasks.strip()
  22. if not raw:
  23. return None, "Provide either 'goal' (single task) or 'tasks' (batch)."
  24. try:
  25. parsed = json.loads(raw)
  26. except json.JSONDecodeError as exc:
  27. return None, f"tasks must be a JSON array of task objects; received a string that could not be parsed as JSON ({exc.msg})."
  28. if not isinstance(parsed, list):
  29. return None, f"tasks must be a JSON array of task objects; parsed {type(parsed).__name__} instead."
  30. return parsed, None
  31. def _validate_batch_tasks(task_list: list[dict[str, Any]]) -> Optional[str]:
  32. """Batch-only quality gate beyond per-task goal presence; actionable error or None. No minimum count: a one-entry
  33. array is the canonical single-task shape (legacy top-level `goal` is wrapped into one). Duplicate goals are
  34. deliberately NOT rejected — identical-goal fan-outs (best-of-N / ensemble sampling) are legitimate and blocking
  35. them broke real workflows. The too-short check applies only to multi-task fan-outs (terse goals there are
  36. usually unexpanded templates); a SINGLE task legitimately uses short goals ("Fix the tests").
  37. See #81141.
  38. """
  39. for i, task in enumerate(task_list):
  40. goal = str(task.get("goal", "")).strip()
  41. if _PLACEHOLDER_GOAL_RE.match(" ".join(goal.lower().split())):
  42. return (
  43. f"Task {i} has a placeholder goal ({goal!r}). Replace it "
  44. "with a specific, self-contained description of what the subagent should accomplish."
  45. )
  46. marker = _TEMPLATE_MARKER_RE.search(goal)
  47. if marker:
  48. return (
  49. f"Task {i} goal contains an unexpanded template marker "
  50. f"({marker.group(0)!r}). Substitute the real value before "
  51. "calling delegate_task — subagents cannot resolve placeholders."
  52. )
  53. if len(goal) < _MIN_BATCH_GOAL_LEN and len(task_list) >= 2:
  54. return (
  55. f"Task {i} goal is too short ({goal!r}). Write a specific, "
  56. f"self-contained goal of at least {_MIN_BATCH_GOAL_LEN} characters so the subagent knows "
  57. "exactly what to do."
  58. )
  59. return None
  60. def _normalize_task_list(
  61. goal, context, tasks, output_schema, top_role: str, max_children: int
  62. ) -> tuple[Optional[list[dict[str, Any]]], Optional[str]]:
  63. """``(task_list, None)`` from ``tasks=[...]`` or the legacy single ``goal``, else ``(None, error)``."""
  64. recovered_tasks, tasks_error = _recover_tasks_from_json_string(tasks)
  65. if tasks_error:
  66. return None, tasks_error
  67. if recovered_tasks is not None:
  68. tasks = recovered_tasks
  69. # Small models emit tasks=[] alongside a single goal: treat as "no batch".
  70. if isinstance(tasks, list) and not tasks:
  71. tasks = None
  72. if tasks and isinstance(tasks, list):
  73. if len(tasks) > max_children:
  74. return None, (
  75. f"Too many tasks: {len(tasks)} provided, but max_concurrent_children is {max_children}. "
  76. f"Either reduce the task count, split into multiple delegate_task calls, or increase "
  77. f"delegation.max_concurrent_children in config.yaml."
  78. )
  79. task_list = tasks
  80. elif goal and isinstance(goal, str) and goal.strip():
  81. task_list = [{"goal": goal, "context": context, "role": top_role}]
  82. if output_schema is not None:
  83. task_list[0]["output_schema"] = output_schema
  84. else:
  85. return None, (
  86. "No tasks provided. Pass tasks=[{goal: '...', context: '...'}, "
  87. "...] — one entry per subagent (a single task is a one-entry array)."
  88. )
  89. for i, task in enumerate(task_list):
  90. if not isinstance(task, dict):
  91. return None, f"Task {i} must be an object, got {type(task).__name__}."
  92. if not task.get("goal", "").strip():
  93. return None, f"Task {i} is missing a 'goal'."
  94. # The single-goal form is exempt from the batch gate (short goals are valid there).
  95. batch_error = _validate_batch_tasks(task_list) if isinstance(tasks, list) else None
  96. return (None, batch_error) if batch_error else (task_list, None)
  97. def _coerce_task_schemas(
  98. task_list: list[dict[str, Any]], output_schema: Optional[dict[str, Any]]
  99. ) -> tuple[list[Optional[dict[str, Any]]], Optional[str]]:
  100. """Per-task coerced output schemas. A malformed output_schema fails the whole call before any child spawns;
  101. schema-less tasks resolve to None and take no new code paths downstream."""
  102. from tools.delegation_output_schema import coerce_output_schema
  103. task_schemas: list[Optional[dict[str, Any]]] = []
  104. for i, task in enumerate(task_list):
  105. raw_schema = task.get("output_schema")
  106. if raw_schema is None and len(task_list) == 1 and output_schema is not None:
  107. raw_schema = output_schema
  108. coerced_schema, schema_err = coerce_output_schema(raw_schema)
  109. if schema_err:
  110. return [], f"Task {i} output_schema invalid: {schema_err}"
  111. task_schemas.append(coerced_schema)
  112. return task_schemas, None
  113. # Per-task image ceiling: enough for screenshots/mocks while keeping the child's first request small.
  114. _MAX_TASK_IMAGES = 8
  115. def _normalize_task_images(task: dict, i: int) -> tuple[Optional[list[str]], Optional[str]]:
  116. """``(cleaned_list_or_None, None)`` for a task's optional ``images`` (local paths, http(s) or data: URLs), else
  117. ``(None, error)``. A bare string is wrapped into a one-entry list (small models emit scalars for arrays)."""
  118. raw = task.get("images")
  119. if raw is None:
  120. return None, None
  121. if isinstance(raw, str):
  122. raw = [raw]
  123. if not isinstance(raw, list):
  124. return None, f"Task {i} 'images' must be an array of local file paths or http(s) URLs."
  125. cleaned: list[str] = []
  126. for item in raw:
  127. if not isinstance(item, str) or not item.strip():
  128. return None, f"Task {i} 'images' entries must be non-empty strings (local file paths or http(s) URLs)."
  129. cleaned.append(item.strip())
  130. if len(cleaned) > _MAX_TASK_IMAGES:
  131. return None, (
  132. f"Task {i} has {len(cleaned)} images; the per-task limit is {_MAX_TASK_IMAGES}. "
  133. "Trim to the images the child actually needs to see."
  134. )
  135. return (cleaned or None), None
  136. def _coerce_task_images(
  137. task_list: list[dict[str, Any]], images: Optional[list[str]]
  138. ) -> tuple[list[Optional[list[str]]], Optional[str]]:
  139. """Per-task validated image lists; a malformed list fails the whole call before any child spawns. The legacy
  140. top-level ``images`` applies to a single task only, like ``output_schema``."""
  141. task_images: list[Optional[list[str]]] = []
  142. for i, task in enumerate(task_list):
  143. if task.get("images") is None and len(task_list) == 1 and images is not None:
  144. task = {**task, "images": images}
  145. cleaned, err = _normalize_task_images(task, i)
  146. if err:
  147. return [], err
  148. task_images.append(cleaned)
  149. return task_images, None