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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,10 @@ Only a built-in connector takes the OAuth client the deployment keeps in its env

`/v1/data/import`, and a change applied after it, writes a `phone_numbers` row only when its vendor is exactly `sip_trunk`; any other row is skipped without an error. A number bought at a vendor stays in the vendor account of the deployment that bought it, and its bills already move with the requests, so the destination neither holds it nor bills its months again. A customer that moves keeps a bought number by buying or porting it at the destination. A number bought here and still held is never updated or deleted by an imported row or a replayed change, whatever id it names. An imported `sip_trunk` number is held to the API's rules (E.164, a vendor, a cost that is not negative), and no imported number keeps its `dlc_use_case_id`, which names a use case of the source.

### A request counts only toward its own organization's spend grants

A request row's cost is added to a grant it names only when the grant belongs to the row's organization or to its app's (the organization the app is in, or the app itself for an app in none), both as the row is written and when the grant uses are reconciled. A grant scoped to an app, or to its key or user, counts only that app's rows. A use refused because the grant is not the row's is counted on `/metrics` as `router_usage_grant_uses_refused_total`. An imported request names no grant: it was drawn from the grants of the deployment that recorded it.

### A customer can no longer list or speak in another customer's cloned voice

Clones made through `/v1/agents/voices` sit in the deployment's shared provider account, which lists them all as its own. A voice id that another customer's voice was prepared as is now refused, whether it is the session's voice or the `voice_id` of TTS overwrites. The voice library lists only the caller's own clones besides the provider's published voices, previews another customer's clone as not found, and no longer suggests one as the closest voice.
Expand Down
2 changes: 1 addition & 1 deletion acceleration/internal/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (s *PolicySuite) TestAGrantAdmitsPastASpentLimitUntilItIsUsed() {
s.NoError(refused)
s.ErrorIs(s.admitted().Spend, ErrBudgetSpent, "the hold uses the grant up at once")

s.spendAs(store.Request{UsageID: usageID, CustomerID: s.app, CostMicros: 2_000_000, LimitGrants: drawn})
s.spendAs(store.Request{UsageID: usageID, CustomerID: s.app, OrganizationID: s.org, CostMicros: 2_000_000, LimitGrants: drawn})
s.enforcer.Written([]routing.Acked{{UsageID: usageID, OrganizationID: s.org, CostMicros: 2_000_000}})
s.never(func() bool {
s.enforcer.money.mu.Lock()
Expand Down
4 changes: 2 additions & 2 deletions acceleration/internal/policy/reserve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ func (s *PolicySuite) TestAGrantsRemainingNeverRisesPastTheTruthAcrossItsCount()
}, time.Second, 2*time.Millisecond, "the other router counts the hold")
truth.Store(1_000_000 - cost)

s.spendAs(store.Request{UsageID: id, CustomerID: s.app, CostMicros: cost, LimitGrants: []int64{grant.ID}})
_, err := s.store.LimitGrantUses.InsertMany(s.ctx, []store.GrantUse{{GrantID: grant.ID, UsageID: id, Micros: cost}})
s.spendAs(store.Request{UsageID: id, CustomerID: s.app, OrganizationID: s.org, CostMicros: cost, LimitGrants: []int64{grant.ID}})
_, err := s.store.LimitGrantUses.InsertMany(s.ctx, []store.GrantUse{{GrantID: grant.ID, OrganizationID: s.org, UsageID: id, Micros: cost}})
s.Require().NoError(err)
a.Written([]routing.Acked{{UsageID: id, OrganizationID: s.org, CostMicros: cost}})
a.Counted([]routing.Acked{{UsageID: id, OrganizationID: s.org, CostMicros: cost}})
Expand Down
5 changes: 3 additions & 2 deletions acceleration/internal/store/datamove.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,10 +1481,11 @@ func (t dataTable) identity() string {
}
// A request was billed by the deployment that recorded it, so this one never ships it, and
// what that one's usage service answered for it is not this one's: a reship of the rows
// this one shipped or had refused leaves it alone.
// this one shipped or had refused leaves it alone. Nor was it drawn from a grant here,
// whose ids are this deployment's own.
// Nor is it billed here, and it counts against the importing app's organization alone.
if t.name == "requests" {
return "jsonb_build_object('customer_id', ?::text, 'to_ship', false, 'shipped_at', NULL, 'ship_error', '')" +
return "jsonb_build_object('customer_id', ?::text, 'to_ship', false, 'shipped_at', NULL, 'ship_error', '', 'limit_grants', '{}'::bigint[])" +
" || jsonb_build_object('billable', false)" +
" || jsonb_build_object('organization_id', ?::text)"
}
Expand Down
10 changes: 10 additions & 0 deletions acceleration/internal/store/datamove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3531,6 +3531,16 @@ func (s *DataMoveSuite) TestAnImportedRequestKeepsItsUsageIDAndIsNeverShipped()
s.Equal("", moved["ship_error"])
}

func (s *DataMoveSuite) TestAnImportedRequestNamesNoGrant() {
s.Require().NoError(s.source.Requests.Insert(s.ctx, &Request{UsageID: uuid.Must(uuid.NewV7()), Modality: "llm", CustomerID: "acme",
Provider: "openai", Model: "m", StartedAt: time.Now().UTC(), Success: true, Billable: true, CostMicros: 500,
LimitGrants: []int64{7, 8}}))

moved := s.movedRequest(false)

s.Empty(moved["limit_grants"], "the source's grant ids name other grants here")
}

// takenRequest writes a request of acme's with the id given, as two deployments' own
// sequences can each hand out.
func (s *DataMoveSuite) takenRequest(store *Store, id int64) *Request {
Expand Down
81 changes: 61 additions & 20 deletions acceleration/internal/store/limit_grant_use.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ import (
"time"

"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"

"github.com/GetStream/Vision-Agents/acceleration/internal/stack"
)

// grantUsesRefused counts what a grant's owner check turned away, so a grant that pays for
// less than it admitted shows why.
var grantUsesRefused = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "router",
Name: "usage_grant_uses_refused_total",
Help: "Attempts to count a usage row toward a live grant of neither its organization nor its app, or scoped to another app, that had not counted it before",
})

// GrantUse is one row counted toward one grant: its billable cost, by its usage id.
// CustomerID and OrganizationID are the row's: a grant of neither the row's organization nor
// its app's counts nothing.
type GrantUse struct {
GrantID int64
UsageID uuid.UUID
Micros int64
GrantID int64
CustomerID string
OrganizationID string
UsageID uuid.UUID
Micros int64
}

// GrantUsesOf is what the rows written of requests (written names them by usage id) count
Expand All @@ -38,7 +52,7 @@ func GrantUsesOf(requests []Request, written []uuid.UUID) []GrantUse {
// Only the first of two rows sent with one usage id was written.
delete(wrote, request.UsageID)
for _, grant := range slices.Compact(slices.Sorted(slices.Values(request.LimitGrants))) {
uses = append(uses, GrantUse{GrantID: grant, UsageID: request.UsageID, Micros: request.CostMicros})
uses = append(uses, GrantUse{GrantID: grant, CustomerID: request.CustomerID, OrganizationID: request.OrganizationID, UsageID: request.UsageID, Micros: request.CostMicros})
}
}
return uses
Expand All @@ -65,8 +79,9 @@ const grantEndedBefore = "least(g.revoked_at, g.expires_at) < now() - make_inter

// InsertMany counts the rows toward their grants in the shared database: each (grant, usage
// id) once, however often it is sent, added to the grant's used_micros. A grant that is not
// there, or ended more than GrantUsesKept ago, counts nothing. It says how many it had not
// counted before.
// there, is of neither the row's organization nor its app's, is scoped to another app, or ended
// more than GrantUsesKept ago, counts nothing; one refused on whose it is is counted on
// /metrics. It says how many it had not counted before.
func (r *LimitGrantUses) InsertMany(ctx context.Context, uses []GrantUse) (int, error) {
added := 0
for start := 0; start < len(uses); start += grantUsesChunk {
Expand All @@ -90,10 +105,12 @@ func addGrantUses(ctx context.Context, tx bun.Tx, uses []GrantUse) (int, error)
return 0, nil
}
grants := make([]int64, len(uses))
customers := make([]string, len(uses))
organizations := make([]string, len(uses))
usageIDs := make([]string, len(uses))
micros := make([]int64, len(uses))
for i, use := range uses {
grants[i], usageIDs[i], micros[i] = use.GrantID, use.UsageID.String(), use.Micros
grants[i], customers[i], organizations[i], usageIDs[i], micros[i] = use.GrantID, use.CustomerID, use.OrganizationID, use.UsageID.String(), use.Micros
}
_, err := tx.NewSelect().Model((*LimitGrant)(nil)).
Column("lg.id").
Expand All @@ -104,14 +121,28 @@ func addGrantUses(ctx context.Context, tx bun.Tx, uses []GrantUse) (int, error)
if err != nil {
return 0, err
}
var added int
// A grant is the row's organization's, or that of the row's app as the enforcer drew it: the
Comment thread
thesyncim marked this conversation as resolved.
Resolved
// organization the app was last seen under, or for an app in none the app itself. Either
// one serves, since a row is written without its organization when the app could not be
// looked up, and the app's membership is recorded in the background after its first grant
// may be drawn. A grant scoped to an app is that app's alone.
var counts struct {
Added int `bun:"added"`
Refused int `bun:"refused"`
}
err = tx.NewRaw(`
WITH sent AS (
SELECT * FROM unnest(?::bigint[], ?::uuid[], ?::bigint[]) AS s (grant_id, usage_id, micros)
SELECT * FROM unnest(?::bigint[], ?::text[], ?::text[], ?::uuid[], ?::bigint[]) AS s (grant_id, customer_id, organization_id, usage_id, micros)
), granted AS (
SELECT s.grant_id, s.usage_id, s.micros,
g.organization_id IN (s.organization_id, coalesce(ao.organization_id, s.customer_id)) AND g.app_id IN ('', s.customer_id) AS owned
FROM sent s
LEFT JOIN app_organizations ao ON ao.app_id = s.customer_id
JOIN limit_grants g ON g.id = s.grant_id
WHERE NOT coalesce(`+grantEndedBefore+`, false)
), counted AS (
INSERT INTO limit_grant_uses (grant_id, usage_id, micros)
SELECT s.grant_id, s.usage_id, s.micros FROM sent s JOIN limit_grants g ON g.id = s.grant_id
WHERE NOT coalesce(`+grantEndedBefore+`, false)
SELECT grant_id, usage_id, micros FROM granted WHERE owned
ON CONFLICT DO NOTHING
RETURNING grant_id, micros
), raised AS (
Expand All @@ -120,8 +151,16 @@ WITH sent AS (
WHERE g.id = c.grant_id
RETURNING 1
)
SELECT count(*) FROM counted`, pgdialect.Array(grants), pgdialect.Array(usageIDs), pgdialect.Array(micros), GrantUsesKept.Seconds()).Scan(ctx, &added)
return added, err
SELECT (SELECT count(*) FROM counted) AS added,
(SELECT count(*) FROM granted r WHERE NOT r.owned
AND NOT EXISTS (SELECT 1 FROM limit_grant_uses u WHERE u.grant_id = r.grant_id AND u.usage_id = r.usage_id)) AS refused`,
pgdialect.Array(grants), pgdialect.Array(customers), pgdialect.Array(organizations), pgdialect.Array(usageIDs), pgdialect.Array(micros),
GrantUsesKept.Seconds()).Scan(ctx, &counts)
if err != nil {
return 0, err
}
grantUsesRefused.Add(float64(counts.Refused))
return counts.Added, nil
}

// Prune forgets which rows counted toward the grants that expired or were revoked
Expand Down Expand Up @@ -249,7 +288,7 @@ func (r *LimitGrantUses) Reconcile(ctx context.Context) (int, error) {

// reconcileGrantsQuery is one page of a shard's rows naming a grant, on from (started_at, id),
// read by requests_limit_grants_started_idx.
const reconcileGrantsQuery = "SELECT id, started_at, usage_id, cost_micros, limit_grants FROM requests " +
const reconcileGrantsQuery = "SELECT id, started_at, customer_id, organization_id, usage_id, cost_micros, limit_grants FROM requests " +
"WHERE limit_grants <> '{}' AND (started_at, id) > (?, ?) AND billable AND cost_micros > 0 AND usage_id IS NOT NULL " +
"ORDER BY started_at, id LIMIT ?"

Expand All @@ -268,11 +307,13 @@ func (r *LimitGrantUses) reconcileShard(ctx context.Context, shard bun.IDB, name
counted := 0
for {
var rows []struct {
ID int64 `bun:"id"`
StartedAt time.Time `bun:"started_at"`
UsageID uuid.UUID `bun:"usage_id"`
CostMicros int64 `bun:"cost_micros"`
LimitGrants []int64 `bun:"limit_grants,array"`
ID int64 `bun:"id"`
StartedAt time.Time `bun:"started_at"`
CustomerID string `bun:"customer_id"`
OrganizationID string `bun:"organization_id"`
UsageID uuid.UUID `bun:"usage_id"`
CostMicros int64 `bun:"cost_micros"`
LimitGrants []int64 `bun:"limit_grants,array"`
}
err := shard.NewRaw(reconcileGrantsQuery, after, afterID, grantReconcilePage).Scan(ctx, &rows)
if err != nil {
Expand All @@ -281,7 +322,7 @@ func (r *LimitGrantUses) reconcileShard(ctx context.Context, shard bun.IDB, name
var uses []GrantUse
for _, row := range rows {
for _, grant := range slices.Compact(slices.Sorted(slices.Values(row.LimitGrants))) {
uses = append(uses, GrantUse{GrantID: grant, UsageID: row.UsageID, Micros: row.CostMicros})
uses = append(uses, GrantUse{GrantID: grant, CustomerID: row.CustomerID, OrganizationID: row.OrganizationID, UsageID: row.UsageID, Micros: row.CostMicros})
}
}
n, err := r.InsertMany(ctx, uses)
Expand Down
Loading
Loading