Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .npm/run.js
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ import (
// - X-CIO-Agent: 1 — set only when the CIO_AGENT env var is "1". The
// sandbox that runs the CLI on behalf of an AI agent sets this so
// downstream metrics can attribute traffic to the agent.
// - X-CIO-Capability-Grant — forwarded from $X_CIO_CAPABILITY_GRANT so the env carries a session-scoped grant without a CLI flag.
func setStandardHeaders(req *http.Request) {
req.Header.Set("User-Agent", useragent.Get())
req.Header.Set("X-Validate", "strict")
if os.Getenv("CIO_AGENT") == "1" {
req.Header.Set("X-CIO-Agent", "1")
}
if grant := os.Getenv("X_CIO_CAPABILITY_GRANT"); grant != "" {
req.Header.Set("X-CIO-Capability-Grant", grant)
}
}

const (
Expand Down
43 changes: 43 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,3 +796,46 @@ func TestClient_Do_AgentHeader(t *testing.T) {
})
}
}

func TestClient_Do_CapabilityGrantHeader(t *testing.T) {
cases := []struct {
name string
envValue string
envSet bool
want string
}{
{"env unset", "", false, ""},
{"env empty", "", true, ""},
{"env set", "grant-jwt-abc", true, "grant-jwt-abc"},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.envSet {
t.Setenv("X_CIO_CAPABILITY_GRANT", tc.envValue)
} else {
t.Setenv("X_CIO_CAPABILITY_GRANT", "")
_ = os.Unsetenv("X_CIO_CAPABILITY_GRANT")
}

var got string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got = r.Header.Get("X-CIO-Capability-Grant")
_, _ = w.Write([]byte(`{"ok":true}`))
}))
defer server.Close()

c := New(Config{
BaseURL: server.URL,
AccessToken: "test-jwt",
RetryConfig: &RetryConfig{MaxRetries: 0, SleepFn: ContextSleep},
})
if _, err := c.Do(context.Background(), "GET", "/test", nil, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tc.want {
t.Errorf("X-CIO-Capability-Grant: got %q, want %q", got, tc.want)
}
})
}
}