Elffuss BraidHub

kikocis/continue public

Merge pull request #6679 from fbricon/restore-config-validation

fix: restore invalid config error reporting
Changesetad83f3cc53014d5a0d0dffc459aab24775ba47e806dacf7fd37685577131a90a
AuthorDallin Romney <dallinromney@gmail.com> · 2025-07-18 17:12 +02:00
CommitterGitHub <noreply@github.com> · 2025-07-18 17:12 +02:00
Parentsbeec9aab9cdf, 36062bf22f47

Browse the files at this changeset

2 files changed

added packages/config-yaml/src/load/unroll.test.ts +91 −0
@@ -0,0 +1,91 @@
1+import { PackageIdentifier } from "../interfaces/slugs.js";
2+import { parseMarkdownRuleOrAssistantUnrolled } from "./unroll.js";
3+
4+describe("parseMarkdownRuleOrAssistantUnrolled tests", () => {
5+ it("parses valid YAML content as AssistantUnrolled", () => {
6+ const mockId: PackageIdentifier = {
7+ uriType: "file",
8+ filePath: "./foo/bar.yaml",
9+ };
10+ const yamlContent = `
11+name: Test Assistant
12+version: 1.0.0
13+
14+models:
15+ - name: model
16+ model: modelname
17+ provider: ollama
18+`;
19+ const result = parseMarkdownRuleOrAssistantUnrolled(yamlContent, mockId);
20+ expect(result).toHaveProperty("name", "Test Assistant");
21+ expect(result).toHaveProperty("version", "1.0.0");
22+ expect(result.models?.length).toBe(1);
23+ expect(result.models?.[0]).toHaveProperty("name", "model");
24+ });
25+
26+ it("parses markdown rule content as AssistantUnrolled", () => {
27+ const mockId: PackageIdentifier = {
28+ uriType: "file",
29+ filePath: "./foo/bar.md",
30+ };
31+ const markdownContent = `
32+---
33+name: rulename
34+description: my rule description
35+pattern: "**"
36+---
37+This is the rule
38+`;
39+ const result = parseMarkdownRuleOrAssistantUnrolled(
40+ markdownContent,
41+ mockId,
42+ );
43+ expect(result).toHaveProperty("name");
44+ expect(result).toHaveProperty("version");
45+ expect(Array.isArray(result.rules)).toBe(true);
46+ expect(result.rules).toBeDefined();
47+ expect(result.rules!.length).toBe(1);
48+ const rule = result.rules![0];
49+ expect(rule).toHaveProperty("name", "rulename");
50+ expect(rule).toHaveProperty("description", "my rule description");
51+ expect(rule).toHaveProperty("globs", "./foo/**/*");
52+ expect(rule).toHaveProperty("rule", "This is the rule");
53+ });
54+
55+ it("throws error for invalid content in yaml file", () => {
56+ const mockId: PackageIdentifier = {
57+ uriType: "file",
58+ filePath: "./foo/bar.yaml",
59+ };
60+ const invalidContent = `
61+name: Test Assistant
62+version: 1.0.0
63+
64+model: # should be models
65+ - name: model
66+ model: modelname
67+ provider: ollama
68+`;
69+ expect(() =>
70+ parseMarkdownRuleOrAssistantUnrolled(invalidContent, mockId),
71+ ).toThrow();
72+ });
73+
74+ it("Every non-YAML file is a rule", () => {
75+ const mockId: PackageIdentifier = {
76+ uriType: "file",
77+ filePath: "./foo/bar",
78+ };
79+ const dubiousContent = `This is not \nproper #YAML#`;
80+ const result = parseMarkdownRuleOrAssistantUnrolled(dubiousContent, mockId);
81+ expect(result).toHaveProperty("name");
82+ expect(result).toHaveProperty("version");
83+ expect(Array.isArray(result.rules)).toBe(true);
84+ expect(result.rules).toBeDefined();
85+ expect(result.rules!.length).toBe(1);
86+ const rule = result.rules![0];
87+ expect(rule).toHaveProperty("name", "foo/bar");
88+ expect(rule).toHaveProperty("globs", "./foo/**/*");
89+ expect(rule).toHaveProperty("rule", dubiousContent);
90+ });
91+});
modified packages/config-yaml/src/load/unroll.ts +25 −52
@@ -286,9 +286,7 @@
286286 options.platformClient,
287287 options.alwaysUseProxy,
288288 );
289- const renderedYaml = renderTemplateData(templatedYaml, {
290- secrets,
291- });
289+ const renderedYaml = renderTemplateData(templatedYaml, { secrets });
292290
293291 // Parse again and replace models with proxy versions where secrets weren't rendered
294292 const finalConfig = useProxyForUnrenderedSecrets(
@@ -298,11 +296,7 @@
298296 options.onPremProxyUrl,
299297 );
300298
301- return {
302- config: finalConfig,
303- errors,
304- configLoadInterrupted,
305- };
299+ return { config: finalConfig, errors, configLoadInterrupted };
306300 }
307301
308302 export async function unrollBlocks(
@@ -366,10 +360,7 @@
366360 return {
367361 index,
368362 block: null,
369- error: {
370- fatal: false,
371- message: msg,
372- },
363+ error: { fatal: false, message: msg },
373364 };
374365 }
375366 } else {
@@ -461,11 +452,7 @@
461452 registry,
462453 );
463454
464- return {
465- blockType,
466- resolvedBlock,
467- error: null,
468- };
455+ return { blockType, resolvedBlock, error: null };
469456 } catch (err) {
470457 let msg = "";
471458 if (injectBlock.uriType === "file") {
@@ -481,10 +468,7 @@
481468 return {
482469 blockType: null,
483470 resolvedBlock: null,
484- error: {
485- fatal: false,
486- message: msg,
487- },
471+ error: { fatal: false, message: msg },
488472 };
489473 }
490474 });
@@ -581,57 +565,46 @@
581565 return parseMarkdownRuleOrAssistantUnrolled(templatedYaml, id);
582566 }
583567
584-function parseMarkdownRuleOrAssistantUnrolled(
568+export function parseMarkdownRuleOrAssistantUnrolled(
585569 content: string,
586570 id: PackageIdentifier,
587571 ): AssistantUnrolled {
588- // Try to parse as YAML first, then as markdown rule if that fails
589- let parsedYaml: AssistantUnrolled;
590- try {
591- parsedYaml = parseBlock(content);
592- } catch (yamlError) {
593- // If YAML parsing fails, try parsing as markdown rule
594- try {
595- const rule = markdownToRule(content, id);
596- // Convert the rule object to the expected format
597- parsedYaml = {
598- name: rule.name,
599- version: "1.0.0",
600- rules: [rule],
601- };
602- } catch (markdownError) {
603- // If both fail, throw the original YAML error
604- throw yamlError;
605- }
606- }
607-
608- return parsedYaml;
572+ return parseYamlOrMarkdownRule<AssistantUnrolled>(content, id, parseBlock);
609573 }
610574
611575 function parseMarkdownRuleOrConfigYaml(
612576 content: string,
613577 id: PackageIdentifier,
614578 ): ConfigYaml {
615- // Try to parse as YAML first, then as markdown rule if that fails
616- let parsedYaml: ConfigYaml;
579+ return parseYamlOrMarkdownRule<ConfigYaml>(content, id, parseConfigYaml);
580+}
581+
582+function parseYamlOrMarkdownRule<T>(
583+ content: string,
584+ id: PackageIdentifier,
585+ parseYamlFn: (content: string) => T,
586+): T {
587+ let parsedYaml: T;
617588 try {
618- parsedYaml = parseConfigYaml(content);
589+ // Try to parse as YAML first, then as markdown rule if that fails
590+ parsedYaml = parseYamlFn(content);
619591 } catch (yamlError) {
592+ if (
593+ id.uriType === "file" &&
594+ [".yaml", ".yml"].some((ext) => id.filePath.endsWith(ext))
595+ ) {
596+ throw yamlError;
597+ }
620598 // If YAML parsing fails, try parsing as markdown rule
621599 try {
622600 const rule = markdownToRule(content, id);
623601 // Convert the rule object to the expected format
624- parsedYaml = {
625- name: rule.name,
626- version: "1.0.0",
627- rules: [rule],
628- };
602+ parsedYaml = { name: rule.name, version: "1.0.0", rules: [rule] } as T;
629603 } catch (markdownError) {
630604 // If both fail, throw the original YAML error
631605 throw yamlError;
632606 }
633607 }
634-
635608 return parsedYaml;
636609 }
637610