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): bound async legacy recovery marker reads

Yuewei-LLd9391d865092
diff --git a/apps/desktop/src/main/__tests__/ownerNamespaceMigration.test.ts b/apps/desktop/src/main/__tests__/ownerNamespaceMigration.test.ts
index 3dabd1a59f..28f4990a94 100644
--- a/apps/desktop/src/main/__tests__/ownerNamespaceMigration.test.ts
+++ b/apps/desktop/src/main/__tests__/ownerNamespaceMigration.test.ts
@@ -2055,21 +2055,28 @@ describe('legacy Ghost plugin recovery', () => {
JSON.stringify({ version: 1, ownerKey, pendingIds: ['queued-plugin'] }),
);
await writeGhostDir(root, 'brain', 'queued-plugin');
- const result = await recoverLegacyGhostPlugins(
- { mode: 'cloud', dataOwnerId: ownerId, user: { id: ownerId } },
- realFsDeps(root, {}, {
- readFile: (file: string) =>
- path.resolve(file) === path.resolve(markerPath)
- ? Promise.reject(Object.assign(new Error('marker locked'), { code: 'EACCES' }))
- : fs.readFile(file, 'utf8'),
- }),
- );
- expect(result).toEqual({
- status: 'deferred',
- moved: 0,
- conflicts: 0,
- deferredReason: 'legacy-discovery-incomplete',
- });
+ // readLegacyGhostRecoveryMarker now uses readBoundedFileNoFollow;
+ // inject EACCES at the bounded reader so the marker stays unreadable.
+ const boundedModule = await import('../utils/readBoundedFile.js');
+ const readerSpy = vi
+ .spyOn(boundedModule, 'readBoundedFileNoFollow')
+ .mockRejectedValueOnce(
+ Object.assign(new Error('marker locked'), { code: 'EACCES' }),
+ );
+ try {
+ const result = await recoverLegacyGhostPlugins(
+ { mode: 'cloud', dataOwnerId: ownerId, user: { id: ownerId } },
+ realFsDeps(root),
+ );
+ expect(result).toEqual({
+ status: 'deferred',
+ moved: 0,
+ conflicts: 0,
+ deferredReason: 'legacy-discovery-incomplete',
+ });
+ } finally {
+ readerSpy.mockRestore();
+ }
await expect(fs.access(path.join(root, 'brain', 'queued-plugin'))).resolves.toBeUndefined();
});
diff --git a/apps/desktop/src/main/ownerNamespaceMigration.ts b/apps/desktop/src/main/ownerNamespaceMigration.ts
index 948c23d3fc..f7ff1d9a43 100644
--- a/apps/desktop/src/main/ownerNamespaceMigration.ts
+++ b/apps/desktop/src/main/ownerNamespaceMigration.ts
@@ -17,7 +17,7 @@ import {
type LegacyGhostRecoveryStatus,
} from '../shared/legacyGhostRecovery.js';
import { readLegacyGhostApprovalProjection } from './cindy-brain/GhostManager.js';
-import { readBoundedFileNoFollowSync } from './utils/readBoundedFile.js';
+import { readBoundedFileNoFollow, readBoundedFileNoFollowSync } from './utils/readBoundedFile.js';
const CLAIM_MARKER = '.owner-namespace-claim-v1.json';
const LEGACY_GHOST_RECOVERY_MARKER = '.legacy-ghost-recovery-v1.json';
@@ -208,12 +208,23 @@ async function readLegacyGhostRecoveryMarker(
deps: MigrationDeps,
markerPath: string,
): Promise<LegacyGhostRecoveryMarkerRead> {
- let text: string;
+ const ownerRoot = path.dirname(markerPath);
+ let ownerRealPath: string;
+ try {
+ ownerRealPath = await fs.realpath(ownerRoot);
+ } catch (error) {
+ return isMissing(error) ? { kind: 'missing' } : { kind: 'deferred' };
+ }
+ let bytes: Buffer | null;
try {
- text = await deps.readFile(markerPath);
+ bytes = await readBoundedFileNoFollow(markerPath, 64 * 1024, {
+ containWithin: ownerRealPath,
+ });
} catch (error) {
return isMissing(error) ? { kind: 'missing' } : { kind: 'deferred' };
}
+ if (bytes === null) return { kind: 'deferred' };
+ const text = bytes.toString('utf8');
try {
const parsed = JSON.parse(text) as Partial<LegacyGhostRecoveryMarker>;
const failedIds = parsed.failedIds ?? [];