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。

feat(desktop): 建组织插件前缀的落盘缓存(纯工厂,尚未接线)

fmfsaisai6eefa0726e0e
diff --git a/apps/desktop/src/main/plugin-market/__tests__/organizationPrefixStore.test.ts b/apps/desktop/src/main/plugin-market/__tests__/organizationPrefixStore.test.ts
new file mode 100644
index 0000000000..628cfb417f
--- /dev/null
+++ b/apps/desktop/src/main/plugin-market/__tests__/organizationPrefixStore.test.ts
@@ -0,0 +1,140 @@
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { createOrganizationPrefixStore } from '../organizationPrefixStore';
+
+const TEMP_PREFIX = 'cindy-org-prefix-';
+
+let tempDir: string | null = null;
+
+function makeTempDir(): string {
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), TEMP_PREFIX));
+ return tempDir;
+}
+
+function storePath(dir: string): string {
+ return path.join(dir, 'organization.v1.json');
+}
+
+afterEach(() => {
+ if (tempDir) {
+ fs.rmSync(tempDir, { recursive: true, force: true });
+ tempDir = null;
+ }
+});
+
+describe('createOrganizationPrefixStore', () => {
+ it('returns known with the remembered prefix', () => {
+ const dir = makeTempDir();
+ const store = createOrganizationPrefixStore(storePath(dir));
+ store.remember('org-acme', 'acme');
+ expect(store.lookup('org-acme')).toEqual({ kind: 'known', pluginPrefix: 'acme' });
+ });
+
+ it('merges writes so a previous organization entry is kept', () => {
+ const dir = makeTempDir();
+ const store = createOrganizationPrefixStore(storePath(dir));
+ store.remember('org-a', 'aaa');
+ store.remember('org-b', 'bbb');
+ expect(store.lookup('org-a')).toEqual({ kind: 'known', pluginPrefix: 'aaa' });
+ expect(store.lookup('org-b')).toEqual({ kind: 'known', pluginPrefix: 'bbb' });
+ });
+
+ it('returns absent when the file is missing or the orgId key is missing', () => {
+ const dir = makeTempDir();
+ const store = createOrganizationPrefixStore(storePath(dir));
+ expect(store.lookup('org-missing')).toEqual({ kind: 'absent' });
+
+ store.remember('org-a', 'aaa');
+ expect(store.lookup('org-b')).toEqual({ kind: 'absent' });
+ });
+
+ it('returns unavailable for unparseable JSON, not absent', () => {
+ const dir = makeTempDir();
+ const file = storePath(dir);
+ fs.writeFileSync(file, '{not-json', 'utf8');
+ const store = createOrganizationPrefixStore(file);
+ expect(store.lookup('org-acme')).toEqual({ kind: 'unavailable' });
+ });
+
+ it('returns unavailable for illegal stored pluginPrefix values', () => {
+ const dir = makeTempDir();
+ const file = storePath(dir);
+ const store = createOrganizationPrefixStore(file);
+ const illegal = ['A', 'x', 'toolongprefixvalue123', 1] as const;
+ for (const pluginPrefix of illegal) {
+ fs.writeFileSync(
+ file,
+ `${JSON.stringify({ version: 1, organizations: { 'org-1': { pluginPrefix } } })}\n`,
+ 'utf8',
+ );
+ expect(store.lookup('org-1'), String(pluginPrefix)).toEqual({ kind: 'unavailable' });
+ }
+ });
+
+ it('treats a remembered null prefix as known, not absent', () => {
+ const dir = makeTempDir();
+ const store = createOrganizationPrefixStore(storePath(dir));
+ store.remember('org-acme', null);
+ expect(store.lookup('org-acme')).toEqual({ kind: 'known', pluginPrefix: null });
+ });
+
+ it('does not create directories or files when the module is imported', async () => {
+ const dir = makeTempDir();
+ await import('../organizationPrefixStore');
+ expect(fs.readdirSync(dir)).toEqual([]);
+ createOrganizationPrefixStore(storePath(dir));
+ expect(fs.readdirSync(dir)).toEqual([]);
+ });
+
+ // 这个文件是可重建的缓存(下次市场列表成功就能重新填)。若 remember 在内容损坏时
+ // 也拒写,一次损坏就会让所有组织插件的特权被永久拒绝,唯一恢复路径是用户手动删文件。
+ it('rebuilds over a corrupt file instead of staying permanently unavailable', () => {
+ const dir = makeTempDir();
+ const file = storePath(dir);
+ fs.writeFileSync(file, '{not-json', 'utf8');
+ const store = createOrganizationPrefixStore(file);
+ expect(store.lookup('org-acme')).toEqual({ kind: 'unavailable' });
+
+ store.remember('org-acme', 'acme');
+ expect(store.lookup('org-acme')).toEqual({ kind: 'known', pluginPrefix: 'acme' });
+ });
+
+ // 版本不认也算内容损坏:同样要能被重建,不能把用户卡在永久 unavailable。
+ it('rebuilds over a document whose version is not recognized', () => {
+ const dir = makeTempDir();
+ const file = storePath(dir);
+ fs.writeFileSync(file, JSON.stringify({ version: 99, organizations: {} }), 'utf8');
+ const store = createOrganizationPrefixStore(file);
+ expect(store.lookup('org-acme')).toEqual({ kind: 'unavailable' });
+
+ store.remember('org-acme', 'acme');
+ expect(store.lookup('org-acme')).toEqual({ kind: 'known', pluginPrefix: 'acme' });
+ });
+
+ // 空 orgId 不是一个组织。调用方本该在个人身份下压根不查,但若哪天有人写成
+ // `lookup(orgId ?? '')`,不能让它命中一条 `""` 键的条目而得出"有组织"的结论 ——
+ // 那是与 installed=false 同类的 fail-open。
+ it('never treats an empty orgId as a real organization', () => {
+ const dir = makeTempDir();
+ const file = storePath(dir);
+ const store = createOrganizationPrefixStore(file);
+ store.remember('', 'acme');
+ expect(fs.existsSync(file)).toBe(false);
+ expect(store.lookup('')).toEqual({ kind: 'unavailable' });
+
+ store.remember('org-acme', 'acme');
+ expect(store.lookup(' ')).toEqual({ kind: 'unavailable' });
+ });
+
+ it('discards illegal non-null prefixes instead of writing them', () => {
+ const dir = makeTempDir();
+ const file = storePath(dir);
+ const store = createOrganizationPrefixStore(file);
+ store.remember('org-acme', 'x');
+ expect(fs.existsSync(file)).toBe(false);
+ expect(store.lookup('org-acme')).toEqual({ kind: 'absent' });
+ });
+});
diff --git a/apps/desktop/src/main/plugin-market/organizationPrefixStore.ts b/apps/desktop/src/main/plugin-market/organizationPrefixStore.ts
new file mode 100644
index 0000000000..0096679be0
--- /dev/null
+++ b/apps/desktop/src/main/plugin-market/organizationPrefixStore.ts
@@ -0,0 +1,143 @@
+/**
+ * 记住某个组织在插件市场登记的插件前缀。
+ *
+ * 前缀只能从服务端市场列表响应拿到,本地推不出来。离线、从未打开过市场页、
+ * 读盘失败时,调用方必须能区分三种结果,绝不能互相折叠:
+ * - `known`:确认读到了该组织的条目。`pluginPrefix` 为 `null` 表示该组织已登录
+ * 但尚未登记前缀——这是确定事实,不是缺失。
+ * - `absent`:确认没有该组织的条目(文件不存在,或文件里没有这个键)。
+ * - `unavailable`:读盘失败、JSON 解析失败、或存量值不合法。
+ *
+ * 尤其不能把 `unavailable` 当成 `absent`:上层对「没有条目」和「读不出来」的
+ * fail-closed 处置不同。
+ */
+import crypto from 'node:crypto';
+import fs from 'node:fs';
+import path from 'node:path';
+
+import { PLUGIN_PREFIX_PATTERN } from '@cindy/plugin-protocol';
+
+const STORE_VERSION = 1 as const;
+
+export type OrganizationPrefixLookup =
+ | { kind: 'known'; pluginPrefix: string | null }
+ | { kind: 'absent' }
+ | { kind: 'unavailable' };
+
+export interface OrganizationPrefixStore {
+ lookup(orgId: string): OrganizationPrefixLookup;
+ remember(orgId: string, pluginPrefix: string | null): void;
+}
+
+interface OrganizationPrefixDocument {
+ version: typeof STORE_VERSION;
+ organizations: Record<string, unknown>;
+}
+
+/**
+ * `corrupt` 与 `unreadable` 对 `lookup` 是同一个答案(都是 `unavailable`),
+ * 但对 `remember` 不是:
+ * - `corrupt`(坏 JSON / 版本不认 / 结构不对):内容没救,但盘是好的。这个文件是
+ * **可重建的缓存**,下次市场列表成功就能重新填,所以要覆盖重建。若在这里也拒写,
+ * 一次损坏就会让所有组织插件的特权被永久拒绝,唯一恢复路径是用户手动删文件。
+ * - `unreadable`(EACCES / EIO 等):盘本身有问题,写大概也会失败,
+ * 不要拿一份空文档去覆盖可能还完好的内容。
+ */
+type DocumentRead =
+ | { kind: 'empty' }
+ | { kind: 'ok'; document: OrganizationPrefixDocument }
+ | { kind: 'corrupt' }
+ | { kind: 'unreadable' };
+
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
+}
+
+function isValidPluginPrefix(value: string | null): boolean {
+ return value === null || PLUGIN_PREFIX_PATTERN.test(value);
+}
+
+function parseDocument(text: string): OrganizationPrefixDocument | null {
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(text);
+ } catch {
+ return null;
+ }
+ if (!isPlainObject(parsed)) return null;
+ if (parsed.version !== STORE_VERSION) return null;
+ if (!isPlainObject(parsed.organizations)) return null;
+ return { version: STORE_VERSION, organizations: parsed.organizations };
+}
+
+function parseEntry(value: unknown): { pluginPrefix: string | null } | null {
+ if (!isPlainObject(value)) return null;
+ if (!Object.prototype.hasOwnProperty.call(value, 'pluginPrefix')) return null;
+ const pluginPrefix = value.pluginPrefix;
+ if (pluginPrefix === null) return { pluginPrefix: null };
+ if (typeof pluginPrefix === 'string' && PLUGIN_PREFIX_PATTERN.test(pluginPrefix)) {
+ return { pluginPrefix };
+ }
+ return null;
+}
+
+function writeDocumentAtomically(filePath: string, document: OrganizationPrefixDocument): void {
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
+ const tempPath = `${filePath}.${crypto.randomUUID()}.tmp`;
+ try {
+ fs.writeFileSync(tempPath, `${JSON.stringify(document, null, 2)}\n`, 'utf8');
+ fs.renameSync(tempPath, filePath);
+ } catch (error) {
+ try {
+ fs.unlinkSync(tempPath);
+ } catch {
+ // 临时文件可能尚未创建,或 rename 已成功后不再存在。
+ }
+ throw error;
+ }
+}
+
+export function createOrganizationPrefixStore(filePath: string): OrganizationPrefixStore {
+ const readDocument = (): DocumentRead => {
+ let text: string;
+ try {
+ text = fs.readFileSync(filePath, 'utf8');
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') return { kind: 'empty' };
+ return { kind: 'unreadable' };
+ }
+ const document = parseDocument(text);
+ if (!document) return { kind: 'corrupt' };
+ return { kind: 'ok', document };
+ };
+
+ return {
+ lookup(orgId: string): OrganizationPrefixLookup {
+ // 空 orgId 不是一个组织。调用方本该在身份为个人时压根不查,
+ // 但若哪天有人写成 `lookup(orgId ?? '')`,不能让它命中一条 `""` 键的条目
+ // 而拿到"有组织"的结论——那是与 installed=false 同类的 fail-open。
+ if (orgId.trim() === '') return { kind: 'unavailable' };
+ const read = readDocument();
+ if (read.kind === 'empty') return { kind: 'absent' };
+ if (read.kind === 'corrupt' || read.kind === 'unreadable') return { kind: 'unavailable' };
+ if (!Object.prototype.hasOwnProperty.call(read.document.organizations, orgId)) {
+ return { kind: 'absent' };
+ }
+ const entry = parseEntry(read.document.organizations[orgId]);
+ if (!entry) return { kind: 'unavailable' };
+ return { kind: 'known', pluginPrefix: entry.pluginPrefix };
+ },
+
+ remember(orgId: string, pluginPrefix: string | null): void {
+ if (orgId.trim() === '') return;
+ if (!isValidPluginPrefix(pluginPrefix)) return;
+ const read = readDocument();
+ // 盘读不了就别写(可能把还完好的内容盖掉);内容坏了要覆盖重建,
+ // 否则一次损坏 = 永久拒绝,见 DocumentRead 的注释。
+ if (read.kind === 'unreadable') return;
+ const organizations = read.kind === 'ok' ? { ...read.document.organizations } : {};
+ organizations[orgId] = { pluginPrefix };
+ writeDocumentAtomically(filePath, { version: STORE_VERSION, organizations });
+ },
+ };
+}