Skip to content

feat(rules): allow rule arrays to compose text from multiple files or parts - #1939

Open
Walternater wants to merge 2 commits into
alibaba:mainfrom
Walternater:feat/rule-array
Open

Walternater wants to merge 2 commits into
alibaba:mainfrom
Walternater:feat/rule-array

Conversation

@Walternater

Copy link
Copy Markdown
Contributor

Summary

.opencodereview/rule.json now accepts an array for rule, in addition to the existing string form:

{
  "rules": [
    { "path": "**/*.sql", "rule": ["sql-baseline.md", "Also flag destructive DDL."] }
  ]
}
  • A string rule is unchanged byte-for-byte.
  • Each array part is resolved exactly like the string form: inline text is kept as-is; a value that looks like a .md/.txt/.markdown path is read relative to the rule file's directory (512 KB per file, symlink/traversal checks unchanged) and replaced by its content.
  • Parts are joined with a blank line in declaration order; composition happens within one entry, so first-match behavior and merge_system_rule semantics are unchanged.
  • A missing/unreadable file part warns and is skipped; the remaining parts are used. If no part contributes text, the entry is ignored.
  • Composed text is capped at 1 MiB and truncated with a warning (UTF-8 safe).
  • A string containing several path-looking tokens is still inline (historical behavior) but now warns and suggests the array form.
  • ocr rules check and rule_config_sha256 see the composed text, since resolution already mutates the entry before either runs.

Tests

  • New 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_rule with arrays, canonical config order sensitivity.
  • TMPDIR=/private/tmp go test ./... — all packages pass.
  • make check — passed.
  • make coverage — 91.7% (threshold 90%).
  • Manual smoke: ocr rules check --rule <tmp>/rule.json src/app.sql prints the composed text.

Docs

pages/src/content/docs/{en,zh,ja,ko,ru}/review-rules.md document 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.

… 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
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment on lines +245 to +249
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · low
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.

@Walternater

Copy link
Copy Markdown
Contributor Author

A couple of notes while this waits for review:

  1. The bot inline finding on system_rules.go:249 is real but low impact, and I would rather fix it here than leave it for a follow-up: a JSON array with a null element decodes to an empty string in []string, and that empty string then reaches resolveRuleParts as inline text, producing a stray blank section in the composed rule. Fix is to reject or drop empty parts — I will push that plus a table case.

  2. State check: rebased-off main is now 0f5a284 (feature still not on main, ProjectRuleEntry.Rule is still a plain string there), all 15 checks green, CLA signed.

@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 null fix into its own commit if you want to review it separately.

…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.
@Walternater

Copy link
Copy Markdown
Contributor Author

Fixed the blank-part finding in e250088 — thanks, it was a real one.

What changed

encoding/json decodes a null element as "", so ["rules.md", null] came
through as a part with no text and resolveRuleParts joined it in, leaving a
blank section in the composed prompt.

  1. ProjectRuleEntry.UnmarshalJSON now rejects an empty or whitespace-only
    array element with its index:
    rule[1] must not be empty: a null or blank part would compose an empty section, so leave the element out instead
  2. resolveRuleParts also skips blank parts, which covers entries assembled in
    Go without going through UnmarshalJSON.

I chose an error over silently filtering because a blank element is a config
mistake, and the index tells you which one to remove; "skip it quietly" would
hide ["rules.md", null] where a file write went wrong.

Tests (internal/config/rules/rule_array_test.go)

  • TestProjectRuleEntry_UnmarshalRejectsBlankArrayPart: null, "", and " "
    elements, each asserting the index in the message.
  • TestResolveRuleEntries_ArraySkipsBlankParts: Go-constructed entry with blank
    parts composes "PRESENT\n\nINLINE".
  • ["rule":[]] added to the accepted-forms table (empty array is still valid,
    just an entry with no text).

Docs: one paragraph added to all five locales noting that null/empty
elements are a config error.

Verification

  • go test ./... — all packages pass
  • make check — check passed (go mod tidy, gofmt -s, vet, non-English scan)

AI-assisted (DeepSeek Harness, deepseek-v4.1-flash).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(rules): compose multiple rule files in a single path entry

1 participant