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(maker-cc-manager): forceful kill 终止窗口内 sendMessage 显式拒绝

yan856640e7fac6
diff --git a/packages/maker-cc-manager/__tests__/kill-pending-send.test.ts b/packages/maker-cc-manager/__tests__/kill-pending-send.test.ts
new file mode 100644
index 0000000000..055f5e6c83
--- /dev/null
+++ b/packages/maker-cc-manager/__tests__/kill-pending-send.test.ts
@@ -0,0 +1,63 @@
+/**
+ * Greptile P1 回归:forceful kill 的终止窗口内 (inputQueue 已 end、consume
+ * loop 未退出、alive 仍 true), sendMessage 必须显式拒绝
+ * (SESSION_KILL_PENDING) — 不得把消息 push 进 ended queue 静默丢弃。
+ */
+
+import { describe, expect, it } from 'vitest';
+
+import {
+ SessionRegistry,
+ type SdkQueryFactory,
+ type SdkQueryLike,
+} from '../src/session-registry.js';
+
+function buildBlockingFactory(): SdkQueryFactory {
+ return (opts): SdkQueryLike => {
+ async function* gen(): AsyncGenerator<unknown> {
+ yield { type: 'system', subtype: 'init', session_id: 'sdk-uuid', cwd: opts.cwd, model: opts.model };
+ for await (const _ of opts.inputStream) {
+ // drain
+ }
+ // inputQueue 已 end, 但 consume loop 还在等"SDK 的最终响应" — 正是
+ // Greptile 描述的「alive 仍 true 的终止窗口」(interrupt 发出到
+ // consume loop 真正退出之间)。
+ await new Promise((r) => setTimeout(r, 150));
+ yield { type: 'result', subtype: 'success' };
+ }
+ const g = gen();
+ return {
+ [Symbol.asyncIterator]: () => g,
+ async interrupt() {},
+ async setModel() {},
+ async setPermissionMode() {},
+ async applyFlagSettings() {},
+ };
+ };
+}
+
+describe('kill 终止窗口的 sendMessage 拒绝 (Greptile P1)', () => {
+ it('rejects sendMessage with SESSION_KILL_PENDING while a forceful kill is still settling', async () => {
+ const registry = new SessionRegistry({ sdkQueryFactory: buildBlockingFactory() });
+ const session = await registry.create({ sessionId: 's1', cwd: '/repo', model: 'm' });
+
+ // kill 发出后 consume loop 仍挂在线上 (inputStream 未消费新消息) —
+ // alive 为 true、inputQueue 已 end 的窗口期。
+ const killP = registry.kill('s1');
+ // kill 内的 interrupt/end 是同步微任务, 先让它推进到窗口态。
+ await new Promise((r) => setTimeout(r, 20));
+ expect(session.alive).toBe(true); // 窗口确认:registry 仍报 alive
+
+ expect(() => registry.sendMessage('s1', { text: 'hello' })).toThrowError(/SESSION_KILL_PENDING|being killed/);
+
+ await killP;
+ });
+
+ it('still throws SESSION_NOT_FOUND after the session fully exits (not the kill-pending code)', async () => {
+ const registry = new SessionRegistry({ sdkQueryFactory: buildBlockingFactory() });
+ await registry.create({ sessionId: 's2', cwd: '/repo', model: 'm' });
+
+ await registry.close('s2'); // close 路径: alive 立即 false
+ expect(() => registry.sendMessage('s2', { text: 'hello' })).toThrowError(/SESSION_NOT_FOUND|no longer alive/);
+ });
+});
diff --git a/packages/maker-cc-manager/src/session-registry.ts b/packages/maker-cc-manager/src/session-registry.ts
index 2cf4ea65cc..32b4c47701 100644
--- a/packages/maker-cc-manager/src/session-registry.ts
+++ b/packages/maker-cc-manager/src/session-registry.ts
@@ -152,6 +152,13 @@ interface SessionState {
sdkSessionId: string | null;
/** Whether the consume loop is still running. */
alive: boolean;
+ /**
+ * forceful kill 已开始 (interrupt 发出、inputQueue 已/将 end) 但 consume
+ * loop 尚未退出 — 此窗口内 alive 仍为 true, sendMessage 必须显式拒绝
+ * (Greptile P1: ended queue 的 push 静默丢, 其他连接 attach+send 会让
+ * 用户消息看似成功实则消失)。
+ */
+ killing?: boolean;
/** Currently attached client's notify function; null when no client is attached. */
attachedNotify: AttachedNotify | null;
/**
@@ -393,6 +400,15 @@ export class SessionRegistry {
if (!s.alive) {
throw makeRegistryError('SESSION_NOT_FOUND', `session ${sessionId} is no longer alive`);
}
+ // kill 窗口 (killing=true 且 consume loop 未退出) 必须显式拒绝:
+ // inputQueue 已 end, push 静默丢 — 看似发送成功实则消息消失
+ // (Greptile P1)。调用方应稍后重试或走 fresh start。
+ if (s.killing) {
+ throw makeRegistryError(
+ 'SESSION_KILL_PENDING',
+ `session ${sessionId} is being killed (input closed) — retry shortly or start a fresh query`,
+ );
+ }
s.inputQueue.push(message);
}
@@ -474,6 +490,9 @@ export class SessionRegistry {
this.sessions.delete(sessionId);
return;
}
+ // 先标 killing 再 interrupt:终止窗口从这一刻起对 sendMessage 可见
+ // (Greptile P1, 见 SessionState.killing)。
+ s.killing = true;
try {
await s.query.interrupt();
} catch (err) {