fix: isolate delivered agent chats by project

This commit is contained in:
2026-09-28 16:21:14 +08:00
parent a21a1f077c
commit 4b41c23a3a
15 changed files with 518 additions and 165 deletions

View File

@@ -36,13 +36,82 @@ function turn(index: number): TeacherRequest {
omittedMessages: 0, status: 'completed', response: '回答 ' + index };
}
describe('continuous conversation persistence', () => {
it('splits mixed global history by recorded project, preserving originals, seen positions and legacy identities', async () => {
const dir = await root(), global = path.join(dir, 'global');
const project = { id: randomUUID(), path: path.join(dir, 'project'), name: '天气' };
const other = randomUUID(), oldId = randomUUID();
const legacyTurn = turn(0);
const imported = { ...legacyTurn, id: randomUUID(), projectId: project.id,
origin: { projectId: project.id, topicId: oldId, requestId: legacyTurn.id } };
const turns = [imported, ...Array.from({ length: 110 }, (_, i) => ({
...turn(i + 1), projectId: i % 2 ? other : project.id, runtimeThreadId: 'mixed-cloud-thread',
})), turn(112)]; // This old turn has no proven project and must remain only in the original archive.
const metadata = { schemaVersion: 1, revision: 4, accountId: 'account', id: randomUUID(),
projectId: other, sourceConversationId: 'project', definition, version: 1,
createdAt: 'now', updatedAt: 'now', conversation: { agentId: 'agent-a', segmentTurns: 0, discussions: {},
seenThrough: turns[42].id } };
const manifest = { topic: metadata, turns: turns.map(({ id, createdAt }) => ({ id, createdAt, messages: [] })) };
await atomicWriteJson(path.join(global, 'conversation.json'), manifest);
for (const request of turns) await atomicWriteJson(path.join(global, 'turns', request.id + '.json'), request);
const orphan = { ...turn(113), projectId: other, status: 'running' };
await atomicWriteJson(path.join(global, 'turns', orphan.id + '.json'), orphan);
const original = await readFile(path.join(global, 'conversation.json'), 'utf8');
const originalTurn = await readFile(path.join(global, 'turns', orphan.id + '.json'), 'utf8');
const target = path.join(dir, 'a');
const a = new TeacherConversationStore(target, 'account', 'agent-a', project.id);
await a.importGlobal(global);
const b = new TeacherConversationStore(path.join(dir, 'b'), 'account', 'agent-a', other);
await b.importGlobal(global);
expect((await a.read()).id).not.toBe((await b.read()).id);
expect((await a.history()).messages).toHaveLength(56 * 2);
expect((await b.history()).messages).toHaveLength(55 * 2); // Interrupted turns stay visible but are not model context.
expect((await a.page()).requests.every(request => request.projectId === project.id)).toBe(true);
expect((await a.read()).conversation?.seenThrough).toBe(turns[41].id);
expect((await b.read()).conversation?.seenThrough).toBe(turns[42].id);
expect((await b.findRequest(orphan.id))?.status).toBe('interrupted');
expect((await a.findRequest(turns[1].id))?.origin?.topicId).toBe(metadata.id);
expect((await a.findRequest(turns[1].id))?.cloudRequestId).toBe(turns[1].id);
const legacy = new TeacherTopicStore(path.join(project.path, '.makelore', 'teacher-conversations', 'account', 'project'));
await legacy.save({ ...metadata, id: oldId, projectId: project.id, conversation: undefined, requests: [legacyTurn] });
await a.importProject(project);
const resumed = new TeacherConversationStore(target, 'account', 'agent-a', project.id);
await resumed.importGlobal(global); await resumed.importProject(project);
expect((await resumed.history()).messages).toHaveLength(56 * 2);
expect(await readFile(path.join(global, 'conversation.json'), 'utf8')).toBe(original);
expect(await readFile(path.join(global, 'turns', orphan.id + '.json'), 'utf8')).toBe(originalTurn);
const foreign = new TeacherConversationStore(path.join(dir, 'foreign'), 'other-account', 'agent-a', project.id);
await foreign.importGlobal(global);
expect(await foreign.exists()).toBe(false);
});
it('resumes a partial global split after an index write failure without duplicate messages', async () => {
const dir = await root(), old = path.join(dir, 'global'), dest = path.join(dir, 'project');
const request = { ...turn(1), projectId: 'project-a' };
await atomicWriteJson(path.join(old, 'conversation.json'), { topic: {
accountId: 'account', id: randomUUID(), definition, version: 1,
conversation: { agentId: 'agent-a', discussions: {} },
}, turns: [{ id: request.id, createdAt: request.createdAt, messages: [] }] });
await atomicWriteJson(path.join(old, 'turns', request.id + '.json'), request);
const store = new TeacherConversationStore(dest, 'account', 'agent-a', 'project-a');
await store.ensure(definition, 1);
const write = atomic.atomicWriteJson;
let fail = true;
vi.spyOn(atomic, 'atomicWriteJson').mockImplementation(async (file, value) => {
if (fail && file === path.join(dest, 'conversation.json')) { fail = false; throw new Error('disk unavailable'); }
return write(file, value);
});
await expect(store.importGlobal(old)).rejects.toThrow('disk unavailable');
await store.importGlobal(old);
expect((await store.read()).requests.map(turn => turn.id)).toEqual([request.id]);
await new TeacherConversationStore(dest, 'account', 'agent-a', 'project-a').importGlobal(old);
expect((await store.history()).messages).toHaveLength(2);
});
it('pages thousands of turns, restores the latest page and preserves repeated questions', async () => {
const dir = await root();
const store = new TeacherConversationStore(dir, 'account', 'agent-a');
const store = new TeacherConversationStore(dir, 'account', 'agent-a', 'project-a');
const topic = await store.ensure(definition, 1);
topic.requests = Array.from({ length: 1001 }, (_, i) => turn(i));
await store.save(topic);
const resumed = new TeacherConversationStore(dir, 'account', 'agent-a');
const resumed = new TeacherConversationStore(dir, 'account', 'agent-a', 'project-a');
expect((await resumed.read()).requests).toHaveLength(50);
const latest = await resumed.page();
expect(latest.requests.at(-1)?.response).toBe('回答 1000');
@@ -59,15 +128,15 @@ describe('continuous conversation persistence', () => {
expect((await tools.executeResult('read_conversation', JSON.stringify({message_id: `teacher:${topic.requests.at(-1)!.id}:assistant`}), signal)).status).toBe('error');
await resumed.markSeen(latest.requests.at(-1)!.id);
await resumed.markSeen(older.requests[0].id);
expect((await new TeacherConversationStore(dir, 'account', 'agent-a').read()).conversation?.seenThrough).toBe(latest.requests.at(-1)!.id);
expect((await new TeacherConversationStore(dir, 'account', 'agent-a', 'project-a').read()).conversation?.seenThrough).toBe(latest.requests.at(-1)!.id);
});
it('recovers a turn written before its index and marks only unfinished work interrupted', async () => {
const dir = await root();
const store = new TeacherConversationStore(dir, 'account', 'agent-a');
const store = new TeacherConversationStore(dir, 'account', 'agent-a', 'project-a');
await store.ensure(definition, 1);
const pending = { ...turn(1), status: 'running' as const };
await atomicWriteJson(path.join(dir, 'turns', pending.id + '.json'), pending);
const resumed = new TeacherConversationStore(dir, 'account', 'agent-a');
const resumed = new TeacherConversationStore(dir, 'account', 'agent-a', 'project-a');
expect((await resumed.read()).requests[0]).toMatchObject({ id: pending.id, status: 'interrupted' });
expect((await resumed.page()).requests).toHaveLength(1);
});
@@ -80,14 +149,14 @@ describe('continuous conversation persistence', () => {
await oldStore.save(old);
const file = path.join(oldStore.directory, old.id + '.json'), before = await readFile(file, 'utf8');
const target = path.join(dir, 'new');
const store = new TeacherConversationStore(target, 'account', 'agent-a');
const store = new TeacherConversationStore(target, 'account', 'agent-a', project.id);
await store.importProject(project);
const resumed = new TeacherConversationStore(target, 'account', 'agent-a');
const resumed = new TeacherConversationStore(target, 'account', 'agent-a', project.id);
await resumed.importProject(project);
expect((await resumed.read()).requests).toHaveLength(2);
expect((await resumed.read()).requests[0].origin?.topicId).toBe(old.id);
expect(await readFile(file, 'utf8')).toBe(before);
const other = new TeacherConversationStore(path.join(dir, 'other'), 'another-account', 'agent-a');
const other = new TeacherConversationStore(path.join(dir, 'other'), 'another-account', 'agent-a', project.id);
await other.importProject(project);
expect(await other.exists()).toBe(false);
});
@@ -97,7 +166,7 @@ describe('continuous conversation persistence', () => {
const old: TeacherTopic = { schemaVersion: 1, revision: 1, accountId: 'account', id: randomUUID(),
projectId: project.id, sourceConversationId: 'project', definition, version: 1, createdAt: 'now', updatedAt: 'now', requests: [turn(1)] };
await oldStore.save(old);
const store = new TeacherConversationStore(path.join(dir, 'chat'), 'account', 'agent-a');
const store = new TeacherConversationStore(path.join(dir, 'chat'), 'account', 'agent-a', project.id);
await store.ensure(definition, 1);
const write = atomic.atomicWriteJson;
let fail = true;
@@ -123,7 +192,9 @@ it('serves paged account chats and compact SSE, preserves legacy source records
{codingProducts: {teacher: f.service}} as HostApiContext); });
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
const address = server.address(); if (!address || typeof address === 'string') throw new Error('no address');
const origin = 'http://127.0.0.1:' + address.port, base = origin + '/api/coding/agent-conversations/agent-a';
const origin = 'http://127.0.0.1:' + address.port;
const base = origin + '/api/coding/projects/' + f.a.project.id + '/agent-conversations/agent-a';
const otherBase = origin + '/api/coding/projects/' + f.b.project.id + '/agent-conversations/agent-a';
const post = (url: string, input: unknown) => fetch(url, {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(input)});
try {
const page = await (await fetch(base)).json();
@@ -135,15 +206,20 @@ it('serves paged account chats and compact SSE, preserves legacy source records
expect((await post(archive, {})).status).toBe(405);
expect(f.run).not.toHaveBeenCalled();
const input = {projectId: f.b.project.id, sourceConversationId: f.bSource.id, requestId: randomUUID(), text: '继续'};
expect((await post(base + '/messages', input)).status).toBe(202);
expect((await post(base + '/messages', input)).status).toBe(202);
await f.settled(); expect(f.run).toHaveBeenCalledTimes(1);
const reader = (await fetch(base + '/events')).body!.getReader();
expect((await (await fetch(otherBase)).json()).topic).toBeNull();
expect((await post(base + '/messages', input)).status).toBe(422);
expect((await post(otherBase + '/messages', input)).status).toBe(202);
expect((await post(otherBase + '/messages', input)).status).toBe(202);
await f.settled(f.b); expect(f.run).toHaveBeenCalledTimes(1);
const reader = (await fetch(otherBase + '/events')).body!.getReader();
const chunk = new TextDecoder().decode((await reader.read()).value);
const snapshot = JSON.parse(chunk.split('data: ')[1].split('\n')[0]);
expect(snapshot.requests).toHaveLength(1); expect(snapshot.requests[0].id).toBe(input.requestId);
await reader.cancel();
expect((await (await post(base + '/seen', {requestId: input.requestId})).json()).conversation.seenThrough).toBe(input.requestId);
expect((await (await post(otherBase + '/seen', {requestId: input.requestId})).json()).conversation.seenThrough).toBe(input.requestId);
expect((await (await post(base + '/seen', {requestId: input.requestId})).json()).conversation.seenThrough).toBeUndefined();
expect((await (await fetch(base)).json()).topic.requests).toHaveLength(50);
expect((await post(otherBase + '/discussion', {projectId: f.a.project.id, action: 'archive'})).status).toBe(422);
expect((await fetch(base + '?before=missing')).status).toBe(400);
} finally { server.closeAllConnections(); await new Promise<void>(resolve => server.close(() => resolve())); }
});
@@ -160,7 +236,7 @@ async function fixture() {
let wait = false, version = 1, enabled = true, accountId = 'account';
const run = vi.fn(async (messages, _signal: AbortSignal, onText: (text: string) => void) => {
onText('已查看');
if (wait) await new Promise<void>(resolve => { release = resolve; });
if (wait) { wait = false; await new Promise<void>(resolve => { release = resolve; }); }
return { inputTokens: 10, outputTokens: 5 };
});
const prepareCloud = vi.fn((_account, _topic, _id, _access) => ({ inputLimit: 8000, run }));
@@ -175,17 +251,47 @@ async function fixture() {
const send = (project = a, session = aSource, requestId = randomUUID()) => service.sendConversation('agent-a', {
projectId: project.project.id, sourceConversationId: session.id, requestId, text: '帮我看看',
});
const settled = async () => {
await vi.waitFor(async () => expect((await service.conversation('agent-a')).topic?.requests.at(-1)?.status).toBe('completed'));
return (await service.conversation('agent-a')).topic!;
const settled = async (project = a) => {
await vi.waitFor(async () => expect((await service.conversation(project.project.id, 'agent-a')).topic?.requests.at(-1)?.status).toBe('completed'));
return (await service.conversation(project.project.id, 'agent-a')).topic!;
};
return { service, a, b, aSource, aOther, bSource, prepareCloud, run, send, settled,
return { dir, service, a, b, aSource, aOther, bSource, prepareCloud, run, send, settled,
wait: () => { wait = true; }, release: () => { wait = false; release?.(); },
version: (v: number) => { version = v; }, enabled: (v: boolean) => { enabled = v; },
account: (id: string) => { accountId = id; } };
}
describe('one account-agent conversation', () => {
it('keeps one visible chat across projects and versions while freezing each execution scope', async () => {
describe('one account-project-agent conversation', () => {
it('starts a clean cloud segment after splitting mixed history and supplies only the selected project messages', async () => {
const f = await fixture(), global = path.join(f.dir, 'agent-conversations', 'account', 'agent-a');
const turns = [
{ ...turn(1), projectId: f.a.project.id, sourceConversationId: f.aSource.id, runtimeThreadId: 'old-mixed-thread', teacherVersion: 1, text: '天气专属历史' },
{ ...turn(2), projectId: f.b.project.id, sourceConversationId: f.bSource.id, runtimeThreadId: 'old-mixed-thread', teacherVersion: 1, text: '游戏专属历史' },
];
await atomicWriteJson(path.join(global, 'conversation.json'), { topic: {
id: randomUUID(), accountId: 'account', definition, version: 1, projectId: f.b.project.id,
conversation: { agentId: 'agent-a', runtimeThreadId: 'old-mixed-thread', discussions: {} },
}, turns: turns.map(({ id, createdAt }) => ({ id, createdAt, messages: [] })) });
for (const turn of turns) await atomicWriteJson(path.join(global, 'turns', turn.id + '.json'), turn);
await f.send(); const current = await f.settled();
expect(current.requests).toHaveLength(2);
expect(current.requests.at(-1)?.runtimeThreadId).not.toBe('old-mixed-thread');
const messages = JSON.stringify(f.run.mock.calls[0][0]);
expect(messages).toContain('天气专属历史');
expect(messages).not.toContain('游戏专属历史');
});
it('isolates the same agent between projects and restores each project chat', async () => {
const f = await fixture();
const a = await f.send(); await f.settled();
const b = await f.send(f.b, f.bSource); await f.settled(f.b);
expect(b.id).not.toBe(a.id);
expect(b.requests).toHaveLength(1);
expect(JSON.stringify(f.run.mock.calls[1][0])).not.toContain('weather');
const back = await f.send(); await f.settled();
expect(back.id).toBe(a.id);
expect(back.requests).toHaveLength(2);
expect(back.requests.every(request => request.projectId === f.a.project.id)).toBe(true);
});
it('keeps one visible chat per project across versions while freezing each execution scope', async () => {
const f = await fixture();
await f.send(); const first = await f.settled();
await f.send(); const same = await f.settled();
@@ -193,18 +299,18 @@ describe('one account-agent conversation', () => {
expect(same.requests[1].runtimeThreadId).toBe(first.requests[0].runtimeThreadId);
// The same cloud checkpoint already owns the previous exchange.
expect(f.run.mock.calls[1][0].filter((m: { content: string }) => m.content.includes('已查看'))).toHaveLength(0);
await f.send(f.b, f.bSource); const second = await f.settled();
expect(second.id).toBe(first.id);
expect(second.requests[2].runtimeThreadId).not.toBe(first.requests[0].runtimeThreadId);
await f.send(f.b, f.bSource); const second = await f.settled(f.b);
expect(second.id).not.toBe(first.id);
expect(second.requests[0].runtimeThreadId).not.toBe(first.requests[0].runtimeThreadId);
expect(f.prepareCloud.mock.calls[2][3].projectPath).toBe(f.b.project.path);
expect(JSON.stringify(f.run.mock.calls[2][0])).toContain('weather');
f.version(2); await f.send(f.b, f.bSource); const updated = await f.settled();
expect(updated.id).toBe(first.id);
expect(updated.requests.map(request => request.teacherVersion)).toEqual([1, 1, 1, 2]);
expect(updated.requests[3].runtimeThreadId).not.toBe(updated.requests[2].runtimeThreadId);
expect(JSON.stringify(f.run.mock.calls[2][0])).not.toContain('weather');
f.version(2); await f.send(f.b, f.bSource); const updated = await f.settled(f.b);
expect(updated.id).toBe(second.id);
expect(updated.requests.map(request => request.teacherVersion)).toEqual([1, 2]);
expect(updated.requests[1].runtimeThreadId).not.toBe(updated.requests[0].runtimeThreadId);
f.enabled(false);
await expect(f.send()).rejects.toMatchObject({ code: 'teacher_disabled' });
expect((await f.service.conversation('agent-a')).topic?.requests).toHaveLength(4);
expect((await f.service.conversation(f.a.project.id, 'agent-a')).topic?.requests).toHaveLength(2);
});
it('deduplicates concurrent submissions, rejects a second active turn and rotates on Pi source change', async () => {
const f = await fixture(); f.wait();
@@ -212,13 +318,19 @@ describe('one account-agent conversation', () => {
const [a, b] = await Promise.all([f.send(f.a, f.aSource, requestId), f.send(f.a, f.aSource, requestId)]);
expect(a.id).toBe(b.id);
expect(f.run).toHaveBeenCalledTimes(1);
await expect(f.send(f.b, f.bSource)).rejects.toMatchObject({ code: 'teacher_topic_busy' });
await expect(f.send()).rejects.toMatchObject({ code: 'teacher_topic_busy' });
const other = await f.send(f.b, f.bSource); await f.settled(f.b);
expect((await f.service.conversation(f.a.project.id, 'agent-a')).topic?.requests[0].status).toBe('running');
const otherScope = { projectId: f.b.project.id, sourceId: 'project', agentId: 'agent-a' };
await f.service.cancel(otherScope, other.id, requestId);
expect(f.run.mock.calls[0][1].aborted).toBe(false);
await expect(f.service.read(otherScope, a.id)).rejects.toMatchObject({ code: 'teacher_topic_not_found' });
expect(f.prepareCloud.mock.calls[0][3].projectPath).toBe(f.a.project.path);
f.release(); await f.settled();
await f.send(f.a, f.aOther); const next = await f.settled();
expect(next.requests[1].runtimeThreadId).not.toBe(next.requests[0].runtimeThreadId);
f.account('other');
expect((await f.service.conversation('agent-a')).topic).toBe(null);
expect((await f.service.conversation(f.a.project.id, 'agent-a')).topic).toBe(null);
await f.send(); expect((await f.settled()).id).not.toBe(a.id);
});
});