| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | // PolicyVersion is the current policy version. Version 1 remains readable. |
| 8 | const PolicyVersion = 2 |
| 9 | |
| 10 | // Byte limits. Payloads, events and artifacts above these are refused |
| 11 | // before any state changes. |
| 12 | const ( |
| 13 | MaxPayloadBytes = 64 << 10 |
| 14 | MaxArtifactBytes = 1 << 20 |
| 15 | MaxEventBytes = 16 << 10 |
| 16 | MaxSteps = 16 |
| 17 | ) |
| 18 | |
| 19 | // Policy is a job's finite execution policy. It is bound at creation, |
| 20 | // stored with the job, and compared by version at every admission. The |
| 21 | // Rates make the currency reservation explicit: a reservation's cost is |
| 22 | // computed from its token and call maxima. Subscription currency is unavailable. |
| 23 | type Policy struct { |
| 24 | Version int `json:"version"` |
| 25 | UsageMode string `json:"usage_mode,omitempty"` |
| 26 | RequireDistinctReviewFamily bool `json:"require_distinct_review_family,omitempty"` |
| 27 | // Job caps the job's own ledger (its reservations plus its children's). |
| 28 | Job Limits `json:"job"` |
| 29 | // Account caps the account's ledger across all its jobs. |
| 30 | Account Limits `json:"account"` |
| 31 | // PerCall caps one provider call's reservation. Calls is 1. |
| 32 | PerCall Limits `json:"per_call"` |
| 33 | Rates Rates `json:"rates"` |
| 34 | |
| 35 | MaxAttemptsPerStep int `json:"max_attempts_per_step"` |
| 36 | MaxConcurrentAttempts int `json:"max_concurrent_attempts"` // per job and per account |
| 37 | MaxDepth int `json:"max_depth"` // 1: a job may have children but no grandchildren |
| 38 | LeaseSeconds int `json:"lease_seconds"` |
| 39 | AdmissionSeconds int `json:"admission_seconds"` |
| 40 | JobSeconds int `json:"job_seconds"` |
| 41 | AttemptSeconds int `json:"attempt_seconds"` |
| 42 | |
| 43 | // RequireDistinctReviewProvider makes a review count only when its |
| 44 | // provider differs from the candidate's. The pilot has it on; turning it |
| 45 | // off needs a policy grant and is not the fixture's business. |
| 46 | RequireDistinctReviewProvider bool `json:"require_distinct_review_provider"` |
| 47 | } |
| 48 | |
| 49 | // Rates is the versioned price table in micro-units per token or call. |
| 50 | type Rates struct { |
| 51 | Version int `json:"version"` |
| 52 | InputMicrosPer int64 `json:"input_micros_per_token"` |
| 53 | OutputMicrosPer int64 `json:"output_micros_per_token"` |
| 54 | CallMicrosPer int64 `json:"call_micros"` |
| 55 | ForgeMicrosPer int64 `json:"forge_call_micros"` |
| 56 | TrackerMicrosPer int64 `json:"tracker_call_micros"` |
| 57 | } |
| 58 | |
| 59 | // DefaultPolicy returns the pilot ceilings from the spec: two concurrent |
| 60 | // attempts, depth one, two attempts per step, 24 provider calls, 45 minutes |
| 61 | // per job and 20 per attempt, 200,000 input and 40,000 generated tokens in |
| 62 | // total, 32,000 input and 4,000 generated per call. The cost cap and rates |
| 63 | // are the caller's: there is no default currency ceiling. |
| 64 | func DefaultPolicy(costCapMicros int64, rates Rates) Policy { |
| 65 | return Policy{ |
| 66 | Version: 1, |
| 67 | Job: Limits{Calls: 24, InputTokens: 200_000, OutputTokens: 40_000, CostMicros: costCapMicros}, |
| 68 | Account: Limits{Calls: 24, InputTokens: 200_000, OutputTokens: 40_000, CostMicros: costCapMicros}, |
| 69 | PerCall: Limits{Calls: 1, InputTokens: 32_000, OutputTokens: 4_000, CostMicros: costCapMicros}, |
| 70 | Rates: rates, |
| 71 | MaxAttemptsPerStep: 2, |
| 72 | MaxConcurrentAttempts: 2, |
| 73 | MaxDepth: 1, |
| 74 | LeaseSeconds: 60, |
| 75 | AdmissionSeconds: 5, |
| 76 | JobSeconds: 45 * 60, |
| 77 | AttemptSeconds: 20 * 60, |
| 78 | RequireDistinctReviewProvider: true, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Validate checks finite limits and the accounting rules for each version. |
| 83 | func (p Policy) Validate() error { |
| 84 | if p.Version != 1 && p.Version != PolicyVersion { |
| 85 | return fail(CodeUnknownKind, "policy version %d", p.Version) |
| 86 | } |
| 87 | if p.Version == 1 && (p.UsageMode != "" || p.RequireDistinctReviewFamily) { |
| 88 | return fail(CodeInvalid, "v2 fields in v1 policy") |
| 89 | } |
| 90 | if p.Version == 2 && !p.RequireDistinctReviewFamily { |
| 91 | return fail(CodeInvalid, "review_family_required") |
| 92 | } |
| 93 | if p.Version == 2 && p.UsageMode != "metered" && p.UsageMode != "subscription" { |
| 94 | return fail(CodeUnknownKind, "usage mode %q", p.UsageMode) |
| 95 | } |
| 96 | for name, l := range map[string]Limits{"job": p.Job, "account": p.Account, "per_call": p.PerCall} { |
| 97 | if p.UsageMode == "subscription" { |
| 98 | if l.CostMicros != 0 { |
| 99 | return fail(CodeInvalid, "subscription currency must be unavailable") |
| 100 | } |
| 101 | l.CostMicros = 1 |
| 102 | } |
| 103 | if err := l.validate(name); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | } |
| 107 | if p.Version == 2 && p.UsageMode == "metered" && (p.Rates.InputMicrosPer <= 0 || p.Rates.OutputMicrosPer <= 0 || p.Rates.CallMicrosPer <= 0) { |
| 108 | return fail(CodeInvalid, "metered rates must be positive") |
| 109 | } |
| 110 | if p.UsageMode == "subscription" && p.Rates != (Rates{}) { |
| 111 | return fail(CodeInvalid, "subscription has no currency rates") |
| 112 | } |
| 113 | if p.PerCall.Calls != 1 { |
| 114 | return fail(CodeInvalid, "per_call.calls must be 1") |
| 115 | } |
| 116 | if p.UsageMode != "subscription" && (p.Rates.Version <= 0 || p.Rates.InputMicrosPer < 0 || p.Rates.OutputMicrosPer < 0 || p.Rates.CallMicrosPer < 0 || p.Rates.ForgeMicrosPer < 0 || p.Rates.TrackerMicrosPer < 0) { |
| 117 | return fail(CodeInvalid, "rates need a positive version and non-negative prices") |
| 118 | } |
| 119 | if p.MaxAttemptsPerStep <= 0 || p.MaxConcurrentAttempts <= 0 || p.MaxDepth < 0 || p.LeaseSeconds <= 0 || p.AdmissionSeconds <= 0 || p.AdmissionSeconds > 5 || p.JobSeconds <= 0 || p.AttemptSeconds <= 0 { |
| 120 | return fail(CodeInvalid, "policy counts and durations must be positive; admission at most 5s") |
| 121 | } |
| 122 | if p.MaxDepth > 1 { |
| 123 | return fail(CodeInvalid, "this prototype supports at most one child-job level") |
| 124 | } |
| 125 | maxSeconds := int64(time.Duration(1<<63-1) / time.Second) |
| 126 | for _, seconds := range []int{p.LeaseSeconds, p.AdmissionSeconds, p.JobSeconds, p.AttemptSeconds} { |
| 127 | if int64(seconds) > maxSeconds { |
| 128 | return fail(CodeInvalid, "policy duration exceeds time.Duration") |
| 129 | } |
| 130 | } |
| 131 | // A per-call reservation must itself be priceable without overflow. |
| 132 | if _, err := p.price(OpProviderCall, Usage{Calls: 1, InputTokens: p.PerCall.InputTokens, OutputTokens: p.PerCall.OutputTokens}); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | return nil |
| 136 | } |
| 137 | |
| 138 | // ParsePolicy decodes a stored or supplied policy. Unknown fields and |
| 139 | // unknown versions fail closed. |
| 140 | func ParsePolicy(raw []byte) (Policy, error) { |
| 141 | var p Policy |
| 142 | if err := strictJSON(raw, &p); err != nil { |
| 143 | return Policy{}, fail(CodeUnknownKind, "policy: %v", err) |
| 144 | } |
| 145 | if err := p.Validate(); err != nil { |
| 146 | return Policy{}, err |
| 147 | } |
| 148 | return p, nil |
| 149 | } |
| 150 | |
| 151 | func (p Policy) lease() time.Duration { return time.Duration(p.LeaseSeconds) * time.Second } |
| 152 | func (p Policy) admission() time.Duration { return time.Duration(p.AdmissionSeconds) * time.Second } |
| 153 | |
| 154 | // price fills Usage.CostMicros from the token and call maxima of a |
| 155 | // reservation at the policy's rates, with overflow checks. The caller's own |
| 156 | // CostMicros is added on top (an adapter-declared fixed charge). |
| 157 | func (p Policy) price(op Operation, u Usage) (Usage, error) { |
| 158 | if p.UsageMode == "subscription" { |
| 159 | if !op.external() { |
| 160 | return Usage{}, fail(CodeUnknownKind, "unpriced operation") |
| 161 | } |
| 162 | u.CostMicros = 0 |
| 163 | u.CostUnavailable = true |
| 164 | return u, nil |
| 165 | } |
| 166 | u.CostUnavailable = false |
| 167 | var perCall int64 |
| 168 | switch op { |
| 169 | case OpProviderCall: |
| 170 | perCall = p.Rates.CallMicrosPer |
| 171 | case OpForgePublish: |
| 172 | perCall = p.Rates.ForgeMicrosPer |
| 173 | case OpTrackerPublish: |
| 174 | perCall = p.Rates.TrackerMicrosPer |
| 175 | default: |
| 176 | return Usage{}, fail(CodeUnknownKind, "operation %q has no price", op) |
| 177 | } |
| 178 | in, err := mulChecked(u.InputTokens, p.Rates.InputMicrosPer) |
| 179 | if err != nil { |
| 180 | return Usage{}, err |
| 181 | } |
| 182 | out, err := mulChecked(u.OutputTokens, p.Rates.OutputMicrosPer) |
| 183 | if err != nil { |
| 184 | return Usage{}, err |
| 185 | } |
| 186 | calls, err := mulChecked(u.Calls, perCall) |
| 187 | if err != nil { |
| 188 | return Usage{}, err |
| 189 | } |
| 190 | total := u.CostMicros |
| 191 | for _, v := range []int64{in, out, calls} { |
| 192 | if total, err = addChecked(total, v); err != nil { |
| 193 | return Usage{}, err |
| 194 | } |
| 195 | } |
| 196 | u.CostMicros = total |
| 197 | return u, nil |
| 198 | } |
| 199 | |
| 200 | // reserveLimits is the per-reservation ceiling for an operation. Provider |
| 201 | // calls are bounded by PerCall; forge and tracker publications carry no |
| 202 | // tokens and exactly one call. |
| 203 | func (p Policy) reserveLimits(op Operation) Limits { |
| 204 | switch op { |
| 205 | case OpProviderCall: |
| 206 | return p.PerCall |
| 207 | default: |
| 208 | return Limits{Calls: 1, InputTokens: 0, OutputTokens: 0, CostMicros: p.PerCall.CostMicros} |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // RunnerPolicy requires explicit token limits calibrated for the runner. |
| 213 | func RunnerPolicy(mode string, limits, perTurn Limits, rates Rates) (Policy, error) { |
| 214 | p := Policy{ |
| 215 | Version: PolicyVersion, UsageMode: mode, |
| 216 | Job: limits, Account: limits, PerCall: perTurn, Rates: rates, |
| 217 | MaxAttemptsPerStep: 2, MaxConcurrentAttempts: 2, MaxDepth: 0, |
| 218 | LeaseSeconds: 60, AdmissionSeconds: 5, JobSeconds: 2700, AttemptSeconds: 1200, |
| 219 | RequireDistinctReviewProvider: true, RequireDistinctReviewFamily: true, |
| 220 | } |
| 221 | p.Job.Calls = min(p.Job.Calls, 24) |
| 222 | if err := p.Validate(); err != nil { |
| 223 | return Policy{}, err |
| 224 | } |
| 225 | return p, nil |
| 226 | } |
| 227 | |