Repository navigation
feat(qdl): YAML 序列化契约——类型标签 + Coeff 双态 + ISO 8601 时长 - #6
Conversation
- 全部 QDL 类型补齐真实 struct tag(yaml 键名与 Intent §2.1 字段名一致) - Coeff 双态:数值标量→常量、字符串→ParamRef;空串引用与映射/序列形态拒绝 - Duration:ISO 8601(PT5H/P7D/P1DT12H,大小写不敏感、日期/时间单位越界拒绝) 与 Go 原生时长双格式,Window.Length 改用该类型承接种子 plan 手写窗长 - go-yaml v1.19.2 转直接依赖(首批依赖提案已获批)
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (19)
📝 WalkthroughWalkthrough新增 QDL YAML 类型系统和加载器。实现字段映射、自定义时长与系数编解码、默认值规范化、枚举校验、 ChangesQDL YAML 契约与编解码
Possibly related PRs
Suggested labels: ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
PR Summary by Qodo建立 QDL YAML 序列化契约与双态标量编解码
AI Description
Diagram
High-Level Assessment
Files changed (15)
|
Code Review by Qodo
1. Negative window lengths accepted
|
| RefillRate Coeff `yaml:"refill_rate"` // token_bucket:单位/秒 | ||
| Burst Coeff `yaml:"burst"` // token_bucket:突发容量 |
There was a problem hiding this comment.
1. Window refs bypass validation 📘 Rule violation ≡ Correctness
The newly YAML-decodable Window.RefillRate and Window.Burst coefficients are not checked against PlanSpec.Parameters. An unresolved ParamRef in either field can therefore survive PlanSpec.Validate.
Agent Prompt
## Issue description
Validate YAML-decoded `Window.RefillRate` and `Window.Burst` references against `PlanSpec.Parameters`.
## Issue Context
`Coeff.UnmarshalYAML` converts strings into parameter references, but `PlanSpec.Validate` currently validates bucket capacity and charge coefficients without validating these two window coefficients.
## Fix Focus Areas
- internal/qdl/window.go[40-41]
- internal/qdl/plan.go[121-148]
- internal/qdl/plan_test.go[1-60]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| SnapCandidates []float64 `yaml:"snap_candidates"` | ||
| Frozen bool `yaml:"frozen"` // gauge 锚定参数不再更新 |
There was a problem hiding this comment.
2. Non-gauge parameters allow freezing 📘 Rule violation ≡ Correctness
The new frozen YAML contract accepts Frozen: true for non-gauge parameter provenance because validation rejects only vendor_doc. Parameters sourced from vendor_api, estimated, or assumed can therefore violate the gauge-only restriction.
Agent Prompt
## Issue description
Reject every frozen parameter whose provenance is not `gauge`.
## Issue Context
The model documents `gauge` as the only provenance allowed to be frozen, but validation currently rejects only the `vendor_doc` combination. Add coverage for all non-gauge provenance values decoded through YAML.
## Fix Focus Areas
- internal/qdl/param.go[8-17]
- internal/qdl/param.go[69-74]
- internal/qdl/plan.go[107-120]
- internal/qdl/plan_test.go[45-55]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| Buckets []Bucket `yaml:"buckets"` | ||
| Channels []Channel `yaml:"channels"` | ||
| Grants []Grant `yaml:"grants"` | ||
| Parameters []Parameter `yaml:"parameters"` |
There was a problem hiding this comment.
3. Planspec remains directly mutable 📘 Rule violation ≡ Correctness
The YAML contract decodes loaded plans into exported mutable slices such as Buckets, Channels, Grants, and Parameters. Consumers can mutate the loaded specification directly, including through lookup methods that return pointers to slice elements.
Agent Prompt
## Issue description
Prevent consumers from directly mutating collections belonging to a loaded `PlanSpec`.
## Issue Context
Use a mutable YAML decoding DTO or custom unmarshalling during construction, then expose an immutable PlanSpec API. Collection accessors should return copies, and lookup methods should not return pointers into internal slices.
## Fix Focus Areas
- internal/qdl/plan.go[41-56]
- internal/qdl/plan.go[59-95]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| PriceUSDPerPeriod float64 `yaml:"price_usd_per_period"` | ||
| Period string `yaml:"period"` // month | year | week | one_off | ||
| SpecVersion string `yaml:"spec_version"` | ||
| VendorDocSnapshotHash string `yaml:"vendor_doc_snapshot_hash"` // 页面语义 diff 用 |
There was a problem hiding this comment.
4. Struct-tag fix lacks regression 📘 Rule violation ▣ Testability
The PR explicitly fixes QDL YAML tags that were previously ineffective, but no test decodes or encodes the actual tagged QDL structures. The added YAML tests use local holder structs, so they would not fail if the production struct-tag fixes were removed.
Agent Prompt
## Issue description
Add a regression test that exercises the corrected YAML tags on actual QDL model types.
## Issue Context
The PR description identifies ineffective struct tags as a bug fix. Construct representative snake_case YAML for `PlanSpec` and nested QDL types, decode it strictly, and assert the corresponding production fields; the test must fail when the tag changes are reverted.
## Fix Focus Areas
- internal/qdl/plan.go[41-56]
- internal/qdl/plan_test.go[1-60]
- internal/qdl/coeff_yaml_test.go[9-34]
- internal/qdl/duration_test.go[45-59]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| if len(s) > 0 && (s[0] == 'P' || s[0] == 'p') { | ||
| return parseISODuration(s) | ||
| } | ||
| return time.ParseDuration(s) |
There was a problem hiding this comment.
5. Negative window lengths accepted 🐞 Bug ≡ Correctness
ParseDuration sends non-P input directly to time.ParseDuration, so YAML such as length: -5h decodes successfully even though a window length cannot be negative. PlanSpec.Validate does not inspect Window.Length, allowing the invalid value to reach downstream window logic.
Agent Prompt
## Issue description
Reject negative durations regardless of whether they use ISO 8601 or Go syntax, so `Window.Length` cannot load as a negative interval.
## Issue Context
The ISO path checks negativity, but the Go-format path directly returns `time.ParseDuration`, which accepts values such as `-5h`. Add coverage for negative Go-format values.
## Fix Focus Areas
- internal/qdl/duration.go[35-40]
- internal/qdl/duration_test.go[38-42]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| if body[i] == 't' && !inTime { | ||
| inTime = true | ||
| i++ | ||
| continue |
There was a problem hiding this comment.
6. Malformed iso durations accepted 🐞 Bug ≡ Correctness
parseISODuration treats T as an optional free-standing separator and does not enforce component order, uniqueness, or the week-only form, so malformed values such as P1DT, P1D1Y, P1D2D, P1W1D, and PT1H1H are accepted despite the documented P[nY][nM][nW][nD][T[nH][nM][nS]] grammar. This violates the advertised ISO 8601 serialization and loader contract by silently normalizing malformed configuration into a different valid duration string.
Agent Prompt
## Issue description
Make `parseISODuration` enforce its declared ISO 8601 grammar instead of independently summing arbitrary recognized components. It must reject a free-standing `T`, duplicate or out-of-order units, and combinations of the week form with other date components.
## Issue Context
The parser currently accepts malformed inputs including `P1DT`, `P1D1Y`, `P1D2D`, `P1W1D`, and `PT1H1H`, then normalizes them to a duration rather than rejecting them under the loader contract. Require at least one time component after `T`, reject every component that is duplicated or appears after a smaller-order component while retaining the existing date/time-side checks, enforce the week-only form, and add negative tests for each malformed shape.
## Fix Focus Areas
- internal/qdl/duration.go[43-105]
- internal/qdl/duration_test.go[10-43]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| total += time.Duration(n * 365 * 24 * float64(time.Hour)) | ||
| case 'm': // 日期部分=月(名义 30 天),时间部分=分钟 | ||
| if inTime { | ||
| total += time.Duration(n * float64(time.Minute)) | ||
| } else { | ||
| total += time.Duration(n * 30 * 24 * float64(time.Hour)) |
There was a problem hiding this comment.
7. Duration overflow returns success 🐞 Bug ≡ Correctness
ISO components are scaled as float64, converted directly to time.Duration, and accumulated without bounds checks, allowing unbounded or repeated inputs to overflow the signed nanosecond count and return an unrelated zero or positive duration. For example, the accepted P200Y200Y200Y adds three individually representable 200-year values whose cumulative overflow wraps positive, bypassing the final total < 0 check.
Agent Prompt
## Issue description
Reject ISO duration components and totals that cannot be represented by `time.Duration`, rather than allowing unchecked conversion or accumulation to return a wrapped duration successfully.
## Issue Context
The parser accepts unbounded magnitudes and repeated components, scales them as `float64`, converts them directly to `time.Duration`, and adds them without range checks. Validate each scaled component against the remaining `time.Duration` range before conversion and addition, reject non-finite parsed values, and do not rely on the final negative-total check; `P200Y200Y200Y` demonstrates cumulative overflow that wraps positive. Add boundary tests around the approximately 290-year representable range and tests for multi-component overflow.
## Fix Focus Areas
- internal/qdl/duration.go[65-112]
- internal/qdl/duration_test.go[10-43]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
LoadBytes 四段管线:泛型树 $ref 展开(JSON pointer 扩展,数组段支持下标 与 id 匹配,深拷贝防共享,深度上限防环)→ goccy 严格解码 → normalizeDefaults 缺省回填 → PlanSpec.Validate(安全契约唯一执法点)。 - Quantize/分布缺省:goccy 对键缺失字段不调用自定义 unmarshaler, 缺省统一在 normalizeDefaults 补齐(mode=none/step=1),保证往返稳定 - 类别型离散分布:probs+categories 书写形态归一到 CategoryProbs - 全 slice/map 字段 omitempty:消除 nil vs empty 的往返差 - 测试:黄金样本抽查、往返等价、负面用例 10 项、$ref 错误专列、 链式引用、fuzz(往返不变量)
概要
A3(QDL YAML loader)的第一步:为全部 QDL 类型建立 YAML 序列化契约,是 loader(下一个 PR)的地基。
Coeff = float | ParamRef):PT5H/P7D/P1DT12H)与 Go 原生(5h);日期单位不得越过T、时间单位不得在T前;Window.Length改用该类型验证
make check全绿(gofmt / go vet / archlint / go test -race)关联 ROADMAP:Phase 0 / A3 前置。
Summary by CodeRabbit
新功能
$ref引用展开、默认值补全、规范化及严格配置校验。测试