Files
coder/codersdk/licenses.go
T
Michael Suchacz 157477e3c5 fix!: remove obsolete managed agent limit license warning (#29657)
## Summary

Deployments that used the legacy Tasks (managed agents) feature in the
past keep seeing the license banner "You have built more workspaces with
managed agents than your license allows." even after installing a
freshly generated license. This removes that warning at its source,
along with its dashboard-only link mapping and stories.

## Why the banner is obsolete

- The warning was appended in `enterprise/coderd/license/license.go`
whenever `managedAgentCount >= limit` for the `managed_agent_limit`
feature.
- The count comes from `GetTotalUsageDCManagedAgentsV1`, which sums
historical `dc_managed_agents_v1` usage events. The only emitter of
those events (the `has_ai_task` start-build path) was removed in #29085,
so nothing produces them anymore and the count can never decrease.
- Premium licenses without an explicit claim get a grandfathered
`managed_agent_limit` default with a usage period spanning decades, so a
deployment that ever exceeded that count keeps the warning forever,
regardless of license renewal.

## What changed

- `enterprise/coderd/license/license.go`: stop appending the warning.
The feature still measures and reports `Actual` for the licenses page.
- `codersdk/licenses.go` and `site/src/api/typesGenerated.ts`: delete
the `LicenseManagedAgentLimitExceededWarningText` constant. This is the
breaking part of the change for external `codersdk` consumers, hence the
`!` in the title.
- `site/src/modules/dashboard/LicenseBanner/`: remove the "View AI
Governance" link mapping and the two stories that rendered the obsolete
message.
- Tests: the two cases that asserted the warning now assert no warning
while `Actual` is still reported. The existing `AgentRuntimeHours/*`
cases keep covering the Coder Agent runtime hours warnings unchanged.

The Coder Agents `agent_runtime_hours` warnings (`hb_agent_runtime_v1`
usage) are unchanged. The `managed_agent_limit` feature, its usage
query, and the `dc_managed_agents_v1` event type are intentionally kept
so that existing licenses and historical reporting keep working.

## Validation

- `go test ./enterprise/coderd/license/`
- `pnpm exec tsc --noEmit` and `biome check` on the touched frontend
files
- Repository pre-commit hook

> Xum acted on behalf of Mike (@ibetitsmike) to prepare this pull
request.
2026-09-21 15:09:25 +02:00

175 lines
6.9 KiB
Go

package codersdk
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/uuid"
"golang.org/x/xerrors"
)
const (
LicenseExpiryClaim = "license_expires"
LicenseTelemetryRequiredErrorText = "License requires telemetry but telemetry is disabled"
LicenseAIGovernance90PercentWarningText = "You have used %d%% of your AI Governance add-on seats."
LicenseAIGovernanceOverLimitWarningText = "Your organization is using %d of %d AI Governance add-on seats (%d over the limit)."
// The dashboard's LicenseBanner matches this text's pre-placeholder
// prefix to render it muted and without a sales link, so the license
// warning texts must stay pairwise distinct before their first
// placeholder. See TestLicenseAgentRuntimeHoursWarningTexts.
LicenseAgentRuntimeHoursSoftLimitWarningText = "Your deployment is approaching its Coder Agent runtime hours allocation: %d of the %d hours included in the current license term are used, at or above the advisory soft limit of %d hours."
LicenseAgentRuntimeHoursAllocationReachedWarningText = "Your deployment has used %d of the %d Coder Agent runtime hours included in the current license term."
LicenseAgentRuntimeUsageUnavailableErrorText = "Unable to determine Coder Agent runtime usage. Reported runtime hours are unavailable until the next successful refresh; workspaces are unaffected. Check the coderd logs for details."
LicenseAgentRuntimeHoursClaimsIgnoredWarningText = "A license contains unusable Coder Agent runtime hour claims, which were ignored. The rest of that license is unaffected. Check the coderd logs for the affected license and claims, and contact support to have the license re-issued."
)
type AddLicenseRequest struct {
License string `json:"license" validate:"required"`
}
// Defines the input payload for requesting a trial license.
type CreateTrialLicenseRequest struct {
Email string `json:"email" validate:"required,email,max=254" example:"jane.doe@example.com" format:"email" maxLength:"254"`
FirstName string `json:"first_name" validate:"required,min=1,max=60" example:"Jane" minLength:"1" maxLength:"60"`
LastName string `json:"last_name" validate:"required,min=1,max=60" example:"Doe" minLength:"1" maxLength:"60"`
PhoneNumber string `json:"phone_number" validate:"required,min=7,max=20" example:"+14155552671" minLength:"7" maxLength:"20"`
JobTitle string `json:"job_title" validate:"required,min=2,max=100" example:"Engineering Manager" minLength:"2" maxLength:"100"`
CompanyName string `json:"company_name" validate:"required,min=2,max=100" example:"Acme Corp" minLength:"2" maxLength:"100"`
Country string `json:"country" validate:"required" example:"United States"`
Developers string `json:"developers" validate:"required"`
// Source is the premium paywall the request came from, for telemetry. It
// is not forwarded to the licensor. Omit it to report "direct".
Source PremiumFunnelSource `json:"source,omitempty"`
// AttributionID is the ID of the cta_click funnel event that led here, so
// that a signup can be joined back to the paywall that produced it.
AttributionID uuid.UUID `json:"attribution_id,omitempty" format:"uuid"`
}
type License struct {
ID int32 `json:"id"`
UUID uuid.UUID `json:"uuid" format:"uuid"`
UploadedAt time.Time `json:"uploaded_at" format:"date-time"`
// Claims are the JWT claims asserted by the license. Here we use
// a generic string map to ensure that all data from the server is
// parsed verbatim, not just the fields this version of Coder
// understands.
Claims map[string]interface{} `json:"claims" table:"claims"`
}
// ExpiresAt returns the expiration time of the license.
// If the claim is missing or has an unexpected type, an error is returned.
func (l *License) ExpiresAt() (time.Time, error) {
expClaim, ok := l.Claims[LicenseExpiryClaim]
if !ok {
return time.Time{}, xerrors.New("license_expires claim is missing")
}
// This claim should be a unix timestamp.
// Everything is already an interface{}, so we need to do some type
// assertions to figure out what we're dealing with.
if unix, ok := expClaim.(json.Number); ok {
i64, err := unix.Int64()
if err != nil {
return time.Time{}, xerrors.Errorf("license_expires claim is not a valid unix timestamp: %w", err)
}
return time.Unix(i64, 0), nil
}
return time.Time{}, xerrors.Errorf("license_expires claim has unexpected type %T", expClaim)
}
func (l *License) Trial() bool {
if trial, ok := l.Claims["trial"].(bool); ok {
return trial
}
return false
}
func (l *License) AllFeaturesClaim() bool {
if all, ok := l.Claims["all_features"].(bool); ok {
return all
}
return false
}
// FeaturesClaims provides the feature claims in license.
// This only returns the explicit claims. If checking for actual usage,
// also check `AllFeaturesClaim`.
func (l *License) FeaturesClaims() (map[FeatureName]int64, error) {
strMap, ok := l.Claims["features"].(map[string]interface{})
if !ok {
return nil, xerrors.New("features key is unexpected type")
}
fMap := make(map[FeatureName]int64)
for k, v := range strMap {
jn, ok := v.(json.Number)
if !ok {
return nil, xerrors.Errorf("feature %q has unexpected type", k)
}
n, err := jn.Int64()
if err != nil {
return nil, err
}
fMap[FeatureName(k)] = n
}
return fMap, nil
}
func (c *Client) AddLicense(ctx context.Context, r AddLicenseRequest) (License, error) {
res, err := c.Request(ctx, http.MethodPost, "/api/v2/licenses", r)
if err != nil {
return License{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
return License{}, ReadBodyAsError(res)
}
var l License
return l, ReadBodyAsJSONUseNumber(res, &l)
}
// CreateTrialLicense requests a trial license from the Coder licensor and install it.
func (c *Client) CreateTrialLicense(ctx context.Context, r CreateTrialLicenseRequest) (License, error) {
res, err := c.Request(ctx, http.MethodPost, "/api/v2/licenses/trial", r)
if err != nil {
return License{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
return License{}, ReadBodyAsError(res)
}
var l License
return l, ReadBodyAsJSONUseNumber(res, &l)
}
func (c *Client) Licenses(ctx context.Context) ([]License, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/licenses", nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, ReadBodyAsError(res)
}
var licenses []License
return licenses, ReadBodyAsJSONUseNumber(res, &licenses)
}
func (c *Client) DeleteLicense(ctx context.Context, id int32) error {
res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/v2/licenses/%d", id), nil)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return ReadBodyAsError(res)
}
return nil
}