| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo" |
| 9 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 10 | ) |
| 11 | |
| 12 | func TestCoordinatorToolRedaction(t *testing.T) { |
| 13 | f, x, p, c := claimTools(t) |
| 14 | secret := "Bearer abcdefghijklmnopqrstuvwxyz123456" |
| 15 | cases := []struct { |
| 16 | name string |
| 17 | args any |
| 18 | }{ |
| 19 | {"ask_person", map[string]any{"message_id": c.Turn.MessageID, "question": secret, "options": []map[string]string{{"id": "one", "label": secret}}}}, |
| 20 | {"suggest_preference", map[string]any{"source_message_id": c.Turn.MessageID, "text": secret}}, |
| 21 | {"suggest_specialist", map[string]any{"source_message_ids": []string{c.Turn.MessageID}, "draft": map[string]string{"name": "Draft", "instructions": secret}}}, |
| 22 | {"suggest_playbook", map[string]any{"source_message_ids": []string{c.Turn.MessageID}, "draft": map[string]string{"name": "Draft", "instructions": secret}}}, |
| 23 | } |
| 24 | for _, tc := range cases { |
| 25 | t.Run(tc.name, func(t *testing.T) { |
| 26 | out := toolCall(t, x, p, c, tc.name, tc.name, tc.args) |
| 27 | if strings.HasPrefix(tc.name, "suggest_") { |
| 28 | var envelope struct{ Result map[string]any } |
| 29 | must(t, json.Unmarshal(out.Value, &envelope)) |
| 30 | if len(envelope.Result) == 0 { |
| 31 | t.Fatal("suggestion result lost its structure") |
| 32 | } |
| 33 | var body string |
| 34 | must(t, f.DB.Writer().QueryRow("SELECT body FROM conversation_messages WHERE id=?", out.ResourceID).Scan(&body)) |
| 35 | var value map[string]any |
| 36 | must(t, json.Unmarshal([]byte(body), &value)) |
| 37 | if len(value) == 0 { |
| 38 | t.Fatal("suggestion body lost its structure") |
| 39 | } |
| 40 | } |
| 41 | if strings.Contains(string(out.Value), "abcdefghijklmnopqrstuvwxyz123456") { |
| 42 | t.Error("tool result leaked secret") |
| 43 | } |
| 44 | }) |
| 45 | } |
| 46 | if f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='coordinator' AND body LIKE '%abcdefghijklmnopqrstuvwxyz123456%'") != 0 { |
| 47 | t.Error("stored message leaked secret") |
| 48 | } |
| 49 | if f.count(t, "SELECT count(*) FROM coordinator_questions WHERE payload LIKE '%abcdefghijklmnopqrstuvwxyz123456%'") != 0 { |
| 50 | t.Error("stored question leaked secret") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestCoordinatorToolQuestionSupersession(t *testing.T) { |
| 55 | for _, earlier := range []bool{false, true} { |
| 56 | name := "same turn" |
| 57 | if earlier { |
| 58 | name = "earlier delivered turn" |
| 59 | } |
| 60 | t.Run(name, func(t *testing.T) { |
| 61 | f, x, p, c := claimTools(t) |
| 62 | first := toolCall(t, x, p, c, "first", "ask_person", map[string]any{"message_id": c.Turn.MessageID, "question": "First?", "options": []any{}}) |
| 63 | if earlier { |
| 64 | // Keep the admitted current turn intact; move the old question to a |
| 65 | // delivered predecessor in the same binding and conversation. |
| 66 | f.exec(t, `INSERT INTO coordinator_turns(id,binding_id,conversation_id,person,generation,state) SELECT 'earlier',binding_id,conversation_id,person,generation,'delivered' FROM coordinator_turns WHERE id=?`, c.Turn.ID) |
| 67 | f.exec(t, `UPDATE coordinator_questions SET turn_id='earlier',payload=json_set(payload,'$.TurnID','earlier') WHERE id=?`, first.ResourceID) |
| 68 | } |
| 69 | toolCall(t, x, p, c, "second", "ask_person", map[string]any{"message_id": c.Turn.MessageID, "question": "Next?", "options": []any{}}) |
| 70 | var payload, state string |
| 71 | var rev int64 |
| 72 | must(t, f.DB.Writer().QueryRow("SELECT state,revision,payload FROM coordinator_questions WHERE id=?", first.ResourceID).Scan(&state, &rev, &payload)) |
| 73 | var q runnerproto.ClarificationQuestion |
| 74 | must(t, json.Unmarshal([]byte(payload), &q)) |
| 75 | if state != "superseded" || q.State != state || q.Revision != rev { |
| 76 | t.Fatalf("inconsistent supersession: column=%s/%d payload=%+v", state, rev, q) |
| 77 | } |
| 78 | if f.count(t, "SELECT count(*) FROM coordinator_questions WHERE state='pending'") != 1 { |
| 79 | t.Fatal("multiple pending questions") |
| 80 | } |
| 81 | if err := f.Store.AnswerClarification(ctx, owner, first.ResourceID, "", "old", equipo.Mutation{RequestID: "old", Revision: 1}); err == nil { |
| 82 | t.Fatal("superseded question accepted") |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |