Source code for tooluniverse.problem_report_tool

"""The tool an agent reaches for when a tool looks broken.

It assembles a report and hands back a link. It has no network access at all:
an agent that files issues by itself turns an issue tracker into a log, and
nobody reads a log. The person decides.

The link it hands back by default is the *blank* issue form, and the report body
comes back beside it as text. That is not a missing feature. A prefilled form
carries its fields in the URL, so opening one sends the whole report -- the
failing arguments included -- to GitHub before anyone presses Submit. The
convenience is real, so the prefilled link is still available, but under a name
that says what it costs and only when the caller asks for it.

It also names the cause. Most failures are an upstream outage, a wall, a rate
limit, a missing key or a bad argument rather than a defect in our own code --
but the maintainers want to hear about almost all of them anyway (a dead
service may need delisting, an argument refused after following the description
means the description is ambiguous), so the verdict is a triage label, not a
gate. The one cause it withholds is a missing local credential, which only the
caller can fix.
"""

from __future__ import annotations

from typing import Any, Dict

from tooluniverse.base_tool import BaseTool
from tooluniverse.problem_report import (
    KINDS,
    REPO_URL,
    build_report,
    issue_url,
    prefilled_issue_url,
    render_markdown,
)
from tooluniverse.tool_registry import register_tool

_NEXT_STEP = {
    "upstream": (
        "This is the service, not ToolUniverse's own code. If it might be a "
        "momentary blip, retry once to confirm; if it keeps happening, it is "
        "worth telling the maintainers -- a tool whose service stays down may "
        "need delisting or a fallback, and they cannot see it from here. Show "
        "the person `report_body` -- that is the whole report, and showing it "
        "sends nothing -- with `report_url`, the blank issue form, for them to "
        "paste and submit. Meanwhile, use another source for the same fact."
    ),
    "configuration": (
        "This installation is missing a credential. Run the setup-keys skill or "
        "`tu status` to see which keys the loaded tools want, and where to put "
        "them. No issue needed."
    ),
    "caller": (
        "The arguments were rejected before anything went out. Read the tool's "
        "own parameter description (`tu info <tool>`) and call it again. If you "
        "followed that description and it still refused you, the description is "
        "ambiguous -- a ToolUniverse defect worth reporting: show the person "
        "`report_body` and `report_url`, the blank issue form, and say in "
        "`what_went_wrong` what the description led you to expect. If you can "
        "see your own slip, just fix the call -- no issue needed."
    ),
    "tooluniverse": (
        "This looks like a defect in ToolUniverse itself. Show the person "
        "`report_body` -- that is the whole report, and showing it sends "
        "nothing -- together with `report_url`, which is the blank issue form. "
        "They paste and submit. Do not open the form yourself. Only if they ask "
        "for the fields filled in for them, call this again with "
        "`prefill_url: true`, and tell them that opening that link hands the "
        "report to GitHub before they press Submit."
    ),
    "unknown": (
        "The cause could not be placed. If the tool's description promised what "
        "you asked for and you can repeat the failure, say so in "
        "`what_went_wrong` and call this again -- a described, repeatable "
        "failure is worth a person's attention even when the cause is unclear."
    ),
}

# A suggestion is feedback on ToolUniverse's own tool or skill, not a failure,
# so it does not get a whose-defect verdict -- it is routed on its own next
# step instead.
_SUGGESTION_NEXT_STEP = (
    "This is a suggestion for a ToolUniverse tool or skill, not a failure. Show "
    "the person `report_body` -- that is the whole suggestion, and showing it "
    "sends nothing -- with `report_url`, the blank issue form, for them to "
    "paste and submit. Describe the suggestion in `what_went_wrong`: with "
    "nothing written there, there is nothing to pass on."
)


[docs] @register_tool( "ProblemReportTool", config={ "name": "ToolUniverse_report_problem", "type": "ProblemReportTool", "category": "special_tools", "description": ( "Turn a tool or skill problem -- a failure, a wrong result, or a " "suggestion to improve one -- into a report a ToolUniverse " "maintainer can act on, and say first whether it is worth reporting " "at all. Name the tool that misbehaved (tool_name) OR the skill " "(skill_name), and for a failure pass the exact result it returned " "and the arguments you called it with; for a suggestion, say what " "could be better in what_went_wrong. Returns a verdict (is this " "ToolUniverse's defect, the service's, a missing API key, or your " "arguments?), what to do about it, and -- for anything a maintainer " "can act on, which is everything except a missing local API key -- " "the issue body plus a link to the blank issue form. A service " "outage and an argument the tool's own description misled you into " "are both worth reporting, not only defects in our code. " "THIS TOOL SENDS NOTHING AND THE LINK IT RETURNS CARRIES NOTHING: " "show the person the body and the form link so they can review it " "and paste it themselves. Set prefill_url if they want the fields " "filled in, and read that parameter's description first -- opening " "a prefilled link hands the report to GitHub before anyone presses " "Submit. Credentials, email addresses and home directories are " "masked before the report is assembled, and the count of masked " "values is returned. Offline, no API key, no network access." ), "parameter": { "type": "object", "properties": { "tool_name": { "type": "string", "description": ( "The tool that misbehaved, e.g. " "'UniProt_get_entry_by_accession'. Give this or " "skill_name." ), }, "skill_name": { "type": "string", "description": ( "The skill the feedback is about, e.g. " "'tooluniverse:literature-sweep', when the problem or " "suggestion is about a skill rather than a single tool. " "Give this or tool_name." ), }, "failure": { "type": "object", "description": ( "The result the tool returned, passed through unchanged. " "This is what the verdict is computed from, so pass it " "whenever you have it rather than retyping the message." ), }, "arguments": { "type": "object", "description": "The arguments the tool was called with, so the failure can be reproduced.", }, "what_went_wrong": { "type": "string", "description": ( "One or two sentences in your own words. Say what you " "asked for and what made the answer wrong -- especially " "for a result that came back as a success but is wrong, " "which nothing here can detect on its own." ), }, "expected": { "type": "string", "description": "What the tool's own description led you to expect instead.", }, "kind": { "type": "string", "description": ( "'tool_failure' (it errored), 'wrong_result' (it " "succeeded and the answer is wrong), " "'declaration_mismatch' (the reply does not match the " "declared return_schema), 'documentation' (the " "description or an example is wrong), 'suggestion' (it " "works but a tool or skill is awkward or could be " "better -- put the idea in what_went_wrong), or " "'other'." ), "enum": list(KINDS), "default": "tool_failure", }, "prefill_url": { "type": "boolean", "description": ( "Also return a GitHub issue link with the report " "already filled in. Off by default, and leave it off " "unless the person asked for it: a prefilled form " "carries its fields in the URL, so opening that link " "transmits the whole report -- including the arguments " "of the failing call, which may say what the person is " "researching -- to GitHub before they press Submit, and " "closing the tab does not take it back. The default " "link is the blank form and the body comes back as text, " "which is enough to paste and send." ), "default": False, }, }, "required": [], }, "return_schema": { "type": "object", "properties": { "status": {"type": "string", "enum": ["success", "error"]}, "error": {"type": "string"}, "data": { "type": "object", "properties": { "verdict": { "type": "string", "enum": [ "tooluniverse", "upstream", "caller", "configuration", "unknown", ], "description": "Whose defect this looks like.", }, "why": { "type": "string", "description": "The evidence the verdict was read from.", }, "worth_reporting": { "type": "boolean", "description": "True for every placed cause except a missing local credential; False for that and for an unplaceable failure.", }, "next_step": { "type": "string", "description": "What to do about it, which for most causes is how to report it.", }, "report_url": { "type": ["string", "null"], "description": ( "The BLANK GitHub issue form, for a person to " "paste 'report_body' into, or null when the " "failure is not ToolUniverse's to fix. It " "carries no part of the report, so opening it " "transmits nothing." ), }, "prefilled_report_url": { "type": ["string", "null"], "description": ( "The same form with the report filled in, " "returned only when 'prefill_url' was set. Null " "otherwise. Opening it sends the report to " "GitHub before anyone presses Submit, because " "the fields travel in the query string." ), }, "report_body": { "type": ["string", "null"], "description": "The issue body as markdown, redacted; null when there is nothing to report.", }, "fingerprint": { "type": "string", "description": ( "A short stable id for this failure shape. Two " "callers hitting one defect get the same " "fingerprint, so a maintainer can tell a repeat " "from a new report." ), }, "redactions": { "type": "integer", "description": "How many values were masked while assembling the report.", }, "report": { "type": "object", "description": ( "The full structured report. Channel-agnostic " "and versioned by 'schema_version'." ), }, }, "required": [ "verdict", "why", "worth_reporting", "next_step", "fingerprint", "redactions", ], }, }, "required": ["status"], }, }, ) class ProblemReportTool(BaseTool): """Classify a failure, assemble a redacted report, return a link. Sends nothing."""
[docs] def run(self, arguments: Dict[str, Any] | None = None) -> Dict[str, Any]: arguments = arguments or {} tool_name = arguments.get("tool_name") skill_name = arguments.get("skill_name") tool_name = tool_name.strip() if isinstance(tool_name, str) else "" skill_name = skill_name.strip() if isinstance(skill_name, str) else "" if not tool_name and not skill_name: return { "status": "error", "error": ( "name the subject: pass tool_name for the tool that " "misbehaved (e.g. 'UniProt_get_entry_by_accession') or " "skill_name for the skill the feedback is about." ), } failure = arguments.get("failure") what = arguments.get("what_went_wrong") or "" kind = arguments.get("kind") or "tool_failure" report = build_report( tool=tool_name, skill=skill_name, failure=failure, arguments=arguments.get("arguments"), what_went_wrong=what, expected=arguments.get("expected") or "", kind=kind, ) verdict = report["classification"]["verdict"] worth = bool(report["classification"]["worth_reporting"]) # A described, repeatable complaint is worth a person's attention even # when the classifier cannot place the cause -- and a wrong *successful* # answer leaves no error text at all, so nothing could ever place it. # The caller's own words are the only evidence in that case, so they are # what promotes it. if ( not worth and what.strip() and kind in { "wrong_result", "declaration_mismatch", "documentation", "suggestion", } ): worth = True report["classification"]["why"] = ( f"{report['classification']['why']}; promoted because the caller " f"described a {kind.replace('_', ' ')} in their own words" ) body = render_markdown(report) if worth else None # The default link is blank, so returning it costs the caller nothing # to show and the person nothing to open. The prefilled one is the # thing with a cost, and it is built only when asked for -- not merely # withheld from the return value, because a URL that exists tends to # get logged. url = issue_url() if worth else None prefilled = None if worth and arguments.get("prefill_url"): prefilled = prefilled_issue_url(report, body=body) return { "status": "success", "data": { "verdict": verdict, "why": report["classification"]["why"], "worth_reporting": worth, "next_step": ( _SUGGESTION_NEXT_STEP if kind == "suggestion" else _NEXT_STEP.get(verdict, _NEXT_STEP["unknown"]) ), "report_url": url, "prefilled_report_url": prefilled, "report_body": body, "fingerprint": report["fingerprint"], "redactions": report["redactions"], "report": report, "repository": REPO_URL, "sent": False, }, }