Merge pull request #100 from phj1081/codex/owner/ejclaw
Extract message runtime handoff adapter
This commit is contained in:
@@ -9,6 +9,7 @@ vi.mock('./db.js', () => ({
|
|||||||
claimPairedTurnReservation: vi.fn(() => true),
|
claimPairedTurnReservation: vi.fn(() => true),
|
||||||
completeServiceHandoffAndAdvanceTargetCursor: vi.fn(() => null),
|
completeServiceHandoffAndAdvanceTargetCursor: vi.fn(() => null),
|
||||||
failServiceHandoff: vi.fn(),
|
failServiceHandoff: vi.fn(),
|
||||||
|
getPairedTaskById: vi.fn(),
|
||||||
getPairedTurnOutputs: vi.fn(() => []),
|
getPairedTurnOutputs: vi.fn(() => []),
|
||||||
getPendingServiceHandoffs: vi.fn(() => []),
|
getPendingServiceHandoffs: vi.fn(() => []),
|
||||||
reservePairedTurnReservation: vi.fn(() => true),
|
reservePairedTurnReservation: vi.fn(() => true),
|
||||||
@@ -25,6 +26,7 @@ vi.mock('./logger.js', () => ({
|
|||||||
|
|
||||||
import * as db from './db.js';
|
import * as db from './db.js';
|
||||||
import {
|
import {
|
||||||
|
enqueueMessageRuntimePendingHandoffs,
|
||||||
enqueuePendingHandoffs,
|
enqueuePendingHandoffs,
|
||||||
processClaimedHandoff,
|
processClaimedHandoff,
|
||||||
} from './message-runtime-handoffs.js';
|
} from './message-runtime-handoffs.js';
|
||||||
@@ -52,6 +54,93 @@ function makeChannel(name: string, ownsChatJid = false): Channel {
|
|||||||
} as unknown as Channel;
|
} as unknown as Channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function makeClaimedHandoff(overrides: Record<string, unknown> = {}) {
|
||||||
|
return {
|
||||||
|
id: 13,
|
||||||
|
chat_jid: 'group@test',
|
||||||
|
group_folder: 'test-group',
|
||||||
|
source_service_id: 'claude',
|
||||||
|
target_service_id: 'codex-main',
|
||||||
|
source_role: 'reviewer',
|
||||||
|
source_agent_type: 'claude-code',
|
||||||
|
target_role: 'owner',
|
||||||
|
target_agent_type: 'codex',
|
||||||
|
prompt: 'owner handoff',
|
||||||
|
status: 'claimed',
|
||||||
|
start_seq: 13,
|
||||||
|
end_seq: 14,
|
||||||
|
reason: 'owner-follow-up',
|
||||||
|
intended_role: 'owner',
|
||||||
|
created_at: '2026-04-10T00:00:00.000Z',
|
||||||
|
claimed_at: '2026-04-10T00:00:01.000Z',
|
||||||
|
completed_at: null,
|
||||||
|
last_error: null,
|
||||||
|
...overrides,
|
||||||
|
} as any;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('enqueueMessageRuntimePendingHandoffs', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('enqueues pending handoffs with runtime-bound processing dependencies', async () => {
|
||||||
|
const handoff = makeClaimedHandoff();
|
||||||
|
vi.mocked(db.getPendingServiceHandoffs).mockReturnValue([handoff]);
|
||||||
|
vi.mocked(db.claimServiceHandoff).mockReturnValue(true);
|
||||||
|
vi.mocked(db.completeServiceHandoffAndAdvanceTargetCursor).mockReturnValue(
|
||||||
|
'14',
|
||||||
|
);
|
||||||
|
const enqueueTask = vi.fn();
|
||||||
|
const executeTurn = vi.fn(async () => ({
|
||||||
|
outputStatus: 'success' as const,
|
||||||
|
deliverySucceeded: true,
|
||||||
|
visiblePhase: 'final',
|
||||||
|
}));
|
||||||
|
const lastAgentTimestamps: Record<string, string> = {};
|
||||||
|
const getLastAgentTimestamps = vi.fn(() => lastAgentTimestamps);
|
||||||
|
const saveState = vi.fn();
|
||||||
|
const enqueueMessageCheck = vi.fn();
|
||||||
|
|
||||||
|
enqueueMessageRuntimePendingHandoffs({
|
||||||
|
enqueueTask,
|
||||||
|
getRoomBindings: () => ({
|
||||||
|
'group@test': makeGroup(),
|
||||||
|
}),
|
||||||
|
channels: [makeChannel('discord-main', true)],
|
||||||
|
executeTurn,
|
||||||
|
getLastAgentTimestamps,
|
||||||
|
saveState,
|
||||||
|
enqueueMessageCheck,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(db.getPendingServiceHandoffs).toHaveBeenCalledWith('codex-main');
|
||||||
|
expect(db.claimServiceHandoff).toHaveBeenCalledWith(13);
|
||||||
|
expect(getLastAgentTimestamps).not.toHaveBeenCalled();
|
||||||
|
expect(enqueueTask).toHaveBeenCalledWith(
|
||||||
|
'group@test',
|
||||||
|
'handoff:13',
|
||||||
|
expect.any(Function),
|
||||||
|
);
|
||||||
|
|
||||||
|
const queuedTask = vi.mocked(enqueueTask).mock.calls[0]?.[2];
|
||||||
|
await queuedTask?.();
|
||||||
|
|
||||||
|
expect(getLastAgentTimestamps).toHaveBeenCalledTimes(1);
|
||||||
|
expect(executeTurn).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
chatJid: 'group@test',
|
||||||
|
runId: 'handoff-13',
|
||||||
|
forcedRole: 'owner',
|
||||||
|
forcedAgentType: 'codex',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(lastAgentTimestamps['group@test']).toBe('14');
|
||||||
|
expect(saveState).toHaveBeenCalledTimes(1);
|
||||||
|
expect(enqueueMessageCheck).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('message-runtime-handoffs', () => {
|
describe('message-runtime-handoffs', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
claimServiceHandoff,
|
claimServiceHandoff,
|
||||||
completeServiceHandoffAndAdvanceTargetCursor,
|
completeServiceHandoffAndAdvanceTargetCursor,
|
||||||
failServiceHandoff,
|
failServiceHandoff,
|
||||||
|
getPairedTaskById,
|
||||||
getPendingServiceHandoffs,
|
getPendingServiceHandoffs,
|
||||||
type ServiceHandoff,
|
type ServiceHandoff,
|
||||||
} from './db.js';
|
} from './db.js';
|
||||||
@@ -45,6 +46,36 @@ export function enqueuePendingHandoffs(args: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function enqueueMessageRuntimePendingHandoffs(args: {
|
||||||
|
enqueueTask?:
|
||||||
|
| ((chatJid: string, taskId: string, task: () => Promise<void>) => void)
|
||||||
|
| undefined;
|
||||||
|
getRoomBindings: () => Record<string, RegisteredGroup>;
|
||||||
|
channels: Channel[];
|
||||||
|
executeTurn: ExecuteTurnFn;
|
||||||
|
getLastAgentTimestamps: () => Record<string, string>;
|
||||||
|
saveState: () => void;
|
||||||
|
enqueueMessageCheck: (chatJid: string) => void;
|
||||||
|
}): void {
|
||||||
|
enqueuePendingHandoffs({
|
||||||
|
enqueueTask: (chatJid, taskId, task) => {
|
||||||
|
args.enqueueTask?.(chatJid, taskId, task);
|
||||||
|
},
|
||||||
|
processClaimedHandoff: async (handoff) => {
|
||||||
|
await processClaimedHandoff({
|
||||||
|
handoff,
|
||||||
|
getRoomBindings: args.getRoomBindings,
|
||||||
|
channels: args.channels,
|
||||||
|
executeTurn: args.executeTurn,
|
||||||
|
lastAgentTimestamps: args.getLastAgentTimestamps(),
|
||||||
|
saveState: args.saveState,
|
||||||
|
getPairedTaskById,
|
||||||
|
enqueueMessageCheck: args.enqueueMessageCheck,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function requeueFailedClaimedPairedTurn(args: {
|
function requeueFailedClaimedPairedTurn(args: {
|
||||||
handoff: ServiceHandoff;
|
handoff: ServiceHandoff;
|
||||||
error: string;
|
error: string;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import {
|
|||||||
getMessagesSinceSeq,
|
getMessagesSinceSeq,
|
||||||
getLatestOpenPairedTaskForChat,
|
getLatestOpenPairedTaskForChat,
|
||||||
markWorkItemDelivered,
|
markWorkItemDelivered,
|
||||||
getPairedTaskById,
|
|
||||||
updatePairedTurnProgressText,
|
updatePairedTurnProgressText,
|
||||||
} from './db.js';
|
} from './db.js';
|
||||||
import {
|
import {
|
||||||
@@ -17,10 +16,7 @@ import { enqueueGenericFollowUpAfterDeliveryRetry as enqueueDeliveryRetryFollowU
|
|||||||
import { processOpenWorkItemDelivery } from './message-runtime-delivery.js';
|
import { processOpenWorkItemDelivery } from './message-runtime-delivery.js';
|
||||||
import { deliverCanonicalOutboundMessage } from './ipc-outbound-delivery.js';
|
import { deliverCanonicalOutboundMessage } from './ipc-outbound-delivery.js';
|
||||||
import { handleQueuedRunGates } from './message-runtime-gating.js';
|
import { handleQueuedRunGates } from './message-runtime-gating.js';
|
||||||
import {
|
import { enqueueMessageRuntimePendingHandoffs } from './message-runtime-handoffs.js';
|
||||||
enqueuePendingHandoffs as enqueueClaimedServiceHandoffs,
|
|
||||||
processClaimedHandoff as processClaimedServiceHandoff,
|
|
||||||
} from './message-runtime-handoffs.js';
|
|
||||||
import {
|
import {
|
||||||
buildScopedMessageCheckEnqueuer,
|
buildScopedMessageCheckEnqueuer,
|
||||||
processMessageLoopTick,
|
processMessageLoopTick,
|
||||||
@@ -229,23 +225,14 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const enqueuePendingHandoffs = (): void => {
|
const enqueuePendingHandoffs = (): void => {
|
||||||
enqueueClaimedServiceHandoffs({
|
enqueueMessageRuntimePendingHandoffs({
|
||||||
enqueueTask: (chatJid, taskId, task) => {
|
enqueueTask: deps.queue.enqueueTask?.bind(deps.queue),
|
||||||
deps.queue.enqueueTask?.(chatJid, taskId, task);
|
getRoomBindings: deps.getRoomBindings,
|
||||||
},
|
channels: deps.channels,
|
||||||
processClaimedHandoff: async (handoff) => {
|
executeTurn,
|
||||||
await processClaimedServiceHandoff({
|
getLastAgentTimestamps: deps.getLastAgentTimestamps,
|
||||||
handoff,
|
saveState: deps.saveState,
|
||||||
getRoomBindings: deps.getRoomBindings,
|
enqueueMessageCheck: (chatJid) => deps.queue.enqueueMessageCheck(chatJid),
|
||||||
channels: deps.channels,
|
|
||||||
executeTurn,
|
|
||||||
lastAgentTimestamps: deps.getLastAgentTimestamps(),
|
|
||||||
saveState: deps.saveState,
|
|
||||||
getPairedTaskById,
|
|
||||||
enqueueMessageCheck: (chatJid) =>
|
|
||||||
deps.queue.enqueueMessageCheck(chatJid),
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user