chat: an escape-heavy direction never stops Build or More like this
Task 7 review, fix round 1. choiceBody clipped the direction by subtracting the formatted body's excess from the raw direction's rune count, but quoting doubles quotes and backslashes (and spells some characters as \U escapes), so a valid 250-byte direction of quotes and backslashes ran the count below zero: choiceBody failed, writeChoice returned ErrInvalid and the answer rolled back. Now the direction is cut to the longest prefix whose fully formatted, quoted body fits (sort.Search: a longer prefix never quotes shorter), ended with an ellipsis. When no prefix fits beside the name, the note falls back to the name-only words, so a valid option never makes the answer fail; only missing copy still refuses it. TDD evidence Command: go test ./internal/equipo/chat/ -run 'TestEscapeHeavyDirectionAnswers|TestChoiceBodyTotal' Failure seen before the fix: TestEscapeHeavyDirectionAnswers "build: chat: invalid" (a 250-byte direction of quotes and backslashes); TestChoiceBodyTotal "quotes: false, 0 characters". A first version of the answer-path test, with a lighter direction (4 escapes in 11 bytes), passed against the old code and was made heavier. After: chat package and internal/equipo -run 'TestChoiceNoteCopy|TestBuildOptionCopy' pass on a clean export of HEAD plus these files; go vet clean. Mutations (each broken, test failed, restored): - search result not less one (first prefix that does not fit): BodyFits, EscapeHeavyDirectionAnswers, BodyTotal fail - no name-only fallback after a direction: BodyTotal fails - half the longest prefix: BodyFits, BodyTotal fail - no ellipsis on a cut: BodyFits, EscapeHeavyDirectionAnswers, BodyTotal fail - fits ignores the bound: BodyFits, EscapeHeavyDirectionAnswers, BodyTotal fail - the n > 0 guard in the search predicate was dropped as redundant (n = 0 falls back to the name either way)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+103
−18
internal/equipo/chat/taste_choices.go+22 −18internal/equipo/chat/taste_choices_test.go+81 −0
diff --git a/internal/equipo/chat/taste_choices.go b/internal/equipo/chat/taste_choices.go| index 8f307b78..c6ca25d4 100644 |
| --- a/internal/equipo/chat/taste_choices.go |
| +++ b/internal/equipo/chat/taste_choices.go |
| @@ -1,6 +1,7 @@ |
| package chat |
| import ( |
| + "sort" |
| "strings" |
| "unicode/utf8" |
| @@ -35,10 +36,11 @@ func (t *txn) writeChoice(person string, k taskRecord, card, choice string, o wi |
| // what the person did, with the option's name and its direction quoted |
| // as Design's words (wire.QuotedData), so nothing reads them as the |
| // person's. The direction is folded onto one line and, when the body |
| -// would pass wire.MaxTasteBox characters, clipped and ended with an |
| -// ellipsis; the name never is. An option with no direction (before |
| -// protocol 39) is named alone. ok is false for any other answer, with no |
| -// words for it, or when nothing fits. |
| +// would pass wire.MaxTasteBox characters, cut to the longest prefix |
| +// whose quoted body fits (quoting can double a character or more), ended |
| +// with an ellipsis; the name never is cut. An option with no direction |
| +// (before protocol 39), or none that fits beside its name, is named |
| +// alone. ok is false for any other answer, or with no words for it. |
| func (t *txn) choiceBody(choice string, o wire.StepDesignOption) (string, bool) { |
| key, ok := choiceKeys[choice] |
| if !ok { |
| @@ -46,25 +48,27 @@ func (t *txn) choiceBody(choice string, o wire.StepDesignOption) (string, bool) |
| } |
| name := wire.QuotedData(o.Name) |
| fits := func(body string) bool { return utf8.RuneCountInString(strings.TrimSpace(body)) <= wire.MaxTasteBox } |
| - dir := []rune(strings.Join(strings.Fields(o.Direction), " ")) |
| - if len(dir) == 0 { |
| - body := t.words(key+"_name", name) |
| - return body, body != "" && fits(body) |
| - } |
| - for n := len(dir); n > 0; { |
| - d := string(dir[:n]) |
| - if n < len(dir) { |
| - d = strings.TrimSpace(d) + "…" |
| + if dir := []rune(strings.Join(strings.Fields(o.Direction), " ")); len(dir) > 0 { |
| + at := func(n int) string { |
| + d := string(dir[:n]) |
| + if n < len(dir) { |
| + d = strings.TrimSpace(d) + "…" |
| + } |
| + return t.words(key, name, wire.QuotedData(d)) |
| } |
| - body := t.words(key, name, wire.QuotedData(d)) |
| + body := at(len(dir)) |
| if body == "" { |
| return "", false |
| } |
| - over := utf8.RuneCountInString(strings.TrimSpace(body)) - wire.MaxTasteBox |
| - if over <= 0 { |
| + if fits(body) { |
| return body, true |
| } |
| - n -= over |
| + // A longer prefix never quotes shorter: the first prefix length |
| + // that does not fit, less one, is the longest that does. |
| + if n := sort.Search(len(dir), func(n int) bool { return !fits(at(n)) }) - 1; n > 0 { |
| + return at(n), true |
| + } |
| } |
| - return "", false |
| + body := t.words(key+"_name", name) |
| + return body, body != "" && fits(body) |
| } |
diff --git a/internal/equipo/chat/taste_choices_test.go b/internal/equipo/chat/taste_choices_test.go| index 5b2650d3..167ae44b 100644 |
| --- a/internal/equipo/chat/taste_choices_test.go |
| +++ b/internal/equipo/chat/taste_choices_test.go |
| @@ -3,6 +3,7 @@ package chat |
| import ( |
| "errors" |
| "fmt" |
| + "strconv" |
| "strings" |
| "testing" |
| "unicode/utf8" |
| @@ -191,3 +192,83 @@ func TestChoiceBodyFits(t *testing.T) { |
| return nil |
| })) |
| } |
| + |
| +// escapeHeavy is a valid 250-byte direction of double quotes and |
| +// backslashes, each of which quoting doubles. |
| +var escapeHeavy = strings.Repeat(`"\`, 125) |
| + |
| +// escapeRound is a protocol-39 exploration whose first round kept three |
| +// options, the second (Writing desk) with direction dir, approved: the |
| +// direction card is open. |
| +func escapeRound(t *testing.T, dir string) (*hkRig, string, stepRecord, cardRecord) { |
| + r, l, task, s := designAt(t, wire.ProtocolDesignFeedback, wire.KindExplore) |
| + r.buildWords() |
| + r.choiceWords() |
| + var opts []wire.DesignOption |
| + for i, name := range []string{"Calm", "Writing desk", "Bold"} { |
| + opts = append(opts, r.option39(l, s, i+1, name, 1)) |
| + } |
| + opts[1].Direction = dir |
| + r.doneWith(l, s, candidateSHA, opts) |
| + r.judgeRound(l, task, candidateSHA, wire.VerdictApprove) |
| + return r, task, r.stepNow(s.ID), r.openDirection(task) |
| +} |
| + |
| +// TestEscapeHeavyDirectionAnswers: a valid direction whose quoted form |
| +// is far longer than its text never stops Build this one or More like |
| +// this: each answers, with one choice note within the bound, its |
| +// direction clipped but not empty. |
| +func TestEscapeHeavyDirectionAnswers(t *testing.T) { |
| + if len(escapeHeavy) != 250 || !strings.Contains(escapeHeavy, `"`) { |
| + t.Fatalf("direction %q", escapeHeavy) |
| + } |
| + for choice, key := range map[string]string{wire.ChoiceBuild: "work.explore.choice_build", wire.ChoiceLike: "work.explore.choice_like"} { |
| + r, task, s, c := escapeRound(t, escapeHeavy) |
| + if err := r.answerCard(c, wire.AnswerCard{Choice: choice, Option: &wire.OptionRef{Step: s.ID, Index: 1}}); err != nil { |
| + t.Fatalf("%s: %v", choice, err) |
| + } |
| + rows := r.tasteRows(paul) |
| + if len(rows) != 1 { |
| + t.Fatalf("%s: notes %+v", choice, rows) |
| + } |
| + head := fmt.Sprintf(choiceWordsText[key], wire.QuotedData("Writing desk"), `"\"\\`) |
| + if b := rows[0].Body; utf8.RuneCountInString(b) > wire.MaxTasteBox || !strings.HasPrefix(b, head) || !strings.HasSuffix(b, `…"`) { |
| + t.Fatalf("%s: body %d characters %q", choice, utf8.RuneCountInString(b), b) |
| + } |
| + if choice == wire.ChoiceBuild && r.load(task).BuiltAs == "" { |
| + t.Fatal("Build built nothing") |
| + } |
| + } |
| +} |
| + |
| +// TestChoiceBodyTotal: escape-heavy directions keep the longest quoted |
| +// prefix that fits (quotes, backslashes, characters quoting spells as |
| +// \U escapes); when no direction fits beside the name, the option is |
| +// named alone. |
| +func TestChoiceBodyTotal(t *testing.T) { |
| + r, _, _, _ := choiceRound(t) |
| + r.must(r.tx(func(t *txn) error { |
| + for name, dir := range map[string]string{ |
| + "quotes": strings.Repeat(`"`, 500), |
| + "backslashes": strings.Repeat(`\`, 500), |
| + "escaped": strings.Repeat("\U000f0000", 125), |
| + } { |
| + body, ok := t.choiceBody(wire.ChoiceBuild, wire.StepDesignOption{Name: "Calm", Direction: dir}) |
| + n := utf8.RuneCountInString(body) |
| + if !ok || n > wire.MaxTasteBox || !strings.HasSuffix(body, `…"`) { |
| + r.t.Fatalf("%s: %v, %d characters %q", name, ok, n, body) |
| + } |
| + // The longest: one more character of the direction would not fit. |
| + unit := len([]rune(strconv.Quote(string([]rune(dir)[:1])))) - 2 |
| + if n+unit <= wire.MaxTasteBox { |
| + r.t.Fatalf("%s: %d characters, room for more", name, n) |
| + } |
| + } |
| + long := strings.Repeat(`"`, 125) |
| + body, ok := t.choiceBody(wire.ChoiceLike, wire.StepDesignOption{Name: long, Direction: "Calm type."}) |
| + if want := fmt.Sprintf(choiceWordsText["work.explore.choice_like_name"], wire.QuotedData(long)); !ok || body != want { |
| + r.t.Fatalf("no room for a direction: %v %q", ok, body) |
| + } |
| + return nil |
| + })) |
| +} |