oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package renderpage is render_page on the runner (spec section 3,
2// "Contract"; plan task 10): the strict input, the runner-wide one
3// render at a time, the per-step limits, the sites exported and scanned
4// before any render, each capture decoded and re-encoded by the runner,
5// compare's diffs, and the text reply with every page string quoted
6// under a "from the rendered page" label.
7//
8// The running step (its counters, its sites, its render directory) is
9// the Step the caller passes; thin implements it.
10package renderpage
11
12import (
13 "bytes"
14 "encoding/json"
15 "errors"
16 "fmt"
17 "io"
18 "path"
19 "strings"
20
21 "amadan.net/oficina/equipo/internal/runner/render"
22)
23
24// Trees a call renders.
25const (
26 TreeBase = "base"
27 TreeWork = "work"
28 // TreeBoth is a compare request's, never an input: base, then work.
29 TreeBoth = "both"
30)
31
32// maxPath bounds a page's path, its query included.
33const maxPath = 1024
34
35// Request is one call's input, checked.
36type Request struct {
37 // Tree is TreeBase or TreeWork, or TreeBoth with Compare (whatever
38 // tree the input named).
39 Tree string
40 Pages []render.Page
41 // Compare renders both trees and pairs their captures.
42 Compare bool
43}
44
45// trees are the trees the request renders, in order.
46func (r Request) trees() []string {
47 if r.Compare {
48 return []string{TreeBase, TreeWork}
49 }
50 return []string{r.Tree}
51}
52
53// errInput is an input render_page refuses: not one of the fixed codes,
54// a tool call the model made wrongly.
55var errInput = errors.New("render_page: invalid input")
56
57func invalid(format string, args ...any) error {
58 return fmt.Errorf("%w: %s", errInput, fmt.Sprintf(format, args...))
59}
60
61// Parse reads render_page's input strictly: one JSON object of exactly
62// tree, pages (1 to render.MaxPages, each path, viewport and full_page)
63// and compare. tree is base or work (spec section 3), required without
64// compare; compare renders both trees, with either tree named or none.
65// Unknown fields, trailing data, a tree other than base or work, a
66// viewport outside the fixed set, a path that is not a clean absolute
67// site path and two pages with the same path and viewport are refused.
68func Parse(raw []byte) (Request, error) {
69 var in struct {
70 Tree *string `json:"tree"`
71 Pages []struct {
72 Path *string `json:"path"`
73 Viewport *string `json:"viewport"`
74 FullPage *bool `json:"full_page"`
75 } `json:"pages"`
76 Compare *bool `json:"compare"`
77 }
78 d := json.NewDecoder(bytes.NewReader(raw))
79 d.DisallowUnknownFields()
80 if err := d.Decode(&in); err != nil {
81 return Request{}, invalid("%v", err)
82 }
83 if _, err := d.Token(); err != io.EOF {
84 return Request{}, invalid("data after the object")
85 }
86 var r Request
87 r.Compare = in.Compare != nil && *in.Compare
88 switch {
89 case in.Tree != nil && *in.Tree != TreeBase && *in.Tree != TreeWork:
90 return Request{}, invalid("tree %q is not base or work", *in.Tree)
91 case r.Compare:
92 // The spec's compare renders both trees, whichever tree is
93 // named, or with none.
94 r.Tree = TreeBoth
95 case in.Tree == nil:
96 return Request{}, invalid("tree is base or work")
97 default:
98 r.Tree = *in.Tree
99 }
100 if len(in.Pages) == 0 || len(in.Pages) > render.MaxPages {
101 return Request{}, invalid("%d pages: 1 to %d", len(in.Pages), render.MaxPages)
102 }
103 type key struct {
104 path string
105 vp render.Viewport
106 }
107 seen := map[key]bool{}
108 for i, p := range in.Pages {
109 if p.Path == nil || p.Viewport == nil {
110 return Request{}, invalid("page %d needs a path and a viewport", i+1)
111 }
112 page := render.Page{Path: *p.Path, Viewport: render.Viewport(*p.Viewport), FullPage: p.FullPage != nil && *p.FullPage}
113 if _, _, ok := page.Viewport.Size(); !ok {
114 return Request{}, invalid("page %d: viewport %q is not phone, tablet or desktop", i+1, *p.Viewport)
115 }
116 if !SitePath(page.Path) {
117 return Request{}, invalid("page %d: %q is not a clean site path such as /about/", i+1, page.Path)
118 }
119 k := key{page.Path, page.Viewport}
120 if seen[k] {
121 return Request{}, invalid("page %d repeats %s at %s", i+1, page.Path, page.Viewport)
122 }
123 seen[k] = true
124 r.Pages = append(r.Pages, page)
125 }
126 return r, nil
127}
128
129// SitePath is a page path render_page accepts: absolute and clean (a
130// trailing slash kept for a directory's index), printable ASCII without
131// space, backslash or fragment, no percent-encoded dot, slash or
132// backslash, and at most maxPath bytes with an optional query.
133func SitePath(p string) bool {
134 if len(p) == 0 || len(p) > maxPath {
135 return false
136 }
137 for i := 0; i < len(p); i++ {
138 if c := p[i]; c <= ' ' || c >= 0x7f || c == '\\' || c == '#' {
139 return false
140 }
141 }
142 name, _, _ := strings.Cut(p, "?")
143 lower := strings.ToLower(name)
144 if strings.Contains(lower, "%2e") || strings.Contains(lower, "%2f") || strings.Contains(lower, "%5c") {
145 return false
146 }
147 if name == "" || name[0] != '/' {
148 return false
149 }
150 clean := path.Clean(name)
151 return name == clean || name == clean+"/" && clean != "/"
152}
153