feat: scope paired owner/reviewer sessions to one paired task

Each paired room used to resume one owner (and one reviewer) session for
weeks, so every call re-read a huge unrelated history (median ~260k input
tokens). A stored session is now resumed only while it belongs to the
running paired task; the first turn of a new task starts fresh and the new
session is bound to that task (router_state paired_session_task:<folder>,
survives restarts). Later turns of the same task (revisions, follow-up
human messages while the task is open) keep resuming it. Arbiter was
already fresh per call; single rooms are unchanged.

The previous task's final answer still reaches the new session through the
carried-forward turn-0 output, so PAIRED_CARRY_FORWARD_LATEST_OWNER_FINAL
now defaults to true.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-01 20:25:35 +09:00
parent 8c68577a24
commit c858b6e446
10 changed files with 307 additions and 11 deletions

View File

@@ -59,6 +59,8 @@ vi.mock('./db.js', () => {
updatePairedTaskIfUnchanged: vi.fn(() => true),
getPairedTurnAttempts: vi.fn(() => []),
getPairedTurnOutputs: vi.fn(() => []),
getRouterState: vi.fn(() => undefined),
setRouterState: vi.fn(),
insertPairedTurnOutput: vi.fn(),
markPairedTurnRunning: vi.fn(),
refreshPairedTaskExecutionLease: vi.fn(() => true),
@@ -1830,6 +1832,11 @@ describe('runAgentForGroup room memory', () => {
...makeDeps(),
getSessions: () => ({ 'test-group:reviewer': 'reviewer-session' }),
};
vi.mocked(db.getRouterState).mockImplementation((key: string) =>
key === 'paired_session_task:test-group:reviewer'
? 'paired-task-review'
: undefined,
);
vi.mocked(serviceRouting.getEffectiveChannelLease).mockReturnValue({
chat_jid: 'group@test',
@@ -3398,3 +3405,146 @@ describe('runAgentForGroup Claude failure recovery', () => {
]);
});
});
describe('runAgentForGroup task-scoped paired sessions', () => {
const pairedTask = (id: string) => ({
id,
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' as const,
arbiter_verdict: null,
arbiter_requested_at: null,
completion_reason: null,
created_at: '2026-03-28T00:00:00.000Z',
updated_at: '2026-03-28T00:00:00.000Z',
});
function prepareOwnerTask(taskId: string) {
vi.mocked(
pairedExecutionContext.preparePairedExecutionContext,
).mockReturnValue({
task: pairedTask(taskId),
workspace: null,
envOverrides: {
EJCLAW_PAIRED_TASK_ID: taskId,
EJCLAW_PAIRED_ROLE: 'owner',
},
});
}
beforeEach(() => {
vi.resetAllMocks();
resetPairedFollowUpScheduleState();
vi.mocked(buildRoomMemoryBriefing).mockResolvedValue(undefined as never);
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
async (_group, _input, _onProcess, onOutput) => {
await onOutput?.({
status: 'success',
result: 'TASK_DONE\n\nok',
output: { visibility: 'public', text: 'TASK_DONE\n\nok' },
phase: 'final',
newSessionId: 'session-new',
});
return {
status: 'success',
result: null,
newSessionId: 'session-new',
};
},
);
});
it('starts a fresh owner session for a new paired task and binds it to that task', async () => {
const group = { ...makeGroup(), folder: 'test-group' };
const deps = {
...makeDeps(),
getSessions: () => ({ 'test-group': 'session-of-old-task' }),
};
vi.mocked(db.getRouterState).mockImplementation((key: string) =>
key === 'paired_session_task:test-group' ? 'old-task' : undefined,
);
prepareOwnerTask('new-task');
await runAgentForGroup(deps, {
group,
prompt: 'new request',
chatJid: 'group@test',
runId: 'run-new-task',
hasHumanMessage: true,
});
expect(agentRunner.runAgentProcess).toHaveBeenCalledWith(
group,
expect.objectContaining({ sessionId: undefined }),
expect.any(Function),
expect.any(Function),
expect.any(Object),
);
expect(deps.persistSession).toHaveBeenCalledWith(
'test-group',
'session-new',
);
expect(db.setRouterState).toHaveBeenCalledWith(
'paired_session_task:test-group',
'new-task',
);
});
it('resumes the owner session for later turns of the same paired task', async () => {
const group = { ...makeGroup(), folder: 'test-group' };
const deps = {
...makeDeps(),
getSessions: () => ({ 'test-group': 'session-of-task' }),
};
vi.mocked(db.getRouterState).mockImplementation((key: string) =>
key === 'paired_session_task:test-group' ? 'same-task' : undefined,
);
prepareOwnerTask('same-task');
await runAgentForGroup(deps, {
group,
prompt: 'follow-up',
chatJid: 'group@test',
runId: 'run-same-task',
});
expect(agentRunner.runAgentProcess).toHaveBeenCalledWith(
group,
expect.objectContaining({ sessionId: 'session-of-task' }),
expect.any(Function),
expect.any(Function),
expect.any(Object),
);
});
it('keeps resuming the room session for single (non-paired) rooms', async () => {
const group = { ...makeGroup(), folder: 'test-group' };
const deps = {
...makeDeps(),
getSessions: () => ({ 'test-group': 'single-room-session' }),
};
await runAgentForGroup(deps, {
group,
prompt: 'hello',
chatJid: 'group@test',
runId: 'run-single',
});
expect(agentRunner.runAgentProcess).toHaveBeenCalledWith(
group,
expect.objectContaining({ sessionId: 'single-room-session' }),
expect.any(Function),
expect.any(Function),
undefined,
);
expect(db.setRouterState).not.toHaveBeenCalled();
});
});