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(bot): resume a chunked watermark at the previous reply; review interaction answers as user text

zqchris539f5c13fad7
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
index 5ead175029..9dec5465c6 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botLearningReview.test.ts
@@ -327,6 +327,18 @@ it('walks an over-long user message in chunks and only counts it covered once th
const written = h.writeWorking.mock.calls.at(-1)![2] as { coveredThroughRowid: number; coveredThroughMessageId?: string; coveredThroughOffset?: number };
expect(written).toMatchObject({ coveredThroughRowid: 5, coveredThroughMessageId: 'reply2' });
expect(written.coveredThroughOffset).toBeUndefined();
+ // 分块水位恰好停在上一条完成回复上(后面的轮次被 rewind 掉了):带偏移时取等号,仍把那条回复取回来补完剩余部分。
+ h.meta = { turnCompleted: true };
+ h.working = { ...ledger, coveredThroughRowid: 3, coveredThroughMessageId: 'prev', coveredThroughOffset: 6000 };
+ h.floorRowid = 3;
+ h.gapRows = [{ id: 'prev', role: 'assistant', content: JSON.stringify('z'.repeat(9000)), order: 3 }];
+ h.model.mockResolvedValueOnce({ ok: true, text: JSON.stringify({ memories: [], skills: [], working: { commitments: [] } }) });
+ await reviewTeammateLearning('session', 'reply-after-rewind');
+ expect(query.mock.calls.at(-1)![1]).toEqual(['session', 2, 3, 'session', 41]);
+ input = JSON.parse(h.model.mock.calls.at(-1)![1] as string);
+ expect(input.messages.find((m: { id: string }) => m.id === 'prev').text).toHaveLength(3000);
+ expect(h.writeWorking.mock.calls.at(-1)![2]).toMatchObject({ coveredThroughRowid: 5, coveredThroughMessageId: 'reply-after-rewind' });
+ expect((h.writeWorking.mock.calls.at(-1)![2] as { coveredThroughOffset?: number }).coveredThroughOffset).toBeUndefined();
// 本轮取材里的长用户消息同样算截断:水位停在上一条完成回复,交给下一次分块回补。
h.meta = { turnCompleted: true };
h.working = { ...ledger, coveredThroughRowid: 3, coveredThroughMessageId: 'prev' };
@@ -364,6 +376,49 @@ it('resolves the ledger floor by client_id so a VACUUM-renumbered rowid cannot f
expect(query.mock.calls.at(-1)![1]).toEqual(['session', 0, 3, 'session', 41]);
});
+it('feeds interaction answers (AskUserQuestion / plan review) to the review as user text and skips empty ones without counting truncation', async () => {
+ const query = (getDbClient() as unknown as { query: ReturnType<typeof vi.fn> }).query;
+ h.working = { version: 1, revision: 4, coveredThroughRowid: 3, coveredThroughMessageId: 'prev', updatedAt: 0, threads: [], commitments: [], waiting: [], corrections: [], recentlyDone: [] };
+ h.floorRowid = 3;
+ h.turnRows = [
+ { id: 'user', role: 'user', content: JSON.stringify('Which format?'), order: 4 },
+ { id: 'ask', role: 'ask_user', content: JSON.stringify({ requestId: 'r', questions: [], status: 'answered', answers: { '用哪种格式?': '先结论后细节' } }), order: 5 },
+ { id: 'plan-ok', role: 'plan_review', content: JSON.stringify({ requestId: 'p', plan: '1. 改模板', planFilePath: '', status: 'approved', feedback: null }), order: 6 },
+ { id: 'plan-no', role: 'plan_review', content: JSON.stringify({ requestId: 'q', plan: 'x', planFilePath: '', status: 'revised', feedback: '先别动样式' }), order: 7 },
+ { id: 'ask-cancel', role: 'ask_user', content: JSON.stringify({ requestId: 's', questions: [], status: 'cancelled', answers: {} }), order: 8 },
+ ];
+ h.model.mockResolvedValueOnce({
+ ok: true,
+ text: JSON.stringify({
+ memories: [],
+ skills: [],
+ // 用户在 AskUserQuestion 里的选择可以作为账本证据(按用户消息对待)。
+ working: { corrections: [{ text: '周报先结论后细节', evidence: { messageId: 'ask', quote: '用哪种格式? → 先结论后细节' } }] },
+ }),
+ });
+ await reviewTeammateLearning('session', 'reply');
+ expect(String(query.mock.calls.at(-1)![0])).toContain("'ask_user'");
+ const input = JSON.parse(h.model.mock.calls.at(-1)![1] as string);
+ const byId = Object.fromEntries(input.messages.map((m: { id: string; role: string; text: string }) => [m.id, m]));
+ expect(byId.ask).toMatchObject({ role: 'user', text: 'Clarifications:\n- 用哪种格式? → 先结论后细节' });
+ expect(byId['plan-ok']).toMatchObject({ role: 'user', text: 'Approved plan:\n1. 改模板' });
+ expect(byId['plan-no']).toMatchObject({ role: 'user', text: '先别动样式' });
+ expect(byId['ask-cancel']).toBeUndefined();
+ // 空的取消行被丢掉不算截断:水位照常推进到当前回复;纠正条目带上 ask 行作为来源。
+ expect(h.writeWorking).toHaveBeenLastCalledWith(
+ '/test-owner',
+ 'bot',
+ expect.objectContaining({
+ coveredThroughRowid: 5,
+ coveredThroughMessageId: 'reply',
+ corrections: [expect.objectContaining({ text: '周报先结论后细节', messageId: 'ask' })],
+ }),
+ 4,
+ );
+ h.turnRows = null;
+ h.floorRowid = null;
+});
+
it('does not advance the watermark past the previous completed reply when the current turn itself was truncated', async () => {
// 本轮取材取满 80 条 = 可能被 LIMIT 截断:水位停在上一条完成回复(3),留给下一次的缺口回补。
h.working = null;
diff --git a/apps/desktop/src/main/maker-ipc/botLearningReview.ts b/apps/desktop/src/main/maker-ipc/botLearningReview.ts
index 45176f8fc1..cf98d8e2eb 100644
--- a/apps/desktop/src/main/maker-ipc/botLearningReview.ts
+++ b/apps/desktop/src/main/maker-ipc/botLearningReview.ts
@@ -32,6 +32,30 @@ const TURN_REVIEW_ROWS = 80;
/** 单条正文每块多少字;用户 / 伙伴的长消息按块分多次复盘,整条补完才算覆盖。 */
const MESSAGE_CHUNK_CHARS = 6000;
const TEXT_ROLES = new Set(['user', 'assistant']);
+/** 交互答复行(AskUserQuestion 的答案、计划审阅的批准 / 反馈):正文是用户的决定,按用户消息复盘。 */
+const INTERACTION_ROLES = new Set(['ask_user', 'plan_review']);
+
+/**
+ * 把交互答复行归一成用户文本(与 messagePersistBroadcaster 落库时的文本投影同口径):
+ * 已回答的问题 → "问题 → 答案" 逐行;批准的计划 → 计划正文;打回的计划 → 用户反馈;
+ * 取消 / 未回答 → 空串(无内容,不进取材)。
+ */
+function interactionText(content: unknown): string {
+ if (!content || typeof content !== 'object') return '';
+ const c = content as Record<string, unknown>;
+ if (c.answers && typeof c.answers === 'object') {
+ if (c.status === 'cancelled') return '';
+ const lines = Object.entries(c.answers as Record<string, unknown>)
+ .filter(([, answer]) => typeof answer === 'string' && answer.trim())
+ .map(([question, answer]) => (question.trim() ? `- ${question.trim()} → ${String(answer).trim()}` : `- ${String(answer).trim()}`));
+ return lines.length ? `Clarifications:\n${lines.join('\n')}` : '';
+ }
+ if (typeof c.status === 'string' && ('plan' in c || 'feedback' in c)) {
+ if (c.status === 'approved') return typeof c.plan === 'string' && c.plan.trim() ? `Approved plan:\n${c.plan.trim()}` : '';
+ if (c.status === 'revised') return typeof c.feedback === 'string' ? c.feedback.trim() : '';
+ }
+ return '';
+}
/** 模型省略 working(本轮没改动)时用的空提案:不加不删,只让水位跟上。 */
const EMPTY_WORKING_PROPOSAL: WorkingMemoryProposal = {
threads: [],
@@ -44,7 +68,7 @@ const EMPTY_WORKING_PROPOSAL: WorkingMemoryProposal = {
/** 缺口回补每批最多取多少条消息 / 多少字符;单条正文已截到 6000 字,所以每批至少前进一条。 */
const GAP_BACKFILL_ROWS = 40;
const GAP_BACKFILL_CHARS = 16000;
-const REVIEW_ROLES = `role IN ('user','assistant','tool_use','tool_result')`;
+const REVIEW_ROLES = `role IN ('user','assistant','tool_use','tool_result','ask_user','plan_review')`;
/** Once per durable successful reply; serial per teammate, outside the chat/agent loop. */
export async function reviewTeammateLearning(sessionId: string, replyId: string): Promise<void> {
const owner = activeOwnerScopeKey();
@@ -101,16 +125,22 @@ export async function reviewTeammateLearning(sessionId: string, replyId: string)
// 的证据来源,纠正 / 承诺可能就在长消息的尾部,没看完不能算覆盖;工具结果不是新条目的来源,
// 截尾只影响 resolved 证据(条目多留一阵),按已覆盖处理,不为它逐块回补。
const toSnapshot = (m: Record_, offset = 0) => {
- const full: string = typeof JSON.parse(m.content) === 'string' ? JSON.parse(m.content) : m.content;
+ const parsed: unknown = JSON.parse(m.content);
+ // 交互答复行的正文是用户的决定,归一成文本后按用户消息对待(证据规则同用户消息)。
+ const interaction = INTERACTION_ROLES.has(m.role);
+ const full: string = interaction ? interactionText(parsed) : typeof parsed === 'string' ? parsed : m.content;
+ const role = interaction ? 'user' : m.role;
return {
id: m.id,
- role: m.role,
+ role,
order: m.order,
text: full.slice(offset, offset + MESSAGE_CHUNK_CHARS),
offset,
- partial: TEXT_ROLES.has(m.role) && full.length > offset + MESSAGE_CHUNK_CHARS,
+ partial: TEXT_ROLES.has(role) && full.length > offset + MESSAGE_CHUNK_CHARS,
};
};
+ // 没有内容的交互行(取消 / 未回答)不进取材,也不算截断。
+ const hasText = (m: ReturnType<typeof toSnapshot>) => m.text.length > 0;
// /clear 之前的消息始终不取。
const clearedAtFilter = `AND created_at > COALESCE((SELECT cleared_at FROM sessions WHERE id=?), 0)`;
const prev = await db.queryOne<{ id: string; rowid: number }>(
@@ -129,13 +159,14 @@ export async function reviewTeammateLearning(sessionId: string, replyId: string)
`,
[sessionId, row.rowid, prevCompleted, sessionId],
);
- const turnMessages = learningSnapshotBudget(records.map((m) => toSnapshot(m)), 40000);
+ const turnCandidates = records.map((m) => toSnapshot(m)).filter(hasText);
+ const turnMessages = learningSnapshotBudget(turnCandidates, 40000);
// 本轮本身被 LIMIT / 预算截断(工具密集的大轮次)、或某条用户 / 伙伴消息被截尾时,漏掉的
// 中间承诺 / 工具结果 / 长消息尾部同样不能算"已覆盖":水位停在上一条完成回复,下一次复盘
// 由缺口回补路径按序(含分块)补齐。
const turnTruncated =
records.length >= TURN_REVIEW_ROWS ||
- turnMessages.length < records.length ||
+ turnMessages.length < turnCandidates.length ||
turnMessages.some((m) => m.partial);
// 缺口回补:账本上次覆盖到的行落后于上一条完成回复(某轮复盘 claim 后失败、CAS 冲突、
// 模型漏回 working),那段内容不会再被单独复盘;按 rowid 升序分批补进本次取材,覆盖水位
@@ -162,7 +193,9 @@ export async function reviewTeammateLearning(sessionId: string, replyId: string)
: { rowid: 0, id: undefined };
let covered: Watermark = turnTruncated ? { rowid: prevCompleted, id: prev?.id } : { rowid: row.rowid, id: replyId };
let gapMessages: ReturnType<typeof toSnapshot>[] = [];
- if (floor.rowid < prevCompleted) {
+ // 水位停在半条消息里时,哪怕那条消息就是上一条完成回复(后面的轮次被 rewind 掉了),
+ // 剩下的部分也得接着补,所以带偏移时取等号。
+ if (floor.rowid < prevCompleted || (floor.offset !== undefined && floor.rowid === prevCompleted)) {
// 水位停在半条消息里时把那条消息本身也取回来(rowid > x-1 即 rowid >= x)。
const gapAfter = floor.offset !== undefined ? floor.rowid - 1 : floor.rowid;
const gapRows = await db.query<Record_>(
@@ -173,19 +206,18 @@ export async function reviewTeammateLearning(sessionId: string, replyId: string)
`,
[sessionId, gapAfter, prevCompleted, sessionId, GAP_BACKFILL_ROWS + 1],
);
- gapMessages = learningSnapshotPrefix(
- gapRows
- .slice(0, GAP_BACKFILL_ROWS)
- .map((m) => toSnapshot(m, floor.offset !== undefined && m.order === floor.rowid ? floor.offset : 0)),
- GAP_BACKFILL_CHARS,
- );
+ const gapCandidates = gapRows
+ .slice(0, GAP_BACKFILL_ROWS)
+ .map((m) => toSnapshot(m, floor.offset !== undefined && m.order === floor.rowid ? floor.offset : 0))
+ .filter(hasText);
+ gapMessages = learningSnapshotPrefix(gapCandidates, GAP_BACKFILL_CHARS);
const partialAt = gapMessages.findIndex((m) => m.partial);
if (partialAt >= 0) {
// 连续前缀停在这条长消息的当前块:水位记到块尾,后面的消息留给下一次。
gapMessages = gapMessages.slice(0, partialAt + 1);
const cut = gapMessages[partialAt]!;
covered = { rowid: cut.order, id: cut.id, offset: cut.offset + MESSAGE_CHUNK_CHARS };
- } else if (gapRows.length > GAP_BACKFILL_ROWS || gapMessages.length < gapRows.length) {
+ } else if (gapRows.length > GAP_BACKFILL_ROWS || gapMessages.length < gapCandidates.length) {
const last = gapMessages[gapMessages.length - 1];
covered = last ? { rowid: last.order, id: last.id } : floor;
}
diff --git a/docs/product-rules/cindy-bots-runtime.md b/docs/product-rules/cindy-bots-runtime.md
index 62713b4f27..4f1f6aecba 100644
--- a/docs/product-rules/cindy-bots-runtime.md
+++ b/docs/product-rules/cindy-bots-runtime.md
@@ -83,7 +83,8 @@ tool 副作用时自动重放。原生后台工作尚未结束时不自动关闭
学习复盘顺手更新,不新增模型调用;复盘取材从账本上次覆盖到的位置接上,某轮复盘失败漏掉的内容
由后续复盘按序分批补进账本(每批有界),覆盖水位只推进到连续补完的位置;本轮取材本身被截断时
同样不推进水位,补完之前账本不算新鲜;超过单条上限的用户/伙伴消息按块分多次复盘,水位可停在
-消息中间,整条看完才算覆盖(工具结果不是新条目的证据来源,仍只取开头);水位以消息的 client_id
+消息中间,整条看完才算覆盖(工具结果不是新条目的证据来源,仍只取开头);用户在提问卡、计划审阅
+里给出的答复与反馈按用户消息进入复盘取材,可作为账本证据;水位以消息的 client_id
为持久判据(rowid 在数据库瘦身后可能重排),老对话第一次建账本从对话开头(清空边界之后)分批
补起,不跳过任何一轮;
新条目必须有本轮用户或伙伴原话证据,