// Package renderpage is render_page on the runner (spec section 3, // "Contract"; plan task 10): the strict input, the runner-wide one // render at a time, the per-step limits, the sites exported and scanned // before any render, each capture decoded and re-encoded by the runner, // compare's diffs, and the text reply with every page string quoted // under a "from the rendered page" label. // // The running step (its counters, its sites, its render directory) is // the Step the caller passes; thin implements it. package renderpage import ( "bytes" "encoding/json" "errors" "fmt" "io" "path" "strings" "amadan.net/oficina/equipo/internal/runner/render" ) // Trees a call renders. const ( TreeBase = "base" TreeWork = "work" // TreeBoth is a compare request's, never an input: base, then work. TreeBoth = "both" ) // maxPath bounds a page's path, its query included. const maxPath = 1024 // Request is one call's input, checked. type Request struct { // Tree is TreeBase or TreeWork, or TreeBoth with Compare (whatever // tree the input named). Tree string Pages []render.Page // Compare renders both trees and pairs their captures. Compare bool } // trees are the trees the request renders, in order. func (r Request) trees() []string { if r.Compare { return []string{TreeBase, TreeWork} } return []string{r.Tree} } // errInput is an input render_page refuses: not one of the fixed codes, // a tool call the model made wrongly. var errInput = errors.New("render_page: invalid input") func invalid(format string, args ...any) error { return fmt.Errorf("%w: %s", errInput, fmt.Sprintf(format, args...)) } // Parse reads render_page's input strictly: one JSON object of exactly // tree, pages (1 to render.MaxPages, each path, viewport and full_page) // and compare. tree is base or work (spec section 3), required without // compare; compare renders both trees, with either tree named or none. // Unknown fields, trailing data, a tree other than base or work, a // viewport outside the fixed set, a path that is not a clean absolute // site path and two pages with the same path and viewport are refused. func Parse(raw []byte) (Request, error) { var in struct { Tree *string `json:"tree"` Pages []struct { Path *string `json:"path"` Viewport *string `json:"viewport"` FullPage *bool `json:"full_page"` } `json:"pages"` Compare *bool `json:"compare"` } d := json.NewDecoder(bytes.NewReader(raw)) d.DisallowUnknownFields() if err := d.Decode(&in); err != nil { return Request{}, invalid("%v", err) } if _, err := d.Token(); err != io.EOF { return Request{}, invalid("data after the object") } var r Request r.Compare = in.Compare != nil && *in.Compare switch { case in.Tree != nil && *in.Tree != TreeBase && *in.Tree != TreeWork: return Request{}, invalid("tree %q is not base or work", *in.Tree) case r.Compare: // The spec's compare renders both trees, whichever tree is // named, or with none. r.Tree = TreeBoth case in.Tree == nil: return Request{}, invalid("tree is base or work") default: r.Tree = *in.Tree } if len(in.Pages) == 0 || len(in.Pages) > render.MaxPages { return Request{}, invalid("%d pages: 1 to %d", len(in.Pages), render.MaxPages) } type key struct { path string vp render.Viewport } seen := map[key]bool{} for i, p := range in.Pages { if p.Path == nil || p.Viewport == nil { return Request{}, invalid("page %d needs a path and a viewport", i+1) } page := render.Page{Path: *p.Path, Viewport: render.Viewport(*p.Viewport), FullPage: p.FullPage != nil && *p.FullPage} if _, _, ok := page.Viewport.Size(); !ok { return Request{}, invalid("page %d: viewport %q is not phone, tablet or desktop", i+1, *p.Viewport) } if !SitePath(page.Path) { return Request{}, invalid("page %d: %q is not a clean site path such as /about/", i+1, page.Path) } k := key{page.Path, page.Viewport} if seen[k] { return Request{}, invalid("page %d repeats %s at %s", i+1, page.Path, page.Viewport) } seen[k] = true r.Pages = append(r.Pages, page) } return r, nil } // SitePath is a page path render_page accepts: absolute and clean (a // trailing slash kept for a directory's index), printable ASCII without // space, backslash or fragment, no percent-encoded dot, slash or // backslash, and at most maxPath bytes with an optional query. func SitePath(p string) bool { if len(p) == 0 || len(p) > maxPath { return false } for i := 0; i < len(p); i++ { if c := p[i]; c <= ' ' || c >= 0x7f || c == '\\' || c == '#' { return false } } name, _, _ := strings.Cut(p, "?") lower := strings.ToLower(name) if strings.Contains(lower, "%2e") || strings.Contains(lower, "%2f") || strings.Contains(lower, "%5c") { return false } if name == "" || name[0] != '/' { return false } clean := path.Clean(name) return name == clean || name == clean+"/" && clean != "/" }