Changesets · Branches · Tags · Compare
| Changeset | 37eef76c38edbffbc99429ee653b7deae607ef8fd8c04fdfd30e66769a88f4b6 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-08-07 13:51 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-08-07 13:51 -07:00 |
| Parents | 091a2bd85f3c, 5f3f8decff54 |
7 files changed
modified packages/config-yaml/src/__tests__/index.test.ts +77 −5
| @@ -93,9 +93,11 @@ | ||
| 93 | 93 | const registry: Registry = { |
| 94 | 94 | getContent: async function (id: PackageIdentifier): Promise<string> { |
| 95 | 95 | const slug = packageIdentifierToShorthandSlug(id); |
| 96 | - return fs | |
| 97 | - .readFileSync(`./src/__tests__/packages/${slug}.yaml`) | |
| 98 | - .toString(); | |
| 96 | + const filePath = | |
| 97 | + id.uriType === "slug" | |
| 98 | + ? `./src/__tests__/packages/${slug}.yaml` | |
| 99 | + : id.filePath; | |
| 100 | + return fs.readFileSync(filePath).toString(); | |
| 99 | 101 | }, |
| 100 | 102 | }; |
| 101 | 103 | |
| @@ -230,7 +232,7 @@ | ||
| 230 | 232 | orgScopeId: "test-org", |
| 231 | 233 | currentUserSlug: "test-user", |
| 232 | 234 | onPremProxyUrl: null, |
| 233 | - // Add an injected block | |
| 235 | + // Add injected blocks | |
| 234 | 236 | injectBlocks: [ |
| 235 | 237 | { |
| 236 | 238 | uriType: "slug", |
| @@ -240,11 +242,16 @@ | ||
| 240 | 242 | versionSlug: "latest", |
| 241 | 243 | }, |
| 242 | 244 | }, |
| 245 | + { | |
| 246 | + uriType: "file", | |
| 247 | + filePath: "./src/__tests__/local-files/rules.yaml", | |
| 248 | + }, | |
| 243 | 249 | ], |
| 244 | 250 | }, |
| 245 | 251 | ); |
| 246 | 252 | |
| 247 | 253 | const config = unrolledConfig.config; |
| 254 | + const errors = unrolledConfig.errors; | |
| 248 | 255 | |
| 249 | 256 | // The original rules array should have two items |
| 250 | 257 | expect(config?.rules?.length).toBe(3); // Now 3 with the injected block |
| @@ -255,7 +262,72 @@ | ||
| 255 | 262 | ); |
| 256 | 263 | |
| 257 | 264 | // Check the injected doc block was added |
| 258 | - expect(config?.rules?.[2]).toBe("Be kind"); | |
| 265 | + expect( | |
| 266 | + typeof config?.rules?.[2] !== "string" && | |
| 267 | + config?.rules?.[2]?.rule === "Be humble", | |
| 268 | + ); | |
| 269 | + | |
| 270 | + // Check if we receive one error that is caused by duplicate rules | |
| 271 | + expect(errors?.length).toBe(1); | |
| 272 | + expect(errors?.[0].message.includes("Duplicate rules detected")); | |
| 273 | + }); | |
| 274 | + | |
| 275 | + it("duplicate detection should happen in the assistant config first and then the intected blocks", async () => { | |
| 276 | + const unrolledConfig = await unrollAssistant( | |
| 277 | + { | |
| 278 | + uriType: "file", | |
| 279 | + filePath: "./src/__tests__/local-files/duplicate-test-assistant.yaml", | |
| 280 | + }, | |
| 281 | + registry, | |
| 282 | + { | |
| 283 | + renderSecrets: true, | |
| 284 | + platformClient, | |
| 285 | + orgScopeId: "test-org", | |
| 286 | + currentUserSlug: "test-user", | |
| 287 | + onPremProxyUrl: null, | |
| 288 | + // Add injected blocks | |
| 289 | + injectBlocks: [ | |
| 290 | + { | |
| 291 | + uriType: "file", | |
| 292 | + filePath: "./src/__tests__/local-files/rules.yaml", | |
| 293 | + }, | |
| 294 | + { | |
| 295 | + uriType: "file", | |
| 296 | + filePath: "./src/__tests__/local-files/mcpServer.yaml", | |
| 297 | + }, | |
| 298 | + { | |
| 299 | + uriType: "file", | |
| 300 | + filePath: "./src/__tests__/local-files/prompt.yaml", | |
| 301 | + }, | |
| 302 | + ], | |
| 303 | + }, | |
| 304 | + ); | |
| 305 | + | |
| 306 | + const config = unrolledConfig.config; | |
| 307 | + const errors = unrolledConfig.errors; | |
| 308 | + | |
| 309 | + // Check if all the duplicate blocks get removed | |
| 310 | + expect(config?.models?.length).toBe(1); | |
| 311 | + expect(config?.context?.length).toBe(1); | |
| 312 | + expect(config?.mcpServers?.length).toBe(1); | |
| 313 | + expect(config?.rules?.length).toBe(1); | |
| 314 | + expect(config?.prompts?.length).toBe(1); | |
| 315 | + expect(config?.docs?.length).toBe(1); | |
| 316 | + | |
| 317 | + // Check if there are 8 duplication detected | |
| 318 | + expect(errors?.length).toBe(8); | |
| 319 | + | |
| 320 | + // Beginning of the assistant config duplication check | |
| 321 | + expect( | |
| 322 | + errors?.[0].message.includes("Duplicate models named gpt-5 detected"), | |
| 323 | + ).toBe(true); | |
| 324 | + // Beginning of the injected blocks duplication check | |
| 325 | + expect(errors?.[4].message.includes("Duplicate rules detected")).toBe(true); | |
| 326 | + expect( | |
| 327 | + errors?.[6].message.includes( | |
| 328 | + "Duplicate mcpServers named Browser search detected", | |
| 329 | + ), | |
| 330 | + ).toBe(true); | |
| 259 | 331 | }); |
| 260 | 332 | |
| 261 | 333 | it.skip("should prioritize org over user / package secrets", () => {}); |
added packages/config-yaml/src/__tests__/local-files/duplicate-test-assistant.yaml +39 −0
| @@ -0,0 +1,39 @@ | ||
| 1 | +name: Duplicate Test Assistant | |
| 2 | +version: 0.0.1 | |
| 3 | +schema: v1 | |
| 4 | + | |
| 5 | +docs: | |
| 6 | + - name: React | |
| 7 | + startUrl: https://react.dev | |
| 8 | + - name: React | |
| 9 | + startUrl: https://react.dev | |
| 10 | + | |
| 11 | +rules: | |
| 12 | + - Be humble | |
| 13 | + - uses: ./src/__tests__/local-files/rules.yaml | |
| 14 | + | |
| 15 | +models: | |
| 16 | + - name: gpt-5 | |
| 17 | + provider: openai | |
| 18 | + model: gpt-5 | |
| 19 | + apiKey: ${{ secrets.OPENAI_API_KEY }} | |
| 20 | + roles: | |
| 21 | + - chat | |
| 22 | + | |
| 23 | + - name: gpt-5 | |
| 24 | + provider: openai | |
| 25 | + model: gpt-5 | |
| 26 | + apiKey: ${{ secrets.OPENAI_API_KEY }} | |
| 27 | + roles: | |
| 28 | + - chat | |
| 29 | + | |
| 30 | +mcpServers: | |
| 31 | + - uses: ./src/__tests__/local-files/mcpServer.yaml | |
| 32 | + | |
| 33 | +prompts: | |
| 34 | + - uses: ./src/__tests__/local-files/prompt.yaml | |
| 35 | + - uses: ./src/__tests__/local-files/prompt.yaml | |
| 36 | + | |
| 37 | +context: | |
| 38 | + - provider: doc | |
| 39 | + - provider: doc |
added packages/config-yaml/src/__tests__/local-files/mcpServer.yaml +8 −0
| @@ -0,0 +1,8 @@ | ||
| 1 | +name: Local MCP Server | |
| 2 | +version: 0.0.1 | |
| 3 | +schema: v1 | |
| 4 | +mcpServers: | |
| 5 | + - name: Browser search | |
| 6 | + command: npx | |
| 7 | + args: | |
| 8 | + - "@playwright/mcp@latest" |
added packages/config-yaml/src/__tests__/local-files/prompt.yaml +7 −0
| @@ -0,0 +1,7 @@ | ||
| 1 | +name: Local prompt | |
| 2 | +version: 0.0.1 | |
| 3 | +schema: v1 | |
| 4 | +prompts: | |
| 5 | + - name: duplication test | |
| 6 | + description: test purpose | |
| 7 | + prompt: Please check that this prompt should only appear once |
added packages/config-yaml/src/__tests__/local-files/rules.yaml +6 −0
| @@ -0,0 +1,6 @@ | ||
| 1 | +name: Rules | |
| 2 | +version: 0.0.1 | |
| 3 | +schema: v1 | |
| 4 | + | |
| 5 | +rules: | |
| 6 | + - Be humble |
added packages/config-yaml/src/load/blockDuplicationDetector.ts +76 −0
| @@ -0,0 +1,76 @@ | ||
| 1 | +import { BLOCK_TYPES, BlockType } from "./getBlockType.js"; | |
| 2 | + | |
| 3 | +export class BlockDuplicationDetector { | |
| 4 | + private records: Map<string, Set<string>>; | |
| 5 | + | |
| 6 | + constructor() { | |
| 7 | + this.records = new Map(); | |
| 8 | + for (const blockType of BLOCK_TYPES) { | |
| 9 | + this.records.set(blockType, new Set()); | |
| 10 | + } | |
| 11 | + } | |
| 12 | + | |
| 13 | + private isRuleDuplicated(rule: any): boolean { | |
| 14 | + if (typeof rule === "string") { | |
| 15 | + return this.check(rule, "rules"); | |
| 16 | + } else { | |
| 17 | + return this.check(rule.name, "rules"); | |
| 18 | + } | |
| 19 | + } | |
| 20 | + | |
| 21 | + private isContextDuplicated(context: any): boolean { | |
| 22 | + return this.check(context.provider, "context"); | |
| 23 | + } | |
| 24 | + | |
| 25 | + private isCommonBlockDuplicated(block: any, blockType: BlockType): boolean { | |
| 26 | + return this.check(block.name, blockType); | |
| 27 | + } | |
| 28 | + | |
| 29 | + private check(identifier: string, blockType: BlockType): boolean { | |
| 30 | + if (this.records.get(blockType)!.has(identifier)) { | |
| 31 | + return true; | |
| 32 | + } else { | |
| 33 | + this.records.get(blockType)!.add(identifier); | |
| 34 | + return false; | |
| 35 | + } | |
| 36 | + } | |
| 37 | + | |
| 38 | + // Check if the name is duplicated within the same blockType | |
| 39 | + isDuplicated( | |
| 40 | + block: any, | |
| 41 | + blockType: BlockType, | |
| 42 | + injectError?: (errorMsg: string) => void, | |
| 43 | + ): boolean { | |
| 44 | + // Not checking any null or undefined object | |
| 45 | + if (block === null || block === undefined) { | |
| 46 | + return false; | |
| 47 | + } | |
| 48 | + | |
| 49 | + switch (blockType) { | |
| 50 | + case "rules": | |
| 51 | + if (this.isRuleDuplicated(block)) { | |
| 52 | + injectError?.( | |
| 53 | + `Duplicate rules${typeof block === "string" ? "" : ` named ${block.name}`} detected. The duplicate one has been deleted for preventing unexpected behavior.`, | |
| 54 | + ); | |
| 55 | + return true; | |
| 56 | + } | |
| 57 | + return false; | |
| 58 | + case "context": | |
| 59 | + if (this.isContextDuplicated(block)) { | |
| 60 | + injectError?.( | |
| 61 | + `Duplicate ${block.provider} context providers detected. The duplicate one has been deleted for preventing unexpected behavior.`, | |
| 62 | + ); | |
| 63 | + return true; | |
| 64 | + } | |
| 65 | + return false; | |
| 66 | + default: | |
| 67 | + if (this.isCommonBlockDuplicated(block, blockType)) { | |
| 68 | + injectError?.( | |
| 69 | + `Duplicate ${blockType} named ${block.name} detected. The duplicate one has been deleted for preventing unexpected behavior.`, | |
| 70 | + ); | |
| 71 | + return true; | |
| 72 | + } | |
| 73 | + return false; | |
| 74 | + } | |
| 75 | + } | |
| 76 | +} |
modified packages/config-yaml/src/load/unroll.ts +29 −6
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | Rule, |
| 21 | 21 | } from "../schemas/index.js"; |
| 22 | 22 | import { ConfigResult, ConfigValidationError } from "../validation.js"; |
| 23 | +import { BlockDuplicationDetector } from "./blockDuplicationDetector.js"; | |
| 23 | 24 | import { |
| 24 | 25 | packageIdentifierToShorthandSlug, |
| 25 | 26 | useProxyForUnrenderedSecrets, |
| @@ -306,6 +307,13 @@ | ||
| 306 | 307 | ): Promise<ConfigResult<AssistantUnrolled>> { |
| 307 | 308 | const errors: ConfigValidationError[] = []; |
| 308 | 309 | |
| 310 | + function injectDuplicationError(errorMsg: string) { | |
| 311 | + errors.push({ | |
| 312 | + fatal: false, | |
| 313 | + message: errorMsg, | |
| 314 | + }); | |
| 315 | + } | |
| 316 | + | |
| 309 | 317 | const unrolledAssistant: AssistantUnrolled = { |
| 310 | 318 | name: assistant.name, |
| 311 | 319 | version: assistant.version, |
| @@ -484,7 +492,7 @@ | ||
| 484 | 492 | const injectedResults = await Promise.all(injectedBlockPromises); |
| 485 | 493 | const injectedErrors: ConfigValidationError[] = []; |
| 486 | 494 | const injectedBlocks: { |
| 487 | - blockType: string; | |
| 495 | + blockType: BlockType; | |
| 488 | 496 | resolvedBlock: any; |
| 489 | 497 | source?: string; |
| 490 | 498 | }[] = []; |
| @@ -521,16 +529,27 @@ | ||
| 521 | 529 | errors.push(...rulesResult.errors); |
| 522 | 530 | errors.push(...injectedResult.errors); |
| 523 | 531 | |
| 532 | + const detector = new BlockDuplicationDetector(); | |
| 533 | + | |
| 524 | 534 | // Assign section results |
| 525 | 535 | for (const sectionResult of sectionResults) { |
| 526 | 536 | if (sectionResult.blocks) { |
| 527 | - unrolledAssistant[sectionResult.section] = sectionResult.blocks; | |
| 537 | + unrolledAssistant[sectionResult.section] = sectionResult.blocks.filter( | |
| 538 | + (block) => | |
| 539 | + !detector.isDuplicated( | |
| 540 | + block, | |
| 541 | + sectionResult.section, | |
| 542 | + injectDuplicationError, | |
| 543 | + ), | |
| 544 | + ); | |
| 528 | 545 | } |
| 529 | 546 | } |
| 530 | 547 | |
| 531 | 548 | // Assign rules result |
| 532 | 549 | if (rulesResult.rules) { |
| 533 | - unrolledAssistant.rules = rulesResult.rules; | |
| 550 | + unrolledAssistant.rules = rulesResult.rules.filter( | |
| 551 | + (rule) => !detector.isDuplicated(rule, "rules", injectDuplicationError), | |
| 552 | + ); | |
| 534 | 553 | } |
| 535 | 554 | |
| 536 | 555 | // Add injected blocks |
| @@ -539,16 +558,20 @@ | ||
| 539 | 558 | resolvedBlock, |
| 540 | 559 | source, |
| 541 | 560 | } of injectedResult.injectedBlocks) { |
| 542 | - const key = blockType as BlockType; | |
| 561 | + const key = blockType; | |
| 543 | 562 | if (!unrolledAssistant[key]) { |
| 544 | 563 | unrolledAssistant[key] = []; |
| 545 | 564 | } |
| 546 | - const blocksWithSourceFiles = injectLocalSourceFile( | |
| 565 | + | |
| 566 | + const filteredBlocks = injectLocalSourceFile( | |
| 547 | 567 | key, |
| 548 | 568 | resolvedBlock, |
| 549 | 569 | source, |
| 570 | + ).filter( | |
| 571 | + (block: any) => | |
| 572 | + !detector.isDuplicated(block, blockType, injectDuplicationError), | |
| 550 | 573 | ); |
| 551 | - unrolledAssistant[key]?.push(...blocksWithSourceFiles); | |
| 574 | + unrolledAssistant[key]?.push(...filteredBlocks); | |
| 552 | 575 | } |
| 553 | 576 | |
| 554 | 577 | const configResult: ConfigResult<AssistantUnrolled> = { |