Skip to content

enhance(ai): make it.contextType() discoverable to the rule agent - #760

Merged
tangcent merged 1 commit into
masterfrom
feat/756-contexttype-discoverability
Aug 31, 2026
Merged

enhance(ai): make it.contextType() discoverable to the rule agent#760
tangcent merged 1 commit into
masterfrom
feat/756-contexttype-discoverability

Conversation

@tangcent

Copy link
Copy Markdown
Owner

Summary

Implements the three concrete changes requested in #756 so the AI rule-authoring agent uses the built-in it.contextType() discriminator instead of Groovy MOP probing:

  • RuleScriptContextCatalog — when a binding can hold more than one context kind (e.g. custom.method.is.api runs with it as a class or a method), its description now states the discriminator explicitly: "Discriminate with it.contextType(), which returns 'method'/'class'." The values are derived from a new ItKind.contextType property that mirrors the ScriptItContext hierarchy ("unknown" for empty contexts, "param" for parameters).
  • agent-base.md — the "Class identity in Groovy is context-sensitive" section now documents contextType() and its return values, and tells the model not to probe the method surface with respondsTo('containingClass').
  • RuleProposalValidator — new soft warning (never blocks) when a proposal uses respondsTo(, suggesting it.contextType() instead. Shares the line-mapping helper with the existing name() warning.

The issue's longer-term item (a description field on ScriptMethodApi / few-shot examples of built-in extension scripts) is deliberately out of scope.

Fixes #756

Testing

  • RuleScriptContextCatalogTest — 3 new cases: multi-kind it binding carries the hint with correct runtime values; single-kind binding carries none; custom.method.* keys state 'method'/'class'.
  • RuleProposalValidatorTest — 4 new cases: inline respondsTo( warns; respondsTo( inside a groovy value-block warns with the right line number; respondsTo( in a comment does not warn; a clean contextType() rule does not warn.
  • Verified no golden-file or prompt-guard regression: AgentBaseCatalogIdGuardTest, RuleAuthoringKnowledgeSemanticsTest, PerceptionToolsTest, RuleProposalValidatorDisabledSourceTest, CustomRuleKeysTest all green.

Risks / rollback

  • Pure prompt/contract surface: no runtime rule evaluation changes. Existing proposals gain at most a soft warning, never a block.
  • Single-commit revert if needed.

AI-authored rules for custom frameworks probed the context kind with
Groovy meta-programming (`it.respondsTo('containingClass')`) instead
of the built-in discriminator, producing non-idiomatic rules that
would break silently if a class context ever grew a method named
containingClass().

Root cause: the official discriminator was invisible to the model —
contextType() appeared only as a bare signature among 100+ reflected
methods, the system prompt never mentioned it, and nothing warned
against respondsTo().

Solution: get_rule_context now states the discriminator in the it
binding description whenever a key accepts several context kinds
("Discriminate with it.contextType(), which returns 'class'/
'method'/…"); the agent prompt documents contextType() in the
class-identity section; and the proposal validator emits a soft
warning on respondsTo( suggesting contextType().

Impact: generated rules become idiomatic and robust; existing
proposals only gain a soft warning and are never blocked. The
ScriptMethodApi description field from the issue's longer-term item
is deliberately not included.

Fixes: #756
@github-actions github-actions Bot added the type: enhancement New feature or request label Aug 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Plugin has been packaged for this PR. You can download easy-api-3.1.9.252.0.zip from the GitHub Actions workflow run by clicking on the "Artifacts" dropdown.

@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.821%. Comparing base (f2acdc3) to head (1d6f72d).

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##            master      #760       +/-   ##
=============================================
+ Coverage   66.795%   66.821%   +0.026%     
=============================================
  Files          442       442               
  Lines        25936     25947       +11     
  Branches      6191      6193        +2     
=============================================
+ Hits         17324     17338       +14     
+ Misses        6291      6289        -2     
+ Partials      2321      2320        -1     
Flag Coverage Δ
unittests 66.821% <100.000%> (+0.026%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...angcent/easyapi/core/rule/RuleProposalValidator.kt 92.453% <100.000%> (+2.257%) ⬆️
...yapi/core/rule/context/RuleScriptContextCatalog.kt 87.397% <100.000%> (+0.526%) ⬆️

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f2acdc3...1d6f72d. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tangcent
tangcent merged commit 840be6a into master Aug 31, 2026
12 checks passed
@tangcent
tangcent deleted the feat/756-contexttype-discoverability branch August 31, 2026 11:49
This was referenced Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Make it.contextType() discoverable to the AI rule agent (avoid respondsTo() in generated rules)

1 participant