-
Notifications
You must be signed in to change notification settings - Fork 427
Generalize early wildcard-target validation across safe-outputs MCP tools #39300
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f9cd020
Plan wildcard safe-output validation
Copilot 4564b6f
Add wildcard target validation to safe-outputs MCP
Copilot 30049ce
Generalize wildcard safe-output target validation
Copilot 0b4b90f
Polish schema-driven wildcard validation
Copilot 5bc2938
Document wildcard validation helpers
Copilot ed1abeb
Read safe output tools JSON explicitly
Copilot 16e6792
Merge branch 'main' into copilot/add-custom-validation-safe-outputs
pelikhan 7ca7e32
Expand wildcard target requirements for additional safe output tools
Copilot f333d9e
Document wildcard target requirements in safe-outputs specification
Copilot 3edf762
Copy safe_outputs_tools.json into safeoutputs setup destination
Copilot 96a8f85
Merge branch 'main' into copilot/add-custom-validation-safe-outputs
github-actions[bot] 3ad28b3
Add changeset
github-actions[bot] 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -25,6 +25,18 @@ const { sanitizeTitle, applyTitlePrefix } = require("./sanitize_title.cjs"); | |
| const { parseDeduplicateByTitle, normalizeTitleForDedup, findDuplicateByTitle } = require("./issue_title_dedup.cjs"); | ||
| const { validateCreatePullRequestIntent, validatePushToPullRequestBranchIntent, validateCreateIssueIntent, validateAddCommentIntent } = require("./intent_probe.cjs"); | ||
| const { globPatternToRegex } = require("./glob_pattern_helpers.cjs"); | ||
| /** | ||
| * Read and parse a JSON file. | ||
| * @param {string} filePath | ||
| * @returns {any} | ||
| */ | ||
| function readJSONFile(filePath) { | ||
| return JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Building the Map once at module load is efficient; a brief comment on its purpose would aid readability. |
||
|
|
||
| const safeOutputsTools = readJSONFile(path.join(__dirname, "safe_outputs_tools.json")); | ||
|
|
||
| const safeOutputsToolMap = new Map(safeOutputsTools.map(tool => [tool.name, tool])); | ||
|
|
||
| /** | ||
| * @param {string} error | ||
|
|
@@ -60,12 +72,30 @@ function buildMissingTemporaryIdError(toolName, configKey) { | |
| return `${toolName} requires 'temporary_id' when safe-outputs.${configKey}.require-temporary-id is enabled. Set temporary_id (for example "${example}") and retry.`; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Record<string, any>} safeOutputsConfig | ||
| * @param {string} toolName | ||
| * @returns {Record<string, any>} | ||
| */ | ||
| function getSafeOutputsToolConfig(safeOutputsConfig, toolName) { | ||
| return safeOutputsConfig?.[toolName] || safeOutputsConfig?.[toolName.replace(/_/g, "-")] || {}; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Record<string, any>} entry | ||
| * @param {string[]} fieldNames | ||
| * @returns {boolean} | ||
| */ | ||
| function hasExplicitAddCommentTargetNumber(entry) { | ||
| return ["item_number", "pr_number", "pr"].some(field => entry[field] !== undefined && entry[field] !== null && String(entry[field]).trim() !== ""); | ||
| function hasExplicitTargetParameter(entry, fieldNames) { | ||
| return fieldNames.some(field => entry[field] !== undefined && entry[field] !== null && String(entry[field]).trim() !== ""); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} toolName | ||
| * @returns {{primary?: string, anyOf?: string[]} | null} | ||
| */ | ||
| function getWildcardTargetRequirement(toolName) { | ||
| return safeOutputsToolMap.get(toolName)?.["x-safe-outputs-target-requirements"]?.["*"] || null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -154,8 +184,34 @@ function resolvePatchWorkspacePath(workspacePath) { | |
| */ | ||
| function createHandlers(server, appendSafeOutput, config = {}) { | ||
| const TOKEN_THRESHOLD = 16000; | ||
| const addCommentConfig = config.add_comment || config["add-comment"] || {}; | ||
| const wildcardAddCommentTargetRequiresItemNumber = addCommentConfig.target === "*"; | ||
|
|
||
| /** | ||
| * Validate schema-declared explicit target parameters for wildcard-target tools. | ||
| * @param {Record<string, any>} entry | ||
| * @returns {{content: Array<{type: "text", text: string}>, isError: true} | null} | ||
| */ | ||
| const validateWildcardTargetRequirement = entry => { | ||
| const toolName = entry?.type; | ||
| const requirement = getWildcardTargetRequirement(toolName); | ||
| if (!requirement) { | ||
| return null; | ||
| } | ||
|
|
||
| const toolConfig = getSafeOutputsToolConfig(config, toolName); | ||
| if (toolConfig.target !== "*") { | ||
| return null; | ||
| } | ||
|
|
||
| const anyOf = Array.isArray(requirement.anyOf) ? requirement.anyOf : []; | ||
| if (anyOf.length === 0 || hasExplicitTargetParameter(entry, anyOf)) { | ||
| return null; | ||
| } | ||
|
|
||
| const configKey = toolName.replace(/_/g, "-"); | ||
| const primary = requirement.primary || anyOf[0]; | ||
| const guidance = anyOf.length === 1 ? primary : `one of: ${anyOf.join(", ")}`; | ||
| return buildIntentErrorResponse(`${toolName} requires ${primary} when safe-outputs.${configKey}.target is '*'. Provide ${guidance} and retry.`); | ||
| }; | ||
|
|
||
| /** | ||
| * Detect and offload large string fields to files. | ||
|
|
@@ -204,6 +260,10 @@ function createHandlers(server, appendSafeOutput, config = {}) { | |
| */ | ||
| const defaultHandler = type => args => { | ||
| const entry = { ...(args || {}), type }; | ||
| const wildcardTargetValidationError = validateWildcardTargetRequirement(entry); | ||
| if (wildcardTargetValidationError) { | ||
| return wildcardTargetValidationError; | ||
| } | ||
| const largeContentResponse = maybeHandleLargeContent(entry); | ||
| if (largeContentResponse) return largeContentResponse; | ||
|
|
||
|
|
@@ -811,6 +871,10 @@ function createHandlers(server, appendSafeOutput, config = {}) { | |
| // Drop it so the agent cannot override the derived source branch. | ||
| const { branch: _agentBranch, ...sanitizedArgs } = args || {}; | ||
| const entry = { ...sanitizedArgs, type: "push_to_pull_request_branch" }; | ||
| const wildcardTargetValidationError = validateWildcardTargetRequirement(entry); | ||
| if (wildcardTargetValidationError) { | ||
| return wildcardTargetValidationError; | ||
| } | ||
|
|
||
| // Resolve target repo configuration and validate the target repo early | ||
| // This is needed before getBaseBranch to ensure we resolve the base branch | ||
|
|
@@ -1561,10 +1625,9 @@ function createHandlers(server, appendSafeOutput, config = {}) { | |
|
|
||
| // Build the entry with a temporary_id | ||
| const entry = { ...(args || {}), type: "add_comment" }; | ||
| if (wildcardAddCommentTargetRequiresItemNumber) { | ||
| if (!hasExplicitAddCommentTargetNumber(entry)) { | ||
| return buildIntentErrorResponse("add_comment requires item_number when safe-outputs.add-comment.target is '*'. Provide item_number (or pr_number/pr alias)."); | ||
| } | ||
| const wildcardTargetValidationError = validateWildcardTargetRequirement(entry); | ||
| if (wildcardTargetValidationError) { | ||
| return wildcardTargetValidationError; | ||
| } | ||
| const intentValidationError = validateAddCommentIntent(entry); | ||
| if (intentValidationError) { | ||
|
|
@@ -1619,7 +1682,9 @@ function createHandlers(server, appendSafeOutput, config = {}) { | |
| // Increment only after the default handler returns successfully; if it throws | ||
| // (e.g. due to large-content rejection or an append write error) the counter | ||
| // must not advance so the empty-review guard remains accurate. | ||
| inlineReviewCommentCount++; | ||
| if (!result?.isError) { | ||
| inlineReviewCommentCount++; | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
Nice helper — consider validating the file exists before parsing to give a clearer error on missing config.