Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(companions): upload large remote selections in bounded chunks
Chris1619903b3969
diff --git a/apps/desktop/src/main/bot-import/README.md b/apps/desktop/src/main/bot-import/README.mdindex 3ce8d80baf..5ecce4e441 100644--- a/apps/desktop/src/main/bot-import/README.md+++ b/apps/desktop/src/main/bot-import/README.md@@ -477,3 +477,15 @@ Ordinary content/routine progress uses indexed checks and at most about 100 fullreceipt snapshots per pass, sized from the selected input. An interrupted batchreplays idempotent writes and `createOnce` under the same IDs. Handover intent,source pause, target enable and rollback milestones still persist immediately.++New hosts also advertise `selectionChunks` on previews. The shared remote client+uploads large selections (including sparse ranges and portraits) in 8 Ki UTF-16+pieces through the existing import action, below its unchanged 64 KiB input limit.+The host bounds retained uploads by owner/controller, expiry and the snapshot byte+budget. Repeated identical pieces are accepted; changed or out-of-order pieces+are rejected. Only a complete upload reaches the existing selection validation+and durable import transaction. A disconnected client can replay from zero using+the same request ID. This recovery affects only that import, never reconnects the+peer or replays other actions. Old hosts keep their original single-action path;+old clients continue sending plain selections to new hosts. No relay or server+change is required.diff --git a/apps/desktop/src/main/bot-import/__tests__/host.test.ts b/apps/desktop/src/main/bot-import/__tests__/host.test.tsindex fe145d538a..04a1529606 100644--- a/apps/desktop/src/main/bot-import/__tests__/host.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/host.test.ts@@ -1,4 +1,4 @@-import { remoteCompanionImportApi, compactCompanionImportSelection } from '@cindy/maker-shared/companion-import';+import { remoteCompanionImportApi, compactCompanionImportSelection, companionImportSubmissions } from '@cindy/maker-shared/companion-import';import fsSync, { promises as fs } from 'node:fs';import os from 'node:os';import path from 'node:path';@@ -68,7 +68,7 @@ vi.mock('../../routines/service.js', () => ({h.routines = h.routines.map(row => row.id === id ? { ...row, ...input, revision: row.revision + 1 } : row);} }),}));-import { readRemoteCompanionImport, listCompanionImportSources, previewCompanionImport, startCompanionImport, getCompanionImportResult, recoverCompanionImports, prepareCompanionImportDeletion, cancelCompanionImportsForDeletion, ensureImportedAutomationReady } from '../host.js';+import { submitRemoteCompanionImport, readRemoteCompanionImport, listCompanionImportSources, previewCompanionImport, startCompanionImport, getCompanionImportResult, recoverCompanionImports, prepareCompanionImportDeletion, cancelCompanionImportsForDeletion, ensureImportedAutomationReady } from '../host.js';import { withBotProfileLocks } from '../../maker-ipc/botProfileLock.js';import { assertImportedAutomationReady, prepareImportedAutomation } from '../automationRuntime.js';import { decodeBotAvatarImage } from '../../localDb/ipc/botAvatarSelection.js';@@ -1192,3 +1192,31 @@ it('retries legacy text and empty memory assets without passing them to the mediawait startCompanionImport(selection, 'phone');expect(h.importDocument).toHaveBeenCalledOnce();});++it('receives sparse selection chunks idempotently and starts only after the complete authorized selection', async () => {+ h.snapshot.items = Array.from({ length: 10_000 }, (_, index) => ({ view: { id: `memory-${index}`, name: `Note ${index}`, category: 'memory' as const, selected: index % 2 === 0 }, text: 'Original' }));+ const [source] = await listCompanionImportSources('phone');+ const preview = await previewCompanionImport(source!.id, 'phone');+ const chosen = preview.entries.filter(entry => entry.selected).map(entry => entry.id);+ const selection = { previewId: preview.id, requestId: 'sparse-selection-fixture', name: 'Ada', takeover: false, deferSetup: true, ...compactCompanionImportSelection(preview, chosen) };+ const parts = [...companionImportSubmissions(selection, preview.selectionChunks)];+ expect(parts.length).toBeGreaterThan(2);+ await expect(submitRemoteCompanionImport(parts[1]!, 'other-phone')).rejects.toThrow('PREVIEW_EXPIRED');+ expect(await submitRemoteCompanionImport(parts[0]!, 'phone')).toBeUndefined();+ expect(await submitRemoteCompanionImport(parts[0]!, 'phone')).toBeUndefined();+ expect(createBotProfile).not.toHaveBeenCalled();+ if (!('selectionChunk' in parts[0]!)) throw Error('expected chunks');+ await expect(submitRemoteCompanionImport({ selectionChunk: { ...parts[0]!.selectionChunk, text: 'x'.repeat(parts[0]!.selectionChunk.text.length) } }, 'phone')).rejects.toThrow('REQUEST_ALREADY_USED');+ h.boundary = true;+ await expect(submitRemoteCompanionImport(parts[1]!, 'phone')).rejects.toThrow('OWNER_CHANGED');+ h.boundary = false;+ let accepted;+ for (const part of parts) accepted = await submitRemoteCompanionImport(part, 'phone');+ const result = await withBotProfileLocks([accepted!.botId], () => getCompanionImportResult(selection.requestId));+ expect(result?.savedEntryIds).toEqual(chosen);+ expect(h.importDocument).toHaveBeenCalledTimes(chosen.length);+ for (const part of parts) await submitRemoteCompanionImport(part, 'phone');+ await withBotProfileLocks([accepted!.botId], () => getCompanionImportResult(selection.requestId));+ expect(createBotProfile).toHaveBeenCalledTimes(1);+ expect(h.importDocument).toHaveBeenCalledTimes(chosen.length);+});diff --git a/apps/desktop/src/main/bot-import/__tests__/registration.test.ts b/apps/desktop/src/main/bot-import/__tests__/registration.test.tsindex ff3c622c8e..bfa67ff9df 100644--- a/apps/desktop/src/main/bot-import/__tests__/registration.test.ts+++ b/apps/desktop/src/main/bot-import/__tests__/registration.test.ts@@ -1,7 +1,7 @@import { beforeAll, expect, it, vi } from 'vitest';const h = vi.hoisted(() => ({ channel: 'maker:remote-resources:invoke', remote: vi.fn(), start: vi.fn(), sources: vi.fn(), preview: vi.fn(), status: vi.fn(), handlers: new Map<string, (event: unknown, operation: unknown, input?: unknown) => Promise<unknown>>() }));vi.mock('electron', () => ({ ipcMain: { handle: (name: string, fn: (event: unknown, operation: unknown, input?: unknown) => Promise<unknown>) => h.handlers.set(name, fn) } }));-vi.mock('../host.js', () => ({ startCompanionImport: h.start, listCompanionImportSources: h.sources, previewCompanionImport: h.preview, getCompanionImportResult: h.status, readRemoteCompanionImport: h.remote }));+vi.mock('../host.js', () => ({ startCompanionImport: h.start, submitRemoteCompanionImport: h.start, listCompanionImportSources: h.sources, previewCompanionImport: h.preview, getCompanionImportResult: h.status, readRemoteCompanionImport: h.remote }));vi.mock('../../security/trustedAppRenderer.js', () => ({ assertTrustedAppRendererEvent: vi.fn() }));vi.mock('../../device-link/invoke-context.js', () => ({ getDeviceLinkInvokeContext: () => ({ controllerDeviceId: 'phone', channel: h.channel }) }));import { registerCompanionImport } from '../registration.js';@@ -50,3 +50,9 @@ it.each([false, true])('negotiates chunk reads without changing the legacy resouawait h.handlers.get('maker:remote-resources:get')!({}, { ref: { collectionId: 'companion-import', kind: 'import', id: 'preview:source' }, client: { protocolVersion: 1, primitives: ['companion-import', ...(supported ? ['companion-import-chunks-v1'] : [])] } });expect(h.remote).toHaveBeenLastCalledWith('preview:source', 'phone', supported);});++it('acknowledges a selection chunk without navigating before the host accepts the full import', async () => {+ h.channel = 'maker:remote-resources:invoke';+ h.start.mockResolvedValueOnce(undefined);+ expect(await invoke()).toMatchObject({ effects: [] });+});diff --git a/apps/desktop/src/main/bot-import/host.ts b/apps/desktop/src/main/bot-import/host.tsindex 819fb0be27..01cb72e660 100644--- a/apps/desktop/src/main/bot-import/host.ts+++ b/apps/desktop/src/main/bot-import/host.ts@@ -11,8 +11,8 @@ import { parseRoutineInput, type RoutineInput } from '@cindy/maker-scheduler';import { normalizeBotName } from '../../shared/botCreation.js';import { listBotRemoteResourceSources } from '../localDb/ipc/bots.js';import { validateBotAvatarBuffer, decodeBotAvatarImage } from '../localDb/ipc/botAvatarSelection.js';-import { COMPANION_IMPORT_CHUNK_LENGTH, COMPANION_IMPORT_READ_MAX_LENGTH } from '@cindy/maker-shared/companion-import';-import type { CompanionImportPreview, CompanionImportResult, CompanionImportSelection, CompanionImportSource } from '@cindy/maker-shared/companion-import';+import { COMPANION_IMPORT_CHUNK_LENGTH, COMPANION_IMPORT_READ_MAX_LENGTH, COMPANION_IMPORT_SELECTION_CHUNK_LENGTH } from '@cindy/maker-shared/companion-import';+import type { CompanionImportPreview, CompanionImportResult, CompanionImportSelection, CompanionImportSubmission, CompanionImportSource } from '@cindy/maker-shared/companion-import';import { 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';@@ -39,6 +39,7 @@ interface Owned<T> { owner: string; controller: string; value: T; createdAt: numconst sources = new Map<string, Owned<ImportSource>>();const previews = new Map<string, Owned<ImportSnapshot> & { bytes: number }>();const remoteReads = new Map<string, Owned<string>>();+const remoteSelections = new Map<string, Owned<{ parts: string[]; offset: number; total: number }>>();const jobs = new Map<string, Promise<CompanionImportResult>>();const TTL = 30 * 60_000;function owner() {@@ -119,7 +120,7 @@ export async function previewCompanionImport(sourceId: string, controller: strinretainPreview(id, { owner: scope.scope, controller, value: snapshot, createdAt: Date.now() });const secrets = previewImportRedactions(snapshot.items);const redact = (text: string) => redactEnvironmentValues(text, secrets);- return { id, selectionRanges: true, source: { id: sourceId, kind: source.kind, name: redact(source.name) }, name: redact(source.name),+ return { id, selectionRanges: true, selectionChunks: true, source: { id: sourceId, kind: source.kind, name: redact(source.name) }, name: redact(source.name),...(snapshot.avatarImageBase64 ? { avatarImageBase64: snapshot.avatarImageBase64 } : {}),entries: snapshot.items.map(({ view }) => ({ ...view, name: redact(view.name),...(view.description === undefined ? {} : { description: redact(view.description) }) })) };@@ -161,6 +162,47 @@ export async function readRemoteCompanionImport(id: string, controller: string,return chunk(token, text, 0);}+/** No import side effects until the entire selection arrives. A reconnect replays+ * chunks from zero under the same request ID, then uses normal durable recovery. */+export async function submitRemoteCompanionImport(input: CompanionImportSubmission, controller: string): Promise<CompanionImportResult | undefined> {+ const scope = owner();+ if (!input || typeof input !== 'object' || !('selectionChunk' in input)) return startCompanionImport(input as CompanionImportSelection, controller);+ const chunk = input.selectionChunk;+ if (!chunk || typeof chunk.id !== 'string' || !/^[A-Za-z0-9_-]{16,100}$/.test(chunk.id)+ || !Number.isSafeInteger(chunk.offset) || chunk.offset < 0 || chunk.offset % COMPANION_IMPORT_SELECTION_CHUNK_LENGTH+ || !Number.isSafeInteger(chunk.total) || chunk.total <= 0 || chunk.total * 2 > MAX_SNAPSHOT_BYTES+ || typeof chunk.text !== 'string' || !chunk.text.length || chunk.text.length > COMPANION_IMPORT_SELECTION_CHUNK_LENGTH+ || chunk.offset + chunk.text.length > chunk.total+ || chunk.offset + chunk.text.length < chunk.total && chunk.text.length !== COMPANION_IMPORT_SELECTION_CHUNK_LENGTH)+ throw new CompanionImportError('INVALID_SELECTION');+ prune(remoteSelections);+ const key = JSON.stringify([scope.scope, controller, chunk.id]);+ if (!remoteSelections.has(key)) {+ if (chunk.offset !== 0) throw new CompanionImportError('PREVIEW_EXPIRED');+ for (const [id, prior] of remoteSelections) if (prior.controller === controller) remoteSelections.delete(id);+ let bytes = [...remoteSelections.values()].reduce((sum, prior) => sum + prior.value.total * 2, 0);+ for (const [id, prior] of remoteSelections) {+ if (remoteSelections.size < 4 && bytes + chunk.total * 2 <= MAX_SNAPSHOT_BYTES) break;+ remoteSelections.delete(id); bytes -= prior.value.total * 2;+ }+ remoteSelections.set(key, { owner: scope.scope, controller, createdAt: Date.now(), value: { parts: [], offset: 0, total: chunk.total } });+ }+ const upload = owned(remoteSelections, key, controller);+ if (upload.total !== chunk.total || chunk.offset > upload.offset) throw new CompanionImportError('INVALID_SELECTION');+ if (chunk.offset < upload.offset) {+ if (upload.parts[chunk.offset / COMPANION_IMPORT_SELECTION_CHUNK_LENGTH] !== chunk.text) throw new CompanionImportError('REQUEST_ALREADY_USED');+ return undefined;+ }+ upload.parts.push(chunk.text); upload.offset += chunk.text.length;+ if (upload.offset < upload.total) return undefined;+ const text = upload.parts.join('');+ upload.parts.length = 0; remoteSelections.delete(key);+ let selection: CompanionImportSelection;+ try { selection = JSON.parse(text); } catch { throw new CompanionImportError('INVALID_SELECTION'); }+ if (!selection || selection.requestId !== chunk.id) throw new CompanionImportError('INVALID_SELECTION');+ return startCompanionImport(selection, controller);+}+function documentEntries(item: ImportItem): Array<[string, string]> {return [...(item.text === undefined ? [] : [[item.view.id, item.text] as [string, string]]),...(item.documents ?? []).map(document => [document.id, document.text] as [string, string])];diff --git a/apps/desktop/src/main/bot-import/registration.ts b/apps/desktop/src/main/bot-import/registration.tsindex 0497c8d027..1ce3f4e674 100644--- a/apps/desktop/src/main/bot-import/registration.ts+++ b/apps/desktop/src/main/bot-import/registration.ts@@ -1,8 +1,8 @@import { ipcMain } from 'electron';-import { COMPANION_IMPORT_CHUNK_PRIMITIVE, type CompanionImportSelection } from '@cindy/maker-shared/companion-import';+import { COMPANION_IMPORT_CHUNK_PRIMITIVE, type CompanionImportSelection, type CompanionImportSubmission } from '@cindy/maker-shared/companion-import';import { remoteResourceRegistry, RemoteResourceRegistryError } from '../device-link/remoteResourceRegistry.js';import { assertTrustedAppRendererEvent } from '../security/trustedAppRenderer.js';-import { listCompanionImportSources, previewCompanionImport, startCompanionImport, getCompanionImportResult, readRemoteCompanionImport } from './host.js';+import { listCompanionImportSources, previewCompanionImport, startCompanionImport, getCompanionImportResult, readRemoteCompanionImport, submitRemoteCompanionImport } from './host.js';import { CompanionImportError } from './types.js';import { isIpcErrorCode } from '../../shared/ipc-errors.js';import { throwIpcError } from '../utils/ipcValidate.js';@@ -39,7 +39,8 @@ export function registerCompanionImport(): void {async invoke(context, request) {try {if (request.actionId !== 'import' || !request.resourceRef?.id.startsWith('preview:')) throw new CompanionImportError('INVALID_REQUEST');- const result = await startCompanionImport(request.input as unknown as CompanionImportSelection, context.controllerDeviceId);+ const result = await submitRemoteCompanionImport(request.input as unknown as CompanionImportSubmission, context.controllerDeviceId);+ if (!result) return { effects: [] };return { effects: [{ kind: 'navigate', target: { kind: 'resource', ref: { collectionId: 'companion-import', kind: 'import', id: `result:${result.requestId}` } } }] };} catch (error) {// Stable host rejections must survive the IPC boundary so the phone candiff --git a/apps/mobile/src/__tests__/CompanionImportSheet.test.tsx b/apps/mobile/src/__tests__/CompanionImportSheet.test.tsxindex 0677d4e145..b4bd91bf40 100644--- a/apps/mobile/src/__tests__/CompanionImportSheet.test.tsx+++ b/apps/mobile/src/__tests__/CompanionImportSheet.test.tsx@@ -202,3 +202,27 @@ it('shows failed filenames and saved parts after a partial import without blockiawait click('devices.companionImport.open');expect(h.created).toHaveBeenCalledWith({ collectionId: 'teammates', kind: 'bot', id: 'bot' });});++it('submits sparse 100,000-entry choices using bounded actions with the original selection intact', async () => {+ const entries = Array.from({ length: 100_000 }, (_, index) => ({ id: `entry-${index}`, name: `Memory ${index}`, category: 'memory', selected: index % 2 === 0 }));+ h.invoke.mockImplementation(async (_host: string, _channel: string, args: any[]) => {+ const id = args[0].ref.id;+ return { blocks: [{ primitive: 'companion-import', data: id === 'sources' ? { sources: [{ id: 'source', name: 'Ada', kind: 'hermes' }] }+ : id.startsWith('preview:') ? { preview: { id: 'preview', selectionRanges: true, selectionChunks: true, name: 'Ada', source: { id: 'source', name: 'Ada', kind: 'hermes' }, entries } }+ : { result: null } }] };+ });+ h.submit.mockReset().mockResolvedValue({ effects: [] });+ const container = document.createElement('div'); root = createRoot(container);+ await act(async () => root!.render(createElement(CompanionImportSheet, { visible: true, deviceId: 'host', deviceName: 'Mac', online: true, onClose() {}, onCreated: h.created })));+ const click = async (text: string) => { await act(async () => [...container.querySelectorAll('button')].find(button => button.textContent === text)!.click()); };+ await click('Ada · Hermes'); await click('devices.companionImport.submit');+ const { parseRemoteActionInvokeRequest, REMOTE_RESOURCE_PROTOCOL_VERSION } = await import('@cindy/device-link');+ expect(h.submit.mock.calls.length).toBeGreaterThan(2);+ let text = '';+ for (const call of h.submit.mock.calls) {+ const request = call[2];+ expect(parseRemoteActionInvokeRequest({ ...request, client: { protocolVersion: REMOTE_RESOURCE_PROTOCOL_VERSION, primitives: [] } })).not.toBeNull();+ text += request.input.selectionChunk.text;+ }+ expect(JSON.parse(text)).toMatchObject({ entryIds: [], entryRanges: entries.filter(entry => entry.selected).map(entry => { const index = Number(entry.id.slice(6)); return [index, index]; }), deferSetup: true });+});diff --git a/apps/mobile/src/session/CompanionImportSheet.tsx b/apps/mobile/src/session/CompanionImportSheet.tsxindex 52b32ca801..9b0a1e3548 100644--- a/apps/mobile/src/session/CompanionImportSheet.tsx+++ b/apps/mobile/src/session/CompanionImportSheet.tsx@@ -3,7 +3,7 @@ import { Pressable, ScrollView, StyleSheet, Switch, View } from 'react-native';import { randomUUID } from 'expo-crypto';import { useTranslation } from 'react-i18next';import { parseRemoteActionInvokeRequest, REMOTE_RESOURCE_GET_CHANNEL, REMOTE_RESOURCE_PROTOCOL_VERSION, type RemoteResourceRef } from '@cindy/device-link';-import { COMPANION_IMPORT_CHUNK_PRIMITIVE, companionImportReasonKey, companionImportErrorCode, compactCompanionImportSelection, areCompanionImportEntriesSelected, toggleCompanionImportEntries, companionImportCategories, remoteCompanionImportApi, type CompanionImportPreview, type CompanionImportResult, type CompanionImportSelection, type CompanionImportSource } from '@cindy/maker-shared/companion-import';+import { COMPANION_IMPORT_CHUNK_PRIMITIVE, companionImportSubmissions, companionImportReasonKey, companionImportErrorCode, compactCompanionImportSelection, areCompanionImportEntriesSelected, toggleCompanionImportEntries, companionImportCategories, remoteCompanionImportApi, type CompanionImportPreview, type CompanionImportResult, type CompanionImportSelection, type CompanionImportSource } from '@cindy/maker-shared/companion-import';import { Text, TextInput } from '@/components/AppText';import { MainWindowActionButton } from '@/components/MobilePrimitives';import { useDeviceLink } from '@/device-link/DeviceLinkContext';@@ -73,11 +73,11 @@ export function CompanionImportSheet({ visible, onClose, onClosed, deviceId, devconst selection = { requestId: randomUUID(), previewId: preview.id, name: name.trim(), avatarImageBase64: avatar, ...compactCompanionImportSelection(preview, selected), takeover, deferSetup: true };// Validate the complete action with the host's wire parser before freezing// this request. Oversized inputs must remain editable, never retry forever.- if (!parseRemoteActionInvokeRequest({+ for (const part of companionImportSubmissions(selection, preview.selectionChunks)) if (!parseRemoteActionInvokeRequest({client: { protocolVersion: REMOTE_RESOURCE_PROTOCOL_VERSION, primitives: ['companion-import'], locale: i18n.language },collectionId: 'companion-import', actionId: 'import',resourceRef: { collectionId: 'companion-import', kind: 'import', id: `preview:${preview.source.id}` },- input: selection,+ input: part,})) throw new Error('INVALID_SELECTION');intent.current = selection;}diff --git a/packages/maker-shared/src/__tests__/companionImport.test.ts b/packages/maker-shared/src/__tests__/companionImport.test.tsindex a7712879e5..1da6e6c2de 100644--- a/packages/maker-shared/src/__tests__/companionImport.test.ts+++ b/packages/maker-shared/src/__tests__/companionImport.test.ts@@ -89,3 +89,27 @@ it('reads a multi-megabyte preview without losing escaped names, descriptions orexpect(reads).toBe(Math.ceil(serialized.length / COMPANION_IMPORT_CHUNK_LENGTH));expect(compactCompanionImportSelection(received, received.entries.map(entry => entry.id))).toEqual({ entryIds: [], entryRanges: [[0, 11999]] });});++it('retries a disconnected 100,000-entry sparse selection upload under the same request without truncation', async () => {+ const entries: CompanionImportEntry[] = Array.from({ length: 100_000 }, (_, index) => ({ id: `memory-${index}`, name: `Note ${index}`, category: 'memory', selected: index % 2 === 0 }));+ const preview = { id: 'preview', source: { id: 'source', kind: 'hermes', name: 'Ada' }, name: 'Ada', selectionRanges: true, selectionChunks: true, entries };+ const result = { requestId: 'sparse-upload-request', botId: 'bot', status: 'complete', checks: [] };+ let calls = 0, disconnected = true, text = '';+ const invoke = vi.fn<Parameters<typeof remoteCompanionImportApi>[1]>(async (_source, input) => {+ calls++;+ if (disconnected && calls === 3) { disconnected = false; throw Error('DISCONNECTED'); }+ if (!('selectionChunk' in input)) throw Error('expected chunks');+ expect(new TextEncoder().encode(JSON.stringify(input)).length).toBeLessThan(64 * 1024);+ if (input.selectionChunk.offset === 0) text = '';+ expect(input.selectionChunk.offset).toBe(text.length);+ text += input.selectionChunk.text;+ });+ const api = remoteCompanionImportApi(async id => ({ blocks: [{ primitive: 'companion-import', data: id.startsWith('preview:') ? { preview } : { result } }] }), invoke);+ const received = await api.preview('source');+ const selection = { previewId: received.id, requestId: result.requestId, name: 'Ada', takeover: false, ...compactCompanionImportSelection(received, entries.filter(entry => entry.selected).map(entry => entry.id)) };+ expect(JSON.stringify(selection).length).toBeGreaterThan(64 * 1024);+ await expect(api.start(selection)).rejects.toThrow('DISCONNECTED');+ await expect(api.start(selection)).resolves.toEqual(result);+ expect(JSON.parse(text)).toEqual(selection);+ expect(calls).toBeGreaterThan(10);+});diff --git a/packages/maker-shared/src/companionImport.ts b/packages/maker-shared/src/companionImport.tsindex 337a9ec00e..7cb2236c50 100644--- a/packages/maker-shared/src/companionImport.ts+++ b/packages/maker-shared/src/companionImport.ts@@ -31,6 +31,8 @@ export interface CompanionImportPreview {entries: CompanionImportEntry[];/** Host understands compact immutable preview-index ranges. */selectionRanges?: true;+ /** Host accepts bounded selection uploads through the existing import action. */+ selectionChunks?: true;}export interface CompanionImportSelection {@@ -47,6 +49,20 @@ export interface CompanionImportSelection {deferSetup?: boolean;}+export type CompanionImportSubmission = CompanionImportSelection | {+ selectionChunk: { id: string; offset: number; total: number; text: string };+};+// Even worst-case JSON escaping stays below the existing 64 KiB action budget.+export const COMPANION_IMPORT_SELECTION_CHUNK_LENGTH = 8 * 1024;+export function* companionImportSubmissions(selection: CompanionImportSelection, chunked = false): Generator<CompanionImportSubmission> {+ if (!chunked) { yield selection; return; }+ const text = JSON.stringify(selection);+ if (text.length <= COMPANION_IMPORT_SELECTION_CHUNK_LENGTH) { yield selection; return; }+ for (let offset = 0; offset < text.length; offset += COMPANION_IMPORT_SELECTION_CHUNK_LENGTH) {+ yield { selectionChunk: { id: selection.requestId, offset, total: text.length, text: text.slice(offset, offset + COMPANION_IMPORT_SELECTION_CHUNK_LENGTH) } };+ }+}+export interface CompanionImportCheck {entryId: string;status: 'copied' | 'verified' | 'needs-attention' | 'taken-over' | 'paused';@@ -135,7 +151,7 @@ export const COMPANION_IMPORT_READ_MAX_LENGTH = 128 * 1024 * 1024;/** Transport-neutral client: Desktop and Mobile use their existing Remote Resource adapters. */export function remoteCompanionImportApi(read: (id: string) => Promise<unknown>,- invoke: (sourceId: string, selection: CompanionImportSelection) => Promise<unknown>,+ invoke: (sourceId: string, selection: CompanionImportSubmission) => Promise<unknown>,): CompanionImportApi {const readData = async (id: string): Promise<Record<string, unknown>> => {const raw = await read(id) as { blocks?: Array<{ primitive?: string; data?: Record<string, unknown> }> };@@ -180,7 +196,7 @@ export function remoteCompanionImportApi(|| !['running', 'complete', 'needs-attention'].includes(result.status))) throw new Error('INVALID_IMPORT_RESPONSE');return result ?? undefined;};- const sources = new Map<string, string>();+ const sources = new Map<string, { id: string; selectionChunks: boolean }>();return {async sources() {const result = (await data('sources')).sources as CompanionImportSource[];@@ -191,13 +207,13 @@ export function remoteCompanionImportApi(const result = (await data(`preview:${sourceId}`)).preview as CompanionImportPreview;if (!result?.id || !Array.isArray(result.entries)|| result.entries.some(entry => !entry.id || typeof entry.name !== 'string' || !companionImportCategories.includes(entry.category))) throw new Error('INVALID_IMPORT_RESPONSE');- sources.set(result.id, sourceId);+ sources.set(result.id, { id: sourceId, selectionChunks: result.selectionChunks === true });return result;},async start(selection) {const sourceId = sources.get(selection.previewId);if (!sourceId) throw new Error('PREVIEW_EXPIRED');- await invoke(sourceId, selection);+ for (const part of companionImportSubmissions(selection, sourceId.selectionChunks)) await invoke(sourceId.id, part);const result = await status(selection.requestId);if (!result) throw new Error('IMPORT_RECEIPT_MISSING');return result;