Files
TORCH/src/lock-utils.mjs
T
google-labs-jules[bot]andPR0M3TH3AN 481d77f33c Refactor lock publisher to use shared error classifier and utils
- Extracted `LockPublisher` and `publishLock` to `src/lock-publisher.mjs` (already existed, but cleaned up).
- Refactored `src/lock-publisher.mjs` to import error constants and classification logic from `src/lock-error-classifier.mjs` instead of duplicating them.
- Refactored `src/lock-publisher.mjs` and `src/lock-utils.mjs` to use `mergeRelayList` from `src/utils.mjs`.
- Consolidated and moved publisher tests from `test/lock-ops.test.mjs` and `test/lock-ops-publish-retry.test.mjs` to `test/lock-publisher.test.mjs`.
- Cleaned up `src/lock-utils.mjs` to remove duplicate `mergeRelayList`.
- Verified changes with tests.

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-21 18:34:18 +00:00

59 lines
1.9 KiB
JavaScript

import fs from 'node:fs/promises';
import path from 'node:path';
import { randomInt } from 'node:crypto';
import { todayDateStr, getIsoWeekStr } from './utils.mjs';
export { relayListLabel, withTimeout, mergeRelayList } from './utils.mjs';
/**
* Scans the local log directory to identify agents that have already completed their task
* for the current period (daily or weekly).
*
* @param {string} cadence - 'daily' or 'weekly'
* @param {string} logDir - Path to the log directory (e.g., 'task-logs')
* @param {Object} deps - Dependency injection for testing
* @returns {Promise<Set<string>>} - Set of agent names that have completed their task
*/
export async function getCompletedAgents(cadence, logDir, deps) {
const { readdir = fs.readdir, getDateStr = todayDateStr, getIsoWeek = getIsoWeekStr } = deps;
const completed = new Set();
const targetDir = path.join(logDir, cadence);
try {
const files = await readdir(targetDir);
const today = getDateStr();
const currentWeek = getIsoWeek();
for (const file of files) {
// Format: YYYY-MM-DDTHH-mm-ssZ__<agent>__<status>.md
const match = file.match(/^(\d{4}-\d{2}-\d{2})T.*__([a-zA-Z0-9-_]+)__(.*)\.md$/);
if (!match) continue;
const [, datePart, agent, status] = match;
if (status === 'completed') {
if (cadence === 'daily' && datePart === today) {
completed.add(agent);
} else if (cadence === 'weekly') {
const fileWeek = getIsoWeek(datePart); // datePart is YYYY-MM-DD
if (fileWeek === currentWeek) {
completed.add(agent);
}
}
}
}
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(`Warning: Failed to read log dir ${targetDir}: ${err.message}`);
}
}
return completed;
}
const MAX_RANDOM = 281474976710655; // 2**48 - 1
export function secureRandom() {
return randomInt(0, MAX_RANDOM) / MAX_RANDOM;
}