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(companions): parse block scalar Skill descriptions in indexes

Chrisddbe5fe79912
diff --git a/apps/desktop/src/main/bot-import/README.md b/apps/desktop/src/main/bot-import/README.md
index c6802dace7..d4494b9445 100644
--- a/apps/desktop/src/main/bot-import/README.md
+++ b/apps/desktop/src/main/bot-import/README.md
@@ -125,6 +125,12 @@ omitting it retains normal companion defaults, and a rejected request can be cor
bodies are not loaded/parsed in full for indexing. Existing legacy migration
remains for small authored files; oversized legacy files retain their original
bytes and use discovery. Authoring/detail APIs still read the originals.
+ Streamed and full-file metadata readers share block-scalar handling: YAML `>`
+ folding, `|` literal lines, indentation and chomping use the existing YAML
+ parser. Runtime previews retain at most 4 KiB of block source per field;
+ query construction retains the complete description for one header at a time.
+ Colons inside block content never become metadata keys. Imported source bytes
+ and original body offsets remain unchanged, including empty block scalars.
Full-metadata queries reuse a separate disk index of enabled and disabled
headers. Construction streams headers (never bodies), sorts byte-bounded runs
and merges two records at a time; an oversized header occupies its own run.
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts
index 9a71f9f5f4..d493846776 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts
@@ -28,6 +28,17 @@ async function write(slug: string, name: string, description = 'Searchable', dis
return file;
}
+it('finds block-scalar description keywords in enabled and disabled Skills, including beyond the runtime preview', async () => {
+ const folded = await write('folded', 'Folded', `>-\n ${'prefix '.repeat(2000)}\n reconciliation needle-folded`);
+ const literal = await write('literal', 'Literal', '|\n invoice processing\n needle-literal', true);
+ const originals = await Promise.all([folded, literal].map(file => fs.readFile(file, 'utf8')));
+ expect(await listBotSkillsForSession({ callerSessionId: 's', query: 'reconciliation needle-folded' }, deps()))
+ .toMatchObject({ ok: true, total: 1, skills: [{ slug: 'folded' }] });
+ expect(await query({ query: 'invoice needle-literal' }))
+ .toMatchObject({ total: 1, skills: [{ slug: 'literal', enabled: false }] });
+ expect(await Promise.all([folded, literal].map(file => fs.readFile(file, 'utf8')))).toEqual(originals);
+});
+
it('reuses its index for different terms/pages without enumerating or opening source Skills', async () => {
// No mutation is signalled in this case. Native notification delivery (which
// may include delayed setup writes) is exercised separately below.
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeSource.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeSource.test.ts
index dcd1df9be2..6c16f6da21 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeSource.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeSource.test.ts
@@ -3,7 +3,8 @@ import path from 'node:path';
import os from 'node:os';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import { iterateBotSkillRuntimeSummaries, iterateBotSkillQuerySummaries } from '../botSkillRuntimeSource';
-import { botSkillsDir } from '../botSkillStore';
+import { botSkillsDir, parseBotSkillFile, readBotSkill } from '../botSkillStore';
+import yaml from 'js-yaml';
let home: string;
beforeEach(async () => { home = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-skill-stream-')); });
@@ -21,8 +22,44 @@ async function read(iterate = iterateBotSkillRuntimeSummaries) {
return items;
}
+it.each(['>', '|', '>-', '|-', '>+', '|2+ # keep newlines', '>2- # folded'])('parses YAML block metadata (%s) consistently without modifying the original', async indicator => {
+ const header = `---\r\nname: fixture\r\ndescription: ${indicator}\r\n invoice reconciliation\r\n keyword: matching\r\n\r\n second paragraph\r\n indented detail\r\n last line\r\n\r\nmetadata:\r\n displayName: |-\r\n Visible name\r\n updatedAt: "2026-09-29"\r\nunknown: |\r\n description: must not replace metadata\r\n---\r\n`;
+ const text = header + 'Original body';
+ const expected = yaml.load(header.slice(5, -5)) as { description: string };
+ const file = await source('block', text);
+ for (const iterate of [iterateBotSkillRuntimeSummaries, iterateBotSkillQuerySummaries]) {
+ const [item] = await read(iterate);
+ expect(item).toMatchObject({ name: 'Visible name', description: expected.description, updatedAt: '2026-09-29', frontmatterBytes: Buffer.byteLength(header), bodyStartLine: header.split('\n').length });
+ }
+ expect(parseBotSkillFile(text)).toMatchObject({ name: 'Visible name', description: expected.description, body: 'Original body' });
+ expect(await fs.readFile(file, 'utf8')).toBe(text);
+});
+
+it('bounds multi-line block previews but keeps the full description in the query stream', async () => {
+ const description = Array.from({ length: 50_000 }, (_, index) => ` Line ${index} 中文`).join('\n');
+ const text = `---\nname: fixture\ndescription: >-\n${description}\n unique-tail-term\nmetadata:\n displayName: Visible\n---\nOriginal body`;
+ const file = await source('long-block', text);
+ const [preview] = await read();
+ expect(preview.description).toContain('Line 0 中文');
+ expect(Buffer.byteLength(preview.description)).toBeLessThan(4200);
+ expect(preview.name).toBe('Visible');
+ const [full] = await read(iterateBotSkillQuerySummaries);
+ expect(full.description).toContain('Line 49999 中文');
+ expect(full.description).toContain('unique-tail-term');
+ expect(full.description).toBe(parseBotSkillFile(text).description);
+ expect(await fs.readFile(file, 'utf8')).toBe(text);
+});
+
+it('does not migrate an empty block scalar as legacy Cindy-authored metadata', async () => {
+ const text = '---\nname: fixture\ndescription: >-\nupdatedAt: "2026-09-29"\n---\nOriginal body\n';
+ const file = await source('empty-block', text);
+ expect(await read()).toMatchObject([{ description: '', name: 'fixture' }]);
+ expect(await readBotSkill(home, 'bot', 'empty-block')).toMatchObject({ description: '', body: 'Original body' });
+ expect(await fs.readFile(file, 'utf8')).toBe(text);
+});
+
it.each([['runtime', iterateBotSkillRuntimeSummaries], ['query', iterateBotSkillQuerySummaries]] as const)('%s uses directory streaming and header-only reads for a large Skill body', async (_kind, iterate) => {
- const file = await source('large-body', `---\nname: fixture\ndescription: Preview\n---\n${'Body '.repeat(2 * 1024 * 1024)}`);
+ const file = await source('large-body', `---\nname: fixture\ndescription: >-\n Preview\n continued\n---\n${'Body '.repeat(2 * 1024 * 1024)}`);
const readdir = vi.spyOn(fs, 'readdir').mockRejectedValue(new Error('unbounded enumeration'));
const readFile = vi.spyOn(fs, 'readFile').mockRejectedValue(new Error('full file read'));
const originalOpen = fs.open.bind(fs);
@@ -37,7 +74,7 @@ it.each([['runtime', iterateBotSkillRuntimeSummaries], ['query', iterateBotSkill
});
return handle;
});
- expect(await read(iterate)).toMatchObject([{ name: 'fixture', description: 'Preview', filePath: file, bodyStartLine: 5 }]);
+ expect(await read(iterate)).toMatchObject([{ name: 'fixture', description: 'Preview continued', filePath: file, bodyStartLine: 7 }]);
expect(bytesRead).toBeLessThanOrEqual(8192);
expect(readdir).not.toHaveBeenCalled(); expect(readFile).not.toHaveBeenCalled();
open.mockRestore();
diff --git a/apps/desktop/src/main/maker-ipc/botSkillFrontmatter.ts b/apps/desktop/src/main/maker-ipc/botSkillFrontmatter.ts
new file mode 100644
index 0000000000..4529756334
--- /dev/null
+++ b/apps/desktop/src/main/maker-ipc/botSkillFrontmatter.ts
@@ -0,0 +1,66 @@
+import yaml from 'js-yaml';
+
+const METADATA_KEYS = new Set(['name', 'displayName', 'description', 'updatedAt']);
+const BLOCK_SCALAR = /^[>|](?:[+-][1-9]?|[1-9][+-]?)?(?:[ \t]+#.*)?$/;
+
+export function isFrontmatterBlock(value: string): boolean { return BLOCK_SCALAR.test(value.trim()); }
+
+export function unescapeFrontmatterValue(raw: string): string {
+ const trimmed = raw.trim();
+ if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2) {
+ return trimmed.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
+ }
+ if (trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2) {
+ return trimmed.slice(1, -1).replace(/''/g, "'");
+ }
+ return trimmed;
+}
+
+/** Same metadata semantics for full reads and streamed, byte-bounded previews. */
+export function createBotSkillFrontmatterReader(maxFieldBytes = Infinity) {
+ const fields = new Map<string, string>();
+ let block: { key?: string; header: string; indent: number; parts: string[]; bytes: number } | undefined;
+ const finishBlock = () => {
+ if (block?.key) {
+ try {
+ // Parse only an isolated scalar, not arbitrary YAML objects/aliases.
+ // js-yaml supplies folding, indentation and chomping semantics.
+ const parsed = yaml.load(`value: ${block.header}\n${block.parts.join('')}`) as { value?: unknown };
+ fields.set(block.key, typeof parsed?.value === 'string' ? parsed.value : '');
+ } catch {
+ // Malformed hand-written metadata stays listable, as before.
+ fields.set(block.key, block.header);
+ }
+ }
+ block = undefined;
+ };
+ return {
+ line(text: string, truncated = false) {
+ const indent = /^ */.exec(text)![0].length;
+ if (block) {
+ if (!text.trim() || indent > block.indent) {
+ if (block.key && block.bytes < maxFieldBytes) {
+ const bytes = Buffer.from(text.slice(block.indent) + '\n');
+ const retained = bytes.subarray(0, maxFieldBytes - block.bytes);
+ block.parts.push(retained.toString('utf8'));
+ block.bytes += retained.length;
+ }
+ return;
+ }
+ finishBlock();
+ }
+ const separator = text.indexOf(':');
+ if (separator <= 0) return;
+ const key = text.slice(0, separator).trim();
+ let value = text.slice(separator + 1).trim();
+ if (isFrontmatterBlock(value)) {
+ // Consume unknown blocks too: their "description:" lines are content.
+ block = { ...(METADATA_KEYS.has(key) ? { key } : {}), header: value, indent, parts: [], bytes: 0 };
+ } else if (METADATA_KEYS.has(key)) {
+ if (truncated && /^["']/.test(value) && !value.endsWith(value[0])) value += value[0];
+ fields.set(key, unescapeFrontmatterValue(value));
+ }
+ },
+ finish() { finishBlock(); return fields; },
+ };
+}
diff --git a/apps/desktop/src/main/maker-ipc/botSkillRuntimeSource.ts b/apps/desktop/src/main/maker-ipc/botSkillRuntimeSource.ts
index 3bd34c71fe..4b73a5ff0a 100644
--- a/apps/desktop/src/main/maker-ipc/botSkillRuntimeSource.ts
+++ b/apps/desktop/src/main/maker-ipc/botSkillRuntimeSource.ts
@@ -2,8 +2,9 @@ import { promises as fs } from 'node:fs';
import path from 'node:path';
import {
BOT_SKILL_MAX_BODY_BYTES, botSkillRootDir, botSkillsDir, readCompatibleBotSkillSource,
- unescapeFrontmatterValue, type BotSkillSummary,
+ type BotSkillSummary,
} from './botSkillStore.js';
+import { createBotSkillFrontmatterReader, isFrontmatterBlock } from './botSkillFrontmatter.js';
// Retain only a preview of any single YAML line, even a multi-MiB scalar.
const LINE_PREVIEW_BYTES = 4096;
@@ -11,7 +12,7 @@ const READ_BYTES = 8192;
async function readRuntimeHeader(filePath: string, slug: string, migrate = true, fullMetadata = false): Promise<BotSkillSummary> {
const handle = await fs.open(filePath, 'r');
- const fields = new Map<string, string>();
+ const metadata = createBotSkillFrontmatterReader(fullMetadata ? Infinity : LINE_PREVIEW_BYTES);
let frontmatterBytes = 0;
let bodyStartLine = 1;
let legacy = true;
@@ -41,14 +42,9 @@ async function readRuntimeHeader(filePath: string, slug: string, migrate = true,
const separator = text.indexOf(':');
const key = text.slice(0, separator).trim();
if (!entire || /^\s/.test(text) || separator <= 0 || legacyKeys.length >= 3
- || !['name', 'description', 'updatedAt'].includes(key)) legacy = false;
+ || !['name', 'description', 'updatedAt'].includes(key) || isFrontmatterBlock(text.slice(separator + 1))) legacy = false;
if (legacy) legacyKeys.push(key);
- if (separator > 0 && ['name', 'displayName', 'description', 'updatedAt'].includes(key)) {
- let value = text.slice(separator + 1).trim();
- // Close only the preview's quote. The original scalar stays untouched.
- if (!fullMetadata && !entire && /^["']/.test(value) && !value.endsWith(value[0])) value += value[0];
- fields.set(key, unescapeFrontmatterValue(value));
- }
+ metadata.line(text, !fullMetadata && !entire);
}
line = Buffer.alloc(0);
lineParts = [];
@@ -73,6 +69,7 @@ async function readRuntimeHeader(filePath: string, slug: string, migrate = true,
}
}
} finally { await handle.close(); }
+ const fields = metadata.finish();
legacy = complete && legacy && ['name', 'description', 'updatedAt'].every(key => legacyKeys.includes(key));
// Keep the existing compatibility migration for bounded, historically authored
// files. Huge hand-written legacy files remain intact and use body discovery.
diff --git a/apps/desktop/src/main/maker-ipc/botSkillStore.ts b/apps/desktop/src/main/maker-ipc/botSkillStore.ts
index 7ee9fcf1aa..12f5fd34e7 100644
--- a/apps/desktop/src/main/maker-ipc/botSkillStore.ts
+++ b/apps/desktop/src/main/maker-ipc/botSkillStore.ts
@@ -33,7 +33,9 @@
import { promises as fs } from 'node:fs';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
+import { createBotSkillFrontmatterReader, isFrontmatterBlock } from './botSkillFrontmatter.js';
import { invalidateBotSkillRuntime } from './botSkillRuntimeCache.js';
+export { unescapeFrontmatterValue } from './botSkillFrontmatter.js';
/** 一个技能在磁盘上的完整形态。 */
export interface BotSkillRecord {
@@ -168,17 +170,6 @@ function escapeFrontmatterValue(value: string): string {
.replace(/[\r\n]+/g, ' ')}"`;
}
-export function unescapeFrontmatterValue(raw: string): string {
- const trimmed = raw.trim();
- if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2) {
- return trimmed.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
- }
- if (trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2) {
- return trimmed.slice(1, -1).replace(/''/g, "'");
- }
- return trimmed;
-}
-
export function renderBotSkillFile(input: {
slug: string;
name: string;
@@ -208,16 +199,13 @@ export function parseBotSkillFile(source: string): {
const normalized = source.replace(/\r\n/g, '\n');
const match = /^---\n([\s\S]*?)\n---\n?/.exec(normalized);
if (!match) return { name: '', description: '', updatedAt: '', body: normalized.trim() };
- const fields: Record<string, string> = {};
- for (const line of match[1].split('\n')) {
- const separator = line.indexOf(':');
- if (separator <= 0) continue;
- fields[line.slice(0, separator).trim()] = unescapeFrontmatterValue(line.slice(separator + 1));
- }
+ const metadata = createBotSkillFrontmatterReader();
+ for (const line of match[1].split('\n')) metadata.line(line);
+ const fields = metadata.finish();
return {
- name: fields.displayName ?? fields.name ?? '',
- description: fields.description ?? '',
- updatedAt: fields.updatedAt ?? '',
+ name: fields.get('displayName') ?? fields.get('name') ?? '',
+ description: fields.get('description') ?? '',
+ updatedAt: fields.get('updatedAt') ?? '',
body: normalized.slice(match[0].length).trim(),
};
}
@@ -235,7 +223,7 @@ export async function readCompatibleBotSkillSource(filePath: string, slug: strin
const lines = match[1].split('\n');
const keys = lines.map((line) => line.slice(0, line.indexOf(':')).trim());
if (
- lines.some((line) => /^\s/.test(line) || line.indexOf(':') <= 0) ||
+ lines.some((line) => /^\s/.test(line) || line.indexOf(':') <= 0 || isFrontmatterBlock(line.slice(line.indexOf(':') + 1))) ||
keys.length !== 3 ||
!['name', 'description', 'updatedAt'].every((key) => keys.includes(key))
) {