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(auto-review): 为重连后的追加指令恢复历史授权限制

Dasha95fc0d0de04
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/autoReviewUserIntent.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/autoReviewUserIntent.test.ts
index ac84245108..30e34f802c 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/autoReviewUserIntent.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/autoReviewUserIntent.test.ts
@@ -1,6 +1,8 @@
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
+import { AUTO_REVIEW_SOURCE_CONTENT, AUTO_REVIEW_USER_INTENT, MAIN_OWNED_SEND_CONTEXT } from '@cindy/maker-core';
import {
restoreAutoReviewUserIntent,
+ restoreAutoReviewSteerIntent,
type AutoReviewHistoryMessage,
} from '../autoReviewUserIntent';
@@ -18,6 +20,62 @@ function user(text: string, clientId = text): AutoReviewHistoryMessage {
}
const current = { clientId: 'latest', content: { text: '修吧,改完跑相关测试。' } };
+describe('steer authorization restoration', () => {
+ it.each([false, true])('restores prior restrictions for queued/direct input (direct=%s)', async (direct) => {
+ const options = direct
+ ? { [MAIN_OWNED_SEND_CONTEXT]: { origin: { kind: 'desktop' as const }, rawChannelText: 'continue' } }
+ : { [AUTO_REVIEW_SOURCE_CONTENT]: 'continue' };
+ const intent = await restoreAutoReviewSteerIntent('plugin rewrite: deploy', options, async () => [
+ { ...user('Fix the bug. Do not deploy.'), createdAt: 1 },
+ { clientId: 'answer', role: 'ask_user', content: {}, createdAt: 2,
+ agentMeta: { autoReviewUserText: { text: 'Only edit src.', acceptedAt: 3 } } },
+ ]);
+ expect(intent).toContain('Do not deploy.');
+ expect(intent).toContain('Only edit src.');
+ expect(intent).toContain('continue');
+ expect(intent).not.toContain('plugin rewrite');
+ });
+
+ it.each(['', 'Inspect the new image.'])('preserves explicit resource replacement %j', async (intent) => {
+ const read = vi.fn(async () => [user('Deploy now.')]);
+ expect(await restoreAutoReviewSteerIntent('continue', { [AUTO_REVIEW_USER_INTENT]: intent }, read)).toBe(intent);
+ expect(read).not.toHaveBeenCalled();
+ });
+
+ it('does not restore owner history for external input or unverified source text', async () => {
+ const read = vi.fn(async () => [user('Deploy now.')]);
+ expect(await restoreAutoReviewSteerIntent('continue', {}, read)).toBeUndefined();
+ expect(await restoreAutoReviewSteerIntent('continue', {
+ [AUTO_REVIEW_SOURCE_CONTENT]: 'continue',
+ [MAIN_OWNED_SEND_CONTEXT]: { origin: { kind: 'im', channel: 'telegram' }, rawChannelText: 'continue' },
+ }, read)).toBeUndefined();
+ expect(read).not.toHaveBeenCalled();
+ });
+
+ it('discards prior targets for direct attachment input', async () => {
+ expect(await restoreAutoReviewSteerIntent([
+ { type: 'text', text: 'inspect' }, { type: 'image', path: '/new.png' },
+ ], { [AUTO_REVIEW_SOURCE_CONTENT]: 'inspect' }, async () => [user('Send the old image.')])).toBe('inspect');
+ });
+
+ it('keeps only current text when history is unavailable or has been cleared', async () => {
+ const options = { [AUTO_REVIEW_SOURCE_CONTENT]: 'continue' };
+ expect(await restoreAutoReviewSteerIntent('continue', options, async () => [])).toBe('continue');
+ expect(await restoreAutoReviewSteerIntent('continue', options, async () => { throw new Error('read failed'); })).toBe('continue');
+ });
+
+ it('rejects cancelled input while history is loading', async () => {
+ const controller = new AbortController();
+ let release!: (history: AutoReviewHistoryMessage[]) => void;
+ const pending = restoreAutoReviewSteerIntent('continue', {
+ signal: controller.signal, [AUTO_REVIEW_SOURCE_CONTENT]: 'continue',
+ }, () => new Promise((resolve) => { release = resolve; }));
+ controller.abort();
+ release([user('Deploy now.')]);
+ await expect(pending).rejects.toThrow();
+ });
+});
+
describe('restored Auto authorization', () => {
it.each(['ask_user', 'plan_review'])('preserves a trusted %s restriction after an ordinary follow-up', (role) => {
const intent = restoreAutoReviewUserIntent([
diff --git a/apps/desktop/src/main/maker-ipc/autoReviewUserIntent.ts b/apps/desktop/src/main/maker-ipc/autoReviewUserIntent.ts
index 90cdd99198..21fd7ca59b 100644
--- a/apps/desktop/src/main/maker-ipc/autoReviewUserIntent.ts
+++ b/apps/desktop/src/main/maker-ipc/autoReviewUserIntent.ts
@@ -1,5 +1,5 @@
-import { appendAutoReviewUserIntent, extractAutoReviewUserIntent } from '@cindy/maker-core';
-import type { UserMessage } from '@cindy/maker-core';
+import { AUTO_REVIEW_SOURCE_CONTENT, AUTO_REVIEW_USER_INTENT, MAIN_OWNED_SEND_CONTEXT, appendAutoReviewUserIntent, extractAutoReviewUserIntent } from '@cindy/maker-core';
+import type { SendOptions, UserMessage } from '@cindy/maker-core';
import { joinChatQuoteTextSegments, parseChatQuoteSegments } from '@cindy/maker-shared/chat-quotes';
import { projectPersistedAgentFacingUserText } from '@cindy/maker-shared/agent-input-projection';
@@ -22,6 +22,28 @@ function interactionAnswer(message: AutoReviewHistoryMessage): { text: string; a
? { text: answer.text, acceptedAt: answer.acceptedAt } : null;
}
+/** Both queued and direct steers may reach a freshly reattached harness. */
+export async function restoreAutoReviewSteerIntent(
+ content: string | ReadonlyArray<{ type: string; [key: string]: unknown }>,
+ options: SendOptions,
+ readHistory: () => Promise<AutoReviewHistoryMessage[]>,
+): Promise<string | undefined> {
+ options.signal?.throwIfAborted();
+ // Resource changes already carry an explicit replacement, including an empty one.
+ if (options[AUTO_REVIEW_USER_INTENT] !== undefined) return options[AUTO_REVIEW_USER_INTENT];
+ const context = options[MAIN_OWNED_SEND_CONTEXT];
+ if (context && context.origin.kind !== 'desktop') return undefined;
+ const text = options[AUTO_REVIEW_SOURCE_CONTENT] ?? context?.rawChannelText;
+ if (typeof text !== 'string') return undefined;
+ const history = await readHistory().catch(() => []);
+ options.signal?.throwIfAborted();
+ return restoreAutoReviewUserIntent(history, {
+ clientId: '', authoredText: text,
+ content: typeof content === 'string' || content.every((block) => block.type === 'text')
+ ? { text } : [],
+ });
+}
+
/** Read only the user's authored text, never the decorated agent-facing projection. */
export function readAutoReviewUserText(content: unknown): string | null {
if (typeof content === 'string') {
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 226fb37b0c..b919250df4 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -31,6 +31,7 @@ import {
piSubagentRunRoot,
} from '@cindy/maker-core/pi-subagent-runs';
import { AUTO_REVIEW_SOURCE_CONTENT, AUTO_REVIEW_USER_INTENT, MAIN_OWNED_SEND_CONTEXT } from '@cindy/maker-core';
+import { restoreAutoReviewSteerIntent } from './autoReviewUserIntent.js';
import type {
AgentEvent,
AgentKind,
@@ -11517,11 +11518,12 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
}
};
+ const readAutoReviewHistory = async (sessionId: string) => {
+ await drainPersistQueue();
+ return listMessagesForAgentHandoff(sessionId, 100, undefined, 'authorization');
+ };
const { sendToAgentAccepted: sendToAgentAcceptedUnlocked } = createMakerSendTransaction({
- readAutoReviewHistory: async (sessionId) => {
- await drainPersistQueue();
- return listMessagesForAgentHandoff(sessionId, 100, undefined, 'authorization');
- },
+ readAutoReviewHistory,
getSession: (sessionId) => maker.getSession(sessionId),
closeSession: (sessionId) => maker.closeSession(sessionId),
getSessionMeta: (sessionId) => maker.getSessionMeta(sessionId),
@@ -12076,6 +12078,17 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
}
const meta = await maker.getSessionMeta(sessionId).catch(() => null);
sess = readCurrentSteerSession();
+ const authorizationSession = sess;
+ const authorizationTurn = sess.getTurnGeneration();
+ const restoredSteerIntent = await restoreAutoReviewSteerIntent(
+ typeof normalized === 'string' ? normalized : normalized.content,
+ so,
+ () => readAutoReviewHistory(sessionId),
+ );
+ sess = readCurrentSteerSession();
+ if (sess !== authorizationSession || sess.getTurnGeneration() !== authorizationTurn) {
+ throw new Error(`[STALE_TURN] Session ${sessionId} changed turns while restoring authorization`);
+ }
// 手机说明同样只进 wire payload(steer 路径不落库用户消息,天然不污染原话)。
// 两个来源都要认:IPC 直连 steer 时 async context 在;coordinator 投递时靠透传。
const steerNote =
@@ -12102,7 +12115,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
signal: so.signal,
[MAIN_OWNED_SEND_CONTEXT]: so[MAIN_OWNED_SEND_CONTEXT],
[AUTO_REVIEW_SOURCE_CONTENT]: so[AUTO_REVIEW_SOURCE_CONTENT],
- [AUTO_REVIEW_USER_INTENT]: so[AUTO_REVIEW_USER_INTENT],
+ [AUTO_REVIEW_USER_INTENT]: restoredSteerIntent,
});
log.info('steer: delivered', { sessionId, agentKind: sess.agentKind });
} catch (err) {