mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-09 16:59:43 +02:00
docs: add JSDoc comments to ralph-loop.ts
Comprehensive documentation for RalphLoop: - Module-level description explaining the loop concept - Event interface documentation - Options interface with descriptions - Class-level lifecycle description - Method documentation for public API Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview Ralph Loop - Autonomous task execution engine
|
||||||
|
*
|
||||||
|
* The Ralph Loop orchestrates autonomous Claude sessions by:
|
||||||
|
* - Polling for available tasks from the task queue
|
||||||
|
* - Assigning tasks to idle sessions
|
||||||
|
* - Monitoring completion and handling failures
|
||||||
|
* - Auto-generating follow-up tasks when min duration not reached
|
||||||
|
*
|
||||||
|
* Named after Ralph Wiggum's persistence ("I'm in danger!"),
|
||||||
|
* this loop keeps Claude working until all tasks are done.
|
||||||
|
*
|
||||||
|
* @module ralph-loop
|
||||||
|
*/
|
||||||
|
|
||||||
import { EventEmitter } from 'node:events';
|
import { EventEmitter } from 'node:events';
|
||||||
import { getSessionManager, SessionManager } from './session-manager.js';
|
import { getSessionManager, SessionManager } from './session-manager.js';
|
||||||
import { getTaskQueue, TaskQueue } from './task-queue.js';
|
import { getTaskQueue, TaskQueue } from './task-queue.js';
|
||||||
@@ -6,6 +21,9 @@ import { Session } from './session.js';
|
|||||||
import { Task } from './task.js';
|
import { Task } from './task.js';
|
||||||
import { RalphLoopStatus } from './types.js';
|
import { RalphLoopStatus } from './types.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Events emitted by RalphLoop
|
||||||
|
*/
|
||||||
export interface RalphLoopEvents {
|
export interface RalphLoopEvents {
|
||||||
started: () => void;
|
started: () => void;
|
||||||
stopped: () => void;
|
stopped: () => void;
|
||||||
@@ -15,12 +33,32 @@ export interface RalphLoopEvents {
|
|||||||
error: (error: Error) => void;
|
error: (error: Error) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration options for RalphLoop
|
||||||
|
*/
|
||||||
export interface RalphLoopOptions {
|
export interface RalphLoopOptions {
|
||||||
|
/** How often to check for new tasks (default from config) */
|
||||||
pollIntervalMs?: number;
|
pollIntervalMs?: number;
|
||||||
|
/** Minimum time to run before stopping (null = no minimum) */
|
||||||
minDurationMs?: number;
|
minDurationMs?: number;
|
||||||
|
/** Auto-generate follow-up tasks when queue is empty */
|
||||||
autoGenerateTasks?: boolean;
|
autoGenerateTasks?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Autonomous task execution loop.
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Manages the lifecycle of task execution:
|
||||||
|
* 1. Start: Begin polling and task assignment
|
||||||
|
* 2. Run: Assign tasks to idle sessions, monitor completion
|
||||||
|
* 3. Stop: When all tasks done and min duration reached
|
||||||
|
*
|
||||||
|
* Supports time-aware loops that continue generating tasks
|
||||||
|
* until a minimum duration is reached.
|
||||||
|
*
|
||||||
|
* @extends EventEmitter
|
||||||
|
*/
|
||||||
export class RalphLoop extends EventEmitter {
|
export class RalphLoop extends EventEmitter {
|
||||||
private sessionManager: SessionManager;
|
private sessionManager: SessionManager;
|
||||||
private taskQueue: TaskQueue;
|
private taskQueue: TaskQueue;
|
||||||
@@ -113,6 +151,7 @@ export class RalphLoop extends EventEmitter {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Starts the task execution loop. */
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
if (this._status === 'running') {
|
if (this._status === 'running') {
|
||||||
return;
|
return;
|
||||||
@@ -135,6 +174,7 @@ export class RalphLoop extends EventEmitter {
|
|||||||
this.runLoop();
|
this.runLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Stops the task execution loop. */
|
||||||
stop(): void {
|
stop(): void {
|
||||||
if (this._status === 'stopped') {
|
if (this._status === 'stopped') {
|
||||||
return;
|
return;
|
||||||
@@ -155,6 +195,7 @@ export class RalphLoop extends EventEmitter {
|
|||||||
this.emit('stopped');
|
this.emit('stopped');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Pauses the loop (can be resumed). */
|
||||||
pause(): void {
|
pause(): void {
|
||||||
if (this._status !== 'running') {
|
if (this._status !== 'running') {
|
||||||
return;
|
return;
|
||||||
@@ -170,6 +211,7 @@ export class RalphLoop extends EventEmitter {
|
|||||||
this.store.setRalphLoopState({ status: 'paused' });
|
this.store.setRalphLoopState({ status: 'paused' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Resumes a paused loop. */
|
||||||
resume(): void {
|
resume(): void {
|
||||||
if (this._status !== 'paused') {
|
if (this._status !== 'paused') {
|
||||||
return;
|
return;
|
||||||
@@ -380,6 +422,7 @@ export class RalphLoop extends EventEmitter {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Sets the minimum duration in hours before the loop can stop. */
|
||||||
setMinDuration(hours: number): void {
|
setMinDuration(hours: number): void {
|
||||||
this.minDurationMs = hours * 60 * 60 * 1000;
|
this.minDurationMs = hours * 60 * 60 * 1000;
|
||||||
this.store.setRalphLoopState({ minDurationMs: this.minDurationMs });
|
this.store.setRalphLoopState({ minDurationMs: this.minDurationMs });
|
||||||
@@ -389,6 +432,7 @@ export class RalphLoop extends EventEmitter {
|
|||||||
// Singleton instance
|
// Singleton instance
|
||||||
let loopInstance: RalphLoop | null = null;
|
let loopInstance: RalphLoop | null = null;
|
||||||
|
|
||||||
|
/** Gets or creates the singleton RalphLoop instance. */
|
||||||
export function getRalphLoop(options?: RalphLoopOptions): RalphLoop {
|
export function getRalphLoop(options?: RalphLoopOptions): RalphLoop {
|
||||||
if (!loopInstance) {
|
if (!loopInstance) {
|
||||||
loopInstance = new RalphLoop(options);
|
loopInstance = new RalphLoop(options);
|
||||||
|
|||||||
Reference in New Issue
Block a user