Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): align duplicate yaml key validation

xd-bobo0cdd0e11593a
diff --git a/apps/desktop/resources/system-skills/cindy-skill-creator/scripts/_frontmatter.py b/apps/desktop/resources/system-skills/cindy-skill-creator/scripts/_frontmatter.py
index de23a16ceb..b8fbf34940 100644
--- a/apps/desktop/resources/system-skills/cindy-skill-creator/scripts/_frontmatter.py
+++ b/apps/desktop/resources/system-skills/cindy-skill-creator/scripts/_frontmatter.py
@@ -1,7 +1,10 @@
"""Frontmatter parsing backed by the vendored PyYAML runtime."""
+import math
import re
import sys
+from datetime import date, datetime, time, timezone
+from decimal import Decimal
from pathlib import Path
@@ -70,8 +73,43 @@ def split_frontmatter(content):
return match.group(1), match.end()
-def _reject_duplicate_mapping_keys(node, visited=None):
- """Match js-yaml's default duplicate-key rejection before construction."""
+def _js_number_key(value):
+ """Return JavaScript's property-key spelling for a YAML number."""
+ try:
+ number = float(value)
+ except OverflowError:
+ return "-Infinity" if value < 0 else "Infinity"
+ if math.isnan(number):
+ return "NaN"
+ if math.isinf(number):
+ return "-Infinity" if number < 0 else "Infinity"
+ if number == 0:
+ return "0"
+ magnitude = abs(number)
+ if 1e-6 <= magnitude < 1e21:
+ return format(Decimal(repr(number)), "f")
+ return re.sub(r"e([+-])0+(\d+)$", r"e\1\2", repr(number).lower())
+
+
+def _js_scalar_key(value):
+ """Mirror js-yaml 3's String(keyNode) mapping-key identity."""
+ if value is None:
+ return "null"
+ if isinstance(value, bool):
+ return "true" if value else "false"
+ if isinstance(value, (int, float)):
+ return _js_number_key(value)
+ if isinstance(value, datetime):
+ normalized = value if value.tzinfo else value.replace(tzinfo=timezone.utc)
+ return f"date:{normalized.astimezone(timezone.utc).isoformat()}"
+ if isinstance(value, date):
+ normalized = datetime.combine(value, time.min, tzinfo=timezone.utc)
+ return f"date:{normalized.isoformat()}"
+ return str(value)
+
+
+def _reject_duplicate_mapping_keys(loader, node, visited=None):
+ """Match js-yaml's duplicate check using each scalar's constructed value."""
if visited is None:
visited = set()
identity = id(node)
@@ -83,26 +121,32 @@ def _reject_duplicate_mapping_keys(node, visited=None):
keys = set()
for key_node, value_node in node.value:
if isinstance(key_node, ScalarNode):
- key = (key_node.tag, key_node.value)
+ key = _js_scalar_key(loader.construct_object(key_node, deep=True))
if key in keys:
raise FrontmatterError(
f"Duplicate mapping key '{key_node.value}' on line {key_node.start_mark.line + 1}"
)
keys.add(key)
- _reject_duplicate_mapping_keys(key_node, visited)
- _reject_duplicate_mapping_keys(value_node, visited)
+ _reject_duplicate_mapping_keys(loader, key_node, visited)
+ _reject_duplicate_mapping_keys(loader, value_node, visited)
elif isinstance(node, SequenceNode):
for value_node in node.value:
- _reject_duplicate_mapping_keys(value_node, visited)
+ _reject_duplicate_mapping_keys(loader, value_node, visited)
def parse_frontmatter(frontmatter_text):
+ loader = None
try:
- root = yaml.compose(frontmatter_text, Loader=FrontmatterLoader)
+ loader = FrontmatterLoader(frontmatter_text)
+ root = loader.get_single_node()
if root is not None:
- _reject_duplicate_mapping_keys(root)
- return yaml.load(frontmatter_text, Loader=FrontmatterLoader)
+ _reject_duplicate_mapping_keys(loader, root)
+ return loader.construct_document(root)
+ return None
except FrontmatterError:
raise
except yaml.YAMLError as exc:
raise FrontmatterError(str(exc)) from exc
+ finally:
+ if loader is not None:
+ loader.dispose()
diff --git a/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts b/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts
index da645f3a40..e7357b6a73 100644
--- a/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts
@@ -64,7 +64,7 @@ describe('built-in Skills', () => {
const link = path.join(input.homeDir, '.agents', 'skills', 'cindy-skill-creator');
expect(first.changed).toBe(true);
expect(first.warnings).toEqual([]);
- expect(manifest).toMatchObject({ schemaVersion: 3, bundleVersion: 8 });
+ expect(manifest).toMatchObject({ schemaVersion: 3, bundleVersion: 9 });
expect(descriptor.absolutePath).toBe(
path.join(root, '.versions', manifest.activeBundle, 'cindy-skill-creator'),
);
@@ -99,7 +99,7 @@ describe('built-in Skills', () => {
expect(fs.existsSync(path.join(descriptor.absolutePath, 'SKILL.md'))).toBe(true);
fs.appendFileSync(path.join(input.source, 'SKILL.md'), '\nUpdated\n');
- const updated = await prepareAndProjectBuiltInSkills({ ...input, bundleVersion: 9 });
+ const updated = await prepareAndProjectBuiltInSkills({ ...input, bundleVersion: 10 });
expect(updated.changed).toBe(true);
const updatedDescriptor = updated.descriptors.find(
(item) => item.name === 'cindy-skill-creator',
@@ -165,7 +165,7 @@ describe('built-in Skills', () => {
);
fs.writeFileSync(manifestPath, JSON.stringify({
schemaVersion: 3,
- bundleVersion: 8,
+ bundleVersion: 9,
fingerprints: {},
activeBundle: 'damaged',
}));
@@ -223,9 +223,9 @@ describe('built-in Skills', () => {
path.join(input.bundledRoot, 'learn', 'SKILL.md.missing'),
);
- const partial = await prepareBuiltInSkills({ ...input, bundleVersion: 9 });
+ const partial = await prepareBuiltInSkills({ ...input, bundleVersion: 10 });
const manifestPath = path.join(input.appDataDir, 'Cindy', 'shared-system-skills', '.cindy-system-skills.json');
- expect(JSON.parse(fs.readFileSync(manifestPath, 'utf8')).bundleVersion).toBe(8);
+ expect(JSON.parse(fs.readFileSync(manifestPath, 'utf8')).bundleVersion).toBe(9);
expect(partial.warnings.join('\n')).toContain('missing SKILL.md');
expect(
fs.readFileSync(path.join(partial.descriptors[0]!.absolutePath, 'SKILL.md'), 'utf8'),
@@ -235,9 +235,9 @@ describe('built-in Skills', () => {
path.join(input.bundledRoot, 'learn', 'SKILL.md.missing'),
path.join(input.bundledRoot, 'learn', 'SKILL.md'),
);
- const retried = await prepareBuiltInSkills({ ...input, bundleVersion: 9 });
+ const retried = await prepareBuiltInSkills({ ...input, bundleVersion: 10 });
expect(retried.warnings).toEqual([]);
- expect(JSON.parse(fs.readFileSync(manifestPath, 'utf8')).bundleVersion).toBe(9);
+ expect(JSON.parse(fs.readFileSync(manifestPath, 'utf8')).bundleVersion).toBe(10);
expect(
fs.readFileSync(
path.join(
@@ -278,7 +278,7 @@ describe('built-in Skills', () => {
await originalRename(source, destination);
});
- const failed = await prepareBuiltInSkills({ ...input, bundleVersion: 9 });
+ const failed = await prepareBuiltInSkills({ ...input, bundleVersion: 10 });
expect(failed.changed).toBe(false);
expect(failed.warnings.join('\n')).toContain('blocked version publish');
@@ -308,7 +308,7 @@ describe('built-in Skills', () => {
originalRename(source, destination);
});
- const failed = await prepareBuiltInSkills({ ...input, bundleVersion: 9 });
+ const failed = await prepareBuiltInSkills({ ...input, bundleVersion: 10 });
expect(failed.changed).toBe(false);
expect(failed.warnings.join('\n')).toContain('blocked manifest switch');
@@ -338,7 +338,7 @@ describe('built-in Skills', () => {
const originalSymlink = fs.promises.symlink.bind(fs.promises);
vi.spyOn(fs.promises, 'symlink').mockImplementation(async (target, linkPath, type) => {
if (
- String(target).includes(`${path.sep}.versions${path.sep}v9-`)
+ String(target).includes(`${path.sep}.versions${path.sep}v10-`)
&& String(linkPath).includes(`learn.next-`)
) {
throw new Error('blocked Learn projection');
@@ -346,7 +346,7 @@ describe('built-in Skills', () => {
await originalSymlink(target, linkPath, type);
});
- const failed = await prepareBuiltInSkills({ ...input, bundleVersion: 9 });
+ const failed = await prepareBuiltInSkills({ ...input, bundleVersion: 10 });
expect(failed.changed).toBe(false);
expect(failed.projectionSafe).toBe(true);
@@ -370,7 +370,7 @@ describe('built-in Skills', () => {
const input = fixture();
const initial = await prepareBuiltInSkills(input);
const root = path.join(input.appDataDir, 'Cindy', 'shared-system-skills');
- const orphanBundle = 'v9-aaaaaaaaaaaaaaaa-00000000-0000-0000-0000-000000000000';
+ const orphanBundle = 'v10-aaaaaaaaaaaaaaaa-00000000-0000-0000-0000-000000000000';
const orphanRoot = path.join(root, '.versions', orphanBundle);
fs.cpSync(path.dirname(initial.descriptors[0]!.absolutePath), orphanRoot, { recursive: true });
const creatorLink = path.join(input.homeDir, '.agents', 'skills', 'cindy-skill-creator');
@@ -460,7 +460,7 @@ describe('built-in Skills', () => {
expect(retried.warnings).toEqual([]);
expect(JSON.parse(fs.readFileSync(manifestPath, 'utf8'))).toMatchObject({
schemaVersion: 3,
- bundleVersion: 8,
+ bundleVersion: 9,
});
});
diff --git a/apps/desktop/src/main/maker-host/built-in-skills.ts b/apps/desktop/src/main/maker-host/built-in-skills.ts
index cd970dca9c..13217dcd8c 100644
--- a/apps/desktop/src/main/maker-host/built-in-skills.ts
+++ b/apps/desktop/src/main/maker-host/built-in-skills.ts
@@ -19,7 +19,7 @@ const MANIFEST_FILE = '.cindy-system-skills.json';
const VERSIONS_DIRECTORY = '.versions';
const BUILT_IN_SKILL_MUTATION_WAIT_MS = 5_000;
/** Increment whenever shipped built-in Skill bytes change between releases. */
-export const BUILT_IN_SKILLS_BUNDLE_VERSION = 8;
+export const BUILT_IN_SKILLS_BUNDLE_VERSION = 9;
export interface BuiltInSkillDescriptor {
name: string;
diff --git a/scripts/__tests__/third-party-notices.test.mjs b/scripts/__tests__/third-party-notices.test.mjs
index 81f632cdcc..225f36c9aa 100644
--- a/scripts/__tests__/third-party-notices.test.mjs
+++ b/scripts/__tests__/third-party-notices.test.mjs
@@ -420,6 +420,23 @@ metadata:
assert.notEqual(scientificNumber.status, 0);
assert.match(scientificNumber.stdout, /Description must be a string, got float/);
+ for (const duplicateKeys of [
+ " true: first\n TRUE: second",
+ " 1: first\n 01: second",
+ " 1: first\n 1.0: second",
+ ' "true": first\n true: second',
+ ]) {
+ fs.writeFileSync(
+ path.join(skillDir, "SKILL.md"),
+ `---\nname: yaml-regression\ndescription: duplicate keys\nmetadata:\n${duplicateKeys}\n---\n`,
+ );
+ const duplicate = spawnSync(command, [...prefix, "-S", validator, skillDir], {
+ encoding: "utf8",
+ });
+ assert.notEqual(duplicate.status, 0);
+ assert.match(duplicate.stdout, /Duplicate mapping key/);
+ }
+
fs.writeFileSync(
path.join(skillDir, "SKILL.md"),
"---\nname: yaml-regression\ndescription: invalid\0value\n---\n",