Merge branch 'worktree-agent-abc9e7b0a4e0a0f54'

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 20:05:02 +00:00
49 changed files with 3067 additions and 133 deletions
+1 -1
View File
@@ -75,7 +75,7 @@ func TestHandshakeListAndCall(t *testing.T) {
t.Fatalf("tool without description or schema: %v", entry)
}
}
if strings.Join(names, ",") != "show_file,show_image,show_html,show_diff,notify" {
if strings.Join(names, ",") != "show_file,show_image,show_html,show_diff,notify,show_run,schedule_followup" {
t.Fatalf("tools = %v", names)
}
if list["id"] != "l" {
+75 -6
View File
@@ -6,17 +6,35 @@ const (
ToolShowImage = "show_image"
ToolShowHTML = "show_html"
ToolShowDiff = "show_diff"
ToolShowRun = "show_run"
ToolNotify = "notify"
// ToolScheduleFollowup queues a prompt into the calling chat for later.
ToolScheduleFollowup = "schedule_followup"
)
// MaxHTMLBytes bounds one show_html document. A larger one is refused, never
// truncated: a cut document would render as something the model did not write.
const MaxHTMLBytes = 512 << 10
// The shown-file bounds: a file up to MaxSnapshotBytes is copied when shown,
// and a web image up to MaxFetchedImageBytes is fetched by Pipelines.
const (
MaxSnapshotBytes = 5 << 20
MaxFetchedImageBytes = 10 << 20
)
// The schedule_followup bounds.
const (
MinFollowupMinutes = 1
MaxFollowupMinutes = 7 * 24 * 60
MaxPendingFollowups = 3
MaxFollowupTextBytes = 8 << 10
)
// IsTool reports whether name is one of the Pipelines tools.
func IsTool(name string) bool {
switch name {
case ToolShowFile, ToolShowImage, ToolShowHTML, ToolShowDiff, ToolNotify:
case ToolShowFile, ToolShowImage, ToolShowHTML, ToolShowDiff, ToolShowRun, ToolNotify, ToolScheduleFollowup:
return true
}
return false
@@ -30,6 +48,8 @@ const Instructions = "These tools act on Pipelines, the app the operator reads t
"Pipelines already notifies them when your reply ends and when you ask a question or need an approval, so do not notify for those " +
"(a notify replaces this turn's end-of-reply notification). It reaches every Pipelines app they are signed in to and their phone, " +
"and clears everywhere once they open the chat. " +
"Use show_run to follow a pipeline run live in the chat, and schedule_followup to come back to this chat later (for example to check on a run). " +
"Every show_* result names the shown item's id: pass it as replace to update that card instead of adding another. " +
"Your thinking is not shown to the operator: before asking them a question (for example with AskUserQuestion), " +
"write what you found and why you are asking as a normal message, because the question form shows only the question and its options."
@@ -49,6 +69,12 @@ func objectSchema(required []string, properties map[string]any) map[string]any {
return map[string]any{"type": "object", "properties": properties, "required": required, "additionalProperties": false}
}
// replaceProperty is the optional id of an earlier shown item to update.
func replaceProperty() map[string]any {
return stringProperty("Optional id of an item you showed earlier in this chat (from that call's result): " +
"the card is updated in place instead of a new one being added. It must be the same kind of item.")
}
// definitions is the tools/list result, in a fixed order.
func definitions() []toolDefinition {
return []toolDefinition{
@@ -58,10 +84,13 @@ func definitions() []toolDefinition {
Description: "Show the operator an existing file from this machine directly in the Pipelines chat: images appear inline, " +
"documents (PDF, Markdown, text, CSV, HTML, and others) as a preview card they can tap to open or download. " +
"Use this instead of telling the operator a path to navigate to. The file must be inside the operator's home directory. " +
"A relative path is resolved against the chat's working directory.",
"A relative path is resolved against the chat's working directory. " +
"A file up to 5 MiB is copied when shown, so the card keeps showing that version; a larger one stays linked " +
"and the card says when it changed or no longer exists. The result names the item's id for replace.",
InputSchema: objectSchema([]string{"path"}, map[string]any{
"path": stringProperty("Absolute path, ~/path, or a path relative to the chat's working directory."),
"title": stringProperty("Optional short caption shown above the file."),
"path": stringProperty("Absolute path, ~/path, or a path relative to the chat's working directory."),
"title": stringProperty("Optional short caption shown above the file."),
"replace": replaceProperty(),
}),
Annotations: map[string]any{"readOnlyHint": true, "openWorldHint": false},
},
@@ -69,11 +98,14 @@ func definitions() []toolDefinition {
Name: ToolShowImage,
Title: "Show an image in the chat",
Description: "Show the operator an image inline in the Pipelines chat, from a file on this machine (inside the operator's home) " +
"or from an https:// URL. The URL is loaded by the operator's device without cookies.",
"or from an https:// URL. Pipelines downloads a URL itself (public https hosts only, at most 10 MiB, image types only) " +
"and keeps a copy, so the operator sees it even where the host refuses other sites; a failed download is reported " +
"to you and nothing is shown. The result names the item's id for replace.",
InputSchema: objectSchema([]string{"url_or_path"}, map[string]any{
"url_or_path": stringProperty("An https:// image URL, or a file path as for show_file."),
"title": stringProperty("Optional short caption."),
"alt": stringProperty("Optional description of the image for accessibility."),
"replace": replaceProperty(),
}),
Annotations: map[string]any{"readOnlyHint": true},
},
@@ -83,11 +115,15 @@ func definitions() []toolDefinition {
Description: "Render a self-contained HTML or SVG document inline in the Pipelines chat: diagrams, charts, tables, small visual views. " +
"It is shown in a sandbox with no scripts, no network and no access to the app: use inline CSS and inline SVG, " +
"<details> for expandable parts and CSS :hover for interaction; JavaScript does not run and external resources do not load. " +
"The operator can open it full screen. At most 512 KiB.",
"A document that supports dark mode follows the operator's light or dark theme: style for both with " +
"@media (prefers-color-scheme: dark), a color-scheme declaration or light-dark() colors (with color-scheme, " +
"unstyled text and the page use the theme's Canvas/CanvasText). A document without any of these is always shown light, on white. " +
"The operator can open it full screen. At most 512 KiB. The result names the item's id: pass it as replace to update the view.",
InputSchema: objectSchema([]string{"html"}, map[string]any{
"html": stringProperty("The complete HTML (or a bare <svg>) document."),
"title": stringProperty("Optional short caption."),
"height_hint": map[string]any{"type": "integer", "description": "Optional preferred inline height in logical pixels (120-600)."},
"replace": replaceProperty(),
}),
Annotations: map[string]any{"readOnlyHint": true, "openWorldHint": false},
},
@@ -100,12 +136,18 @@ func definitions() []toolDefinition {
"Pipelines runs Git itself, so do not paste the diff. Without base it shows uncommitted work (staged, unstaged and untracked files) against HEAD. " +
"With base (the pull request's target branch, such as main or origin/main) it shows everything since the branch left base, " +
"which is what the pull request would contain, plus uncommitted work unless committed_only is true. " +
"With head (a branch or commit, such as pipelines/run-123) it compares that revision instead of the working tree: " +
"against base when given (everything since head left base), else against HEAD. paths limits the diff to those files, " +
"directories or globs inside the repository. " +
"The repository must be inside the operator's home directory. Very large diffs are shown partially and say so.",
InputSchema: objectSchema([]string{}, map[string]any{
"path": stringProperty("Optional repository directory: absolute, ~/path, or relative to the chat's working directory. Defaults to the chat's working directory."),
"base": stringProperty("Optional branch, tag or commit to compare against, such as main or origin/main. Omit to show uncommitted changes."),
"head": stringProperty("Optional branch, tag or commit to show instead of the working tree, such as pipelines/run-123."),
"paths": map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": "Optional repository-relative files, directories or globs (such as internal/server or *.go) to limit the diff to; at most 50."},
"committed_only": map[string]any{"type": "boolean", "description": "Only with base: show the committed changes (base...HEAD) and leave out uncommitted work."},
"title": stringProperty("Optional short caption shown above the diff."),
"replace": replaceProperty(),
}),
Annotations: map[string]any{"readOnlyHint": true, "openWorldHint": false},
},
@@ -124,5 +166,32 @@ func definitions() []toolDefinition {
}),
Annotations: map[string]any{"readOnlyHint": false, "destructiveHint": false, "openWorldHint": true},
},
{
Name: ToolShowRun,
Title: "Show a pipeline run in the chat",
Description: "Show the operator a live card for a Pipelines run: its pipeline, title, status, current stage and duration, " +
"kept current as the run progresses; tapping it opens the run. Use it after starting a run, or when the operator asks about one. " +
"The result names the item's id for replace.",
InputSchema: objectSchema([]string{"run_id"}, map[string]any{
"run_id": map[string]any{"type": "integer", "description": "The run's numeric id (as in `pipelines status <id>` or pipelines/run-<id>)."},
"title": stringProperty("Optional short caption shown above the run."),
"replace": replaceProperty(),
}),
Annotations: map[string]any{"readOnlyHint": true, "openWorldHint": false},
},
{
Name: ToolScheduleFollowup,
Title: "Schedule a follow-up in this chat",
Description: "Queue a message to yourself in this chat, delivered as a new prompt at a later time (for example to check on a long run). " +
"The operator sees it in the chat as a scheduled message from the agent and can remove it. " +
"Give at (an RFC 3339 time) or after_minutes, between 1 minute and 7 days from now. At most 3 follow-ups can be pending per chat. " +
"It is delivered when the chat is free at that time, so do not wait for it in the current turn.",
InputSchema: objectSchema([]string{"message"}, map[string]any{
"message": stringProperty("What the follow-up prompt should say: enough for you to know what to do then."),
"at": stringProperty("When to deliver it, as an RFC 3339 time such as 2026-05-01T09:30:00Z. Give this or after_minutes."),
"after_minutes": map[string]any{"type": "integer", "description": "Minutes from now to deliver it (1-10080). Give this or at."},
}),
Annotations: map[string]any{"readOnlyHint": false, "destructiveHint": false, "openWorldHint": false},
},
}
}
+134
View File
@@ -0,0 +1,134 @@
package chattools
import (
"bytes"
"encoding/json"
"fmt"
"slices"
"sort"
"strings"
)
// ValidateArguments checks a call's arguments against the tool's declared
// input schema: a JSON object holding only declared fields, every required
// one, each of its declared type (and enum). The schemas promise
// additionalProperties false, so a misnamed field is refused with a message
// naming the expected ones rather than silently ignored.
func ValidateArguments(tool string, arguments json.RawMessage) error {
schema := toolSchema(tool)
if schema == nil {
return fmt.Errorf("unknown tool %s", tool)
}
properties, _ := schema["properties"].(map[string]any)
required, _ := schema["required"].([]string)
expected := expectedFields(properties, required)
var fields map[string]json.RawMessage
decoder := json.NewDecoder(bytes.NewReader(arguments))
if err := decoder.Decode(&fields); err != nil || fields == nil {
return fmt.Errorf("the arguments must be a JSON object; %s takes %s", tool, expected)
}
var unknown []string
for name := range fields {
if _, ok := properties[name]; !ok {
unknown = append(unknown, name)
}
}
if len(unknown) > 0 {
sort.Strings(unknown)
return fmt.Errorf("unknown %s %s; %s takes %s", plural(len(unknown), "argument", "arguments"), quoteAll(unknown), tool, expected)
}
for _, name := range required {
if raw, ok := fields[name]; !ok || string(raw) == "null" {
return fmt.Errorf("%s is required; %s takes %s", name, tool, expected)
}
}
for name, raw := range fields {
if string(raw) == "null" {
continue
}
property, _ := properties[name].(map[string]any)
if err := checkType(name, property, raw); err != nil {
return err
}
}
return nil
}
func toolSchema(tool string) map[string]any {
for _, definition := range definitions() {
if definition.Name == tool {
return definition.InputSchema
}
}
return nil
}
func checkType(name string, property map[string]any, raw json.RawMessage) error {
kind, _ := property["type"].(string)
switch kind {
case "string":
var text string
if json.Unmarshal(raw, &text) != nil {
return fmt.Errorf("%s must be a string", name)
}
if values, ok := property["enum"].([]string); ok && !slices.Contains(values, text) {
return fmt.Errorf("%s must be one of %s", name, strings.Join(values, ", "))
}
case "integer":
var number json.Number
decoder := json.NewDecoder(bytes.NewReader(raw))
decoder.UseNumber()
if bytes.HasPrefix(bytes.TrimSpace(raw), []byte(`"`)) || decoder.Decode(&number) != nil {
return fmt.Errorf("%s must be an integer", name)
}
if _, err := number.Int64(); err != nil {
return fmt.Errorf("%s must be an integer", name)
}
case "boolean":
var flag bool
if json.Unmarshal(raw, &flag) != nil {
return fmt.Errorf("%s must be true or false", name)
}
case "array":
var list []string
if json.Unmarshal(raw, &list) != nil {
return fmt.Errorf("%s must be an array of strings", name)
}
}
return nil
}
// expectedFields names a tool's fields, required ones first and marked.
func expectedFields(properties map[string]any, required []string) string {
var names []string
for _, name := range required {
names = append(names, name+" (required)")
}
var optional []string
for name := range properties {
if !slices.Contains(required, name) {
optional = append(optional, name)
}
}
sort.Strings(optional)
names = append(names, optional...)
if len(names) == 0 {
return "no arguments"
}
return strings.Join(names, ", ")
}
func quoteAll(names []string) string {
quoted := make([]string, len(names))
for i, name := range names {
quoted[i] = fmt.Sprintf("%q", name)
}
return strings.Join(quoted, ", ")
}
func plural(n int, one, many string) string {
if n == 1 {
return one
}
return many
}
+51
View File
@@ -0,0 +1,51 @@
package chattools
import (
"encoding/json"
"strings"
"testing"
)
func TestValidateArgumentsRefusesUnknownAndMistypedFields(t *testing.T) {
cases := []struct {
tool, args, want string
}{
{ToolShowFile, `{"path":"a.txt"}`, ""},
{ToolShowFile, `{"path":"a.txt","replace":"shown_1","title":"x"}`, ""},
{ToolShowFile, `{"file":"a.txt"}`, `unknown argument "file"; show_file takes path (required), replace, title`},
{ToolShowFile, `{}`, "path is required"},
{ToolShowFile, `{"path":3}`, "path must be a string"},
{ToolShowFile, `[1]`, "must be a JSON object"},
{ToolShowImage, `{"url":"https://x/y.png"}`, `unknown argument "url"; show_image takes url_or_path (required), alt, replace, title`},
{ToolShowHTML, `{"html":"<p>","height_hint":"200"}`, "height_hint must be an integer"},
{ToolShowHTML, `{"html":"<p>","height_hint":200.5}`, "height_hint must be an integer"},
{ToolShowDiff, `{"paths":"a.go"}`, "paths must be an array of strings"},
{ToolShowDiff, `{"paths":["a.go"],"head":"main","committed_only":true}`, ""},
{ToolShowDiff, `{"committed_only":"yes"}`, "committed_only must be true or false"},
{ToolShowRun, `{"run_id":12}`, ""},
{ToolShowRun, `{"id":12}`, `unknown argument "id"`},
{ToolNotify, `{"title":"t","message":"m","priority":"urgent"}`, "priority must be one of low, default, high"},
{ToolScheduleFollowup, `{"message":"m","after_minutes":5}`, ""},
{ToolScheduleFollowup, `{"message":"m","in_minutes":5,"delay":1}`, `unknown arguments "delay", "in_minutes"`},
}
for _, c := range cases {
err := ValidateArguments(c.tool, json.RawMessage(c.args))
switch {
case c.want == "" && err != nil:
t.Errorf("%s %s: unexpected %v", c.tool, c.args, err)
case c.want != "" && (err == nil || !strings.Contains(err.Error(), c.want)):
t.Errorf("%s %s: got %v, want %q", c.tool, c.args, err, c.want)
}
}
}
func TestEveryListedToolIsATool(t *testing.T) {
for _, definition := range definitions() {
if !IsTool(definition.Name) {
t.Errorf("%s is listed but not a tool", definition.Name)
}
if definition.InputSchema["additionalProperties"] != false {
t.Errorf("%s schema allows additional properties", definition.Name)
}
}
}