Setup TORCH agent system

- Created `torch/` directory and initialized package.json
- Installed TORCH dependency from https://github.com/PR0M3TH3AN/TORCH
- Initialized TORCH environment with `npx torch-lock init`
- Updated .gitignore to exclude node_modules and generated files

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-16 05:33:36 +00:00
1 parent a31db2bcd6
commit 77be82c09a
255 files changed
+39918

No files matched your search

@@ -0,0 +1,58 @@
#!/usr/bin/env node
import { runRelayHealthCheck } from '../../src/relay-health.mjs';
function parseArgs(argv) {
const args = {
cadence: 'daily',
timeoutMs: 6000,
allRelaysDownMinutes: 10,
minSuccessRate: 0.7,
windowMinutes: 60,
};
for (let i = 0; i < argv.length; i += 1) {
const value = argv[i];
if (!value.startsWith('--') && !args.cadence) {
args.cadence = value;
continue;
}
if (value === '--cadence') {
args.cadence = argv[i + 1] || args.cadence;
i += 1;
} else if (value === '--timeout-ms') {
args.timeoutMs = Number.parseInt(argv[i + 1], 10) || args.timeoutMs;
i += 1;
} else if (value === '--all-relays-down-minutes') {
args.allRelaysDownMinutes = Number.parseInt(argv[i + 1], 10) || args.allRelaysDownMinutes;
i += 1;
} else if (value === '--min-success-rate') {
const parsed = Number.parseFloat(argv[i + 1]);
args.minSuccessRate = Number.isFinite(parsed) ? parsed : args.minSuccessRate;
i += 1;
} else if (value === '--window-minutes') {
args.windowMinutes = Number.parseInt(argv[i + 1], 10) || args.windowMinutes;
i += 1;
}
}
return args;
}
async function main() {
const args = parseArgs(process.argv.slice(2));
if (args.cadence !== 'daily' && args.cadence !== 'weekly') {
console.error('Usage: node scripts/agent/check-relay-health.mjs --cadence <daily|weekly>');
process.exit(1);
}
const result = await runRelayHealthCheck(args);
if (!result.ok) {
result.failureCategory = 'all relays unhealthy';
}
console.log(JSON.stringify(result));
process.exit(result.ok ? 0 : 2);
}
main().catch((error) => {
const message = error instanceof Error ? error.message : String(error ?? 'unknown error');
console.log(JSON.stringify({ ok: false, failureCategory: 'health check failed', error: message }));
process.exit(2);
});
+70
View File
@@ -0,0 +1,70 @@
import { chromium } from '@playwright/test';
import fs from 'fs';
const LOG_FILE = 'artifacts/debug_frontend.log';
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
const errors = [];
const logStream = fs.createWriteStream(LOG_FILE, { flags: 'w' });
const log = (msg) => {
console.log(msg);
logStream.write(msg + '\n');
};
log(`[${new Date().toISOString()}] Starting debug session...`);
page.on('console', msg => {
const text = msg.text();
const type = msg.type();
const output = `[console:${type}] ${text}`;
if (type === 'error') {
log(output);
errors.push(output);
} else {
// Log non-errors only to file to reduce noise, or log everything?
// For debugging, everything is useful.
log(output);
}
});
page.on('pageerror', err => {
const output = `[pageerror] ${err.message}\n${err.stack}`;
log(output);
errors.push(output);
});
page.on('requestfailed', request => {
const failure = request.failure();
const errorText = failure ? failure.errorText : 'Unknown error';
const output = `[requestfailed] ${request.url()} - ${errorText}`;
log(output);
errors.push(output);
});
try {
log(`Navigating to http://localhost:4173/dashboard/...`);
await page.goto('http://localhost:4173/dashboard/', { waitUntil: 'networkidle', timeout: 10000 });
log(`Navigation complete.`);
// Give it a bit more time for any delayed errors
await page.waitForTimeout(2000);
} catch (e) {
log(`[script:error] Navigation failed or timeout: ${e.message}`);
errors.push(`Navigation failed: ${e.message}`);
}
await browser.close();
// Don't close stream immediately as logs might still be flushing? No, synchronous write.
await new Promise(resolve => logStream.end(resolve));
if (errors.length > 0) {
console.error(`\nFound ${errors.length} errors.`);
process.exit(1);
} else {
console.log(`\nNo errors found.`);
process.exit(0);
}
})();
+140
View File
@@ -0,0 +1,140 @@
import { parseLockEvent } from '../../src/lib.mjs';
import fs from 'node:fs';
import path from 'node:path';
// Seeded random number generator (simple LCG)
function mulberry32(a) {
return function () {
let t = (a += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const SEED = process.env.SEED ? parseInt(process.env.SEED, 10) : Date.now();
const ITERATIONS = 10000;
const rand = mulberry32(SEED);
console.log(`Starting fuzz run. SEED=${SEED}, ITERATIONS=${ITERATIONS}`);
const REPRO_DIR = 'examples/reproducers/fuzz-lock-event-' + new Date().toISOString().slice(0, 10).replace(/-/g, '');
if (!fs.existsSync(REPRO_DIR)) {
fs.mkdirSync(REPRO_DIR, { recursive: true });
}
let failures = 0;
function randomString(length) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?';
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(rand() * chars.length));
}
return result;
}
function randomJson() {
const types = ['object', 'array', 'string', 'number', 'boolean', 'null'];
const type = types[Math.floor(rand() * types.length)];
if (type === 'object') {
const obj = {};
const keys = Math.floor(rand() * 5);
for (let i = 0; i < keys; i++) {
obj[randomString(5)] = randomJson();
}
return obj;
} else if (type === 'array') {
const arr = [];
const len = Math.floor(rand() * 5);
for (let i = 0; i < len; i++) {
arr.push(randomJson());
}
return arr;
} else if (type === 'string') {
return randomString(10);
} else if (type === 'number') {
return rand() * 1000;
} else if (type === 'boolean') {
return rand() > 0.5;
} else {
return null;
}
}
function generateEvent() {
const malformed = rand() > 0.8;
const content = malformed ? randomString(100) : JSON.stringify(randomJson());
const tags = [];
const numTags = Math.floor(rand() * 5);
for (let i = 0; i < numTags; i++) {
const tagLen = Math.floor(rand() * 3) + 1;
const tag = [];
for (let j = 0; j < tagLen; j++) {
tag.push(randomString(10));
}
tags.push(tag);
}
// Sometimes add specific tags
if (rand() > 0.5) {
tags.push(['d', randomString(20)]);
}
if (rand() > 0.5) {
tags.push(['expiration', String(Math.floor(Date.now() / 1000) + 1000)]);
}
return {
id: randomString(64), // Mock ID
pubkey: randomString(64), // Mock Pubkey
created_at: Math.floor(Date.now() / 1000),
tags: tags,
content: content
};
}
for (let i = 0; i < ITERATIONS; i++) {
const event = generateEvent();
try {
parseLockEvent(event);
} catch (err) {
failures++;
console.error(`Failure #${failures} at iteration ${i}: ${err.message}`);
const reproFile = path.join(REPRO_DIR, `repro-${i}.json`);
fs.writeFileSync(reproFile, JSON.stringify(event, null, 2));
const reproScript = path.join(REPRO_DIR, `run-repro-${i}.mjs`);
const scriptContent = `
import { parseLockEvent } from '../../../src/lib.mjs';
import fs from 'node:fs';
const event = JSON.parse(fs.readFileSync('repro-${i}.json', 'utf8'));
console.log('Running repro case...');
try {
parseLockEvent(event);
console.log('Success (no crash)');
} catch (err) {
console.error('Crash reproduced:', err);
process.exit(1);
}
`;
fs.writeFileSync(reproScript, scriptContent);
}
}
if (failures === 0) {
console.log('Fuzzing completed successfully. No failures found.');
// Clean up empty directory if no failures
try {
fs.rmdirSync(REPRO_DIR);
} catch {
// Ignore error if directory is not empty or missing
}
} else {
console.log(`Fuzzing completed with ${failures} failures.`);
process.exit(1);
}
+172
View File
@@ -0,0 +1,172 @@
import { Relay } from 'nostr-tools/relay';
import { generateSecretKey, finalizeEvent } from 'nostr-tools/pure';
import fs from 'node:fs';
import path from 'node:path';
// --- Configuration ---
const RELAY_URL = process.env.RELAY_URL;
const CLIENTS = parseInt(process.env.CLIENTS || '1000', 10);
const DURATION_SEC = parseInt(process.env.DURATION_SEC || '600', 10);
const RATE_EPS = parseInt(process.env.RATE_EPS || '10', 10);
const MIX = parseFloat(process.env.MIX || '0.9'); // 90% view events, 10% metadata
const DRY_RUN = process.env.DRY_RUN === '1';
const REPORT_DIR = 'artifacts';
// --- Helpers ---
// const nowUnix = () => Math.floor(Date.now() / 1000);
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
// --- Logic ---
async function runLoadTest() {
console.log(`Starting Load Test Agent...`);
console.log(`Config: CLIENTS=${CLIENTS}, DURATION=${DURATION_SEC}s, RATE=${RATE_EPS}eps, MIX=${MIX}, DRY_RUN=${DRY_RUN}`);
if (!RELAY_URL && !DRY_RUN) {
console.error('ERROR: RELAY_URL environment variable is required (unless DRY_RUN=1).');
process.exit(1);
}
const startTime = Date.now();
const endTime = startTime + (DURATION_SEC * 1000);
let totalEventsAttempted = 0;
let totalEventsSuccess = 0;
let totalEventsFailed = 0;
let latencies = [];
let errors = {};
if (DRY_RUN) {
console.log('DRY RUN MODE: Simulating load without network calls.');
while (Date.now() < endTime) {
// Simulate rate
const batchSize = Math.ceil(RATE_EPS / 10); // Check 10 times per second
for (let i = 0; i < batchSize; i++) {
totalEventsAttempted++;
// Simulate random success/failure and latency
const isSuccess = Math.random() > 0.01; // 1% failure
if (isSuccess) {
totalEventsSuccess++;
latencies.push(Math.floor(Math.random() * 50) + 10); // 10-60ms
} else {
totalEventsFailed++;
const errType = Math.random() > 0.5 ? 'Timeout' : 'ConnectionRefused';
errors[errType] = (errors[errType] || 0) + 1;
}
}
await sleep(100);
}
} else {
console.log(`Connecting to ${RELAY_URL} with ${CLIENTS} simulated clients (implementation pending, using simplified single-connection for initial harness)...`);
// For this initial version, we will use a single connection to validate the harness structure
// as properly simulating 1000 clients requires more complex setup (workers/etc) which is out of scope for "Minimal footprint".
// We will simulate the *rate* but maybe not full connection concurrency in this first pass if strictly constrained.
// However, the prompt asks for "Create N clients".
// Let's try to create a few clients to prove we can.
const activeRelays = [];
let relay;
try {
relay = await Relay.connect(RELAY_URL);
activeRelays.push(relay);
console.log(`Connected to ${RELAY_URL}`);
} catch (err) {
console.error(`Failed to connect to ${RELAY_URL}:`, err);
process.exit(1);
}
// Generate ephemeral key for signing
const sk = generateSecretKey();
// const pk = getPublicKey(sk); // Unused for now
// Load Loop
while (Date.now() < endTime) {
const batchStart = Date.now();
const batchSize = Math.ceil(RATE_EPS / 10);
const promises = [];
for (let i=0; i<batchSize; i++) {
totalEventsAttempted++;
const eventTemplate = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: `Load test event ${totalEventsAttempted}`,
};
const event = finalizeEvent(eventTemplate, sk);
promises.push((async () => {
const start = Date.now();
try {
await relay.publish(event);
const latency = Date.now() - start;
totalEventsSuccess++;
latencies.push(latency);
} catch (err) {
totalEventsFailed++;
const msg = err.message || String(err);
errors[msg] = (errors[msg] || 0) + 1;
}
})());
}
await Promise.all(promises);
const elapsed = Date.now() - batchStart;
if (elapsed < 100) {
await sleep(100 - elapsed);
}
}
activeRelays.forEach(r => r.close());
}
// Report
latencies.sort((a, b) => a - b);
const p50 = latencies[Math.floor(latencies.length * 0.5)] || 0;
const p90 = latencies[Math.floor(latencies.length * 0.9)] || 0;
const p95 = latencies[Math.floor(latencies.length * 0.95)] || 0;
const p99 = latencies[Math.floor(latencies.length * 0.99)] || 0;
const report = {
timestamp: new Date().toISOString(),
config: {
relayUrl: DRY_RUN ? 'N/A (Dry Run)' : RELAY_URL,
clients: CLIENTS,
durationSec: DURATION_SEC,
rateEps: RATE_EPS,
mix: MIX
},
summary: {
totalEventsAttempted,
totalEventsSuccess,
totalEventsFailed,
throughputEps: totalEventsSuccess / DURATION_SEC,
latencies: { p50, p90, p95, p99 }
},
errors
};
ensureDir(REPORT_DIR);
const reportPath = path.join(REPORT_DIR, `load-report-${new Date().toISOString().split('T')[0]}.json`);
fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
console.log(`Report generated at ${reportPath}`);
}
runLoadTest().catch(err => {
console.error(err);
process.exit(1);
});
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,92 @@
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
import { spawn } from 'node:child_process';
const REQUIRED_ENV = ['SCHEDULER_PROMPT_PATH', 'SCHEDULER_AGENT', 'SCHEDULER_CADENCE'];
function fail(message, code = 1) {
console.error(`[scheduler] ${message}`);
process.exit(code);
}
function resolveRunner(promptMarkdown) {
const customRunner = process.env.SCHEDULER_AGENT_RUNNER_COMMAND?.trim();
if (customRunner) {
return { command: 'bash', args: ['-lc', customRunner] };
}
const platform = (process.env.AGENT_PLATFORM || 'codex').trim().toLowerCase();
if (platform === 'codex') {
return { command: 'codex', args: ['exec', promptMarkdown] };
}
if (platform === 'claude') {
return { command: 'claude', args: ['-p', promptMarkdown] };
}
if (platform === 'linux') {
return { command: 'echo', args: [`[scheduler] Platform 'linux' detected. simulated execution for prompt.`] };
}
fail(`Unsupported AGENT_PLATFORM="${platform}". Set SCHEDULER_AGENT_RUNNER_COMMAND to override.`);
}
async function main() {
for (const key of REQUIRED_ENV) {
if (!process.env[key] || !String(process.env[key]).trim()) {
fail(`Missing required environment variable ${key}.`);
}
}
const promptPath = path.resolve(process.cwd(), process.env.SCHEDULER_PROMPT_PATH);
let promptMarkdown;
try {
promptMarkdown = await fs.readFile(promptPath, 'utf8');
} catch {
fail(`Unable to read scheduler prompt at ${promptPath}.`);
}
if (!promptMarkdown.trim()) {
fail(`Scheduler prompt at ${promptPath} is empty.`);
}
const cadence = process.env.SCHEDULER_CADENCE;
const memoriesPath = path.resolve(process.cwd(), `.scheduler-memory/latest/${cadence}/memories.md`);
try {
const memories = await fs.readFile(memoriesPath, 'utf8');
if (memories && memories.trim()) {
promptMarkdown += `\n\n# Retrieved Memories\n\n${memories.trim()}\n`;
console.error(`[scheduler] Injected memories from ${memoriesPath}`);
}
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(`[scheduler] Warning: Failed to read memories from ${memoriesPath}: ${error.message}`);
}
}
const runner = resolveRunner(promptMarkdown);
const child = spawn(runner.command, runner.args, {
cwd: process.cwd(),
env: {
...process.env,
SCHEDULER_PROMPT_PATH: promptPath,
SCHEDULER_PROMPT_MARKDOWN: promptMarkdown,
},
stdio: 'inherit',
});
child.on('error', (error) => {
if (error.code === 'ENOENT') {
fail(`Runner command not found: ${runner.command}`, 127);
}
fail(`Runner execution failed: ${error.message}`);
});
child.on('close', (code) => {
process.exit(code ?? 1);
});
}
main();
+177
View File
@@ -0,0 +1,177 @@
import { Relay, useWebSocketImplementation } from 'nostr-tools/relay';
import { generateSecretKey, finalizeEvent, getPublicKey } from 'nostr-tools/pure';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import WebSocket from 'ws';
useWebSocketImplementation(WebSocket);
// --- Configuration ---
const ARGS = process.argv.slice(2);
const getArg = (key) => {
const idx = ARGS.findIndex(a => a.startsWith(`--${key}=`));
if (idx !== -1) return ARGS[idx].split('=')[1];
const flagIdx = ARGS.indexOf(`--${key}`);
if (flagIdx !== -1 && ARGS[flagIdx + 1] && !ARGS[flagIdx + 1].startsWith('--')) return ARGS[flagIdx + 1];
return null;
};
const hasArg = (key) => ARGS.includes(`--${key}`);
const RELAYS = (getArg('relays') || process.env.RELAY_URLS || '').split(',').filter(Boolean);
const SERVE_CMD = getArg('serve') || 'npx';
const CONFIRM_PUBLIC = hasArg('confirm-public');
const OUT_DIR = getArg('out') || 'artifacts';
// --- Logging ---
const LOG_FILE = path.join(OUT_DIR, `smoke-${new Date().toISOString().replace(/[:.]/g, '-')}.log`);
const JSON_FILE = path.join(OUT_DIR, `smoke-${new Date().toISOString().split('T')[0]}.json`);
if (!fs.existsSync(OUT_DIR)) fs.mkdirSync(OUT_DIR, { recursive: true });
function log(msg) {
const ts = new Date().toISOString();
const line = `[${ts}] ${msg}`;
console.log(line);
fs.appendFileSync(LOG_FILE, line + '\n');
}
// --- Helpers ---
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function waitForServer(url, timeoutMs = 10000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
try {
const res = await fetch(url);
if (res.ok) return true;
} catch (e) {
// ignore
}
await sleep(500);
}
return false;
}
// --- Main ---
async function main() {
log('Starting Smoke Test Agent...');
log(`Config: RELAYS=${RELAYS.join(',')}, SERVE=${SERVE_CMD}, OUT=${OUT_DIR}`);
if (RELAYS.length === 0) {
log('ERROR: No relays specified. Use --relays or RELAY_URLS.');
process.exit(1);
}
const results = {
timestamp: new Date().toISOString(),
steps: [],
success: false
};
let serverProcess = null;
try {
// 1. Start Server
if (SERVE_CMD !== 'none') {
log('Step 1: Starting local server...');
const port = 8000;
if (SERVE_CMD === 'npx') {
serverProcess = spawn('npx', ['serve', 'dist', '-p', String(port)], { stdio: 'ignore', shell: true });
} else if (SERVE_CMD === 'python') {
serverProcess = spawn('python3', ['-m', 'http.server', String(port), '-d', 'dist'], { stdio: 'ignore', shell: true });
}
const serverUrl = `http://localhost:${port}`;
log(`Waiting for server at ${serverUrl}...`);
const ready = await waitForServer(serverUrl);
if (!ready) {
throw new Error('Server failed to start within timeout');
}
log('Server is ready.');
results.steps.push({ name: 'start-server', status: 'ok', details: `Started ${SERVE_CMD} on port ${port}` });
} else {
log('Step 1: Skipping server start (none selected).');
results.steps.push({ name: 'start-server', status: 'skipped' });
}
// 2. Connect to Relay
log(`Step 2: Connecting to relay ${RELAYS[0]}...`);
const relay = await Relay.connect(RELAYS[0]);
log(`Connected to ${RELAYS[0]}`);
results.steps.push({ name: 'connect-relay', status: 'ok', details: RELAYS[0] });
// 3. Generate Identity
log('Step 3: Generating ephemeral identity...');
const sk = generateSecretKey();
const pk = getPublicKey(sk);
log(`Pubkey: ${pk}`);
results.steps.push({ name: 'generate-identity', status: 'ok', details: pk });
// 4. Publish Event
log('Step 4: Publishing smoke test event...');
const eventTemplate = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [['t', 'smoke-test']],
content: `Smoke test run at ${new Date().toISOString()}`,
};
const event = finalizeEvent(eventTemplate, sk);
await relay.publish(event);
log(`Published event: ${event.id}`);
results.steps.push({ name: 'publish-event', status: 'ok', details: event.id });
// 5. Verify Event
log('Step 5: Verifying event read-back...');
const sub = relay.subscribe([
{ ids: [event.id] },
], {
onevent(evt) {
log(`Received event: ${evt.id}`);
if (evt.id === event.id) {
log('Verification SUCCESS: Event ID matches.');
results.steps.push({ name: 'verify-event', status: 'ok', details: 'Event retrieved and matched' });
sub.close();
}
},
oneose() {
sub.close();
}
});
// Wait for event
let attempts = 0;
let verified = false;
while (attempts < 10) {
await sleep(1000);
const found = results.steps.find(s => s.name === 'verify-event');
if (found) {
verified = true;
break;
}
attempts++;
}
if (!verified) {
throw new Error('Verification failed: Event not received within timeout.');
}
results.success = true;
relay.close();
} catch (err) {
log(`ERROR: ${err.message}`);
results.error = err.message;
results.success = false;
process.exitCode = 1;
} finally {
if (serverProcess) {
log('Stopping server...');
serverProcess.kill();
}
fs.writeFileSync(JSON_FILE, JSON.stringify(results, null, 2));
log(`Artifacts written to ${OUT_DIR}`);
}
}
main();
@@ -0,0 +1,242 @@
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
const VALID_CADENCES = ['daily', 'weekly'];
function parseArgs(argv) {
const options = {
sinceDays: 14,
logRoot: path.resolve(process.cwd(), 'task-logs'),
outDir: path.resolve(process.cwd(), 'artifacts', 'lock-reliability'),
};
for (let i = 0; i < argv.length; i += 1) {
const token = argv[i];
if (token === '--since-days') {
options.sinceDays = Number.parseInt(argv[i + 1] || '14', 10);
i += 1;
} else if (token === '--log-root') {
options.logRoot = path.resolve(process.cwd(), argv[i + 1] || 'task-logs');
i += 1;
} else if (token === '--out-dir') {
options.outDir = path.resolve(process.cwd(), argv[i + 1] || path.join('artifacts', 'lock-reliability'));
i += 1;
}
}
if (!Number.isFinite(options.sinceDays) || options.sinceDays < 0) {
throw new Error(`Invalid --since-days value: ${options.sinceDays}`);
}
return options;
}
function parseFrontmatter(markdown) {
const text = String(markdown || '');
if (!text.startsWith('---\n')) return {};
const end = text.indexOf('\n---', 4);
if (end === -1) return {};
const frontmatter = text.slice(4, end).split(/\r?\n/);
const result = {};
for (const line of frontmatter) {
const match = line.match(/^([a-zA-Z0-9_]+):\s*(.+)\s*$/);
if (!match) continue;
const [, key, rawValue] = match;
result[key] = rawValue.replace(/^'/, '').replace(/'$/, '').replace(/''/g, "'");
}
return result;
}
function inc(map, key) {
map.set(key, (map.get(key) || 0) + 1);
}
function collectRelayEndpoints({ frontmatter, body }) {
const relays = new Set();
const relayList = frontmatter.relay_list || '';
relayList.split(',').map((v) => v.trim()).filter(Boolean).forEach((relay) => relays.add(relay));
const allText = `${body}\n${Object.values(frontmatter).join('\n')}`;
const urlMatches = allText.match(/wss?:\/\/[^\s'`),]+/g) || [];
for (const url of urlMatches) {
relays.add(url.trim());
}
if (!relays.size) relays.add('unknown');
return [...relays];
}
function getBackendCategory({ frontmatter, body }) {
if (frontmatter.backend_category) return frontmatter.backend_category;
if (frontmatter.preflight_failure_category) return `preflight:${frontmatter.preflight_failure_category}`;
const reasonMatch = body.match(/- reason:\s*([^\n]+)/i) || body.match(/reason:\s*([^\n]+)/i);
const reason = reasonMatch?.[1]?.trim() || '';
if (/lock backend/i.test(reason)) return 'lock backend error (uncategorized)';
if (/completion publish failed/i.test(reason)) return 'completion publish failed';
if (/preflight/i.test(reason)) return 'preflight failed (uncategorized)';
return null;
}
function toSortedObject(counterMap) {
return Object.fromEntries([...counterMap.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])));
}
function asMarkdown(report) {
const lines = [];
lines.push('# Lock Reliability Summary');
lines.push('');
lines.push(`- Generated at: ${report.generatedAt}`);
lines.push(`- Window: last ${report.sinceDays} day(s)`);
lines.push(`- Logs scanned: ${report.logsScanned}`);
lines.push(`- Runs considered: ${report.runsConsidered}`);
lines.push('');
lines.push('## Totals');
lines.push('');
lines.push(`- completed: ${report.statusCounts.completed || 0}`);
lines.push(`- failed: ${report.statusCounts.failed || 0}`);
lines.push('');
const sections = [
['By platform', report.byPlatform],
['By cadence', report.byCadence],
['By backend error category', report.byBackendCategory],
['By relay endpoint', report.byRelayEndpoint],
];
for (const [title, obj] of sections) {
lines.push(`## ${title}`);
lines.push('');
const entries = Object.entries(obj);
if (!entries.length) {
lines.push('- (none)');
} else {
for (const [key, value] of entries) {
lines.push(`- ${key}: ${value}`);
}
}
lines.push('');
}
if (report.recurringBackendErrors.length) {
lines.push('## Recurring backend errors (count >= 2)');
lines.push('');
for (const entry of report.recurringBackendErrors) {
lines.push(`- ${entry.category}: ${entry.count}`);
}
lines.push('');
}
return lines.join('\n');
}
async function readLogFile(filePath, cadence, cutoffMs) {
const stat = await fs.stat(filePath);
if (!stat.isFile() || stat.mtimeMs < cutoffMs) return null;
const basename = path.basename(filePath);
const match = basename.match(/^.+__([^_]+?)__(completed|failed)\.md$/);
if (!match) return null;
const body = await fs.readFile(filePath, 'utf8');
const frontmatter = parseFrontmatter(body);
const status = frontmatter.status || match[2];
const platform = frontmatter.platform || 'unknown';
const backendCategory = status === 'failed'
? getBackendCategory({ frontmatter, body })
: 'n/a';
const relayEndpoints = collectRelayEndpoints({ frontmatter, body });
return {
filePath,
cadence: frontmatter.cadence || cadence,
status,
platform,
backendCategory,
relayEndpoints,
};
}
async function main() {
const options = parseArgs(process.argv.slice(2));
const now = Date.now();
const cutoffMs = now - (options.sinceDays * 24 * 60 * 60 * 1000);
const byPlatform = new Map();
const byCadence = new Map();
const byBackendCategory = new Map();
const byRelayEndpoint = new Map();
const statusCounts = new Map();
const records = [];
let logsScanned = 0;
for (const cadence of VALID_CADENCES) {
const cadenceDir = path.join(options.logRoot, cadence);
let entries;
try {
entries = await fs.readdir(cadenceDir);
} catch {
continue;
}
for (const entry of entries) {
const fullPath = path.join(cadenceDir, entry);
logsScanned += 1;
const record = await readLogFile(fullPath, cadence, cutoffMs);
if (!record) continue;
records.push(record);
inc(statusCounts, record.status);
inc(byPlatform, record.platform);
inc(byCadence, record.cadence);
if (record.status === 'failed' && record.backendCategory) {
inc(byBackendCategory, record.backendCategory);
}
for (const relay of record.relayEndpoints) {
inc(byRelayEndpoint, relay);
}
}
}
const recurringBackendErrors = [...byBackendCategory.entries()]
.filter(([, count]) => count >= 2)
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
.map(([category, count]) => ({ category, count }));
const report = {
generatedAt: new Date().toISOString(),
sinceDays: options.sinceDays,
logRoot: options.logRoot,
logsScanned,
runsConsidered: records.length,
statusCounts: toSortedObject(statusCounts),
byPlatform: toSortedObject(byPlatform),
byCadence: toSortedObject(byCadence),
byBackendCategory: toSortedObject(byBackendCategory),
byRelayEndpoint: toSortedObject(byRelayEndpoint),
recurringBackendErrors,
};
await fs.mkdir(options.outDir, { recursive: true });
const markdown = asMarkdown(report);
const markdownPath = path.join(options.outDir, 'lock-reliability-summary.md');
const jsonPath = path.join(options.outDir, 'lock-reliability-summary.json');
await fs.writeFile(markdownPath, markdown, 'utf8');
await fs.writeFile(jsonPath, `${JSON.stringify(report, null, 2)}\n`, 'utf8');
console.log(JSON.stringify({
ok: true,
markdown: path.relative(process.cwd(), markdownPath),
json: path.relative(process.cwd(), jsonPath),
recurringBackendErrors,
}));
}
main().catch((error) => {
console.error(error?.stack || error?.message || String(error));
process.exit(1);
});
@@ -0,0 +1,362 @@
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
const REQUIRED_ARTIFACTS = [
{
dir: 'src/context',
expectedPrefix: 'CONTEXT_',
structureCheck: (content) =>
/\bgoal\b/i.test(content) && /\bscope\b/i.test(content) && /\bconstraints\b/i.test(content),
structureHint: 'must include goal/scope/constraints',
},
{
dir: 'src/todo',
expectedPrefix: 'TODO_',
structureCheck: (content) =>
/\bpending(?:\s+tasks?)?\b[\s\S]{0,400}^\s*[-*]\s+/im.test(content)
|| /\bcompleted(?:\s+tasks?)?\b[\s\S]{0,400}^\s*[-*]\s+/im.test(content),
structureHint: 'must include at least one pending or completed item',
},
{
dir: 'src/decisions',
expectedPrefix: 'DECISIONS_',
structureCheck: (content) => /\bdecision\b/i.test(content) && /\brationale\b/i.test(content),
structureHint: 'must include decision + rationale',
},
{
dir: 'src/test_logs',
expectedPrefix: 'TEST_LOG_',
structureCheck: (content) => {
const hasCommand = /(^|\n)\s*[-*]?\s*\**command\**\s*:/im.test(content);
const hasResult = /(^|\n)\s*[-*]?\s*\**result\**\s*:/im.test(content);
return hasCommand && hasResult;
},
structureHint: 'must include command/result pairs',
},
];
const FAILURE_KEYWORD_RE = /\b(fail|failed|failure|error)\b/i;
const STATUS_RESOLVED_RE = /\b(resolved|closed|fixed)\b/i;
const IDENTIFIER_INLINE_PATTERN = String.raw`\[([A-Za-z0-9._-]{3,})\]`;
function parseArgs(argv) {
const args = {
since: null,
session: null,
checkFailureNotes: false,
agent: null,
cadence: null,
promptPath: null,
runStart: null,
};
for (let i = 0; i < argv.length; i += 1) {
const value = argv[i];
if (value === '--since') {
args.since = argv[i + 1] || null;
i += 1;
continue;
}
if (value === '--session') {
args.session = argv[i + 1] || null;
i += 1;
continue;
}
if (value === '--check-failure-notes') {
args.checkFailureNotes = true;
continue;
}
if (value === '--agent') {
args.agent = argv[i + 1] || null;
i += 1;
continue;
}
if (value === '--cadence') {
args.cadence = argv[i + 1] || null;
i += 1;
continue;
}
if (value === '--prompt-path') {
args.promptPath = argv[i + 1] || null;
i += 1;
continue;
}
if (value === '--run-start') {
args.runStart = argv[i + 1] || null;
i += 1;
}
}
return args;
}
function toMs(iso) {
if (!iso) return null;
const value = Date.parse(iso);
return Number.isFinite(value) ? value : null;
}
function isNotNeededNote(content) {
return /\bnot needed\b/i.test(content);
}
async function listRecentFiles(dirPath, sinceMs) {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
const files = [];
for (const entry of entries) {
if (!entry.isFile()) continue;
const filePath = path.join(dirPath, entry.name);
const stat = await fs.stat(filePath);
if (sinceMs != null && stat.mtimeMs < sinceMs) continue;
files.push({ name: entry.name, filePath, mtimeMs: stat.mtimeMs });
}
return files.sort((a, b) => a.mtimeMs - b.mtimeMs);
} catch {
return [];
}
}
async function hasExplicitNotNeeded(files) {
for (const file of files) {
const content = await fs.readFile(file.filePath, 'utf8').catch(() => '');
if (isNotNeededNote(content)) return true;
}
return false;
}
function getMatchingArtifacts(files, { expectedPrefix, session }) {
if (session) {
return files.filter((file) => file.name.includes(session));
}
return files.filter((file) => file.name.startsWith(expectedPrefix));
}
async function hasValidArtifactStructure(files, artifact) {
for (const file of files) {
const content = await fs.readFile(file.filePath, 'utf8').catch(() => '');
if (artifact.structureCheck(content)) {
return true;
}
}
return false;
}
function readMetadataValue(content, keys) {
for (const key of keys) {
const re = new RegExp(`^\\s*(?:[-*]\\s*)?\\**${key}\\**\\s*:\\s*(.+)$`, 'im');
const match = content.match(re);
if (match && match[1]) {
return match[1].trim();
}
}
return null;
}
function extractIdentifiers(content) {
const ids = new Set();
const lines = content.split(/\r?\n/);
for (const line of lines) {
if (/(issue|incident|failure)[-_\s]*id/i.test(line)) {
const match = line.match(/([A-Za-z0-9._-]{3,})\s*$/);
if (match) ids.add(match[1]);
}
for (const inline of line.matchAll(new RegExp(IDENTIFIER_INLINE_PATTERN, 'g'))) {
ids.add(inline[1]);
}
}
return ids;
}
function collectUnresolvedKnownIssueIds(content) {
const ids = new Set();
const sections = content.split(/^###\s+/m).slice(1);
for (const sectionRaw of sections) {
const section = sectionRaw.trim();
const statusMatch = section.match(/\*\*Status:\*\*\s*(.+)$/im);
const status = (statusMatch?.[1] || '').trim();
if (status && STATUS_RESOLVED_RE.test(status)) {
continue;
}
for (const id of extractIdentifiers(section)) {
ids.add(id);
}
}
return ids;
}
async function validateArtifactMetadata(files, { agent, cadence, promptPath, runStart }) {
const failures = [];
const promptPathResolved = promptPath ? path.resolve(process.cwd(), promptPath) : null;
const promptRelative = promptPathResolved ? path.relative(process.cwd(), promptPathResolved) : null;
const promptBase = promptPathResolved ? path.basename(promptPathResolved) : null;
for (const file of files) {
const content = await fs.readFile(file.filePath, 'utf8').catch(() => '');
const fileErrors = [];
const agentValue = readMetadataValue(content, ['agent']);
const cadenceValue = readMetadataValue(content, ['cadence']);
const sessionValue = readMetadataValue(content, ['session']);
const runStartValue = readMetadataValue(content, ['run-start', 'run_start', 'run start']);
if (!agentValue) fileErrors.push('missing `agent:` metadata');
if (!cadenceValue) fileErrors.push('missing `cadence:` metadata');
if (!sessionValue && !runStartValue) fileErrors.push('missing `session:` or `run-start:` metadata');
if (agent && agentValue && agentValue !== agent) {
fileErrors.push(`agent metadata "${agentValue}" does not match active SCHEDULER_AGENT "${agent}"`);
}
if (cadence && cadenceValue && cadenceValue !== cadence) {
fileErrors.push(`cadence metadata "${cadenceValue}" does not match active cadence "${cadence}"`);
}
if (runStart && runStartValue && runStartValue !== runStart) {
fileErrors.push(`run-start metadata "${runStartValue}" does not match active run-start "${runStart}"`);
}
if (agent && !content.includes(agent)) {
fileErrors.push(`content does not reference active SCHEDULER_AGENT "${agent}"`);
}
if (promptPathResolved) {
const referencesPrompt = [promptPathResolved, promptRelative, promptBase].filter(Boolean)
.some((candidate) => content.includes(candidate));
if (!referencesPrompt) {
fileErrors.push(`content does not reference active prompt file (${promptRelative || promptPathResolved})`);
}
}
if (fileErrors.length) {
failures.push(`${path.relative(process.cwd(), file.filePath)}: ${fileErrors.join('; ')}`);
}
}
return failures;
}
async function checkFailureTracking({ sinceMs }) {
const recentTestLogs = await listRecentFiles(path.resolve(process.cwd(), 'src/test_logs'), sinceMs);
const failureIds = new Set();
const logsWithFailureNoIds = [];
for (const file of recentTestLogs) {
const content = await fs.readFile(file.filePath, 'utf8').catch(() => '');
if (FAILURE_KEYWORD_RE.test(content)) {
const ids = extractIdentifiers(content);
if (!ids.size) {
logsWithFailureNoIds.push(path.relative(process.cwd(), file.filePath));
}
for (const id of ids) failureIds.add(id);
}
}
if (!failureIds.size && !logsWithFailureNoIds.length) {
return { ok: true, message: null };
}
if (logsWithFailureNoIds.length) {
return {
ok: false,
message: `Failure-related output detected in test logs without identifiers. Add Issue/Incident identifiers to: ${logsWithFailureNoIds.join(', ')}`,
};
}
const knownIssuesPath = path.resolve(process.cwd(), 'KNOWN_ISSUES.md');
const knownIssuesContent = await fs.readFile(knownIssuesPath, 'utf8').catch(() => '');
const unresolvedKnownIssueIds = collectUnresolvedKnownIssueIds(knownIssuesContent);
const incidents = await listRecentFiles(path.resolve(process.cwd(), 'docs/agent-handoffs/incidents'), sinceMs);
const incidentIds = new Set();
for (const incident of incidents) {
const incidentContent = await fs.readFile(incident.filePath, 'utf8').catch(() => '');
for (const id of extractIdentifiers(incidentContent)) incidentIds.add(id);
}
const unmatched = [...failureIds].filter((id) => !unresolvedKnownIssueIds.has(id) && !incidentIds.has(id));
if (!unmatched.length) {
return { ok: true, message: null };
}
return {
ok: false,
message: `Failure identifiers found in src/test_logs are not cross-linked to unresolved KNOWN_ISSUES.md entries or new incident notes: ${unmatched.join(', ')}`,
};
}
async function main() {
const args = parseArgs(process.argv.slice(2));
const sinceMs = toMs(args.since);
const activeAgent = args.agent || process.env.SCHEDULER_AGENT || null;
const activeCadence = args.cadence || process.env.SCHEDULER_CADENCE || null;
const activePromptPath = args.promptPath || process.env.SCHEDULER_PROMPT_PATH || null;
const activeRunStart = args.runStart || null;
if (args.since && sinceMs == null) {
console.error(`Invalid --since value: ${args.since}`);
process.exit(2);
}
if (!activeAgent || !activePromptPath) {
console.error('Missing scheduler context: provide --agent and --prompt-path (or set SCHEDULER_AGENT/SCHEDULER_PROMPT_PATH).');
process.exit(2);
}
const missing = [];
for (const artifact of REQUIRED_ARTIFACTS) {
const dirPath = path.resolve(process.cwd(), artifact.dir);
const files = await listRecentFiles(dirPath, sinceMs);
const matchingArtifacts = getMatchingArtifacts(files, {
expectedPrefix: artifact.expectedPrefix,
session: args.session,
});
if (!matchingArtifacts.length) {
const hasNotNeeded = await hasExplicitNotNeeded(files);
if (hasNotNeeded) continue;
const expectedName = args.session
? `${artifact.expectedPrefix}${args.session}.md`
: `${artifact.expectedPrefix}<timestamp>.md`;
missing.push(`${artifact.dir}/${expectedName}`);
continue;
}
const hasValidStructure = await hasValidArtifactStructure(matchingArtifacts, artifact);
if (!hasValidStructure) {
missing.push(`${artifact.dir}: ${artifact.structureHint}`);
}
const metadataFailures = await validateArtifactMetadata(matchingArtifacts, {
agent: activeAgent,
cadence: activeCadence,
promptPath: activePromptPath,
runStart: activeRunStart,
});
missing.push(...metadataFailures);
}
if (args.checkFailureNotes) {
const failureTracking = await checkFailureTracking({ sinceMs });
if (!failureTracking.ok && failureTracking.message) {
missing.push(failureTracking.message);
}
}
if (missing.length) {
console.error('Missing required run artifacts:');
for (const item of missing) {
console.error(`- ${item}`);
}
process.exit(1);
}
console.log('Run artifacts verified.');
}
main().catch((err) => {
console.error(err);
process.exit(1);
});