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(local-agent): persist steer before dispatch and reject invalid resume replay

DavidShen494d1382cc02
diff --git a/apps/desktop/src/main/local-agent/__tests__/runtime.integration.test.ts b/apps/desktop/src/main/local-agent/__tests__/runtime.integration.test.ts
index d9c1cb5149..a302ad3514 100644
--- a/apps/desktop/src/main/local-agent/__tests__/runtime.integration.test.ts
+++ b/apps/desktop/src/main/local-agent/__tests__/runtime.integration.test.ts
@@ -104,6 +104,27 @@ async function inputFiles(directory: string) {
const message = { parts: [{ type: 'text', text: 'hello' }], effort: 'high' };
describe('local client runtime lifecycle', () => {
+ // PR #7684: send persistence coverage did not exercise same-turn input.
+ // A failed image/database write must reject steer before provider delivery.
+ it('persists visible steer before delivery and never delivers a failed write', async () => {
+ const f = await fixture(true);
+ await f.runtime.call('send', { parts: message.parts });
+ const delivered = vi.fn();
+ f.first.steer.mockImplementation(async (...args: any[]) => {
+ await args[1]?.beforeProviderStart?.();
+ delivered();
+ });
+ f.host.persistInput.mockRejectedValueOnce(new Error('disk unavailable'));
+ await expect(f.runtime.call('steer', { parts: [image] })).rejects.toThrow('disk unavailable');
+ expect(delivered).not.toHaveBeenCalled();
+ expect(await inputFiles(f.directory)).toEqual([]);
+ f.host.persistInput.mockImplementationOnce(async () => {
+ expect(delivered).not.toHaveBeenCalled();
+ });
+ await f.runtime.call('steer', { parts: message.parts });
+ expect(delivered).toHaveBeenCalledOnce();
+ });
+
// #7631 review P0: the existing visible-session tests only mock persistence.
// A Cindy input must await close/handoff and fence all subsequent app input.
// Minimal input: one live visible runtime followed by a host takeover (integration).
@@ -229,7 +250,7 @@ describe('local client runtime lifecycle', () => {
expect(f.first.send).toHaveBeenCalledWith({
type: 'user',
content: [{ type: 'text', text: ' /workspace/example' }],
- });
+ }, undefined);
});
it('keeps the app execution busy across a provider continuation until Session observes its final boundary', async () => {
const f = await fixture();
diff --git a/apps/desktop/src/main/local-agent/runtime.ts b/apps/desktop/src/main/local-agent/runtime.ts
index 0ff71d6851..721d87ea7c 100644
--- a/apps/desktop/src/main/local-agent/runtime.ts
+++ b/apps/desktop/src/main/local-agent/runtime.ts
@@ -496,15 +496,16 @@ export async function openLocalAgentRuntime(
try {
assertCurrent(current);
if (!busy) throw new LocalAgentError('NOT_RUNNING', '当前没有正在运行的回答。');
- await handle.steer(wireMessage(content));
- if (visibleHost)
- await visibleHost.persistInput(
- sessionId,
- content,
- randomUUID(),
- () => assertCurrent(current),
- source!,
- );
+ await handle.steer(wireMessage(content), visibleHost ? {
+ beforeProviderStart: () =>
+ visibleHost.persistInput(
+ sessionId,
+ content,
+ randomUUID(),
+ () => assertCurrent(current),
+ source!,
+ ),
+ } : undefined);
turnInputs.push(content);
} catch (error) {
await discard(content);
@@ -541,21 +542,19 @@ export async function openLocalAgentRuntime(
releaseInputs();
turnInputs.push(sending);
void (async () => {
- const result = visibleHost
- ? await handle.send(wireMessage(sending), {
- onAccepted: (signal) =>
- visibleHost.persistInput(
- sessionId,
- sending,
- randomUUID(),
- () => {
- assertCurrent(current);
- signal?.throwIfAborted();
- },
- source!,
- ),
- })
- : await handle.send(sending);
+ const result = await handle.send(wireMessage(sending), visibleHost ? {
+ onAccepted: (signal) =>
+ visibleHost.persistInput(
+ sessionId,
+ sending,
+ randomUUID(),
+ () => {
+ assertCurrent(current);
+ signal?.throwIfAborted();
+ },
+ source!,
+ ),
+ } : undefined);
if (!result.accepted) await discard(sending);
assertSendDispatched(result, 'local-agent/send');
})().catch(() => {
diff --git a/packages/maker-core/src/maker.test.ts b/packages/maker-core/src/maker.test.ts
index 5f9c4ccc57..47b2c40137 100644
--- a/packages/maker-core/src/maker.test.ts
+++ b/packages/maker-core/src/maker.test.ts
@@ -4276,6 +4276,39 @@ describe('Session permission mode leases', () => {
});
describe('Maker invalid-resume persistence bridge', () => {
+ // PR #7684: a visible local app uses createSession, unlike the private bridge.
+ // Exercise late invalid-history detection during Session.send: it must not
+ // clear the app's saved native id or authorize an automatic replay (unit).
+ it.each(['claude-code', 'pi'] as const)('does not recover or replay a visible %s local app session', async (agentKind) => {
+ const storage = createStorage();
+ const replay = vi.fn();
+ const startSession = vi.fn(async (opts: CreateSessionOptions) => {
+ const handle = createHandle({ id: 'sdk-old', agentKind });
+ handle.send = vi.fn(async () => {
+ if (await opts.onInvalidResumeSession?.('sdk-old')) replay();
+ else throw new Error('native history unavailable');
+ });
+ return handle;
+ });
+ const maker = new Maker({
+ agents: { [agentKind]: createAgent(startSession, agentKind) },
+ storage,
+ logger: createLogger(),
+ });
+ const session = await maker.createSession({
+ id: 'visible-local-app', agentKind, workingDir: '/repo', model: 'm',
+ resumeSessionId: 'sdk-old',
+ [LOCAL_AGENT_CLIENT]: { url: 'http://127.0.0.1:1', token: 'invalid-fixture', hostToolsOnly: true },
+ });
+ try {
+ await expect(session.send('app input')).rejects.toThrow('native history unavailable');
+ expect(replay).not.toHaveBeenCalled();
+ expect((await storage.get(session.id))?.sdkSessionId).toBe('sdk-old');
+ } finally {
+ await session.close();
+ }
+ });
+
it('injects a compare-and-clear callback for resumed Claude sessions', async () => {
const storage = createStorage();
await storage.create({
diff --git a/packages/maker-core/src/maker.ts b/packages/maker-core/src/maker.ts
index b0df44224c..3959986a66 100644
--- a/packages/maker-core/src/maker.ts
+++ b/packages/maker-core/src/maker.ts
@@ -786,6 +786,15 @@ export class Maker {
let codexThreadClaim: CodexThreadClaimLease | null = null;
let handle: AgentSessionHandle;
let agentStartAttempted = false;
+ // Local apps own their native history and require an explicit failed resume,
+ // including failures discovered on first send. Visible tasks still use this
+ // createSession path; never authorize a fresh conversation/replay for them.
+ const onInvalidResumeSession: StartSessionOptions['onInvalidResumeSession'] =
+ startOpts[LOCAL_AGENT_CLIENT]
+ ? async () => false
+ : opts.agentKind === 'claude-code' || opts.agentKind === 'pi'
+ ? (expectedSdkSessionId) => this.invalidateAndClearSdkSessionId(id, expectedSdkSessionId)
+ : undefined;
try {
if (opts.agentKind === 'codex' && isClaimableCodexThreadId(startOpts.resumeSessionId)) {
codexThreadClaim = this.claimCodexThread({
@@ -815,11 +824,7 @@ export class Maker {
...startOpts,
sessionId: id,
sessionInstanceId,
- onInvalidResumeSession:
- opts.agentKind === 'claude-code' || opts.agentKind === 'pi'
- ? (expectedSdkSessionId) =>
- this.invalidateAndClearSdkSessionId(id, expectedSdkSessionId)
- : undefined,
+ onInvalidResumeSession,
},
});
} else handle = await agent.startSession({
@@ -832,11 +837,7 @@ export class Maker {
// 就把 SDK 回填、已落库的 sdk_session_id 变成幽灵 id(见 claude-code/index.ts
// 的 fresh-session self-reference 恢复),需要同一把 CAS 才能把它清掉,否则下一次
// send 会 resume 同一个不存在的会话反复失败。
- onInvalidResumeSession:
- opts.agentKind === 'claude-code' || opts.agentKind === 'pi'
- ? (expectedSdkSessionId) =>
- this.invalidateAndClearSdkSessionId(id, expectedSdkSessionId)
- : undefined,
+ onInvalidResumeSession,
});
handle = withFastModePolicy(handle, startOpts.fastModePolicy);
} catch (error) {