ff660354f3
hermes_cli/kanban.py 3,565 -> 2,912; kanban_diagnostics 1,216 -> 996; kanban_decompose 468 -> 393; kanban_transfer 478 -> 443; kanban_specify 264 -> 229; kanban_swarm 390 -> 378; active_sessions 871 -> 775. `hermes kanban [sub] --help` byte-identical for all 55 parsers. - kanban.py: _err / _print_json / _json_out / _fmt_counts / _bulk_apply / _obj_dict field tuples replace repeated print/JSON/exit-code blocks; action and board subcommand routing via dict dispatch; shared run-state and triage-sweep argparse blocks; argparse declarations re-packed (AST-identical). - specify/decompose: one _run_triage_sweep driver, shared _extract_json_blob / _truncate / _profile_author / _title_body / _resolve_profile_from_cfg. - diagnostics: rule helpers (_first_field / _latest_event_ts / _log_hint_action / _error_snippet), _rows_by_task fleet fetch; unreferenced DIAGNOSTIC_KINDS dropped. - swarm: graph nodes share one create_task kwarg set. - active_sessions: one _flock per platform, _pid_alive via _pid_liveness, shared _read_live_entries / _without_lease / _clean_metadata, table-driven strict registry validation. - Docstrings/comments hand-compacted (AST-identical), invariants kept.
230 lines
7.3 KiB
Python
230 lines
7.3 KiB
Python
"""Kanban triage specifier — flesh out a one-liner into a real spec.
|
|
|
|
``hermes kanban specify [task_id | --all]`` asks the auxiliary LLM for a
|
|
tightened title + concrete body for a Triage task, then flips it
|
|
``triage -> todo`` via ``kanban_db.specify_triage_task``.
|
|
|
|
Mirrors ``hermes_cli/goals.py``: same aux-client pattern, same "empty config
|
|
=> skip, don't crash" tolerance. One shot, no retry loop. JSON mode is not
|
|
requested (works on providers without it); the parse is lenient and falls
|
|
back to "whole reply is the body" so a malformed reply never strands a task.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import os
|
|
import re
|
|
from dataclasses import dataclass
|
|
from typing import Optional
|
|
|
|
from hermes_cli import kanban_db as kb
|
|
|
|
from utils import env_int
|
|
|
|
HERMES_KANBAN_SPECIFY_MAX_TOKENS = max(
|
|
1500,
|
|
env_int("HERMES_KANBAN_SPECIFY_MAX_TOKENS", 6000),
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
_SYSTEM_PROMPT = """You are the Kanban triage specifier for the Hermes Agent board.
|
|
A user dropped a rough idea into the Triage column. Your job is to turn it
|
|
into a concrete, actionable task spec that an autonomous worker can pick up
|
|
and execute without further clarification.
|
|
|
|
Output a single JSON object with exactly two keys:
|
|
|
|
{
|
|
"title": "<tightened task title, <= 80 chars, imperative voice>",
|
|
"body": "<multi-line spec, see structure below>"
|
|
}
|
|
|
|
The body MUST include these sections, each prefixed with a bold markdown
|
|
heading, in this order:
|
|
|
|
**Goal** — one sentence, user-facing outcome.
|
|
**Approach** — 2-5 bullets on how a worker should tackle it.
|
|
**Acceptance criteria** — checklist of concrete, verifiable conditions.
|
|
**Out of scope** — short list of things NOT to touch (omit if nothing
|
|
obvious; never invent scope creep).
|
|
|
|
Rules:
|
|
- Keep the tightened title close in meaning to the original idea — do
|
|
NOT invent a different project.
|
|
- If the original idea is already detailed, preserve its substance and
|
|
just reformat into the sections above.
|
|
- Never add invented requirements the user didn't hint at.
|
|
- No preamble, no closing remarks, no code fences around the JSON.
|
|
- Output only the JSON object and nothing else.
|
|
"""
|
|
|
|
|
|
_USER_TEMPLATE = """Task id: {task_id}
|
|
Current title: {title}
|
|
Current body:
|
|
{body}
|
|
"""
|
|
|
|
|
|
@dataclass
|
|
class SpecifyOutcome:
|
|
"""Result of specifying a single triage task."""
|
|
|
|
task_id: str
|
|
ok: bool
|
|
reason: str = ""
|
|
new_title: Optional[str] = None
|
|
|
|
|
|
def _truncate(text: str, limit: int) -> str:
|
|
if len(text) <= limit:
|
|
return text
|
|
return text[: limit - 1] + "…"
|
|
|
|
|
|
_FENCE_RE = re.compile(r"^\s*```(?:json)?\s*|\s*```\s*$", re.IGNORECASE)
|
|
|
|
|
|
def _extract_json_blob(raw: str, fence_re: re.Pattern = _FENCE_RE) -> Optional[dict]:
|
|
"""Lenient JSON object extraction: strip code fences, take the first ``{``
|
|
to the last ``}``. None if nothing parses to a dict."""
|
|
if not raw:
|
|
return None
|
|
stripped = fence_re.sub("", raw.strip())
|
|
first = stripped.find("{")
|
|
last = stripped.rfind("}")
|
|
if first == -1 or last == -1 or last <= first:
|
|
return None
|
|
candidate = stripped[first : last + 1]
|
|
try:
|
|
val = json.loads(candidate)
|
|
except (ValueError, json.JSONDecodeError):
|
|
return None
|
|
return val if isinstance(val, dict) else None
|
|
|
|
|
|
def _nonblank(v) -> Optional[str]:
|
|
return v if isinstance(v, str) and v.strip() else None
|
|
|
|
|
|
def _title_body(parsed: dict) -> tuple[Optional[str], Optional[str]]:
|
|
"""``(title, body)`` from an LLM reply: title stripped, body verbatim,
|
|
either None when missing/blank."""
|
|
title = _nonblank(parsed.get("title"))
|
|
return (title.strip() if title else None), _nonblank(parsed.get("body"))
|
|
|
|
|
|
def _profile_author(default: str = "specifier") -> str:
|
|
"""Mirror of ``hermes_cli.kanban._profile_author``. Kept local to
|
|
avoid a circular import when kanban.py imports this module."""
|
|
return os.environ.get("HERMES_PROFILE") or os.environ.get("USER") or default
|
|
|
|
|
|
def specify_task(
|
|
task_id: str,
|
|
*,
|
|
author: Optional[str] = None,
|
|
timeout: Optional[int] = None,
|
|
) -> SpecifyOutcome:
|
|
"""Specify one triage task and promote it to ``todo``. Expected failures
|
|
(not in triage, no aux client, API error, malformed reply) surface as
|
|
``ok=False`` so an ``--all`` sweep continues."""
|
|
with kb.connect_closing() as conn:
|
|
task = kb.get_task(conn, task_id)
|
|
if task is None:
|
|
return SpecifyOutcome(task_id, False, "unknown task id")
|
|
if task.status != "triage":
|
|
return SpecifyOutcome(
|
|
task_id, False, f"task is not in triage (status={task.status!r})"
|
|
)
|
|
|
|
try:
|
|
from agent.auxiliary_client import call_llm
|
|
except Exception as exc: # pragma: no cover — import smoke test
|
|
logger.debug("specify: auxiliary client import failed: %s", exc)
|
|
return SpecifyOutcome(task_id, False, "auxiliary client unavailable")
|
|
|
|
user_msg = _USER_TEMPLATE.format(
|
|
task_id=task.id,
|
|
title=_truncate(task.title or "", 400),
|
|
body=_truncate(task.body or "(no body)", 4000),
|
|
)
|
|
|
|
try:
|
|
# call_llm applies all auxiliary.triage_specifier.* config
|
|
# (provider/model/base_url, extra_body, reasoning_effort, retries).
|
|
resp = call_llm(
|
|
task="triage_specifier",
|
|
messages=[
|
|
{"role": "system", "content": _SYSTEM_PROMPT},
|
|
{"role": "user", "content": user_msg},
|
|
],
|
|
temperature=0.3,
|
|
max_tokens=HERMES_KANBAN_SPECIFY_MAX_TOKENS,
|
|
timeout=timeout or 120,
|
|
)
|
|
except Exception as exc:
|
|
logger.info(
|
|
"specify: API call failed for %s (%s) — skipping",
|
|
task_id, exc,
|
|
)
|
|
return SpecifyOutcome(
|
|
task_id, False, f"LLM error: {type(exc).__name__}"
|
|
)
|
|
|
|
try:
|
|
raw = (resp.choices[0].message.content or "").strip()
|
|
except Exception:
|
|
raw = ""
|
|
|
|
parsed = _extract_json_blob(raw)
|
|
|
|
new_title: Optional[str]
|
|
new_body: Optional[str]
|
|
if parsed is None:
|
|
# Whole reply becomes the body; the user can edit afterward.
|
|
stripped_raw = raw.strip()
|
|
if not stripped_raw:
|
|
return SpecifyOutcome(
|
|
task_id, False, "LLM returned an empty response"
|
|
)
|
|
new_title = None
|
|
new_body = stripped_raw
|
|
else:
|
|
new_title, new_body = _title_body(parsed)
|
|
if new_body is None and new_title is None:
|
|
return SpecifyOutcome(
|
|
task_id, False, "LLM response missing title and body"
|
|
)
|
|
|
|
with kb.connect_closing() as conn:
|
|
ok = kb.specify_triage_task(
|
|
conn,
|
|
task_id,
|
|
title=new_title,
|
|
body=new_body,
|
|
author=author or _profile_author(),
|
|
)
|
|
if not ok:
|
|
# Race: promoted/archived between our read and the write.
|
|
return SpecifyOutcome(
|
|
task_id, False, "task moved out of triage before promotion"
|
|
)
|
|
return SpecifyOutcome(task_id, True, "specified", new_title=new_title)
|
|
|
|
|
|
def list_triage_ids(*, tenant: Optional[str] = None) -> list[str]:
|
|
"""Task ids in the triage column; ``tenant`` narrows the sweep."""
|
|
with kb.connect_closing() as conn:
|
|
tasks = kb.list_tasks(
|
|
conn,
|
|
status="triage",
|
|
tenant=tenant,
|
|
include_archived=False,
|
|
)
|
|
return [t.id for t in tasks]
|