Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(companions): bound selection work and aggregate import progress
zqchrisb6b30b340422
diff --git a/apps/desktop/src/main/bot-import/README.md b/apps/desktop/src/main/bot-import/README.mdindex 20ed52aa3a..fa83db5d4a 100644--- a/apps/desktop/src/main/bot-import/README.md+++ b/apps/desktop/src/main/bot-import/README.md@@ -58,10 +58,13 @@ omitting it retains normal companion defaults, and a rejected request can be cor- Selected identity/user/instruction documents and memories retain their originaltext in the encrypted environment. Profile prompt fields and the native memorystore receive copies with known selected env/MCP/auth credentials masked, while- ordinary text stays unchanged. Import summaries/titles obey the storage UTF-16+ ordinary text stays unchanged. Oversized profile fields retain a UTF-8-safe source+ prefix within the existing profile capacity; the complete documents remain in memory+ and the encrypted archive, without synthesized system instructions. Import summaries/titles obey the storage UTF-16length bounds, and document parts preserve source frontmatter and whitespace.Each new part is read back before it counts as saved; failures preserve part- progress and safe storage reasons. Existing edited parts are not overwritten. Env references and original credentials remain+ progress accumulated across all documents in a recovered directory, and safe storage+ reasons. Existing edited parts are not overwritten. Env references and original credentials remainavailable to host execution; no model call or extra confirmation is added.Selected skill folders retain theirreal scripts, templates, executable bits and `SKILL.md`.diff --git a/apps/desktop/src/main/bot-import/__tests__/host.test.ts b/apps/desktop/src/main/bot-import/__tests__/host.test.tsindex 145fc743ab..f69e4b4f5d 100644--- a/apps/desktop/src/main/bot-import/__tests__/host.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/host.test.ts@@ -17,6 +17,7 @@ import { CompanionImportError } from '../types.js';import { previewImportRedactions } from '../environmentSelection.js';import { redactEnvironmentValues } from '../process.js';import { DEFAULT_MEMORY_CONFIG, MemoryStorage, type MakerMemoryStore } from '@cindy/maker-core';+import { BOT_MEMORY_BODY_MAX_BYTES } from '../../../shared/botMemory.js';import { createBotMemoryService } from '../../maker-ipc/botMemoryService.js';const h = vi.hoisted(() => ({ root: '', botId: '', created: false, verified: false, sourceEnabled: true, failReadyWrite: false, boundary: false,@@ -1124,3 +1125,52 @@ it('publishes a partial-save notice with the failed filename and safe reason, thexpect(completed?.status).toBe('complete');expect(vi.mocked(createMessage).mock.calls.at(-1)![1]).toMatchObject({ clientId: `companion-import:${selection.requestId}:ready`, content: t('bots.import.chatReady') });});++it.each([0, 1])('counts earlier recovered documents when a later document fails after %s chunks', async failedAfter => {+ const storage = new MemoryStorage(path.join(h.root, 'real-memory'), DEFAULT_MEMORY_CONFIG);+ await storage.init(h.root);+ const write = storage.write.bind(storage);+ let fail = true;+ vi.spyOn(storage, 'write').mockImplementation(async input => {+ if (fail && input.name === `import_second_${failedAfter}`) throw Object.assign(new Error('private disk path'), { code: 'ENOSPC' });+ return write(input);+ });+ const memory = createBotMemoryService({ getStore: async () => storage as unknown as MakerMemoryStore,+ readBot: async () => ({ canonicalSessionId: null }), requestRefresh: async () => {} });+ h.importDocument.mockImplementation(memory.importDocument);+ h.snapshot.items = [{ view: { id: 'memory-directory', name: 'Recovered directory', category: 'memory', selected: true }, documents: [+ { id: 'first', name: 'first.md', text: 'a'.repeat(BOT_MEMORY_BODY_MAX_BYTES + 1) },+ { id: 'second', name: 'second.md', text: 'b'.repeat(BOT_MEMORY_BODY_MAX_BYTES + 1) },+ { id: 'third', name: 'third.md', text: 'Still to save' },+ ] }];+ const [source] = await listCompanionImportSources('fixture');+ const preview = await previewCompanionImport(source!.id, 'fixture');+ const selection = { requestId: 'fixture-directory-progress', previewId: preview.id, name: 'Ada', entryIds: ['memory-directory'], takeover: false, deferSetup: true };+ const accepted = await startCompanionImport(selection, 'fixture');+ const first = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ expect(first?.checks).toContainEqual({ entryId: 'memory-directory', status: 'needs-attention', message: 'IMPORT_DISK_FULL', progress: { saved: 2 + failedAfter, total: 5 } });+ expect(await storage.list()).toHaveLength(2 + failedAfter);+ fail = false;+ await startCompanionImport(selection, 'reconnected');+ const completed = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ expect(completed).toMatchObject({ status: 'complete', savedEntryIds: ['memory-directory'] });+ expect(await storage.list()).toHaveLength(5);+});++it('keeps oversized profile text as original Unicode while preserving the entire source in memory', async () => {+ const text = 'Source instructions\n' + '😀原文'.repeat(20_000);+ h.snapshot.items = [{ view: { id: 'instructions', name: 'AGENTS.md', category: 'personality', selected: true }, role: 'instructions', text }];+ const [source] = await listCompanionImportSources('fixture');+ const preview = await previewCompanionImport(source!.id, 'fixture');+ const selection = { requestId: 'fixture-original-profile', previewId: preview.id, name: 'Ada', entryIds: ['instructions'], takeover: false, deferSetup: true };+ const accepted = await startCompanionImport(selection, 'fixture');+ const result = await withBotProfileLocks([accepted.botId], () => getCompanionImportResult(selection.requestId));+ expect(result?.status).toBe('complete');+ const profile = h.writeProfile.mock.calls.at(-1)![2].systemPromptOverride as string;+ expect(text.startsWith(profile)).toBe(true);+ expect(Buffer.byteLength(profile, 'utf8')).toBeLessThanOrEqual(100_000);+ expect(Buffer.byteLength(profile, 'utf8')).toBeGreaterThan(99_996);+ expect(profile).not.toContain('�');+ expect(h.importDocument).toHaveBeenCalledWith(accepted.botId, 'instructions', 'AGENTS.md', text, 'reference');+ expect((await h.store.read(h.root, accepted.botId, () => {}))?.documents?.instructions).toBe(text);+});diff --git a/apps/desktop/src/main/bot-import/__tests__/transfer.test.ts b/apps/desktop/src/main/bot-import/__tests__/transfer.test.tsindex b271204df7..9d3800b9a1 100644--- a/apps/desktop/src/main/bot-import/__tests__/transfer.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/transfer.test.ts@@ -2,7 +2,7 @@ import { promises as fs } from 'node:fs';import path from 'node:path';import os from 'node:os';import { describe, expect, it, vi } from 'vitest';-import { transferCompanion, type ImportReceipt, type TransferDeps } from '../transfer.js';+import { validateImportSelection, transferCompanion, type ImportReceipt, type TransferDeps } from '../transfer.js';import { CompanionImportError, type ImportSnapshot } from '../types.js';import type { CompanionImportSelection } from '@cindy/maker-shared/companion-import';import { normalizeAutomation } from '../sourceAutomations.js';@@ -390,3 +390,24 @@ it('preserves concrete memory failures and partial progress in a retryable receiexpect(retried.savedEntryIds).toContain('memory');expect(retried.checks.find(check => check.entryId === 'memory')?.progress).toBeUndefined();});++it('validates 100,000 sparse selections in linear work and rejects oversized ranges before expansion', () => {+ const size = 100_000;+ let reads = 0;+ const entryRanges: Array<[number, number]> = Array.from({ length: size / 2 }, (_, i) => {+ const range: [number, number] = [i * 2, i * 2];+ Object.defineProperty(range, 0, { get() { reads++; return i * 2; } });+ return range;+ });+ const indexed = { ...snapshot, items: Array.from({ length: size }, (_, sourceIndex) => ({+ sourceIndex, view: { id: `memory-${sourceIndex}`, name: 'Memory', category: 'memory' as const, selected: true },+ })) };+ const chosen = validateImportSelection({ ...selection, entryIds: [], entryRanges }, indexed);+ expect(chosen.map(item => item.sourceIndex)).toEqual(Array.from({ length: size / 2 }, (_, i) => i * 2));+ expect(reads).toBeLessThan(size * 10);+ // Selected-only checkpoints need not be contiguous or in index order.+ expect(validateImportSelection({ ...selection, entryIds: [], entryRanges }, { ...indexed, items: chosen.toReversed() }))+ .toEqual(chosen.toReversed());+ expect(() => validateImportSelection({ ...selection, entryIds: [], entryRanges: [[0, Number.MAX_SAFE_INTEGER - 1]] }, indexed))+ .toThrow('SELECTION_CHANGED');+});diff --git a/apps/desktop/src/main/bot-import/host.ts b/apps/desktop/src/main/bot-import/host.tsindex ee409982f0..e8f510e555 100644--- a/apps/desktop/src/main/bot-import/host.ts+++ b/apps/desktop/src/main/bot-import/host.ts@@ -16,6 +16,7 @@ import type { CompanionImportPreview, CompanionImportResult, CompanionImportSeleimport { activeOwnerScopeKey, getActiveAppSession, isAppSessionBoundaryPending, ownerScopedUserDataPath } from '../appSessionState.js';import { createBotCanonicalSession, createBotProfile, getBotMemoryService, getBotRemoteResourceSource, reconcileBotProfileFolder } from '../localDb/ipc/bots.js';import { readBotProfileFolder, writeBotProfileFolder, BOT_PROFILE_TEXT_MAX_BYTES } from '../maker-ipc/botProfileFolder.js';+import { splitImportedMemoryText } from '../maker-ipc/botMemoryService.js';import { importBotSkillFiles, normalizeBotSkillSlug, validateBotSkillFiles } from '../maker-ipc/botSkillStore.js';import { withBotProfileLocks } from '../maker-ipc/botProfileLock.js';import { getRoutineEngine, routineTools, updateBotRoutineLifecycle } from '../routines/service.js';@@ -468,9 +469,25 @@ export async function startCompanionImport(selection: CompanionImportSelection,documents.push({ id: `memory-${fingerprint([item.view.id, file.name]).slice(0, 32)}`, name: file.name,text: `![${redactText(file.name).replace(/[\[\]\r\n]/g, '')}](${url})`, role: undefined });}- for (const document of documents) {- scope.assert();- await getBotMemoryService().importDocument(botId, document.id, redactText(document.name), redactText(document.text), document.role === 'user' ? 'user' : 'reference');+ const prepared = documents.map(document => {+ const text = redactText(document.text);+ return { ...document, text, parts: splitImportedMemoryText(text).length };+ });+ const total = prepared.reduce((sum, document) => sum + document.parts, 0);+ let saved = 0;+ for (const document of prepared) {+ try {+ scope.assert();+ await getBotMemoryService().importDocument(botId, document.id, redactText(document.name), document.text, document.role === 'user' ? 'user' : 'reference');+ saved += document.parts;+ } catch (error) {+ if (error instanceof Error) {+ const progress = (error as Error & { importProgress?: { saved: number; total: number } }).importProgress;+ Object.assign(error, { importProgress: { saved: saved + (progress?.saved ?? 0),+ total: total + (progress ? progress.total - document.parts : 0) } });+ }+ throw error;+ }}}if (prior?.environmentSaved) await companionEnvironmentStore.update(scope.root, botId, scope.assert, environment => {@@ -524,7 +541,16 @@ export async function startCompanionImport(selection: CompanionImportSelection,}, scope.assert);const roleText = (role: 'identity' | 'user' | 'instructions') => {const text = redactText(items.flatMap(item => [...(item.role === role ? [item.text] : []), ...(item.documents ?? []).filter(document => document.role === role).map(document => document.text)]).join('\n\n'));- return Buffer.byteLength(text, 'utf8') <= BOT_PROFILE_TEXT_MAX_BYTES ? text : `The imported ${role} documents are preserved in your personal memory. Search and read those original documents when applying the imported profile.`;+ if (Buffer.byteLength(text, 'utf8') <= BOT_PROFILE_TEXT_MAX_BYTES) return text;+ // Keep the original profile text without synthesizing new system instructions.+ // The entire source remains in personal memory and the encrypted document archive.+ let bytes = 0, end = 0;+ for (const char of text) {+ bytes += Buffer.byteLength(char, 'utf8');+ if (bytes > BOT_PROFILE_TEXT_MAX_BYTES) break;+ end += char.length;+ }+ return text.slice(0, end);};const folder = await readBotProfileFolder(scope.root, botId); scope.assert();await writeBotProfileFolder(scope.root, botId, {diff --git a/apps/desktop/src/main/bot-import/transfer.ts b/apps/desktop/src/main/bot-import/transfer.tsindex 52cf7e67ea..75ea48bd32 100644--- a/apps/desktop/src/main/bot-import/transfer.ts+++ b/apps/desktop/src/main/bot-import/transfer.ts@@ -65,9 +65,14 @@ export function validateImportSelection(value: CompanionImportSelection, snapsho}ranges = value.entryRanges;}- const items = snapshot.items.filter(item => ranges ? item.sourceIndex !== undefined && ranges.some(([first, last]) => item.sourceIndex! >= first && item.sourceIndex! <= last) : selected.has(item.view.id));const count = ranges ? ranges.reduce((sum, [first, last]) => sum + last - first + 1, 0) : selected.size;- if (!Number.isSafeInteger(count) || items.length !== count) throw new CompanionImportError('SELECTION_CHANGED');+ // Bound expansion by the actual snapshot before allocating indexes. This also+ // supports selected-only checkpoints without assuming their storage order.+ if (!Number.isSafeInteger(count) || count > snapshot.items.length) throw new CompanionImportError('SELECTION_CHANGED');+ const remaining = new Set<number>();+ if (ranges) for (const [first, last] of ranges) for (let index = first; index <= last; index++) remaining.add(index);+ const items = snapshot.items.filter(item => ranges ? item.sourceIndex !== undefined && remaining.delete(item.sourceIndex) : selected.has(item.view.id));+ if (items.length !== count || remaining.size) throw new CompanionImportError('SELECTION_CHANGED');selectedImportEnvironment(items);return resolveImportEnvironmentDependencies(items);}diff --git a/apps/desktop/src/main/maker-ipc/botMemoryService.ts b/apps/desktop/src/main/maker-ipc/botMemoryService.tsindex 5146fbd4ba..fbc1cec736 100644--- a/apps/desktop/src/main/maker-ipc/botMemoryService.ts+++ b/apps/desktop/src/main/maker-ipc/botMemoryService.ts@@ -38,6 +38,19 @@ const SEARCH_LIMIT = 50;const REFRESH_COALESCE_MS = 3_000;const FILENAME_RE = /^(user|feedback|project|reference)_[a-z0-9_-]{1,64}\.md$/;+/** Shared with import progress so counts use the storage service's exact boundaries. */+export function splitImportedMemoryText(text: string): string[] {+ const chunks: string[] = [];+ let chunk = '', bytes = 0;+ for (const char of text) {+ const size = Buffer.byteLength(char, 'utf8');+ if (bytes + size > BOT_MEMORY_BODY_MAX_BYTES) { chunks.push(chunk); chunk = ''; bytes = 0; }+ chunk += char; bytes += size;+ }+ if (chunk) chunks.push(chunk);+ return chunks;+}+interface BotMemoryOwner {canonicalSessionId: string | null;assertCurrent?: () => void;@@ -194,14 +207,7 @@ export function createBotMemoryService(deps: BotMemoryServiceDeps) {async importDocument(botId: string, id: string, title: string, text: string, type: 'user' | 'reference' = 'reference'): Promise<void> {if (!/^[a-z0-9_-]{1,40}$/.test(id) || !text.trim()) throwIpcError('INVALID_PARAMS', 'Invalid imported memory');const { store, owner } = await storeOf(botId);- const chunks: string[] = [];- let chunk = '', bytes = 0;- for (const char of text) {- const size = Buffer.byteLength(char, 'utf8');- if (bytes + size > BOT_MEMORY_BODY_MAX_BYTES) { chunks.push(chunk); chunk = ''; bytes = 0; }- chunk += char; bytes += size;- }- if (chunk) chunks.push(chunk);+ const chunks = splitImportedMemoryText(text);for (const [index, body] of chunks.entries()) {// Whitespace inside a source document is content too.if (!body) continue;