2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* @fileoverview Respawn Controller for autonomous Claude Code session cycling
|
|
|
|
|
|
*
|
|
|
|
|
|
* The RespawnController manages automatic respawning of Claude Code sessions.
|
|
|
|
|
|
* When Claude finishes working (detected by idle prompt), it automatically
|
|
|
|
|
|
* cycles through update → clear → init steps to keep the session productive.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## State Machine
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```
|
|
|
|
|
|
* WATCHING → SENDING_UPDATE → WAITING_UPDATE → SENDING_CLEAR → WAITING_CLEAR
|
|
|
|
|
|
* ↑ │
|
|
|
|
|
|
* │ ▼
|
|
|
|
|
|
* │ SENDING_INIT → WAITING_INIT → MONITORING_INIT ───────┘
|
|
|
|
|
|
* │ │
|
|
|
|
|
|
* │ ▼ (if no work triggered)
|
|
|
|
|
|
* └── SENDING_KICKSTART → WAITING_KICKSTART ──┘
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## Configuration
|
|
|
|
|
|
*
|
|
|
|
|
|
* - `sendClear`: Whether to send /clear after update (default: true)
|
|
|
|
|
|
* - `sendInit`: Whether to send /init after clear (default: true)
|
|
|
|
|
|
* - `kickstartPrompt`: Optional prompt if /init doesn't trigger work
|
|
|
|
|
|
*
|
|
|
|
|
|
* @module respawn-controller
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-01-18 07:31:19 +01:00
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
|
|
import { Session } from './session.js';
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
// ========== Configuration Constants ==========
|
2026-01-19 12:32:36 +01:00
|
|
|
|
|
2026-01-18 07:31:19 +01:00
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Maximum terminal buffer size for respawn controller.
|
|
|
|
|
|
* Buffer is trimmed when this limit is exceeded to prevent memory issues.
|
|
|
|
|
|
*/
|
|
|
|
|
|
const MAX_RESPAWN_BUFFER_SIZE = 1024 * 1024; // 1MB
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Size to trim buffer to when MAX_RESPAWN_BUFFER_SIZE is exceeded.
|
|
|
|
|
|
* Keeps the most recent output for pattern detection.
|
|
|
|
|
|
*/
|
|
|
|
|
|
const RESPAWN_BUFFER_TRIM_SIZE = 512 * 1024; // 512KB
|
|
|
|
|
|
|
2026-01-21 04:59:15 +01:00
|
|
|
|
// ========== Constants ==========
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The definitive "ready for input" indicator.
|
|
|
|
|
|
* When Claude shows a suggestion, this appears and indicates idle state.
|
|
|
|
|
|
*/
|
|
|
|
|
|
const READY_INDICATOR = '↵ send';
|
|
|
|
|
|
|
2026-01-21 04:55:48 +01:00
|
|
|
|
// ========== Buffer Accumulator ==========
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* High-performance buffer accumulator using array-based collection.
|
|
|
|
|
|
* Reduces GC pressure by avoiding repeated string concatenation.
|
|
|
|
|
|
*/
|
|
|
|
|
|
class BufferAccumulator {
|
|
|
|
|
|
private chunks: string[] = [];
|
|
|
|
|
|
private totalLength: number = 0;
|
|
|
|
|
|
private readonly maxSize: number;
|
|
|
|
|
|
private readonly trimSize: number;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(maxSize: number, trimSize: number) {
|
|
|
|
|
|
this.maxSize = maxSize;
|
|
|
|
|
|
this.trimSize = trimSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
append(data: string): void {
|
|
|
|
|
|
if (!data) return;
|
|
|
|
|
|
this.chunks.push(data);
|
|
|
|
|
|
this.totalLength += data.length;
|
|
|
|
|
|
if (this.totalLength > this.maxSize) {
|
|
|
|
|
|
this.trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
get value(): string {
|
|
|
|
|
|
if (this.chunks.length === 0) return '';
|
|
|
|
|
|
if (this.chunks.length === 1) return this.chunks[0];
|
|
|
|
|
|
const result = this.chunks.join('');
|
|
|
|
|
|
this.chunks = [result];
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
get length(): number {
|
|
|
|
|
|
return this.totalLength;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
clear(): void {
|
|
|
|
|
|
this.chunks = [];
|
|
|
|
|
|
this.totalLength = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private trim(): void {
|
|
|
|
|
|
const full = this.chunks.join('');
|
|
|
|
|
|
const trimmed = full.slice(-this.trimSize);
|
|
|
|
|
|
this.chunks = [trimmed];
|
|
|
|
|
|
this.totalLength = trimmed.length;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
// ========== Type Definitions ==========
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Respawn sequence states.
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*
|
|
|
|
|
|
* The controller cycles through these states:
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* ```
|
|
|
|
|
|
* WATCHING → SENDING_UPDATE → WAITING_UPDATE →
|
|
|
|
|
|
* SENDING_CLEAR → WAITING_CLEAR →
|
|
|
|
|
|
* SENDING_INIT → WAITING_INIT →
|
|
|
|
|
|
* MONITORING_INIT → (maybe SENDING_KICKSTART → WAITING_KICKSTART) →
|
|
|
|
|
|
* WATCHING (repeat)
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* Steps can be skipped via config (`sendClear: false`, `sendInit: false`).
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
export type RespawnState =
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Watching for idle, ready to start respawn sequence */
|
|
|
|
|
|
| 'watching'
|
|
|
|
|
|
/** About to send the update docs prompt */
|
|
|
|
|
|
| 'sending_update'
|
|
|
|
|
|
/** Waiting for update to complete */
|
|
|
|
|
|
| 'waiting_update'
|
|
|
|
|
|
/** About to send /clear command */
|
|
|
|
|
|
| 'sending_clear'
|
|
|
|
|
|
/** Waiting for clear to complete */
|
|
|
|
|
|
| 'waiting_clear'
|
|
|
|
|
|
/** About to send /init command */
|
|
|
|
|
|
| 'sending_init'
|
|
|
|
|
|
/** Waiting for init to complete */
|
|
|
|
|
|
| 'waiting_init'
|
|
|
|
|
|
/** Monitoring if /init triggered work */
|
|
|
|
|
|
| 'monitoring_init'
|
|
|
|
|
|
/** About to send kickstart prompt */
|
|
|
|
|
|
| 'sending_kickstart'
|
|
|
|
|
|
/** Waiting for kickstart to complete */
|
|
|
|
|
|
| 'waiting_kickstart'
|
|
|
|
|
|
/** Controller stopped (not running) */
|
|
|
|
|
|
| 'stopped';
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Configuration options for the RespawnController.
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
export interface RespawnConfig {
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* How long to wait after seeing prompt before considering truly idle.
|
|
|
|
|
|
* Prevents premature cycling when user is about to type.
|
|
|
|
|
|
* @default 10000 (10 seconds)
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
idleTimeoutMs: number;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The prompt to send when updating docs.
|
|
|
|
|
|
* Sent at the start of each respawn cycle.
|
|
|
|
|
|
* @default 'update all the docs and CLAUDE.md'
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
updatePrompt: string;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Delay between sending steps (ms).
|
|
|
|
|
|
* Gives Claude time to process each command.
|
|
|
|
|
|
* @default 1000 (1 second)
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
interStepDelayMs: number;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Whether the respawn loop is enabled.
|
|
|
|
|
|
* When false, start() will be a no-op.
|
|
|
|
|
|
* @default true
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
enabled: boolean;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Whether to send /clear after update prompt completes.
|
|
|
|
|
|
* Resets Claude's context for fresh start.
|
|
|
|
|
|
* @default true
|
|
|
|
|
|
*/
|
2026-01-19 00:38:11 +01:00
|
|
|
|
sendClear: boolean;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Whether to send /init after /clear completes.
|
|
|
|
|
|
* Re-initializes Claude with CLAUDE.md context.
|
|
|
|
|
|
* @default true
|
|
|
|
|
|
*/
|
2026-01-19 00:38:11 +01:00
|
|
|
|
sendInit: boolean;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Optional prompt to send if /init doesn't trigger work.
|
|
|
|
|
|
* Used as a fallback when /init completes but Claude doesn't start working.
|
|
|
|
|
|
* @default undefined
|
|
|
|
|
|
*/
|
2026-01-19 13:08:53 +01:00
|
|
|
|
kickstartPrompt?: string;
|
2026-01-18 07:31:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Events emitted by RespawnController.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @event stateChanged - Fired when state machine transitions
|
|
|
|
|
|
* @event respawnCycleStarted - Fired when a new cycle begins
|
|
|
|
|
|
* @event respawnCycleCompleted - Fired when a cycle finishes
|
|
|
|
|
|
* @event stepSent - Fired when a command is sent to the session
|
|
|
|
|
|
* @event stepCompleted - Fired when a step finishes (ready indicator detected)
|
|
|
|
|
|
* @event error - Fired on errors
|
|
|
|
|
|
* @event log - Fired for debug logging
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
export interface RespawnEvents {
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** State machine transition */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
stateChanged: (state: RespawnState, prevState: RespawnState) => void;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** New respawn cycle started */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
respawnCycleStarted: (cycleNumber: number) => void;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Respawn cycle finished */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
respawnCycleCompleted: (cycleNumber: number) => void;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Command sent to session */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
stepSent: (step: string, input: string) => void;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Step completed (ready indicator detected) */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
stepCompleted: (step: string) => void;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Error occurred */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
error: (error: Error) => void;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Debug log message */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
log: (message: string) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Default configuration values */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
const DEFAULT_CONFIG: RespawnConfig = {
|
2026-01-19 12:24:50 +01:00
|
|
|
|
idleTimeoutMs: 10000, // 10 seconds of no activity after prompt
|
2026-01-18 07:31:19 +01:00
|
|
|
|
updatePrompt: 'update all the docs and CLAUDE.md',
|
|
|
|
|
|
interStepDelayMs: 1000, // 1 second between steps
|
|
|
|
|
|
enabled: true,
|
2026-01-19 00:38:11 +01:00
|
|
|
|
sendClear: true, // send /clear after update prompt
|
|
|
|
|
|
sendInit: true, // send /init after /clear
|
2026-01-18 07:31:19 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* RespawnController - Automatic session cycling for continuous Claude work.
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Monitors a Claude Code session for idle state and automatically cycles
|
|
|
|
|
|
* through update → clear → init steps to keep the session productive.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## How It Works
|
|
|
|
|
|
*
|
|
|
|
|
|
* 1. **Idle Detection**: Watches terminal output for `↵ send` indicator
|
|
|
|
|
|
* 2. **Update**: Sends configured prompt (e.g., "update all docs")
|
|
|
|
|
|
* 3. **Clear**: Sends `/clear` to reset context (optional)
|
|
|
|
|
|
* 4. **Init**: Sends `/init` to re-initialize with CLAUDE.md (optional)
|
|
|
|
|
|
* 5. **Kickstart**: If /init doesn't trigger work, sends fallback prompt (optional)
|
|
|
|
|
|
* 6. **Repeat**: Returns to watching state for next cycle
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## Idle Detection
|
|
|
|
|
|
*
|
|
|
|
|
|
* Primary indicator: `↵ send` - Claude's suggestion prompt
|
|
|
|
|
|
* Fallback indicators: Various prompt characters (❯, ⏵, etc.)
|
|
|
|
|
|
*
|
|
|
|
|
|
* Working indicators: Thinking, Writing, spinner characters, etc.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## Events
|
|
|
|
|
|
*
|
|
|
|
|
|
* - `stateChanged`: State machine transition
|
|
|
|
|
|
* - `respawnCycleStarted`: New cycle began
|
|
|
|
|
|
* - `respawnCycleCompleted`: Cycle finished
|
|
|
|
|
|
* - `stepSent`: Command sent to session
|
|
|
|
|
|
* - `stepCompleted`: Step finished
|
|
|
|
|
|
* - `log`: Debug messages
|
|
|
|
|
|
*
|
|
|
|
|
|
* @extends EventEmitter
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ```typescript
|
|
|
|
|
|
* const respawn = new RespawnController(session, {
|
|
|
|
|
|
* updatePrompt: 'continue working on the task',
|
|
|
|
|
|
* idleTimeoutMs: 5000,
|
|
|
|
|
|
* });
|
|
|
|
|
|
*
|
|
|
|
|
|
* respawn.on('respawnCycleCompleted', (cycle) => {
|
|
|
|
|
|
* console.log(`Completed cycle ${cycle}`);
|
|
|
|
|
|
* });
|
|
|
|
|
|
*
|
|
|
|
|
|
* respawn.start();
|
|
|
|
|
|
* ```
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
export class RespawnController extends EventEmitter {
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** The session being controlled */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private session: Session;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Current configuration */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private config: RespawnConfig;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Current state machine state */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private _state: RespawnState = 'stopped';
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Timer for idle detection timeout */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private idleTimer: NodeJS.Timeout | null = null;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Timer for step delays */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private stepTimer: NodeJS.Timeout | null = null;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Number of completed respawn cycles */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private cycleCount: number = 0;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Timestamp of last terminal activity */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private lastActivityTime: number = 0;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
2026-01-21 04:55:48 +01:00
|
|
|
|
/** Buffer for recent terminal output (uses BufferAccumulator to reduce GC pressure) */
|
|
|
|
|
|
private terminalBuffer = new BufferAccumulator(MAX_RESPAWN_BUFFER_SIZE, RESPAWN_BUFFER_TRIM_SIZE);
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Whether a prompt indicator was detected */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private promptDetected: boolean = false;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Whether a working indicator was detected */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private workingDetected: boolean = false;
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/** Reference to terminal event handler (for cleanup) */
|
2026-01-19 15:12:36 +01:00
|
|
|
|
private terminalHandler: ((data: string) => void) | null = null;
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Patterns indicating Claude is ready for input.
|
|
|
|
|
|
* Primary: `↵ send` (suggestion prompt)
|
|
|
|
|
|
* Fallback: Various prompt characters
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private readonly PROMPT_PATTERNS = [
|
2026-01-19 12:26:37 +01:00
|
|
|
|
'↵ send', // Suggestion ready to send (strongest indicator of idle)
|
2026-01-18 07:31:19 +01:00
|
|
|
|
'❯', // Standard prompt
|
|
|
|
|
|
'\u276f', // Unicode variant
|
|
|
|
|
|
'⏵', // Claude Code prompt variant
|
|
|
|
|
|
'> ', // Fallback
|
|
|
|
|
|
'tokens', // The status line shows "X tokens" when at prompt
|
|
|
|
|
|
];
|
2026-01-21 03:46:11 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Patterns indicating Claude is actively working.
|
|
|
|
|
|
* When detected, resets idle detection.
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private readonly WORKING_PATTERNS = [
|
|
|
|
|
|
'Thinking', 'Writing', 'Reading', 'Running', 'Searching',
|
|
|
|
|
|
'Editing', 'Creating', 'Deleting', 'Analyzing', 'Executing',
|
2026-01-19 12:26:37 +01:00
|
|
|
|
'Synthesizing', 'Brewing', // Claude's processing indicators
|
2026-01-18 07:31:19 +01:00
|
|
|
|
'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏', // Spinner chars
|
2026-01-19 12:26:37 +01:00
|
|
|
|
'✻', '✽', // Activity indicators (spinning star)
|
2026-01-18 07:31:19 +01:00
|
|
|
|
];
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Creates a new RespawnController.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param session - The Session instance to control
|
|
|
|
|
|
* @param config - Partial configuration (merged with defaults)
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
constructor(session: Session, config: Partial<RespawnConfig> = {}) {
|
|
|
|
|
|
super();
|
|
|
|
|
|
this.session = session;
|
|
|
|
|
|
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Get the current state machine state.
|
|
|
|
|
|
* @returns Current RespawnState
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
get state(): RespawnState {
|
|
|
|
|
|
return this._state;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Get the current respawn cycle count.
|
|
|
|
|
|
* Increments each time a new cycle starts.
|
|
|
|
|
|
* @returns Number of cycles started
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
get currentCycle(): number {
|
|
|
|
|
|
return this.cycleCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Check if the controller is currently running.
|
|
|
|
|
|
* @returns True if state is not 'stopped'
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
get isRunning(): boolean {
|
|
|
|
|
|
return this._state !== 'stopped';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Transition to a new state.
|
|
|
|
|
|
* Emits 'stateChanged' event with old and new states.
|
|
|
|
|
|
* No-op if already in the target state.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param newState - State to transition to
|
|
|
|
|
|
* @fires stateChanged
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private setState(newState: RespawnState): void {
|
|
|
|
|
|
if (newState === this._state) return;
|
|
|
|
|
|
|
|
|
|
|
|
const prevState = this._state;
|
|
|
|
|
|
this._state = newState;
|
|
|
|
|
|
this.log(`State: ${prevState} → ${newState}`);
|
|
|
|
|
|
this.emit('stateChanged', newState, prevState);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Emit a timestamped log message.
|
|
|
|
|
|
* @param message - Log message content
|
|
|
|
|
|
* @fires log
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private log(message: string): void {
|
|
|
|
|
|
const timestamp = new Date().toISOString();
|
|
|
|
|
|
this.emit('log', `[${timestamp}] [Respawn] ${message}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Start watching the session for idle state.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Begins monitoring terminal output for idle indicators.
|
|
|
|
|
|
* When idle is detected, starts the respawn cycle.
|
|
|
|
|
|
*
|
|
|
|
|
|
* No-op if:
|
|
|
|
|
|
* - `config.enabled` is false
|
|
|
|
|
|
* - Already running (state !== 'stopped')
|
|
|
|
|
|
*
|
|
|
|
|
|
* @fires stateChanged - Transitions to 'watching'
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
start(): void {
|
|
|
|
|
|
if (!this.config.enabled) {
|
|
|
|
|
|
this.log('Respawn is disabled');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this._state !== 'stopped') {
|
|
|
|
|
|
this.log('Already running');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.log('Starting respawn controller');
|
|
|
|
|
|
this.setState('watching');
|
|
|
|
|
|
this.setupTerminalListener();
|
|
|
|
|
|
this.lastActivityTime = Date.now();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Stop the respawn controller.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Clears all timers, removes terminal listener, and sets state to 'stopped'.
|
|
|
|
|
|
* Safe to call multiple times.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @fires stateChanged - Transitions to 'stopped'
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
stop(): void {
|
|
|
|
|
|
this.log('Stopping respawn controller');
|
|
|
|
|
|
this.clearTimers();
|
|
|
|
|
|
this.setState('stopped');
|
2026-01-19 15:12:36 +01:00
|
|
|
|
if (this.terminalHandler) {
|
|
|
|
|
|
this.session.off('terminal', this.terminalHandler);
|
|
|
|
|
|
this.terminalHandler = null;
|
|
|
|
|
|
}
|
2026-01-18 07:31:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Pause respawn without stopping.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Clears timers but keeps listening to terminal.
|
|
|
|
|
|
* State is preserved; won't trigger idle detection while paused.
|
|
|
|
|
|
* Use resume() to continue.
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
pause(): void {
|
|
|
|
|
|
this.log('Pausing respawn');
|
|
|
|
|
|
this.clearTimers();
|
|
|
|
|
|
// Stay in current state but clear timers
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Resume respawn after pause.
|
|
|
|
|
|
*
|
|
|
|
|
|
* If in 'watching' state, immediately checks for idle condition.
|
|
|
|
|
|
* Otherwise, continues from current state.
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
resume(): void {
|
|
|
|
|
|
this.log('Resuming respawn');
|
|
|
|
|
|
if (this._state === 'watching') {
|
|
|
|
|
|
this.checkIdleAndMaybeStart();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Set up terminal output listener on the session.
|
|
|
|
|
|
* Removes any previous listener first to avoid duplicates.
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private setupTerminalListener(): void {
|
2026-01-19 15:12:36 +01:00
|
|
|
|
// Remove our previous listener if any (don't remove other listeners!)
|
|
|
|
|
|
if (this.terminalHandler) {
|
|
|
|
|
|
this.session.off('terminal', this.terminalHandler);
|
|
|
|
|
|
}
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
2026-01-19 15:12:36 +01:00
|
|
|
|
this.terminalHandler = (data: string) => {
|
2026-01-18 07:31:19 +01:00
|
|
|
|
this.handleTerminalData(data);
|
2026-01-19 15:12:36 +01:00
|
|
|
|
};
|
|
|
|
|
|
this.session.on('terminal', this.terminalHandler);
|
2026-01-18 07:31:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Process terminal data for idle/working detection.
|
|
|
|
|
|
*
|
|
|
|
|
|
* 1. Buffers data (with size limit)
|
|
|
|
|
|
* 2. Detects working indicators → resets idle
|
|
|
|
|
|
* 3. Detects ready indicator → triggers state-specific action
|
|
|
|
|
|
* 4. Detects prompt indicators → starts idle timer
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param data - Raw terminal output data
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private handleTerminalData(data: string): void {
|
2026-01-21 04:55:48 +01:00
|
|
|
|
// BufferAccumulator handles auto-trimming when max size exceeded
|
|
|
|
|
|
this.terminalBuffer.append(data);
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
2026-01-19 12:32:36 +01:00
|
|
|
|
// Check for the definitive "ready for input" indicator
|
|
|
|
|
|
const isReady = data.includes(READY_INDICATOR);
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
|
|
|
|
|
// Detect working state
|
|
|
|
|
|
const isWorking = this.WORKING_PATTERNS.some(pattern => data.includes(pattern));
|
|
|
|
|
|
if (isWorking) {
|
|
|
|
|
|
this.workingDetected = true;
|
|
|
|
|
|
this.promptDetected = false;
|
|
|
|
|
|
this.lastActivityTime = Date.now();
|
|
|
|
|
|
this.clearIdleTimer();
|
2026-01-19 13:08:53 +01:00
|
|
|
|
|
|
|
|
|
|
// If we're monitoring init and work started, go to watching (no kickstart needed)
|
|
|
|
|
|
if (this._state === 'monitoring_init') {
|
|
|
|
|
|
this.log('/init triggered work, skipping kickstart');
|
|
|
|
|
|
this.emit('stepCompleted', 'init');
|
|
|
|
|
|
this.completeCycle();
|
|
|
|
|
|
}
|
2026-01-18 07:31:19 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 15:00:49 +01:00
|
|
|
|
// Detect ready state (↵ send indicator)
|
2026-01-19 12:32:36 +01:00
|
|
|
|
if (isReady) {
|
|
|
|
|
|
this.promptDetected = true;
|
|
|
|
|
|
this.workingDetected = false;
|
|
|
|
|
|
this.lastActivityTime = Date.now();
|
|
|
|
|
|
this.log('Ready indicator detected (↵ send)');
|
|
|
|
|
|
|
2026-01-19 15:00:49 +01:00
|
|
|
|
// Handle based on current state
|
2026-01-19 12:32:36 +01:00
|
|
|
|
switch (this._state) {
|
|
|
|
|
|
case 'watching':
|
2026-01-19 15:00:49 +01:00
|
|
|
|
// Start idle timer instead of immediate action - gives user time to type
|
|
|
|
|
|
this.startIdleTimer();
|
2026-01-19 12:32:36 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case 'waiting_update':
|
|
|
|
|
|
this.checkUpdateComplete();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'waiting_clear':
|
|
|
|
|
|
this.checkClearComplete();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'waiting_init':
|
|
|
|
|
|
this.checkInitComplete();
|
|
|
|
|
|
break;
|
2026-01-19 13:08:53 +01:00
|
|
|
|
case 'monitoring_init':
|
|
|
|
|
|
this.checkMonitoringInitIdle();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'waiting_kickstart':
|
|
|
|
|
|
this.checkKickstartComplete();
|
|
|
|
|
|
break;
|
2026-01-19 12:32:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback: detect prompt characters - start timeout-based check
|
2026-01-18 07:31:19 +01:00
|
|
|
|
const hasPrompt = this.PROMPT_PATTERNS.some(pattern => data.includes(pattern));
|
|
|
|
|
|
if (hasPrompt) {
|
|
|
|
|
|
const wasPromptDetected = this.promptDetected;
|
|
|
|
|
|
this.promptDetected = true;
|
|
|
|
|
|
this.workingDetected = false;
|
2026-01-19 12:32:36 +01:00
|
|
|
|
this.lastActivityTime = Date.now();
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
|
|
|
|
|
if (!wasPromptDetected) {
|
|
|
|
|
|
this.log('Prompt detected');
|
2026-01-19 12:32:36 +01:00
|
|
|
|
// Start fallback timeout for watching state
|
2026-01-18 07:31:19 +01:00
|
|
|
|
if (this._state === 'watching') {
|
|
|
|
|
|
this.startIdleTimer();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-19 12:32:36 +01:00
|
|
|
|
}
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Handle update step completion.
|
|
|
|
|
|
* Called when ready indicator detected in waiting_update state.
|
|
|
|
|
|
* Proceeds to clear, init, or completes cycle based on config.
|
|
|
|
|
|
* @fires stepCompleted - With step 'update'
|
|
|
|
|
|
*/
|
2026-01-19 12:32:36 +01:00
|
|
|
|
private checkUpdateComplete(): void {
|
|
|
|
|
|
this.clearIdleTimer();
|
|
|
|
|
|
this.log('Update completed (ready indicator)');
|
|
|
|
|
|
this.emit('stepCompleted', 'update');
|
|
|
|
|
|
|
|
|
|
|
|
if (this.config.sendClear) {
|
|
|
|
|
|
this.sendClear();
|
|
|
|
|
|
} else if (this.config.sendInit) {
|
|
|
|
|
|
this.sendInit();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.completeCycle();
|
2026-01-18 07:31:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Handle /clear step completion.
|
|
|
|
|
|
* Proceeds to init or completes cycle based on config.
|
|
|
|
|
|
* @fires stepCompleted - With step 'clear'
|
|
|
|
|
|
*/
|
2026-01-19 12:32:36 +01:00
|
|
|
|
private checkClearComplete(): void {
|
|
|
|
|
|
this.clearIdleTimer();
|
|
|
|
|
|
this.log('/clear completed (ready indicator)');
|
|
|
|
|
|
this.emit('stepCompleted', 'clear');
|
|
|
|
|
|
|
|
|
|
|
|
if (this.config.sendInit) {
|
|
|
|
|
|
this.sendInit();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.completeCycle();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Handle /init step completion.
|
|
|
|
|
|
* If kickstart is configured, monitors for work.
|
|
|
|
|
|
* Otherwise completes cycle.
|
|
|
|
|
|
* @fires stepCompleted - With step 'init' (if no kickstart)
|
|
|
|
|
|
*/
|
2026-01-19 12:32:36 +01:00
|
|
|
|
private checkInitComplete(): void {
|
|
|
|
|
|
this.clearIdleTimer();
|
|
|
|
|
|
this.log('/init completed (ready indicator)');
|
2026-01-19 13:08:53 +01:00
|
|
|
|
|
|
|
|
|
|
// If kickstart prompt is configured, monitor to see if /init triggered work
|
|
|
|
|
|
if (this.config.kickstartPrompt) {
|
|
|
|
|
|
this.startMonitoringInit();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.emit('stepCompleted', 'init');
|
|
|
|
|
|
this.completeCycle();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Start monitoring to see if /init triggered work.
|
|
|
|
|
|
* Enters 'monitoring_init' state and waits 3s grace period.
|
|
|
|
|
|
* If no work detected, sends kickstart prompt.
|
|
|
|
|
|
*/
|
2026-01-19 13:08:53 +01:00
|
|
|
|
private startMonitoringInit(): void {
|
|
|
|
|
|
this.setState('monitoring_init');
|
2026-01-21 04:55:48 +01:00
|
|
|
|
this.terminalBuffer.clear();
|
2026-01-19 13:08:53 +01:00
|
|
|
|
this.workingDetected = false;
|
|
|
|
|
|
this.log('Monitoring if /init triggered work...');
|
|
|
|
|
|
|
|
|
|
|
|
// Give Claude a moment to start working before checking for idle
|
|
|
|
|
|
this.stepTimer = setTimeout(() => {
|
|
|
|
|
|
// If still in monitoring state and no work detected, consider it idle
|
|
|
|
|
|
if (this._state === 'monitoring_init' && !this.workingDetected) {
|
|
|
|
|
|
this.checkMonitoringInitIdle();
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 3000); // 3 second grace period for /init to trigger work
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Handle monitoring timeout when /init didn't trigger work.
|
|
|
|
|
|
* Sends kickstart prompt as fallback.
|
|
|
|
|
|
* @fires stepCompleted - With step 'init'
|
|
|
|
|
|
*/
|
2026-01-19 13:08:53 +01:00
|
|
|
|
private checkMonitoringInitIdle(): void {
|
|
|
|
|
|
this.clearIdleTimer();
|
|
|
|
|
|
if (this.stepTimer) {
|
|
|
|
|
|
clearTimeout(this.stepTimer);
|
|
|
|
|
|
this.stepTimer = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.log('/init did not trigger work, sending kickstart prompt');
|
2026-01-19 12:32:36 +01:00
|
|
|
|
this.emit('stepCompleted', 'init');
|
2026-01-19 13:08:53 +01:00
|
|
|
|
this.sendKickstart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Send the kickstart prompt to get Claude working.
|
|
|
|
|
|
* @fires stepSent - With step 'kickstart'
|
|
|
|
|
|
*/
|
2026-01-19 13:08:53 +01:00
|
|
|
|
private sendKickstart(): void {
|
|
|
|
|
|
this.setState('sending_kickstart');
|
2026-01-21 04:55:48 +01:00
|
|
|
|
this.terminalBuffer.clear();
|
2026-01-19 13:08:53 +01:00
|
|
|
|
|
|
|
|
|
|
this.stepTimer = setTimeout(() => {
|
|
|
|
|
|
const prompt = this.config.kickstartPrompt!;
|
|
|
|
|
|
this.log(`Sending kickstart prompt: "${prompt}"`);
|
2026-01-19 15:35:41 +01:00
|
|
|
|
this.session.writeViaScreen(prompt + '\r'); // \r triggers key.return in Ink/Claude CLI
|
2026-01-19 13:08:53 +01:00
|
|
|
|
this.emit('stepSent', 'kickstart', prompt);
|
|
|
|
|
|
this.setState('waiting_kickstart');
|
|
|
|
|
|
this.promptDetected = false;
|
|
|
|
|
|
this.workingDetected = false;
|
|
|
|
|
|
}, this.config.interStepDelayMs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Handle kickstart step completion.
|
|
|
|
|
|
* @fires stepCompleted - With step 'kickstart'
|
|
|
|
|
|
*/
|
2026-01-19 13:08:53 +01:00
|
|
|
|
private checkKickstartComplete(): void {
|
|
|
|
|
|
this.clearIdleTimer();
|
|
|
|
|
|
this.log('Kickstart completed (ready indicator)');
|
|
|
|
|
|
this.emit('stepCompleted', 'kickstart');
|
2026-01-19 12:32:36 +01:00
|
|
|
|
this.completeCycle();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Start the idle detection timer.
|
|
|
|
|
|
* After idleTimeoutMs, triggers onIdleDetected if still idle.
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private startIdleTimer(): void {
|
|
|
|
|
|
this.clearIdleTimer();
|
|
|
|
|
|
|
|
|
|
|
|
this.idleTimer = setTimeout(() => {
|
|
|
|
|
|
// Double-check we're still idle and in watching state
|
|
|
|
|
|
if (this._state === 'watching' && this.promptDetected && !this.workingDetected) {
|
|
|
|
|
|
const timeSinceActivity = Date.now() - this.lastActivityTime;
|
|
|
|
|
|
this.log(`Idle timeout fired (${timeSinceActivity}ms since last activity)`);
|
|
|
|
|
|
this.onIdleDetected();
|
|
|
|
|
|
}
|
|
|
|
|
|
}, this.config.idleTimeoutMs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Clear the idle detection timer if running */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private clearIdleTimer(): void {
|
|
|
|
|
|
if (this.idleTimer) {
|
|
|
|
|
|
clearTimeout(this.idleTimer);
|
|
|
|
|
|
this.idleTimer = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/** Clear all timers (idle and step) */
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private clearTimers(): void {
|
|
|
|
|
|
this.clearIdleTimer();
|
|
|
|
|
|
if (this.stepTimer) {
|
|
|
|
|
|
clearTimeout(this.stepTimer);
|
|
|
|
|
|
this.stepTimer = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Handle confirmed idle detection.
|
|
|
|
|
|
* Starts a new respawn cycle.
|
|
|
|
|
|
* @fires respawnCycleStarted
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private onIdleDetected(): void {
|
|
|
|
|
|
if (this._state !== 'watching') {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start the respawn cycle
|
|
|
|
|
|
this.cycleCount++;
|
|
|
|
|
|
this.log(`Starting respawn cycle #${this.cycleCount}`);
|
|
|
|
|
|
this.emit('respawnCycleStarted', this.cycleCount);
|
|
|
|
|
|
|
|
|
|
|
|
this.sendUpdateDocs();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Send the update docs prompt (first step of cycle).
|
|
|
|
|
|
* @fires stepSent - With step 'update'
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private sendUpdateDocs(): void {
|
|
|
|
|
|
this.setState('sending_update');
|
2026-01-21 04:55:48 +01:00
|
|
|
|
this.terminalBuffer.clear(); // Clear buffer for fresh detection
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
|
|
|
|
|
this.stepTimer = setTimeout(() => {
|
2026-01-19 16:39:45 +01:00
|
|
|
|
const input = this.config.updatePrompt + '\r'; // \r triggers Enter in Ink/Claude CLI
|
2026-01-18 07:31:19 +01:00
|
|
|
|
this.log(`Sending update prompt: "${this.config.updatePrompt}"`);
|
2026-01-19 15:35:41 +01:00
|
|
|
|
this.session.writeViaScreen(input);
|
2026-01-18 07:31:19 +01:00
|
|
|
|
this.emit('stepSent', 'update', this.config.updatePrompt);
|
|
|
|
|
|
this.setState('waiting_update');
|
|
|
|
|
|
this.promptDetected = false;
|
|
|
|
|
|
this.workingDetected = false;
|
|
|
|
|
|
}, this.config.interStepDelayMs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Send /clear command.
|
|
|
|
|
|
* @fires stepSent - With step 'clear'
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private sendClear(): void {
|
|
|
|
|
|
this.setState('sending_clear');
|
2026-01-21 04:55:48 +01:00
|
|
|
|
this.terminalBuffer.clear();
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
|
|
|
|
|
this.stepTimer = setTimeout(() => {
|
|
|
|
|
|
this.log('Sending /clear');
|
2026-01-19 16:39:45 +01:00
|
|
|
|
this.session.writeViaScreen('/clear\r'); // \r triggers Enter in Ink/Claude CLI
|
2026-01-18 07:31:19 +01:00
|
|
|
|
this.emit('stepSent', 'clear', '/clear');
|
|
|
|
|
|
this.setState('waiting_clear');
|
|
|
|
|
|
this.promptDetected = false;
|
|
|
|
|
|
}, this.config.interStepDelayMs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Send /init command.
|
|
|
|
|
|
* @fires stepSent - With step 'init'
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private sendInit(): void {
|
|
|
|
|
|
this.setState('sending_init');
|
2026-01-21 04:55:48 +01:00
|
|
|
|
this.terminalBuffer.clear();
|
2026-01-18 07:31:19 +01:00
|
|
|
|
|
|
|
|
|
|
this.stepTimer = setTimeout(() => {
|
|
|
|
|
|
this.log('Sending /init');
|
2026-01-19 16:39:45 +01:00
|
|
|
|
this.session.writeViaScreen('/init\r'); // \r triggers Enter in Ink/Claude CLI
|
2026-01-18 07:31:19 +01:00
|
|
|
|
this.emit('stepSent', 'init', '/init');
|
|
|
|
|
|
this.setState('waiting_init');
|
|
|
|
|
|
this.promptDetected = false;
|
|
|
|
|
|
this.workingDetected = false;
|
|
|
|
|
|
}, this.config.interStepDelayMs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Complete the current respawn cycle.
|
|
|
|
|
|
* Returns to watching state for next cycle.
|
|
|
|
|
|
* @fires respawnCycleCompleted
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private completeCycle(): void {
|
|
|
|
|
|
this.log(`Respawn cycle #${this.cycleCount} completed`);
|
|
|
|
|
|
this.emit('respawnCycleCompleted', this.cycleCount);
|
|
|
|
|
|
|
|
|
|
|
|
// Go back to watching state for next cycle
|
|
|
|
|
|
this.setState('watching');
|
2026-01-21 04:55:48 +01:00
|
|
|
|
this.terminalBuffer.clear();
|
2026-01-18 07:31:19 +01:00
|
|
|
|
this.promptDetected = false;
|
|
|
|
|
|
this.workingDetected = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 03:46:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Check if already idle and start cycle if so.
|
|
|
|
|
|
* Used when resuming from pause.
|
|
|
|
|
|
*/
|
2026-01-18 07:31:19 +01:00
|
|
|
|
private checkIdleAndMaybeStart(): void {
|
|
|
|
|
|
// Check if already idle
|
|
|
|
|
|
const timeSinceActivity = Date.now() - this.lastActivityTime;
|
|
|
|
|
|
if (timeSinceActivity > this.config.idleTimeoutMs && this.promptDetected) {
|
|
|
|
|
|
this.onIdleDetected();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Update configuration at runtime.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Merges provided config with existing config.
|
|
|
|
|
|
* Takes effect immediately for new operations.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param config - Partial configuration to merge
|
|
|
|
|
|
* @fires log - With updated config details
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
updateConfig(config: Partial<RespawnConfig>): void {
|
|
|
|
|
|
this.config = { ...this.config, ...config };
|
|
|
|
|
|
this.log(`Config updated: ${JSON.stringify(config)}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Get current configuration.
|
|
|
|
|
|
* @returns Copy of current config (safe to modify)
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
getConfig(): RespawnConfig {
|
|
|
|
|
|
return { ...this.config };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 03:46:11 +01:00
|
|
|
|
* Get comprehensive status information.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Useful for debugging and monitoring.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns Status object with:
|
|
|
|
|
|
* - state: Current state machine state
|
|
|
|
|
|
* - cycleCount: Number of cycles started
|
|
|
|
|
|
* - lastActivityTime: Timestamp of last activity
|
|
|
|
|
|
* - timeSinceActivity: Milliseconds since last activity
|
|
|
|
|
|
* - promptDetected: Whether prompt indicator seen
|
|
|
|
|
|
* - workingDetected: Whether working indicator seen
|
|
|
|
|
|
* - config: Current configuration
|
2026-01-18 07:31:19 +01:00
|
|
|
|
*/
|
|
|
|
|
|
getStatus() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
state: this._state,
|
|
|
|
|
|
cycleCount: this.cycleCount,
|
|
|
|
|
|
lastActivityTime: this.lastActivityTime,
|
|
|
|
|
|
timeSinceActivity: Date.now() - this.lastActivityTime,
|
|
|
|
|
|
promptDetected: this.promptDetected,
|
|
|
|
|
|
workingDetected: this.workingDetected,
|
|
|
|
|
|
config: this.config,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|