fix(chat): 阻止同会话并发 Agent Run 导致消息错乱

前端 submit 改用 chatStateRef 同步拦截连发;后端 createRun 检测同 session 活跃任务并返回 409,避免多条回复交错落库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-07-07 11:48:59 +08:00
parent ac520d8ae5
commit 1e4f432754
5 changed files with 162 additions and 5 deletions
+37
View File
@@ -464,6 +464,43 @@ test('POST /agent/runs rejects a session the user does not own', async () => {
assert.deepEqual(res.body, { message: '无权访问该会话' });
});
test('POST /agent/runs returns 409 when session already has active run', async () => {
const handler = createPostAgentRunsHandler({
userAuth: {
async ownsSession() {
return true;
},
},
agentRunGateway: {
async createRun() {
const err = new Error('该会话有正在处理的任务,请等待完成后再发送');
err.code = 'SESSION_RUN_CONFLICT';
err.status = 409;
throw err;
},
},
});
const res = createResponseRecorder();
await handler(
{
currentUser: { id: 'user-1' },
body: {
session_id: 'session-busy',
request_id: 'req-busy',
user_message: { role: 'user', content: [{ type: 'text', text: 'hello' }] },
},
},
res,
);
assert.equal(res.statusCode, 409);
assert.deepEqual(res.body, {
message: '该会话有正在处理的任务,请等待完成后再发送',
code: 'SESSION_RUN_CONFLICT',
});
});
test('POST /agent/runs validates ownership through sessionAccess when provided', async () => {
const validated = [];
const handler = createPostAgentRunsHandler({