forked from autotest/autotest
-
Notifications
You must be signed in to change notification settings - Fork 0
Add MCP doctor for deterministic parameter resolution #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
binrogithub
wants to merge
1
commit into
master
Choose a base branch
from
codex/create-doctor.py-for-resolving-parameters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """hc_agent package.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """MCP helpers for hc_agent.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| """Resolve missing MCP parameters using deterministic rules. | ||
|
|
||
| Rules order: | ||
| 1) Explicit values already in ctx win. | ||
| 2) Tagged defaults (e.g. "default", "recommended") are preferred. | ||
| 3) Stable tie-break among remaining candidates. | ||
|
|
||
| This module only performs read-only listing calls provided by callers. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any, Callable, Dict, Iterable, List, Mapping, MutableMapping, Optional, Sequence, Set | ||
|
|
||
| DEFAULT_TAGS: Set[str] = {"default", "recommended"} | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Candidate: | ||
| """A possible value for a missing parameter.""" | ||
|
|
||
| value: Any | ||
| tags: Set[str] | ||
| raw: Any | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Resolution: | ||
| """Resolution result with a context patch and human-readable explanations.""" | ||
|
|
||
| ctx_patch: Dict[str, Any] | ||
| explanations: List[str] | ||
|
|
||
|
|
||
| def _coerce_candidate(item: Any) -> Candidate: | ||
| if isinstance(item, Candidate): | ||
| return item | ||
| if isinstance(item, Mapping): | ||
| value = item.get("value") | ||
| if value is None: | ||
| for key in ("name", "id", "key"): | ||
| if key in item: | ||
| value = item[key] | ||
| break | ||
| tags = set(item.get("tags", []) or []) | ||
| return Candidate(value=value, tags=tags, raw=item) | ||
| return Candidate(value=item, tags=set(), raw=item) | ||
|
|
||
|
|
||
| def _stable_key(candidate: Candidate) -> str: | ||
| return f"{repr(candidate.value)}|{sorted(candidate.tags)}" | ||
|
|
||
|
|
||
| def resolve_missing_parameters( | ||
| ctx: Mapping[str, Any], | ||
| missing: Iterable[str], | ||
| list_calls: Mapping[str, Callable[[], Sequence[Any]]], | ||
| *, | ||
| default_tags: Optional[Set[str]] = None, | ||
| ) -> Resolution: | ||
| """Resolve missing parameters and return a ctx_patch with explanations. | ||
|
|
||
| Args: | ||
| ctx: Current context containing explicit values. | ||
| missing: Names of parameters that need resolution. | ||
| list_calls: Mapping of parameter -> callable that returns candidate values. | ||
| These callables must only perform read-only listing operations. | ||
| default_tags: Optional override for which tags represent defaults. | ||
| """ | ||
|
|
||
| tags = default_tags or DEFAULT_TAGS | ||
| ctx_patch: Dict[str, Any] = {} | ||
| explanations: List[str] = [] | ||
|
|
||
| for param in missing: | ||
| if param in ctx and ctx[param] is not None: | ||
| explanations.append( | ||
| f"{param}: explicit value '{ctx[param]}' retained; no resolution needed." | ||
| ) | ||
| continue | ||
|
|
||
| list_call = list_calls.get(param) | ||
| if list_call is None: | ||
| explanations.append(f"{param}: no listing call available; left unresolved.") | ||
| continue | ||
|
|
||
| candidates_raw = list_call() or [] | ||
| candidates = [_coerce_candidate(item) for item in candidates_raw] | ||
| if not candidates: | ||
| explanations.append(f"{param}: listing returned no candidates; left unresolved.") | ||
| continue | ||
|
|
||
| tagged = [c for c in candidates if c.tags.intersection(tags)] | ||
| pool = tagged if tagged else candidates | ||
| chosen = sorted(pool, key=_stable_key)[0] | ||
| ctx_patch[param] = chosen.value | ||
| if tagged: | ||
| explanations.append( | ||
| f"{param}: selected tagged default '{chosen.value}' from {len(tagged)} candidates." | ||
| ) | ||
| else: | ||
| explanations.append( | ||
| f"{param}: selected '{chosen.value}' via stable tie-break from {len(candidates)} candidates." | ||
| ) | ||
|
|
||
| return Resolution(ctx_patch=ctx_patch, explanations=explanations) | ||
|
|
||
|
|
||
| def apply_ctx_patch(ctx: MutableMapping[str, Any], patch: Mapping[str, Any]) -> None: | ||
| """Apply a ctx patch in-place.""" | ||
|
|
||
| ctx.update(patch) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When callers pass an empty set to
default_tagsto disable tagged-default preference,tags = default_tags or DEFAULT_TAGStreats the empty set as falsy and silently falls back toDEFAULT_TAGS. This makes it impossible to opt out of tagged defaults, and the resolution will still prefer tagged candidates even though the API suggests the override should be honored. Use an explicitif default_tags is Nonecheck soset()is respected.Useful? React with 👍 / 👎.