| 1 | // provider-network-probe performs one bounded, synthetic Chat Completions call. |
| 2 | // It never executes tools and outputs only the provider's safe diagnostic model. |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "net" |
| 13 | "net/http" |
| 14 | "net/url" |
| 15 | "os" |
| 16 | "strings" |
| 17 | "sync" |
| 18 | "sync/atomic" |
| 19 | "time" |
| 20 | |
| 21 | "reasonix/internal/provider" |
| 22 | "reasonix/internal/secrets" |
| 23 | ) |
| 24 | |
| 25 | type singleTransport struct { |
| 26 | base http.RoundTripper |
| 27 | used atomic.Bool |
| 28 | } |
| 29 | |
| 30 | func (t *singleTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| 31 | if t.used.Swap(true) { |
| 32 | return nil, errors.New("probe request limit reached") |
| 33 | } |
| 34 | return t.base.RoundTrip(r) |
| 35 | } |
| 36 | |
| 37 | func main() { |
| 38 | endpoint := flag.String("endpoint", "https://api.deepseek.com/chat/completions", "complete endpoint URL; no credentials or query") |
| 39 | model := flag.String("model", "deepseek-v4-flash", "fixed model for the comparison") |
| 40 | protocol := flag.String("protocol", "h2", "h1 or h2; observed protocol is included in output") |
| 41 | bodyBytes := flag.Int("body-bytes", 1024, "target synthetic JSON bytes (1024..4194304)") |
| 42 | expect := flag.Bool("expect-continue", false, "independent Expect: 100-continue experiment") |
| 43 | budget := flag.Duration("timeout", 360*time.Second, "whole probe budget (1s..10m)") |
| 44 | flag.Parse() |
| 45 | if err := run(*endpoint, *model, *protocol, *bodyBytes, *expect, *budget); err != nil { |
| 46 | // Validation errors are fixed local prose, never transport error strings. |
| 47 | fmt.Fprintln(os.Stderr, err) |
| 48 | os.Exit(1) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func run(endpoint, model, protocol string, bodyBytes int, expect bool, budget time.Duration) error { |
| 53 | u, err := url.Parse(endpoint) |
| 54 | if err != nil || u.Hostname() == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || (u.Scheme != "https" && u.Scheme != "http") { |
| 55 | return errors.New("endpoint must be an HTTP(S) URL without credentials, query or fragment") |
| 56 | } |
| 57 | if protocol != "h1" && protocol != "h2" { |
| 58 | return errors.New("protocol must be h1 or h2") |
| 59 | } |
| 60 | if bodyBytes < 1024 || bodyBytes > 4<<20 || budget < time.Second || budget > 10*time.Minute { |
| 61 | return errors.New("body size or timeout outside probe limits") |
| 62 | } |
| 63 | key := os.Getenv("REASONIX_PROBE_API_KEY") |
| 64 | if key == "" { |
| 65 | return errors.New("set REASONIX_PROBE_API_KEY in the environment") |
| 66 | } |
| 67 | if u.Scheme != "https" && u.Hostname() != "127.0.0.1" && u.Hostname() != "localhost" && u.Hostname() != "::1" { |
| 68 | return errors.New("remote probes require HTTPS") |
| 69 | } |
| 70 | body := syntheticBody(model, bodyBytes) |
| 71 | tr := http.DefaultTransport.(*http.Transport).Clone() |
| 72 | tr.DialContext = (&net.Dialer{Timeout: 30 * time.Second}).DialContext |
| 73 | tr.TLSHandshakeTimeout = 30 * time.Second |
| 74 | tr.ResponseHeaderTimeout = 300 * time.Second |
| 75 | tr.ExpectContinueTimeout = time.Second |
| 76 | tr.Protocols = new(http.Protocols) |
| 77 | tr.Protocols.SetHTTP1(protocol == "h1") |
| 78 | tr.Protocols.SetHTTP2(protocol == "h2") |
| 79 | defer tr.CloseIdleConnections() |
| 80 | client := &http.Client{Transport: &singleTransport{base: tr}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| 81 | ctx, cancel := context.WithTimeout(context.Background(), budget) |
| 82 | defer cancel() |
| 83 | var mu sync.Mutex |
| 84 | var observation provider.RequestObservation |
| 85 | ctx = provider.WithRequestObserver(ctx, func(v provider.RequestObservation) { mu.Lock(); observation = v; mu.Unlock() }) |
| 86 | resp, requestErr := provider.SendOnce(ctx, client, provider.SendOptions{Provider: "probe", Protocol: "openai"}, func(ctx context.Context) (*http.Request, error) { |
| 87 | r, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(string(body))) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | // Prevent request-body replay inside Transport. A new transport is used |
| 92 | // for this invocation; the wrapper also rejects any application resend. |
| 93 | r.GetBody = nil |
| 94 | r.Header.Set("Authorization", "Bearer "+key) |
| 95 | r.Header.Set("Content-Type", "application/json") |
| 96 | if expect { |
| 97 | r.Header.Set("Expect", "100-continue") |
| 98 | } |
| 99 | return r, nil |
| 100 | }) |
| 101 | traceID := "" |
| 102 | if resp != nil { |
| 103 | traceID = provider.DiagnoseFailure(&provider.APIError{TraceID: resp.Header.Get("x-trace-id")}).TraceID |
| 104 | if traceID == "" { |
| 105 | traceID = provider.DiagnoseFailure(&provider.APIError{TraceID: resp.Header.Get("trace-id")}).TraceID |
| 106 | } |
| 107 | if traceID == "" { |
| 108 | traceID = provider.DiagnoseFailure(&provider.APIError{TraceID: resp.Header.Get("trace_id")}).TraceID |
| 109 | } |
| 110 | _, readErr := io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<20)) |
| 111 | resp.Body.Close() |
| 112 | if requestErr == nil { |
| 113 | requestErr = readErr |
| 114 | } |
| 115 | } |
| 116 | mu.Lock() |
| 117 | result := struct { |
| 118 | SchemaVersion int `json:"schemaVersion"` |
| 119 | RequestedProtocol string `json:"requestedProtocol"` |
| 120 | ExpectContinue bool `json:"expectContinue"` |
| 121 | TraceID string `json:"traceId,omitempty"` |
| 122 | Request provider.RequestObservation `json:"request"` |
| 123 | Failure *provider.FailureDiagnostic `json:"failure,omitempty"` |
| 124 | }{1, protocol, expect, traceID, observation, nil} |
| 125 | mu.Unlock() |
| 126 | if requestErr != nil { |
| 127 | d := provider.DiagnoseFailure(requestErr) |
| 128 | result.Failure = d |
| 129 | } |
| 130 | encoded, err := json.Marshal(result) |
| 131 | if err != nil { |
| 132 | return errors.New("cannot encode safe probe result") |
| 133 | } |
| 134 | encoded, err = secrets.RedactJSON(encoded) |
| 135 | if err != nil || !json.Valid(encoded) { |
| 136 | return errors.New("cannot redact probe result") |
| 137 | } |
| 138 | fmt.Println(string(encoded)) |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | func syntheticBody(model string, size int) []byte { |
| 143 | request := map[string]any{"model": model, "stream": true, "max_tokens": 1, "messages": []map[string]string{{"role": "user", "content": "Reply OK. Synthetic padding: "}}} |
| 144 | base, _ := json.Marshal(request) |
| 145 | if padding := size - len(base); padding > 0 { |
| 146 | request["messages"].([]map[string]string)[0]["content"] += strings.Repeat("x", padding) |
| 147 | } |
| 148 | body, _ := json.Marshal(request) |
| 149 | return body |
| 150 | } |
| 151 |