Repository navigation
feat(rules): allow rule arrays to compose text from multiple files or parts - #1939
Walternater wants to merge 2 commits into
Conversation
… parts `rule` now accepts either a string (unchanged) or an array of strings. Each part is resolved exactly like the string form: inline text is kept as-is, while .md/.txt/.markdown references are resolved relative to the rule file's directory (512 KB per file) and replaced by their content. Parts are joined with a blank line in declaration order, so first-match and merge_system_rule stay per entry. A missing or unreadable part warns and is skipped; an all-empty result ignores the entry. Composed text is capped at 1 MiB with UTF-8-safe truncation. A string containing several path-looking tokens now warns and points at the array form. Fixes alibaba#1934
|
🔍 OpenCodeReview found 1 issue(s) in this PR.
|
| var parts []string | ||
| if err := json.Unmarshal(raw.Rule, &parts); err != nil { | ||
| return fmt.Errorf("rule must be a string or an array of strings: %w", err) | ||
| } | ||
| e.ruleParts = parts |
There was a problem hiding this comment.
When raw.Rule is a JSON array containing non-string elements (e.g., ["rules.md", 42]), json.Unmarshal(raw.Rule, &parts) returns an error that is correctly caught. However, if the array contains null elements (e.g., ["rules.md", null]), json.Unmarshal into []string succeeds with the null decoded as an empty string "". This empty string then passes through resolveRuleParts as inline text, producing a spurious blank-line-separated empty section in the composed rule.
Consider validating that no element in parts is empty after unmarshaling, or filtering out empty parts in resolveRuleParts.
|
A couple of notes while this waits for review:
@lizhengfeng101 when you get a chance — it is a self-contained config change with tests on the parser, the resolver, the 1 MiB cap and the canonical-config digest, plus the docs section in all five locales. Happy to split the |
…ction A null element in a rule array (["rules.md", null]) is decoded as "", so it passed through resolveRuleParts as inline text and left a blank blank-line-separated section in the composed prompt. Reject null and blank parts with the element index at unmarshal time, and skip them in resolveRuleParts for entries assembled without UnmarshalJSON.
|
Fixed the blank-part finding in What changed
I chose an error over silently filtering because a blank element is a config Tests (
Docs: one paragraph added to all five locales noting that null/empty Verification
AI-assisted (DeepSeek Harness, deepseek-v4.1-flash). |
Summary
.opencodereview/rule.jsonnow accepts an array forrule, in addition to the existing string form:{ "rules": [ { "path": "**/*.sql", "rule": ["sql-baseline.md", "Also flag destructive DDL."] } ] }ruleis unchanged byte-for-byte..md/.txt/.markdownpath is read relative to the rule file's directory (512 KB per file, symlink/traversal checks unchanged) and replaced by its content.merge_system_rulesemantics are unchanged.ocr rules checkandrule_config_sha256see the composed text, since resolution already mutates the entry before either runs.Tests
internal/config/rules/rule_array_test.go: string/array/absent/null parsing, rejection of non-string rule types, inline+file composition and ordering, missing-part skip, all-missing ignore, confinement, 1 MiB cap,merge_system_rulewith arrays, canonical config order sensitivity.TMPDIR=/private/tmp go test ./...— all packages pass.make check— passed.make coverage— 91.7% (threshold 90%).ocr rules check --rule <tmp>/rule.json src/app.sqlprints the composed text.Docs
pages/src/content/docs/{en,zh,ja,ko,ru}/review-rules.mddocument the array form, resolution rules, missing-file behavior, cap, and the ambiguous-string warning.Fixes #1934
AI disclosure
Implemented with DeepSeek Harness (
deepseek-v4.1-flash); I reviewed the diff and ran the tests above. No AI attribution trailers in the commit.