75 lines
4.1 KiB
JavaScript
75 lines
4.1 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert/strict");
|
|
const test = require("node:test");
|
|
const { Job } = require("bullmq");
|
|
const { agentOccurrencePredatesEnable, reconcileAgentQueueState } = require("../AgentWorker/queue-state");
|
|
const { recoverInterruptedAgentJobs } = require("../AgentWorker/job-store");
|
|
|
|
test("OFF cancels durable work and re-enabling establishes a new schedule boundary", async () => {
|
|
let enabled = true;
|
|
let generation = 3;
|
|
const statements = [];
|
|
const connection = {
|
|
beginTransaction: async () => statements.push("begin"),
|
|
commit: async () => statements.push("commit"),
|
|
release: () => statements.push("release"),
|
|
execute: async (sql, params) => {
|
|
if (sql.includes("FOR UPDATE")) {
|
|
statements.push("lock");
|
|
return [[{ enabled: Number(enabled), generation, enabled_since_ms: enabled ? 400 : null }]];
|
|
}
|
|
if (sql.includes("UPDATE ai_agent_worker_state")) {
|
|
statements.push("change");
|
|
enabled = params[0];
|
|
generation += 1;
|
|
} else if (sql.includes("agent_automatic_edit_upgrade")) {
|
|
assert.match(sql, /WHERE status = 'queued' AND auto_approve_file_edits = TRUE AND auto_file_edit_generation IS NULL/);
|
|
statements.push("fence-old-edits");
|
|
} else if (sql.includes("UPDATE ai_agent_jobs")) {
|
|
assert.match(sql, /status IN \('queued','running'\)/);
|
|
assert.match(sql, /SET status = 'cancelled'/);
|
|
statements.push("cancel");
|
|
} else assert.fail(`Unexpected SQL: ${sql}`);
|
|
},
|
|
};
|
|
const pool = { getConnection: async () => connection };
|
|
const disabled = await reconcileAgentQueueState(pool, false);
|
|
assert.deepEqual(statements, ["begin", "lock", "change", "cancel", "lock", "commit", "release"]);
|
|
assert.deepEqual(disabled, { enabled: false, generation: "4", enabledSinceMs: 0 });
|
|
assert.equal(agentOccurrencePredatesEnable({ timestamp: 10, delay: 500 }, disabled), true);
|
|
const active = await reconcileAgentQueueState(pool, true);
|
|
assert.equal(active.generation, "5");
|
|
assert.equal(agentOccurrencePredatesEnable({ timestamp: 10, delay: 200 }, active), true);
|
|
assert.equal(agentOccurrencePredatesEnable({ timestamp: 10, delay: 500 }, active), false);
|
|
assert.equal(agentOccurrencePredatesEnable({ timestamp: 700, opts: { prevMillis: 150 } }, active), true);
|
|
const unchanged = await reconcileAgentQueueState(pool, true);
|
|
assert.deepEqual(unchanged, active);
|
|
assert.deepEqual(statements.slice(-6), ["begin", "lock", "fence-old-edits", "lock", "commit", "release"]);
|
|
});
|
|
|
|
test("BullMQ promotion does not make a one-shot due after enable look overdue", () => {
|
|
const state = { enabled: true, generation: "2", enabledSinceMs: 400 };
|
|
const queue = { keys: {}, toKey: (key) => `fixture:${key}` };
|
|
for (const [initialDelay, skipped] of [[200, true], [500, false]]) {
|
|
const job = Job.fromJSON(queue, { id: "1", name: "scheduled-task", data: "{}",
|
|
timestamp: "10", delay: "0", opts: JSON.stringify({ delay: initialDelay }) });
|
|
assert.equal(job.delay, 0);
|
|
assert.equal(agentOccurrencePredatesEnable(job, state), skipped);
|
|
}
|
|
});
|
|
|
|
test("consumer recovery fails interrupted runs and periodic recovery leaves fresh runs alone", async () => {
|
|
const statements = [];
|
|
const db = { query: async (...args) => statements.push(args) };
|
|
await recoverInterruptedAgentJobs(db);
|
|
await recoverInterruptedAgentJobs(db, 240);
|
|
|
|
const [[startupSql, startupParams], [startupQuarantine], [periodicSql, periodicParams], [periodicQuarantine]] = statements;
|
|
assert.match(startupSql, /SET status = 'failed', completed_at = NOW\(\),\s+error = CASE WHEN auto_approve_file_edits = TRUE AND auto_file_edit_attempts > 0 THEN 'agent_automatic_edit_interrupted' ELSE 'agent_interrupted' END\s+WHERE status = 'running'$/);
|
|
assert.deepEqual(startupParams, []);
|
|
assert.equal(periodicSql, `${startupSql} AND started_at < DATE_SUB(NOW(), INTERVAL ? SECOND)`);
|
|
assert.deepEqual(periodicParams, [240]);
|
|
assert.match(startupQuarantine, /SET status = 'failed'.*'agent_automatic_edit_upgrade'\s+WHERE status = 'queued' AND auto_approve_file_edits = TRUE AND auto_file_edit_generation IS NULL/);
|
|
assert.equal(periodicQuarantine, startupQuarantine);
|
|
}); |