Fix paired owner completion handoff race
This commit is contained in:
@@ -1314,6 +1314,88 @@ describe('runAgentForGroup room memory', () => {
|
|||||||
expect(deps.queue.enqueueMessageCheck).not.toHaveBeenCalled();
|
expect(deps.queue.enqueueMessageCheck).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('stores owner turn output and completes the paired task before delivery', async () => {
|
||||||
|
const group = { ...makeGroup(), folder: 'test-group' };
|
||||||
|
const deps = makeDeps();
|
||||||
|
const onOutput = vi.fn(async () => {});
|
||||||
|
|
||||||
|
vi.mocked(
|
||||||
|
pairedExecutionContext.preparePairedExecutionContext,
|
||||||
|
).mockReturnValue({
|
||||||
|
task: {
|
||||||
|
id: 'paired-task-owner-output-order',
|
||||||
|
chat_jid: 'group@test',
|
||||||
|
group_folder: 'test-group',
|
||||||
|
owner_service_id: 'claude',
|
||||||
|
reviewer_service_id: 'codex-review',
|
||||||
|
title: null,
|
||||||
|
source_ref: 'HEAD',
|
||||||
|
plan_notes: null,
|
||||||
|
round_trip_count: 0,
|
||||||
|
review_requested_at: null,
|
||||||
|
status: 'active',
|
||||||
|
arbiter_verdict: null,
|
||||||
|
arbiter_requested_at: null,
|
||||||
|
completion_reason: null,
|
||||||
|
created_at: '2026-03-31T00:00:00.000Z',
|
||||||
|
updated_at: '2026-03-31T00:00:00.000Z',
|
||||||
|
},
|
||||||
|
workspace: null,
|
||||||
|
envOverrides: {},
|
||||||
|
});
|
||||||
|
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
|
||||||
|
async (_group, _input, _onProcess, forwardOutput) => {
|
||||||
|
await forwardOutput?.({
|
||||||
|
status: 'success',
|
||||||
|
result: 'DONE_WITH_CONCERNS\nowner complete',
|
||||||
|
output: {
|
||||||
|
visibility: 'public',
|
||||||
|
text: 'DONE_WITH_CONCERNS\nowner complete',
|
||||||
|
},
|
||||||
|
phase: 'final',
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
status: 'success',
|
||||||
|
result: 'DONE_WITH_CONCERNS\nowner complete',
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await runAgentForGroup(deps, {
|
||||||
|
group,
|
||||||
|
prompt: 'please implement',
|
||||||
|
chatJid: 'group@test',
|
||||||
|
runId: 'run-owner-output-order',
|
||||||
|
onOutput,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toBe('success');
|
||||||
|
expect(db.insertPairedTurnOutput).toHaveBeenCalledWith(
|
||||||
|
'paired-task-owner-output-order',
|
||||||
|
1,
|
||||||
|
'owner',
|
||||||
|
'DONE_WITH_CONCERNS\nowner complete',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
vi.mocked(db.insertPairedTurnOutput).mock.invocationCallOrder[0],
|
||||||
|
).toBeLessThan(
|
||||||
|
vi.mocked(pairedExecutionContext.completePairedExecutionContext).mock
|
||||||
|
.invocationCallOrder[0],
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
vi.mocked(pairedExecutionContext.completePairedExecutionContext).mock
|
||||||
|
.invocationCallOrder[0],
|
||||||
|
).toBeLessThan(onOutput.mock.invocationCallOrder[0]);
|
||||||
|
expect(
|
||||||
|
vi.mocked(pairedExecutionContext.completePairedExecutionContext),
|
||||||
|
).toHaveBeenCalledWith({
|
||||||
|
taskId: 'paired-task-owner-output-order',
|
||||||
|
role: 'owner',
|
||||||
|
status: 'succeeded',
|
||||||
|
summary: 'DONE_WITH_CONCERNS\nowner complete',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('stores reviewer turn output before transitioning the paired task back to active', async () => {
|
it('stores reviewer turn output before transitioning the paired task back to active', async () => {
|
||||||
const group = { ...makeGroup(), folder: 'test-group' };
|
const group = { ...makeGroup(), folder: 'test-group' };
|
||||||
const deps = makeDeps();
|
const deps = makeDeps();
|
||||||
|
|||||||
@@ -337,6 +337,30 @@ export async function runAgentForGroup(
|
|||||||
pairedTurnOutputPersisted = true;
|
pairedTurnOutputPersisted = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const completeSuccessfulOwnerTurnBeforeDeliveryIfNeeded = () => {
|
||||||
|
const completedRole = roomRoleContext?.role ?? 'owner';
|
||||||
|
if (
|
||||||
|
completedRole !== 'owner' ||
|
||||||
|
!pairedExecutionContext ||
|
||||||
|
pairedExecutionCompleted ||
|
||||||
|
!pairedFinalOutput ||
|
||||||
|
pairedFinalOutput.length === 0
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pairedExecutionStatus = 'succeeded';
|
||||||
|
pairedSawOutput = true;
|
||||||
|
persistPairedTurnOutputIfNeeded(completedRole);
|
||||||
|
completePairedExecutionContext({
|
||||||
|
taskId: pairedExecutionContext.task.id,
|
||||||
|
role: completedRole,
|
||||||
|
status: 'succeeded',
|
||||||
|
summary: pairedExecutionSummary,
|
||||||
|
});
|
||||||
|
pairedExecutionCompleted = true;
|
||||||
|
};
|
||||||
|
|
||||||
const maybeHandoffToCodex = (
|
const maybeHandoffToCodex = (
|
||||||
reason: AgentTriggerReason,
|
reason: AgentTriggerReason,
|
||||||
sawVisibleOutput: boolean,
|
sawVisibleOutput: boolean,
|
||||||
@@ -567,11 +591,12 @@ export async function runAgentForGroup(
|
|||||||
) {
|
) {
|
||||||
pairedFinalOutput = outputText;
|
pairedFinalOutput = outputText;
|
||||||
try {
|
try {
|
||||||
|
completeSuccessfulOwnerTurnBeforeDeliveryIfNeeded();
|
||||||
persistPairedTurnOutputIfNeeded(roomRoleContext?.role ?? 'owner');
|
persistPairedTurnOutputIfNeeded(roomRoleContext?.role ?? 'owner');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.warn(
|
log.warn(
|
||||||
{ pairedTaskId: pairedExecutionContext?.task.id ?? null, err },
|
{ pairedTaskId: pairedExecutionContext?.task.id ?? null, err },
|
||||||
'Failed to persist paired turn output before delivery',
|
'Failed to persist paired turn output and status before delivery',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1645,6 +1645,59 @@ describe('createMessageRuntime', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('requeues the reviewer follow-up once owner output is persisted under review_ready', () => {
|
||||||
|
const chatJid = 'group@test';
|
||||||
|
const group = makeGroup('codex');
|
||||||
|
const task = {
|
||||||
|
id: 'task-reviewer-follow-up-after-owner-output',
|
||||||
|
chat_jid: chatJid,
|
||||||
|
group_folder: group.folder,
|
||||||
|
owner_service_id: 'claude',
|
||||||
|
reviewer_service_id: 'codex-main',
|
||||||
|
title: null,
|
||||||
|
source_ref: 'HEAD',
|
||||||
|
plan_notes: null,
|
||||||
|
review_requested_at: '2026-03-30T00:00:00.000Z',
|
||||||
|
round_trip_count: 1,
|
||||||
|
status: 'review_ready',
|
||||||
|
arbiter_verdict: null,
|
||||||
|
arbiter_requested_at: null,
|
||||||
|
completion_reason: null,
|
||||||
|
created_at: '2026-03-30T00:00:00.000Z',
|
||||||
|
updated_at: '2026-03-30T00:00:00.000Z',
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
vi.mocked(serviceRouting.hasReviewerLease).mockReturnValue(true);
|
||||||
|
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
task_id: 'task-reviewer-follow-up-after-owner-output',
|
||||||
|
turn_number: 1,
|
||||||
|
role: 'owner',
|
||||||
|
output_text: 'owner 응답',
|
||||||
|
created_at: '2026-03-30T00:00:01.000Z',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
resolveBotOnlyPairedFollowUpAction({
|
||||||
|
chatJid,
|
||||||
|
task,
|
||||||
|
isBotOnlyPairedFollowUp: true,
|
||||||
|
pendingCursorSource: {
|
||||||
|
seq: 42,
|
||||||
|
timestamp: '2026-03-30T00:00:04.000Z',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
kind: 'requeue-pending-turn',
|
||||||
|
task,
|
||||||
|
cursor: 42,
|
||||||
|
cursorKey: `${chatJid}:reviewer`,
|
||||||
|
nextRole: 'reviewer',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('consumes stale bot-only owner messages once the finalize turn output is already persisted', () => {
|
it('consumes stale bot-only owner messages once the finalize turn output is already persisted', () => {
|
||||||
const chatJid = 'group@test';
|
const chatJid = 'group@test';
|
||||||
const group = makeGroup('codex');
|
const group = makeGroup('codex');
|
||||||
|
|||||||
Reference in New Issue
Block a user