| 1 | package cliconversation |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | type approvalCapture struct{ bytes.Buffer } |
| 12 | |
| 13 | func (*approvalCapture) Close() error { return nil } |
| 14 | |
| 15 | func TestDecisionsFollowTheOriginalRequestThroughSupervisor(t *testing.T) { |
| 16 | ctx, cancel := context.WithCancel(context.Background()) |
| 17 | defer cancel() |
| 18 | wire := new(approvalCapture) |
| 19 | release := make(chan struct{}) |
| 20 | p := &approvalStream{ |
| 21 | ctx: ctx, cancel: cancel, input: wire, supervised: true, turn: Turn{SessionID: testSessionID, Prompt: "fixture"}, |
| 22 | pending: make(map[string]context.CancelFunc), seen: make(map[string]bool), |
| 23 | approve: func(ctx context.Context, r PermissionRequest) bool { |
| 24 | var args struct{ Command string } |
| 25 | if err := json.Unmarshal(r.Input, &args); err != nil { |
| 26 | t.Error(err) |
| 27 | return false |
| 28 | } |
| 29 | switch args.Command { |
| 30 | case "first": |
| 31 | <-release |
| 32 | clear(r.Input) |
| 33 | return true |
| 34 | case "second": |
| 35 | return false |
| 36 | case "revoked": |
| 37 | <-ctx.Done() |
| 38 | return true |
| 39 | default: |
| 40 | t.Error("oversized input reached the approval UI") |
| 41 | return false |
| 42 | } |
| 43 | }, |
| 44 | } |
| 45 | if err := p.begin(); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if err := p.control("control_response", []byte(`{"response":{"subtype":"success","request_id":"wrong-init"}}`)); err == nil { |
| 49 | t.Fatal("accepted an unrelated initialization reply") |
| 50 | } |
| 51 | if err := p.control("control_response", []byte(`{"response":{"subtype":"success","request_id":"cage-cli-initialize"}}`)); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | request := func(id, command string) error { |
| 55 | line, _ := json.Marshal(map[string]any{"request_id": id, "request": map[string]any{ |
| 56 | "subtype": "can_use_tool", "tool_name": "Bash", "input": map[string]string{"command": command}, |
| 57 | }}) |
| 58 | return p.control("control_request", line) |
| 59 | } |
| 60 | for id, command := range map[string]string{"one": "first", "two": "second", "old": "revoked", "large": strings.Repeat("x", maxApprovalBytes)} { |
| 61 | if err := request(id, command); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | } |
| 65 | if err := request("one", "first"); err == nil { |
| 66 | t.Fatal("accepted a duplicate approval ID") |
| 67 | } |
| 68 | if err := p.control("control_cancel_request", []byte(`{"request_id":"old"}`)); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | close(release) |
| 72 | p.workers.Wait() |
| 73 | if err := p.finish(); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | lines := bytes.Split(bytes.TrimSpace(wire.Bytes()), []byte("\n")) |
| 77 | if string(lines[0]) != `{"stream":true}` || string(lines[len(lines)-1]) != `{"finish":true}` { |
| 78 | t.Fatal("supervisor did not get explicit stream start and finish") |
| 79 | } |
| 80 | decisions := map[string]string{} |
| 81 | for _, line := range lines[1 : len(lines)-1] { |
| 82 | var packet struct { |
| 83 | Input struct { |
| 84 | Type string `json:"type"` |
| 85 | Response struct { |
| 86 | ID string `json:"request_id"` |
| 87 | Answer struct { |
| 88 | Behavior string `json:"behavior"` |
| 89 | Updated struct{ Command string } `json:"updatedInput"` |
| 90 | } `json:"response"` |
| 91 | } `json:"response"` |
| 92 | } `json:"input"` |
| 93 | } |
| 94 | if err := json.Unmarshal(line, &packet); err != nil || packet.Input.Type == "" { |
| 95 | t.Fatalf("invalid supervisor input: %s", line) |
| 96 | } |
| 97 | if packet.Input.Type == "control_response" { |
| 98 | response := packet.Input.Response |
| 99 | if _, duplicate := decisions[response.ID]; duplicate { |
| 100 | t.Fatal("sent two answers for one request") |
| 101 | } |
| 102 | decisions[response.ID] = response.Answer.Behavior + ":" + response.Answer.Updated.Command |
| 103 | } |
| 104 | } |
| 105 | if len(decisions) != 3 || decisions["one"] != "allow:first" || decisions["two"] != "deny:" || decisions["large"] != "deny:" { |
| 106 | t.Errorf("approval crossed requests, accepted a late decision, or changed input: %#v", decisions) |
| 107 | } |
| 108 | } |