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(providers): 补齐旧线程存储归属与 OAuth 来源校验

Dashcadcf4586ef2
diff --git a/apps/desktop/src/main/maker-host/__tests__/codex-thread-locations.test.ts b/apps/desktop/src/main/maker-host/__tests__/codex-thread-locations.test.ts
index 87adb62bf6..43d81d3011 100644
--- a/apps/desktop/src/main/maker-host/__tests__/codex-thread-locations.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/codex-thread-locations.test.ts
@@ -1,7 +1,7 @@
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
-import { afterEach, describe, expect, it } from 'vitest';
+import { afterEach, describe, expect, it, vi } from 'vitest';
import { CodexThreadLocations } from '../codex-thread-locations';
const directories: string[] = [];
@@ -30,6 +30,30 @@ describe('native thread locations across accounts', () => {
expect(await reopened.readStorageHome('unknown')).toBeUndefined();
expect(await fs.readFile(rollout, 'utf8')).toBe('history');
});
+ it('resolves unindexed legacy history before account host startup and remembers its native home', async () => {
+ const { root, locations } = await fixture();
+ const home = path.join(root, 'codex-home');
+ const rollout = path.join(home, 'sessions', 'old.jsonl');
+ await fs.mkdir(path.dirname(rollout), { recursive: true });
+ await fs.writeFile(rollout, 'old native history\n');
+ const prepare = vi.fn().mockResolvedValue(rollout);
+ expect(await locations.readStorageHome('old-thread', { home, prepare })).toBe(home);
+ expect(prepare).toHaveBeenCalledWith('old-thread');
+ expect(await locations.read('old-thread')).toBe(rollout);
+ expect(await locations.readStorageHome('old-thread', { home, prepare })).toBe(home);
+ expect(prepare).toHaveBeenCalledTimes(1);
+ expect(await fs.readFile(rollout, 'utf8')).toBe('old native history\n');
+ await fs.rm(rollout);
+ await expect(locations.readStorageHome('old-thread', { home, prepare })).rejects.toThrow();
+ expect(prepare).toHaveBeenCalledTimes(1);
+ });
+ it('does not invent a storage home when legacy preparation finds no history', async () => {
+ const { root, locations } = await fixture();
+ const prepare = vi.fn().mockResolvedValue(undefined);
+ expect(await locations.readStorageHome('missing', { home: root, prepare })).toBeUndefined();
+ expect(await locations.read('missing')).toBeUndefined();
+ });
+
it('preserves the latest history when A resumes in B and returns to A', async () => {
const { root, locations } = await fixture();
const rollout = path.join(root, 'account-a.jsonl');
diff --git a/apps/desktop/src/main/maker-host/codex-thread-locations.ts b/apps/desktop/src/main/maker-host/codex-thread-locations.ts
index 1098166fa1..9808e78951 100644
--- a/apps/desktop/src/main/maker-host/codex-thread-locations.ts
+++ b/apps/desktop/src/main/maker-host/codex-thread-locations.ts
@@ -33,9 +33,19 @@ export class CodexThreadLocations {
return value.path;
}
- async readStorageHome(threadId: string): Promise<string | undefined> {
+ async readStorageHome(
+ threadId: string,
+ legacy?: { home: string; prepare: (threadId: string) => Promise<string | undefined> },
+ ): Promise<string | undefined> {
const rollout = await this.read(threadId);
- if (!rollout) return;
+ if (!rollout) {
+ // Pre-multi-account threads have no location record. Resolve their native
+ // storage before an account-specific host starts, without moving history.
+ const legacyRollout = await legacy?.prepare(threadId);
+ if (!legacy || !legacyRollout) return;
+ await this.record(threadId, legacyRollout, legacy.home);
+ return legacy.home;
+ }
const value = JSON.parse(await fs.readFile(this.file(threadId), 'utf8'));
if (value.sqliteHome !== undefined) {
if (typeof value.sqliteHome !== 'string' || !path.isAbsolute(value.sqliteHome)) throw new Error('Invalid Codex history storage');
diff --git a/apps/desktop/src/main/maker-host/index.ts b/apps/desktop/src/main/maker-host/index.ts
index 8374872f8c..b7dcc6baaf 100644
--- a/apps/desktop/src/main/maker-host/index.ts
+++ b/apps/desktop/src/main/maker-host/index.ts
@@ -1855,7 +1855,10 @@ export function getMaker(): Maker {
},
resolveCodexThreadStorageHome: async (threadId) => {
if (!getActiveAppSession().dataOwnerId) return;
- return new CodexThreadLocations(path.join(codexAccountHome('thread-index'), 'locations')).readStorageHome(threadId);
+ return new CodexThreadLocations(path.join(codexAccountHome('thread-index'), 'locations')).readStorageHome(threadId, {
+ home: getCodexHome(),
+ prepare: prepareExternalCodexSessionForResume,
+ });
},
recordCodexThreadLocation: async (threadId, storageHome, rolloutPath) => {
if (!rolloutPath || !getActiveAppSession().dataOwnerId) return;
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/providerHandlers.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/providerHandlers.test.ts
index 780441e7fc..954780c71c 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/providerHandlers.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/providerHandlers.test.ts
@@ -965,6 +965,23 @@ describe('provider:models-auto-refresh handler', () => {
});
});
+describe('provider OAuth sender boundary', () => {
+ it.each([false, true])('rejects all OAuth mutations before side effects (missing guard=%s)', async (missing) => {
+ const harness = new IpcHarness();
+ const guard = vi.fn(() => { throwIpcError('PERMISSION_DENIED', 'untrusted sender'); });
+ const deps = makeDeps({ assertTrustedSender: missing ? undefined : guard });
+ registerProviderHandlers(harness, deps);
+ for (const channel of [MAKER_INVOKE.PROVIDER_OAUTH_LOGIN, MAKER_INVOKE.PROVIDER_OAUTH_LOGOUT, MAKER_INVOKE.PROVIDER_OAUTH_CANCEL]) {
+ await expect(harness.invokeFrom(123, channel, 'openai-account')).rejects.toThrow(/PERMISSION_DENIED/);
+ }
+ if (!missing) expect(guard).toHaveBeenCalledTimes(3);
+ expect(deps.oauthLogin).not.toHaveBeenCalled();
+ expect(deps.oauthLogout).not.toHaveBeenCalled();
+ expect(deps.oauthCancel).not.toHaveBeenCalled();
+ expect(deps.beginRouteMutation).not.toHaveBeenCalled();
+ });
+});
+
describe('provider:custom:* CRUD handlers', () => {
it('rejects credential-mutating CRUD before parsing or touching secrets for an untrusted sender', async () => {
const harness = new IpcHarness();
diff --git a/apps/desktop/src/main/maker-ipc/providerHandlers.ts b/apps/desktop/src/main/maker-ipc/providerHandlers.ts
index b5d22a5f48..828fb1fe43 100644
--- a/apps/desktop/src/main/maker-ipc/providerHandlers.ts
+++ b/apps/desktop/src/main/maker-ipc/providerHandlers.ts
@@ -2034,6 +2034,7 @@ export function registerProviderHandlers(
registry.handle(
MAKER_INVOKE.PROVIDER_OAUTH_LOGIN,
async (event, providerId: unknown, rawOptions?: unknown) => {
+ assertTrustedProviderMutationSender(event);
const id = requireProviderId(providerId);
const { ownerId } = requireProviderOAuthLoginOptions(rawOptions);
const sender = providerOAuthRendererSender(event);
@@ -2092,7 +2093,8 @@ export function registerProviderHandlers(
}
},
);
- registry.handle(MAKER_INVOKE.PROVIDER_OAUTH_LOGOUT, async (_event, providerId: unknown) => {
+ registry.handle(MAKER_INVOKE.PROVIDER_OAUTH_LOGOUT, async (event, providerId: unknown) => {
+ assertTrustedProviderMutationSender(event);
const id = requireProviderId(providerId);
const ownerAtIngress = captureProviderOwnerSession();
const generation = beginOAuthMutation(id);
@@ -2139,6 +2141,7 @@ export function registerProviderHandlers(
registry.handle(
MAKER_INVOKE.PROVIDER_OAUTH_CANCEL,
async (event, providerId: unknown, rawOptions?: unknown) => {
+ assertTrustedProviderMutationSender(event);
const id = requireProviderId(providerId);
const { releaseOwner, ownerId } = requireProviderOAuthCancelOptions(rawOptions);
if (releaseOwner) {