fix(agent-jobs): recovery cron skips already-terminal jobs
This commit is contained in:
@@ -15,6 +15,10 @@ const seedJob = async (
|
||||
status?: ActiveStatus;
|
||||
claimedAt?: number;
|
||||
lastHeartbeatAt?: number;
|
||||
workspaceStatus?: string;
|
||||
completedAt?: number;
|
||||
threadStatus?: 'running' | 'resolved' | 'ignored' | 'waiting_for_user';
|
||||
threadResolvedAt?: number;
|
||||
},
|
||||
) =>
|
||||
await t.mutation(async (ctx) => {
|
||||
@@ -55,9 +59,12 @@ const seedJob = async (
|
||||
spoonId,
|
||||
title: 'Recover the worker job',
|
||||
source: 'user_request',
|
||||
status: 'running',
|
||||
status: args.threadStatus ?? 'running',
|
||||
priority: 'normal',
|
||||
relatedAgentRequestId: requestId,
|
||||
...(args.threadResolvedAt === undefined
|
||||
? {}
|
||||
: { resolvedAt: args.threadResolvedAt }),
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
@@ -69,7 +76,7 @@ const seedJob = async (
|
||||
status: args.status ?? 'running',
|
||||
prompt: 'Recover the worker job',
|
||||
runtime: 'opencode',
|
||||
workspaceStatus: 'active',
|
||||
workspaceStatus: args.workspaceStatus ?? 'active',
|
||||
baseBranch: 'main',
|
||||
workBranch: 'spoon/recover-worker-job',
|
||||
forkOwner: 'team',
|
||||
@@ -85,6 +92,9 @@ const seedJob = async (
|
||||
...(args.lastHeartbeatAt === undefined
|
||||
? {}
|
||||
: { lastHeartbeatAt: args.lastHeartbeatAt }),
|
||||
...(args.completedAt === undefined
|
||||
? {}
|
||||
: { completedAt: args.completedAt }),
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
@@ -126,6 +136,31 @@ describe('recoverStaleJobs', () => {
|
||||
expect(state.thread?.resolvedAt).toBeTypeOf('number');
|
||||
});
|
||||
|
||||
test('leaves a terminal-workspace maintenance job untouched', async () => {
|
||||
const t = convexTest(schema, modules);
|
||||
const resolvedAt = Date.now() - 5 * 60 * 1000;
|
||||
const ids = await seedJob(t, {
|
||||
status: 'running',
|
||||
workspaceStatus: 'stopped',
|
||||
completedAt: resolvedAt,
|
||||
threadStatus: 'resolved',
|
||||
threadResolvedAt: resolvedAt,
|
||||
// stale heartbeat: the worker stopped its heartbeat after applying the
|
||||
// maintenance decision, so a naive cron would time this job out
|
||||
claimedAt: Date.now() - 10 * 60 * 1000,
|
||||
lastHeartbeatAt: Date.now() - 10 * 60 * 1000,
|
||||
});
|
||||
|
||||
const result = await t.mutation(internal.agentJobs.recoverStaleJobs, {});
|
||||
const state = await readState(t, ids);
|
||||
|
||||
expect(result).toEqual({ recovered: 0 });
|
||||
expect(state.job?.status).toBe('running');
|
||||
expect(state.job?.workspaceStatus).toBe('stopped');
|
||||
expect(state.thread?.status).toBe('resolved');
|
||||
expect(state.thread?.resolvedAt).toBe(resolvedAt);
|
||||
});
|
||||
|
||||
test('leaves a running job with a fresh heartbeat untouched', async () => {
|
||||
const t = convexTest(schema, modules);
|
||||
const ids = await seedJob(t, { lastHeartbeatAt: Date.now() });
|
||||
|
||||
Reference in New Issue
Block a user