refactor: package-own long task watchdog entrypoint

This commit is contained in:
Eve
2026-05-08 17:38:22 +08:00
parent 6af759eec4
commit 61a32b0857
5 changed files with 199 additions and 276 deletions

View File

@@ -8,13 +8,18 @@ import process from 'node:process';
import { spawnSync } from 'node:child_process';
const ROOT_DIR = path.resolve(import.meta.dirname, '..');
const WATCHDOG_SCRIPT = path.join(ROOT_DIR, 'scripts', 'long_task_watchdog.mjs');
const REPO_SHIM = path.join(ROOT_DIR, 'scripts', 'long_task_watchdog.mjs');
const PACKAGE_ENTRY = path.join(ROOT_DIR, 'plugins', 'reporting-governance', 'scripts', 'long_task_watchdog.mjs');
function createFixtureRunner() {
function createFixtureRunner(scriptPath) {
const fixtureRoot = mkdtempSync(path.join(tmpdir(), 'long-task-watchdog-test-'));
const statePath = path.join(fixtureRoot, 'watchdog-state.json');
const evidenceDir = path.join(fixtureRoot, 'evidence');
const eventDir = path.join(fixtureRoot, 'events');
const notificationDir = path.join(fixtureRoot, 'notifications');
mkdirSync(evidenceDir, { recursive: true });
mkdirSync(eventDir, { recursive: true });
mkdirSync(notificationDir, { recursive: true });
function writeState(content) {
const body = typeof content === 'string' ? content : JSON.stringify(content, null, 2);
@@ -23,10 +28,21 @@ function createFixtureRunner() {
}
function run(args = []) {
const result = spawnSync(process.execPath, [WATCHDOG_SCRIPT, '--state', statePath, '--evidence-dir', evidenceDir, ...args], {
cwd: ROOT_DIR,
encoding: 'utf8',
});
const result = spawnSync(
process.execPath,
[
scriptPath,
'--state', statePath,
'--evidence-dir', evidenceDir,
'--event-dir', eventDir,
'--notification-dir', notificationDir,
...args,
],
{
cwd: ROOT_DIR,
encoding: 'utf8',
},
);
return {
status: result.status,
stdout: result.stdout ?? '',
@@ -42,11 +58,36 @@ function createFixtureRunner() {
return readdirSync(evidenceDir).sort();
}
function listEvents() {
return readdirSync(eventDir).sort();
}
function listNotifications() {
return readdirSync(notificationDir).sort();
}
function readJson(dirPath, fileName) {
return JSON.parse(readFileSync(path.join(dirPath, fileName), 'utf8'));
}
function cleanup() {
rmSync(fixtureRoot, { recursive: true, force: true });
}
return { statePath, evidenceDir, writeState, run, readState, listEvidence, cleanup };
return {
statePath,
evidenceDir,
eventDir,
notificationDir,
writeState,
run,
readState,
listEvidence,
listEvents,
listNotifications,
readJson,
cleanup,
};
}
const tests = [];
@@ -56,8 +97,59 @@ function printResult(prefix, name, detail = '') {
process.stdout.write(`${prefix} ${name}${detail ? ` ${detail}` : ''}\n`);
}
test('inactive watchdogs do not emit evidence', () => {
const runner = createFixtureRunner();
function normalizePayload(payload) {
return {
...payload,
statePath: '<state>',
evidenceDir: '<evidence>',
eventDir: '<events>',
notificationDir: '<notifications>',
result: {
...payload.result,
evidenceWrites: payload.result.evidenceWrites.map((item) => ({ ...item, path: '<evidence>' })),
eventWrites: payload.result.eventWrites.map((item) => ({ ...item, path: '<event>', eventId: '<event-id>' })),
notificationWrites: payload.result.notificationWrites.map((item) => ({ ...item, path: '<notification>', notificationId: '<notification-id>' })),
},
};
}
test('repo shim matches package entrypoint for same overdue watchdog payload', () => {
const shim = createFixtureRunner(REPO_SHIM);
const pkg = createFixtureRunner(PACKAGE_ENTRY);
const state = {
version: 1,
watchdogs: [
{
id: 'reporting-governance-plugin-watchdog',
task: 'reporting-governance plugin spec development',
status: 'active',
ownerSessionKey: 'agent:coder:main',
reportChannel: 'telegram',
reportTarget: '864811879',
intervalMinutes: 10,
lastMilestoneAt: '2026-05-07T08:00:00.000Z',
lastAlertAt: null,
},
],
};
try {
shim.writeState(state);
pkg.writeState(state);
const shimResult = shim.run(['--compact', '--write-state', '--now', '2026-05-07T08:20:00.000Z']);
const pkgResult = pkg.run(['--compact', '--write-state', '--now', '2026-05-07T08:20:00.000Z']);
assert.equal(shimResult.status, 0, shimResult.stderr);
assert.equal(pkgResult.status, 0, pkgResult.stderr);
assert.deepEqual(normalizePayload(JSON.parse(shimResult.stdout)), normalizePayload(JSON.parse(pkgResult.stdout)));
assert.deepEqual(shim.readState(), pkg.readState());
} finally {
shim.cleanup();
pkg.cleanup();
}
});
test('inactive watchdogs do not emit evidence, event, or notification queue items', () => {
const runner = createFixtureRunner(PACKAGE_ENTRY);
try {
runner.writeState({
version: 1,
@@ -76,14 +168,18 @@ test('inactive watchdogs do not emit evidence', () => {
assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(result.stdout);
assert.equal(payload.result.emittedCount, 0);
assert.equal(payload.result.eventCount, 0);
assert.equal(payload.result.notificationCount, 0);
assert.deepEqual(runner.listEvidence(), []);
assert.deepEqual(runner.listEvents(), []);
assert.deepEqual(runner.listNotifications(), []);
} finally {
runner.cleanup();
}
});
test('overdue active watchdog emits external evidence and updates lastAlertAt when write-state is enabled', () => {
const runner = createFixtureRunner();
test('overdue active watchdog emits evidence, canonical event, notification queue item, and updates lastAlertAt', () => {
const runner = createFixtureRunner(PACKAGE_ENTRY);
try {
runner.writeState({
version: 1,
@@ -106,18 +202,39 @@ test('overdue active watchdog emits external evidence and updates lastAlertAt wh
assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(result.stdout);
assert.equal(payload.result.emittedCount, 1);
assert.equal(payload.result.eventCount, 1);
assert.equal(payload.result.notificationCount, 1);
const evidenceFiles = runner.listEvidence();
const eventFiles = runner.listEvents();
const notificationFiles = runner.listNotifications();
assert.equal(evidenceFiles.length, 1);
assert.equal(eventFiles.length, 1);
assert.equal(notificationFiles.length, 1);
const eventPayload = runner.readJson(runner.eventDir, eventFiles[0]);
assert.equal(eventPayload.event_type, 'watchdog_fired');
assert.equal(eventPayload.operator_context.channel, 'telegram');
assert.equal(eventPayload.operator_context.operator_id, '864811879');
const notificationPayload = runner.readJson(runner.notificationDir, notificationFiles[0]);
assert.equal(notificationPayload.kind, 'notify_operator');
assert.equal(notificationPayload.status, 'pending');
assert.equal(notificationPayload.operator_notice.channel, 'telegram');
assert.equal(notificationPayload.operator_notice.target, '864811879');
assert.equal(notificationPayload.blocked_gap, null);
assert.equal(notificationPayload.dispatch_hint.tool, 'message.send');
const nextState = runner.readState();
assert.equal(nextState.watchdogs[0].lastAlertAt, '2026-05-07T08:20:00.000Z');
assert.equal(nextState.watchdogs[0].lastNudgeAt, '2026-05-07T08:20:00.000Z');
} finally {
runner.cleanup();
}
});
test('same interval is not alerted twice once lastAlertAt covers the overdue window', () => {
const runner = createFixtureRunner();
const runner = createFixtureRunner(PACKAGE_ENTRY);
try {
runner.writeState({
version: 1,
@@ -137,7 +254,43 @@ test('same interval is not alerted twice once lastAlertAt covers the overdue win
assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(result.stdout);
assert.equal(payload.result.emittedCount, 0);
assert.equal(payload.result.eventCount, 0);
assert.equal(payload.result.notificationCount, 0);
assert.deepEqual(runner.listEvidence(), []);
assert.deepEqual(runner.listEvents(), []);
assert.deepEqual(runner.listNotifications(), []);
} finally {
runner.cleanup();
}
});
test('notification queue item records blocked gap when report channel/target is missing', () => {
const runner = createFixtureRunner(PACKAGE_ENTRY);
try {
runner.writeState({
version: 1,
watchdogs: [
{
id: 'missing-target-watchdog',
task: 'targetless task',
status: 'active',
ownerSessionKey: 'agent:coder:main',
intervalMinutes: 10,
lastMilestoneAt: '2026-05-07T08:00:00.000Z',
lastAlertAt: null,
},
],
});
const result = runner.run(['--compact', '--now', '2026-05-07T08:20:00.000Z']);
assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse(result.stdout);
assert.equal(payload.result.notificationCount, 1);
assert.equal(payload.result.notificationWrites[0].dispatchReady, false);
const notificationFiles = runner.listNotifications();
const notificationPayload = runner.readJson(runner.notificationDir, notificationFiles[0]);
assert.match(notificationPayload.blocked_gap, /reportChannel\/reportTarget/);
} finally {
runner.cleanup();
}