mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-11-02 10:51:54 +00:00
refactor(core): extract parseArgs to cli-parser.mjs
- Extracted `parseArgs` function from `src/lib.mjs` to `src/cli-parser.mjs`. - Added unit tests for argument parsing logic in `test/cli-parser.test.mjs`. - Updated `src/lib.mjs` to import `parseArgs`. - Fixed roster drift in `src/prompts/weekly-scheduler.md` to match `roster.json`. - Verified with `npm test` and `npm run lint`. - Completed weekly scheduler task for `refactor-agent`. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
2ff1c36991
commit
dba1d43c94
9 files changed
+258
-115
No files matched your search
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"cadence": "weekly",
|
||||||
|
"operation": "retrieve",
|
||||||
|
"runId": "session-mlnww6r0",
|
||||||
|
"servicePath": "src/services/memory/index.js#getRelevantMemories",
|
||||||
|
"inputs": {
|
||||||
|
"agentId": "refactor-agent",
|
||||||
|
"promptPath": "",
|
||||||
|
"promptIntent": "scheduler memory retrieval",
|
||||||
|
"query": "scheduler | weekly | agent | refactor-agent | intent | scheduler memory retrieval",
|
||||||
|
"events": 1
|
||||||
|
},
|
||||||
|
"outputs": {
|
||||||
|
"ingestedCount": 1,
|
||||||
|
"retrievedCount": 1
|
||||||
|
},
|
||||||
|
"status": "ok"
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
MEMORY_RETRIEVED
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"cadence": "weekly",
|
||||||
|
"operation": "store",
|
||||||
|
"runId": "session-mlnx056f",
|
||||||
|
"servicePath": "src/services/memory/index.js#ingestEvents",
|
||||||
|
"inputs": {
|
||||||
|
"agentId": "refactor-agent",
|
||||||
|
"promptPath": "",
|
||||||
|
"promptIntent": "scheduler memory store",
|
||||||
|
"events": 2
|
||||||
|
},
|
||||||
|
"outputs": {
|
||||||
|
"storedCount": 1,
|
||||||
|
"summaries": [
|
||||||
|
"Store memory event A for weekly :: scheduler memory store Store memory event B for weekly :: scheduler memory store"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"status": "ok"
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
MEMORY_STORED
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import { DEFAULT_DASHBOARD_PORT } from './constants.mjs';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses command-line arguments into a structured object.
|
||||||
|
*
|
||||||
|
* @param {string[]} argv - Arguments from process.argv.slice(2)
|
||||||
|
* @returns {Object} - Parsed arguments (command, flags, values)
|
||||||
|
*/
|
||||||
|
export function parseArgs(argv) {
|
||||||
|
const args = {
|
||||||
|
command: null,
|
||||||
|
agent: null,
|
||||||
|
cadence: null,
|
||||||
|
dryRun: false,
|
||||||
|
force: false,
|
||||||
|
port: DEFAULT_DASHBOARD_PORT,
|
||||||
|
logDir: 'task-logs',
|
||||||
|
ignoreLogs: false,
|
||||||
|
id: null,
|
||||||
|
type: null,
|
||||||
|
tags: [],
|
||||||
|
pinned: null,
|
||||||
|
limit: null,
|
||||||
|
offset: null,
|
||||||
|
retentionMs: null,
|
||||||
|
windowMs: null,
|
||||||
|
timeoutMs: null,
|
||||||
|
allRelaysDownMinutes: null,
|
||||||
|
minSuccessRate: null,
|
||||||
|
windowMinutes: null,
|
||||||
|
};
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
if (argv.length > 0 && !argv[0].startsWith('-')) {
|
||||||
|
args.command = argv[0];
|
||||||
|
i = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; i < argv.length; i++) {
|
||||||
|
const arg = argv[i];
|
||||||
|
if (arg === '--agent' || arg === '-a') {
|
||||||
|
args.agent = argv[++i];
|
||||||
|
} else if (arg === '--cadence' || arg === '-c') {
|
||||||
|
args.cadence = argv[++i];
|
||||||
|
} else if (arg.startsWith('--agent=')) {
|
||||||
|
args.agent = arg.split('=')[1];
|
||||||
|
} else if (arg.startsWith('--cadence=')) {
|
||||||
|
args.cadence = arg.split('=')[1];
|
||||||
|
} else if (arg === '--dry-run') {
|
||||||
|
args.dryRun = true;
|
||||||
|
} else if (arg === '--force') {
|
||||||
|
args.force = true;
|
||||||
|
} else if (arg === '--port') {
|
||||||
|
args.port = parseInt(argv[++i], 10) || DEFAULT_DASHBOARD_PORT;
|
||||||
|
} else if (arg === '--log-dir') {
|
||||||
|
args.logDir = argv[++i];
|
||||||
|
} else if (arg === '--ignore-logs') {
|
||||||
|
args.ignoreLogs = true;
|
||||||
|
} else if (arg === '--id') {
|
||||||
|
args.id = argv[++i];
|
||||||
|
} else if (arg.startsWith('--id=')) {
|
||||||
|
args.id = arg.split('=')[1];
|
||||||
|
} else if (arg === '--type') {
|
||||||
|
args.type = argv[++i];
|
||||||
|
} else if (arg.startsWith('--type=')) {
|
||||||
|
args.type = arg.split('=')[1];
|
||||||
|
} else if (arg === '--tags') {
|
||||||
|
args.tags = String(argv[++i]).split(',').map((tag) => tag.trim()).filter(Boolean);
|
||||||
|
} else if (arg.startsWith('--tags=')) {
|
||||||
|
args.tags = String(arg.split('=')[1]).split(',').map((tag) => tag.trim()).filter(Boolean);
|
||||||
|
} else if (arg === '--pinned') {
|
||||||
|
const value = String(argv[++i]).toLowerCase();
|
||||||
|
args.pinned = value === 'true' ? true : value === 'false' ? false : null;
|
||||||
|
} else if (arg.startsWith('--pinned=')) {
|
||||||
|
const value = String(arg.split('=')[1]).toLowerCase();
|
||||||
|
args.pinned = value === 'true' ? true : value === 'false' ? false : null;
|
||||||
|
} else if (arg === '--limit') {
|
||||||
|
args.limit = parseInt(argv[++i], 10);
|
||||||
|
} else if (arg.startsWith('--limit=')) {
|
||||||
|
args.limit = parseInt(arg.split('=')[1], 10);
|
||||||
|
} else if (arg === '--offset') {
|
||||||
|
args.offset = parseInt(argv[++i], 10);
|
||||||
|
} else if (arg.startsWith('--offset=')) {
|
||||||
|
args.offset = parseInt(arg.split('=')[1], 10);
|
||||||
|
} else if (arg === '--retention-ms') {
|
||||||
|
args.retentionMs = parseInt(argv[++i], 10);
|
||||||
|
} else if (arg.startsWith('--retention-ms=')) {
|
||||||
|
args.retentionMs = parseInt(arg.split('=')[1], 10);
|
||||||
|
} else if (arg === '--window-ms') {
|
||||||
|
args.windowMs = parseInt(argv[++i], 10);
|
||||||
|
} else if (arg.startsWith('--window-ms=')) {
|
||||||
|
args.windowMs = parseInt(arg.split('=')[1], 10);
|
||||||
|
} else if (arg === '--timeout-ms') {
|
||||||
|
args.timeoutMs = parseInt(argv[++i], 10);
|
||||||
|
} else if (arg.startsWith('--timeout-ms=')) {
|
||||||
|
args.timeoutMs = parseInt(arg.split('=')[1], 10);
|
||||||
|
} else if (arg === '--all-relays-down-minutes') {
|
||||||
|
args.allRelaysDownMinutes = parseInt(argv[++i], 10);
|
||||||
|
} else if (arg.startsWith('--all-relays-down-minutes=')) {
|
||||||
|
args.allRelaysDownMinutes = parseInt(arg.split('=')[1], 10);
|
||||||
|
} else if (arg === '--min-success-rate') {
|
||||||
|
args.minSuccessRate = parseFloat(argv[++i]);
|
||||||
|
} else if (arg.startsWith('--min-success-rate=')) {
|
||||||
|
args.minSuccessRate = parseFloat(arg.split('=')[1]);
|
||||||
|
} else if (arg === '--window-minutes') {
|
||||||
|
args.windowMinutes = parseInt(argv[++i], 10);
|
||||||
|
} else if (arg.startsWith('--window-minutes=')) {
|
||||||
|
args.windowMinutes = parseInt(arg.split('=')[1], 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
+1
-112
@@ -19,6 +19,7 @@ import {
|
|||||||
RACE_CHECK_DELAY_MS,
|
RACE_CHECK_DELAY_MS,
|
||||||
} from './constants.mjs';
|
} from './constants.mjs';
|
||||||
import { cmdInit, cmdUpdate } from './ops.mjs';
|
import { cmdInit, cmdUpdate } from './ops.mjs';
|
||||||
|
import { parseArgs } from './cli-parser.mjs';
|
||||||
import { getRoster as _getRoster } from './roster.mjs';
|
import { getRoster as _getRoster } from './roster.mjs';
|
||||||
import { queryLocks as _queryLocks, publishLock as _publishLock, parseLockEvent } from './lock-ops.mjs';
|
import { queryLocks as _queryLocks, publishLock as _publishLock, parseLockEvent } from './lock-ops.mjs';
|
||||||
import { cmdDashboard } from './dashboard.mjs';
|
import { cmdDashboard } from './dashboard.mjs';
|
||||||
@@ -504,118 +505,6 @@ export async function cmdComplete(agent, cadence, dryRun = false, deps = {}) {
|
|||||||
return { status: 'completed', eventId: event.id };
|
return { status: 'completed', eventId: event.id };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parses command-line arguments into a structured object.
|
|
||||||
*
|
|
||||||
* @param {string[]} argv - Arguments from process.argv.slice(2)
|
|
||||||
* @returns {Object} - Parsed arguments (command, flags, values)
|
|
||||||
*/
|
|
||||||
function parseArgs(argv) {
|
|
||||||
const args = {
|
|
||||||
command: null,
|
|
||||||
agent: null,
|
|
||||||
cadence: null,
|
|
||||||
dryRun: false,
|
|
||||||
force: false,
|
|
||||||
port: DEFAULT_DASHBOARD_PORT,
|
|
||||||
logDir: 'task-logs',
|
|
||||||
ignoreLogs: false,
|
|
||||||
id: null,
|
|
||||||
type: null,
|
|
||||||
tags: [],
|
|
||||||
pinned: null,
|
|
||||||
limit: null,
|
|
||||||
offset: null,
|
|
||||||
retentionMs: null,
|
|
||||||
windowMs: null,
|
|
||||||
timeoutMs: null,
|
|
||||||
allRelaysDownMinutes: null,
|
|
||||||
minSuccessRate: null,
|
|
||||||
windowMinutes: null,
|
|
||||||
};
|
|
||||||
let i = 0;
|
|
||||||
|
|
||||||
if (argv.length > 0 && !argv[0].startsWith('-')) {
|
|
||||||
args.command = argv[0];
|
|
||||||
i = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; i < argv.length; i++) {
|
|
||||||
const arg = argv[i];
|
|
||||||
if (arg === '--agent' || arg === '-a') {
|
|
||||||
args.agent = argv[++i];
|
|
||||||
} else if (arg === '--cadence' || arg === '-c') {
|
|
||||||
args.cadence = argv[++i];
|
|
||||||
} else if (arg.startsWith('--agent=')) {
|
|
||||||
args.agent = arg.split('=')[1];
|
|
||||||
} else if (arg.startsWith('--cadence=')) {
|
|
||||||
args.cadence = arg.split('=')[1];
|
|
||||||
} else if (arg === '--dry-run') {
|
|
||||||
args.dryRun = true;
|
|
||||||
} else if (arg === '--force') {
|
|
||||||
args.force = true;
|
|
||||||
} else if (arg === '--port') {
|
|
||||||
args.port = parseInt(argv[++i], 10) || DEFAULT_DASHBOARD_PORT;
|
|
||||||
} else if (arg === '--log-dir') {
|
|
||||||
args.logDir = argv[++i];
|
|
||||||
} else if (arg === '--ignore-logs') {
|
|
||||||
args.ignoreLogs = true;
|
|
||||||
} else if (arg === '--id') {
|
|
||||||
args.id = argv[++i];
|
|
||||||
} else if (arg.startsWith('--id=')) {
|
|
||||||
args.id = arg.split('=')[1];
|
|
||||||
} else if (arg === '--type') {
|
|
||||||
args.type = argv[++i];
|
|
||||||
} else if (arg.startsWith('--type=')) {
|
|
||||||
args.type = arg.split('=')[1];
|
|
||||||
} else if (arg === '--tags') {
|
|
||||||
args.tags = String(argv[++i]).split(',').map((tag) => tag.trim()).filter(Boolean);
|
|
||||||
} else if (arg.startsWith('--tags=')) {
|
|
||||||
args.tags = String(arg.split('=')[1]).split(',').map((tag) => tag.trim()).filter(Boolean);
|
|
||||||
} else if (arg === '--pinned') {
|
|
||||||
const value = String(argv[++i]).toLowerCase();
|
|
||||||
args.pinned = value === 'true' ? true : value === 'false' ? false : null;
|
|
||||||
} else if (arg.startsWith('--pinned=')) {
|
|
||||||
const value = String(arg.split('=')[1]).toLowerCase();
|
|
||||||
args.pinned = value === 'true' ? true : value === 'false' ? false : null;
|
|
||||||
} else if (arg === '--limit') {
|
|
||||||
args.limit = parseInt(argv[++i], 10);
|
|
||||||
} else if (arg.startsWith('--limit=')) {
|
|
||||||
args.limit = parseInt(arg.split('=')[1], 10);
|
|
||||||
} else if (arg === '--offset') {
|
|
||||||
args.offset = parseInt(argv[++i], 10);
|
|
||||||
} else if (arg.startsWith('--offset=')) {
|
|
||||||
args.offset = parseInt(arg.split('=')[1], 10);
|
|
||||||
} else if (arg === '--retention-ms') {
|
|
||||||
args.retentionMs = parseInt(argv[++i], 10);
|
|
||||||
} else if (arg.startsWith('--retention-ms=')) {
|
|
||||||
args.retentionMs = parseInt(arg.split('=')[1], 10);
|
|
||||||
} else if (arg === '--window-ms') {
|
|
||||||
args.windowMs = parseInt(argv[++i], 10);
|
|
||||||
} else if (arg.startsWith('--window-ms=')) {
|
|
||||||
args.windowMs = parseInt(arg.split('=')[1], 10);
|
|
||||||
} else if (arg === '--timeout-ms') {
|
|
||||||
args.timeoutMs = parseInt(argv[++i], 10);
|
|
||||||
} else if (arg.startsWith('--timeout-ms=')) {
|
|
||||||
args.timeoutMs = parseInt(arg.split('=')[1], 10);
|
|
||||||
} else if (arg === '--all-relays-down-minutes') {
|
|
||||||
args.allRelaysDownMinutes = parseInt(argv[++i], 10);
|
|
||||||
} else if (arg.startsWith('--all-relays-down-minutes=')) {
|
|
||||||
args.allRelaysDownMinutes = parseInt(arg.split('=')[1], 10);
|
|
||||||
} else if (arg === '--min-success-rate') {
|
|
||||||
args.minSuccessRate = parseFloat(argv[++i]);
|
|
||||||
} else if (arg.startsWith('--min-success-rate=')) {
|
|
||||||
args.minSuccessRate = parseFloat(arg.split('=')[1]);
|
|
||||||
} else if (arg === '--window-minutes') {
|
|
||||||
args.windowMinutes = parseInt(argv[++i], 10);
|
|
||||||
} else if (arg.startsWith('--window-minutes=')) {
|
|
||||||
args.windowMinutes = parseInt(arg.split('=')[1], 10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return args;
|
|
||||||
}
|
|
||||||
|
|
||||||
function usage() {
|
function usage() {
|
||||||
console.error(`Usage: torch-lock <command> [options]
|
console.error(`Usage: torch-lock <command> [options]
|
||||||
|
|
||||||
|
|||||||
@@ -33,9 +33,9 @@ Canonical roster source: `src/prompts/roster.json` (`weekly` key).
|
|||||||
| 7 | perf-deepdive-agent | `perf-deepdive-agent.md` |
|
| 7 | perf-deepdive-agent | `perf-deepdive-agent.md` |
|
||||||
| 8 | perf-optimization-agent | `perf-optimization-agent.md` |
|
| 8 | perf-optimization-agent | `perf-optimization-agent.md` |
|
||||||
| 9 | pr-review-agent | `pr-review-agent.md` |
|
| 9 | pr-review-agent | `pr-review-agent.md` |
|
||||||
| 10 | prompt-fixer-agent | `prompt-fixer-agent.md` |
|
| 10 | prompt-maintenance-agent | `prompt-maintenance-agent.md` |
|
||||||
| 11 | prompt-maintenance-agent | `prompt-maintenance-agent.md` |
|
| 11 | prompt-safety-agent | `prompt-safety-agent.md` |
|
||||||
| 12 | prompt-safety-agent | `prompt-safety-agent.md` |
|
| 12 | prompt-fixer-agent | `prompt-fixer-agent.md` |
|
||||||
| 13 | race-condition-agent | `race-condition-agent.md` |
|
| 13 | race-condition-agent | `race-condition-agent.md` |
|
||||||
| 14 | refactor-agent | `refactor-agent.md` |
|
| 14 | refactor-agent | `refactor-agent.md` |
|
||||||
| 15 | repo-fit-agent | `repo-fit-agent.md` |
|
| 15 | repo-fit-agent | `repo-fit-agent.md` |
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
---
|
||||||
|
agent: refactor-agent
|
||||||
|
cadence: weekly
|
||||||
|
date: 2026-02-15
|
||||||
|
status: completed
|
||||||
|
---
|
||||||
|
|
||||||
|
# Weekly Task: Refactor Agent
|
||||||
|
|
||||||
|
**Goal**: Perform small, incremental refactors.
|
||||||
|
|
||||||
|
**Work**:
|
||||||
|
- Extracted `parseArgs` function from `src/lib.mjs` to a new module `src/cli-parser.mjs`.
|
||||||
|
- Added unit tests for `parseArgs` in `test/cli-parser.test.mjs`.
|
||||||
|
- Verified changes with `npm test` and `npm run lint`.
|
||||||
|
- Fixed roster drift in `src/prompts/weekly-scheduler.md`.
|
||||||
|
|
||||||
|
**Result**:
|
||||||
|
- `src/lib.mjs` is cleaner and imports `parseArgs`.
|
||||||
|
- `src/cli-parser.mjs` is testable and tested.
|
||||||
|
- `src/prompts/weekly-scheduler.md` is synced with `roster.json`.
|
||||||
|
- All tests passed.
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import { test } from 'node:test';
|
||||||
|
import assert from 'node:assert';
|
||||||
|
import { parseArgs } from '../src/cli-parser.mjs';
|
||||||
|
import { DEFAULT_DASHBOARD_PORT } from '../src/constants.mjs';
|
||||||
|
|
||||||
|
test('parseArgs should parse basic command', () => {
|
||||||
|
const args = parseArgs(['check', '--cadence', 'daily']);
|
||||||
|
assert.strictEqual(args.command, 'check');
|
||||||
|
assert.strictEqual(args.cadence, 'daily');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse agent and cadence', () => {
|
||||||
|
const args = parseArgs(['lock', '--agent', 'my-agent', '--cadence', 'weekly']);
|
||||||
|
assert.strictEqual(args.command, 'lock');
|
||||||
|
assert.strictEqual(args.agent, 'my-agent');
|
||||||
|
assert.strictEqual(args.cadence, 'weekly');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse equals sign syntax', () => {
|
||||||
|
const args = parseArgs(['lock', '--agent=my-agent', '--cadence=weekly']);
|
||||||
|
assert.strictEqual(args.command, 'lock');
|
||||||
|
assert.strictEqual(args.agent, 'my-agent');
|
||||||
|
assert.strictEqual(args.cadence, 'weekly');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse dry-run flag', () => {
|
||||||
|
const args = parseArgs(['lock', '--dry-run']);
|
||||||
|
assert.strictEqual(args.dryRun, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse port with default', () => {
|
||||||
|
const args = parseArgs(['dashboard']);
|
||||||
|
assert.strictEqual(args.port, DEFAULT_DASHBOARD_PORT);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse custom port', () => {
|
||||||
|
const args = parseArgs(['dashboard', '--port', '8080']);
|
||||||
|
assert.strictEqual(args.port, 8080);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse ignore-logs flag', () => {
|
||||||
|
const args = parseArgs(['check', '--ignore-logs']);
|
||||||
|
assert.strictEqual(args.ignoreLogs, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse memory flags', () => {
|
||||||
|
const args = parseArgs([
|
||||||
|
'list-memories',
|
||||||
|
'--id', '123',
|
||||||
|
'--type', 'retrieve',
|
||||||
|
'--tags', 'a,b',
|
||||||
|
'--pinned', 'true',
|
||||||
|
'--limit', '10',
|
||||||
|
'--offset', '5'
|
||||||
|
]);
|
||||||
|
assert.strictEqual(args.id, '123');
|
||||||
|
assert.strictEqual(args.type, 'retrieve');
|
||||||
|
assert.deepStrictEqual(args.tags, ['a', 'b']);
|
||||||
|
assert.strictEqual(args.pinned, true);
|
||||||
|
assert.strictEqual(args.limit, 10);
|
||||||
|
assert.strictEqual(args.offset, 5);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseArgs should parse memory flags with equals', () => {
|
||||||
|
const args = parseArgs([
|
||||||
|
'list-memories',
|
||||||
|
'--id=123',
|
||||||
|
'--type=retrieve',
|
||||||
|
'--tags=a,b',
|
||||||
|
'--pinned=false',
|
||||||
|
'--limit=10',
|
||||||
|
'--offset=5'
|
||||||
|
]);
|
||||||
|
assert.strictEqual(args.id, '123');
|
||||||
|
assert.strictEqual(args.type, 'retrieve');
|
||||||
|
assert.deepStrictEqual(args.tags, ['a', 'b']);
|
||||||
|
assert.strictEqual(args.pinned, false);
|
||||||
|
assert.strictEqual(args.limit, 10);
|
||||||
|
assert.strictEqual(args.offset, 5);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user