orchard
mirrorEvery site I host, in one repo, along with the Cloudflare Tunnel and Caddy that front them. It's all Go, Vite, and SQLite, and it runs on a desktop at home with nothing listening on an inbound port.
blogbuncaddycloudflare-tunneldockergogolanghomelabhtml-templatemonorepoself-hostedseosqlitestatic-sitetypstuptime-monitoringviteweb-analytics
1package main
2
3// The two things the contract asks for and does not get.
4//
5// Both are repaired in Go rather than asked for again, for cite.go's reason:
6// the model writes what it writes, and a rule it has already been given and
7// ignored is not worth a second model call. Neither of these changes what an
8// answer says, only what is left on the end of it.
9
10import (
11 "regexp"
12 "strings"
13)
14
15// A closing offer of more help. Isaac's stored preference says not to write
16// one, the contract repeats it and the final turn repeats it again, and four
17// answers on 2026-09-08 still ended on "Want me to...?".
18//
19// Every pattern needs a first person subject or an imperative aimed at him, so
20// a genuine question about what he meant survives. "Which of the two did you
21// mean?" is the turn needing an answer to continue and is not an offer.
22var closingOffers = []*regexp.Regexp{
23 regexp.MustCompile(`(?i)^(want|would you like|do you want|shall i|should i)\b.*\?$`),
24 regexp.MustCompile(`(?i)^(if you want|if you'?d like|if that helps|let me know)\b.*[.?!]$`),
25 regexp.MustCompile(`(?i)^i can\b.*\bif you\b.*[.?!]$`),
26 regexp.MustCompile(`(?i)^(just )?(say|tell me) (the )?(which|what|when)\b.*\band i'?ll\b.*[.?!]$`),
27}
28
29// dropClosingOffer removes an offer of further help from the end of an answer.
30// Only from the end, and only when something else is left: an answer that is
31// nothing but an offer had a reason to be, and the gate is what deals with it.
32func dropClosingOffer(text string) string {
33 lines := strings.Split(text, "\n")
34 for i := len(lines) - 1; i >= 0; i-- {
35 t := strings.TrimSpace(lines[i])
36 if t == "" {
37 continue
38 }
39 // A list item or a heading is structure rather than a sign off, and a
40 // fence is code.
41 if strings.HasPrefix(t, "#") || strings.HasPrefix(t, ">") ||
42 strings.HasPrefix(t, "|") || strings.HasPrefix(t, "```") {
43 return text
44 }
45 if _, ok := listItem(t); ok {
46 return text
47 }
48 kept := withoutOffer(t)
49 if kept == t {
50 return text
51 }
52 if strings.TrimSpace(strings.Join(lines[:i], "")) == "" && kept == "" {
53 // The offer was the whole answer. Leave it: an empty reply is worse
54 // than one that asks a question.
55 return text
56 }
57 if kept == "" {
58 lines = lines[:i]
59 } else {
60 lines[i] = kept
61 }
62 return strings.TrimRight(strings.Join(lines, "\n"), "\n ")
63 }
64 return text
65}
66
67// withoutOffer drops the trailing sentences of one line that are offers.
68func withoutOffer(line string) string {
69 parts := sentences(line)
70 cut := len(parts)
71 for i := len(parts) - 1; i >= 0; i-- {
72 s := strings.TrimSpace(stripEmphasis(parts[i]))
73 matched := false
74 for _, re := range closingOffers {
75 if re.MatchString(s) {
76 matched = true
77 break
78 }
79 }
80 if !matched {
81 break
82 }
83 cut = i
84 }
85 if cut == len(parts) {
86 return line
87 }
88 return strings.TrimSpace(strings.Join(parts[:cut], " "))
89}
90
91func stripEmphasis(s string) string { return strings.Trim(strings.TrimSpace(s), "*_ ") }
92
93// A bracketed label the model wrote where a citation number goes. It picked the
94// shape up from the numbered sources and filled it with a name, which lands in
95// the answer as "The S&P 500 is down 26.05 to 7692.55 [S&P 500]." and links to
96// nothing.
97//
98// RE2 has no lookahead, so a markdown link cannot be excluded in the pattern
99// and the character after the match is checked instead.
100var labelMark = regexp.MustCompile(`\s*\[[^\]\n]{1,40}\]`)
101
102// dropLabelMarks removes those, outside code and outside a fence, and leaves
103// anything that is really markdown alone.
104func dropLabelMarks(text string) string {
105 lines := strings.Split(text, "\n")
106 fenced := false
107 for i, line := range lines {
108 t := strings.TrimSpace(line)
109 if strings.HasPrefix(t, "```") || strings.HasPrefix(t, "~~~") {
110 fenced = !fenced
111 continue
112 }
113 if fenced {
114 continue
115 }
116 lines[i] = eachOutsideCode(line, dropLabelsIn)
117 }
118 return strings.Join(lines, "\n")
119}
120
121func dropLabelsIn(part string) string {
122 found := labelMark.FindAllStringIndex(part, -1)
123 if found == nil {
124 return part
125 }
126 var b strings.Builder
127 last := 0
128 for _, loc := range found {
129 start, end := loc[0], loc[1]
130 // A markdown link is the same shape followed by its address.
131 if end < len(part) && part[end] == '(' {
132 continue
133 }
134 inner := strings.TrimSpace(strings.Trim(strings.TrimSpace(part[start:end]), "[]"))
135 // A citation is a number and stays. So does a task list box and a
136 // footnote reference, which are markdown a reader wrote.
137 if inner == "" || isAllDigits(inner) || inner == "x" || inner == "X" ||
138 strings.HasPrefix(inner, "^") {
139 continue
140 }
141 b.WriteString(part[last:start])
142 last = end
143 }
144 if last == 0 {
145 return part
146 }
147 b.WriteString(part[last:])
148 return b.String()
149}
150
151func isAllDigits(s string) bool {
152 for _, r := range s {
153 if r < '0' || r > '9' {
154 return false
155 }
156 }
157 return s != ""
158}