Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix: security hardening and performance optimization across 4 modules

Claudece0da6205487
diff --git a/v3/@claude-flow/guidance/src/adversarial.ts b/v3/@claude-flow/guidance/src/adversarial.ts
index c5de8d0d04..d50ce4ead3 100644
--- a/v3/@claude-flow/guidance/src/adversarial.ts
+++ b/v3/@claude-flow/guidance/src/adversarial.ts
@@ -483,12 +483,15 @@ export class CollusionDetector {
detectCollusion(): CollusionReport {
const patterns: CollusionReport['suspiciousPatterns'] = [];
+ // Build graph once and pass to all detectors (avoids 3x rebuild)
+ const graph = this.getInteractionGraph();
+
// Detect ring topologies
- const rings = this.detectRingTopologies();
+ const rings = this.detectRingTopologies(graph);
patterns.push(...rings);
// Detect unusual frequency
- const frequency = this.detectUnusualFrequency();
+ const frequency = this.detectUnusualFrequency(graph);
patterns.push(...frequency);
// Detect coordinated timing
@@ -522,9 +525,8 @@ export class CollusionDetector {
/**
* Detect ring topology patterns (A→B→C→A)
*/
- private detectRingTopologies(): CollusionReport['suspiciousPatterns'] {
+ private detectRingTopologies(graph: Map<string, Map<string, number>>): CollusionReport['suspiciousPatterns'] {
const patterns: CollusionReport['suspiciousPatterns'] = [];
- const graph = this.getInteractionGraph();
// Simple cycle detection using DFS
const visited = new Set<string>();
@@ -574,9 +576,8 @@ export class CollusionDetector {
/**
* Detect unusual interaction frequency between specific pairs
*/
- private detectUnusualFrequency(): CollusionReport['suspiciousPatterns'] {
+ private detectUnusualFrequency(graph: Map<string, Map<string, number>>): CollusionReport['suspiciousPatterns'] {
const patterns: CollusionReport['suspiciousPatterns'] = [];
- const graph = this.getInteractionGraph();
for (const [from, targets] of graph) {
for (const [to, count] of targets) {
@@ -666,10 +667,16 @@ export class MemoryQuorum {
this.proposals.set(proposalId, proposal);
- // Evict oldest proposals if at capacity
+ // Evict oldest proposal if at capacity (O(n) min-find, not O(n log n) sort)
if (this.proposals.size > this.maxProposals) {
- const oldestId = Array.from(this.proposals.entries())
- .sort((a, b) => a[1].timestamp - b[1].timestamp)[0]?.[0];
+ let oldestId: string | undefined;
+ let oldestTimestamp = Infinity;
+ for (const [id, proposal] of this.proposals) {
+ if (proposal.timestamp < oldestTimestamp) {
+ oldestTimestamp = proposal.timestamp;
+ oldestId = id;
+ }
+ }
if (oldestId) {
this.proposals.delete(oldestId);
}
diff --git a/v3/@claude-flow/guidance/src/authority.ts b/v3/@claude-flow/guidance/src/authority.ts
index bca08736ac..97533e2f5e 100644
--- a/v3/@claude-flow/guidance/src/authority.ts
+++ b/v3/@claude-flow/guidance/src/authority.ts
@@ -658,8 +658,23 @@ export class IrreversibilityClassifier {
/**
* Add a pattern to a classification.
+ *
+ * Validates the pattern against ReDoS heuristics before accepting it.
+ * Rejects patterns with nested quantifiers (e.g., `(a+)+`) that can
+ * cause catastrophic backtracking.
+ *
+ * @throws Error if the pattern is invalid regex or contains ReDoS-prone constructs
*/
addPattern(classification: IrreversibilityClass, pattern: string): void {
+ // ReDoS heuristic: reject nested quantifiers like (a+)+, (a*)+, (a+)*, etc.
+ if (/([+*]|\{[0-9]+,?\})\s*\)[\s]*[+*]|\{[0-9]+,?\}/.test(pattern)) {
+ throw new Error(`Pattern rejected: nested quantifiers detected (potential ReDoS): ${pattern}`);
+ }
+ // Also reject patterns longer than 500 chars as a sanity bound
+ if (pattern.length > 500) {
+ throw new Error(`Pattern rejected: exceeds maximum length of 500 characters`);
+ }
+
const regex = new RegExp(pattern, 'i');
switch (classification) {
diff --git a/v3/@claude-flow/guidance/src/continue-gate.ts b/v3/@claude-flow/guidance/src/continue-gate.ts
index 1d36985419..b7ed753cd5 100644
--- a/v3/@claude-flow/guidance/src/continue-gate.ts
+++ b/v3/@claude-flow/guidance/src/continue-gate.ts
@@ -316,21 +316,35 @@ export class ContinueGate {
evaluateWithHistory(context: StepContext): ContinueDecision {
const now = Date.now();
- // Cooldown check
+ // Cooldown check — but always evaluate critical stop conditions
+ // to prevent agents from timing steps to bypass safety checks
if (now - this.lastEvaluationTime < this.config.cooldownMs) {
- // Too soon since last evaluation; return continue with minimal overhead
- const decision: ContinueDecision = {
+ // Even during cooldown, check hard-stop conditions
+ if (context.coherenceScore < this.config.minCoherenceForContinue) {
+ return this.createDecision('stop',
+ ['Coherence below threshold (checked during cooldown)'],
+ { budgetSlope: 0, reworkRatio: 0, stepsUntilCheckpoint: 0, coherenceLevel: 'critical', uncertaintyLevel: 'low' },
+ 'Halt execution and review coherence issues');
+ }
+ if (context.budgetRemaining.tokens <= 0 || context.budgetRemaining.toolCalls <= 0 || context.budgetRemaining.timeMs <= 0) {
+ return this.createDecision('stop',
+ ['Budget exhausted (checked during cooldown)'],
+ { budgetSlope: 0, reworkRatio: 0, stepsUntilCheckpoint: 0, coherenceLevel: 'healthy', uncertaintyLevel: 'low' },
+ 'Increase budget or simplify task scope');
+ }
+
+ // Non-critical checks can be skipped during cooldown
+ return {
decision: 'continue',
reasons: ['Cooldown period active; skipping full evaluation'],
metrics: {
budgetSlope: 0,
reworkRatio: 0,
stepsUntilCheckpoint: 0,
- coherenceLevel: 'healthy',
- uncertaintyLevel: 'low',
+ coherenceLevel: this.getCoherenceLevel(context.coherenceScore),
+ uncertaintyLevel: this.getUncertaintyLevel(context.uncertaintyScore),
},
};
- return decision;
}
this.lastEvaluationTime = now;
diff --git a/v3/@claude-flow/guidance/src/gateway.ts b/v3/@claude-flow/guidance/src/gateway.ts
index 6032c702e7..2e3b73bf01 100644
--- a/v3/@claude-flow/guidance/src/gateway.ts
+++ b/v3/@claude-flow/guidance/src/gateway.ts
@@ -107,6 +107,8 @@ export interface ToolGatewayConfig {
budget?: Partial<Budget>;
/** Default TTL for idempotency records in milliseconds */
idempotencyTtlMs?: number;
+ /** Maximum idempotency cache entries (default 10000) */
+ maxCacheSize?: number;
/** If true, evidence must be non-empty for allow decisions */
requireEvidence?: boolean;
/** Gate configuration passed through to EnforcementGates */
@@ -122,8 +124,11 @@ export class DeterministicToolGateway {
private readonly schemas: Map<string, ToolSchema>;
private budget: Budget;
private readonly idempotencyTtlMs: number;
+ private readonly maxCacheSize: number;
private readonly requireEvidence: boolean;
private readonly idempotencyCache: Map<string, IdempotencyRecord> = new Map();
+ private lastCleanupTime = 0;
+ private static readonly CLEANUP_INTERVAL_MS = 30_000; // batch cleanup every 30s
constructor(config: ToolGatewayConfig = {}) {
this.gates = new EnforcementGates(config.gateConfig);
@@ -139,6 +144,7 @@ export class DeterministicToolGateway {
// Merge partial budget with defaults
this.budget = this.mergeBudget(config.budget);
this.idempotencyTtlMs = config.idempotencyTtlMs ?? 300_000; // 5 minutes default
+ this.maxCacheSize = config.maxCacheSize ?? 10_000;
this.requireEvidence = config.requireEvidence ?? false;
}
@@ -163,8 +169,8 @@ export class DeterministicToolGateway {
): GatewayDecision {
const evidence: Record<string, unknown> = {};
- // Step 1: Idempotency check
- this.cleanExpiredIdempotency();
+ // Step 1: Idempotency check (batch cleanup on interval, not every call)
+ this.maybeCleanExpiredIdempotency();
const idempotencyKey = this.getIdempotencyKey(toolName, params);
const cached = this.idempotencyCache.get(idempotencyKey);
@@ -313,7 +319,7 @@ export class DeterministicToolGateway {
this.budget.costBudget.usedUsd += (tokenCount / 1000) * 0.003;
}
- // Store in idempotency cache
+ // Store in idempotency cache with size-based eviction
const key = this.getIdempotencyKey(toolName, params);
const paramsHash = this.computeParamsHash(params);
@@ -325,6 +331,18 @@ export class DeterministicToolGateway {
timestamp: Date.now(),
ttlMs: this.idempotencyTtlMs,
});
+
+ // Evict oldest entries if cache exceeds max size
+ if (this.idempotencyCache.size > this.maxCacheSize) {
+ // Map iterates in insertion order; delete the first (oldest) entries
+ const excess = this.idempotencyCache.size - this.maxCacheSize;
+ let removed = 0;
+ for (const [k] of this.idempotencyCache) {
+ if (removed >= excess) break;
+ this.idempotencyCache.delete(k);
+ removed++;
+ }
+ }
}
/**
@@ -464,10 +482,15 @@ export class DeterministicToolGateway {
// =========================================================================
/**
- * Remove expired idempotency records.
+ * Remove expired idempotency records (batched on interval to avoid per-call overhead).
*/
- private cleanExpiredIdempotency(): void {
+ private maybeCleanExpiredIdempotency(): void {
const now = Date.now();
+ if (now - this.lastCleanupTime < DeterministicToolGateway.CLEANUP_INTERVAL_MS) {
+ return; // Skip cleanup until interval has passed
+ }
+ this.lastCleanupTime = now;
+
for (const [key, record] of this.idempotencyCache) {
if (now - record.timestamp > record.ttlMs) {
this.idempotencyCache.delete(key);
diff --git a/v3/@claude-flow/guidance/tests/continue-gate.test.ts b/v3/@claude-flow/guidance/tests/continue-gate.test.ts
new file mode 100644
index 0000000000..d6d5c428e7
--- /dev/null
+++ b/v3/@claude-flow/guidance/tests/continue-gate.test.ts
@@ -0,0 +1,527 @@
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+import {
+ type ContinueGateConfig,
+ type StepContext,
+ type ContinueDecision,
+ ContinueGate,
+ createContinueGate,
+} from '../src/continue-gate.ts';
+
+// ============================================================================
+// Helpers
+// ============================================================================
+
+function makeContext(overrides: Partial<StepContext> = {}): StepContext {
+ return {
+ stepNumber: 10,
+ totalTokensUsed: 5000,
+ totalToolCalls: 20,
+ reworkCount: 1,
+ coherenceScore: 0.8,
+ uncertaintyScore: 0.3,
+ elapsedMs: 30000,
+ lastCheckpointStep: 0,
+ budgetRemaining: {
+ tokens: 100000,
+ toolCalls: 500,
+ timeMs: 600000,
+ },
+ recentDecisions: [],
+ ...overrides,
+ };
+}
+
+// ============================================================================
+// Factory Function
+// ============================================================================
+
+describe('createContinueGate()', () => {
+ it('should create a ContinueGate with default config', () => {
+ const gate = createContinueGate();
+ expect(gate).toBeInstanceOf(ContinueGate);
+ const config = gate.getConfig();
+ expect(config.maxConsecutiveSteps).toBe(100);
+ expect(config.maxBudgetSlopePerStep).toBe(0.02);
+ expect(config.minCoherenceForContinue).toBe(0.4);
+ expect(config.maxUncertaintyForContinue).toBe(0.8);
+ expect(config.maxReworkRatio).toBe(0.3);
+ expect(config.checkpointIntervalSteps).toBe(25);
+ expect(config.cooldownMs).toBe(5000);
+ });
+
+ it('should create a ContinueGate with custom config', () => {
+ const gate = createContinueGate({
+ maxConsecutiveSteps: 50,
+ minCoherenceForContinue: 0.6,
+ });
+ const config = gate.getConfig();
+ expect(config.maxConsecutiveSteps).toBe(50);
+ expect(config.minCoherenceForContinue).toBe(0.6);
+ // defaults preserved
+ expect(config.maxBudgetSlopePerStep).toBe(0.02);
+ });
+});
+
+// ============================================================================
+// ContinueGate.evaluate() - Decision Logic
+// ============================================================================
+
+describe('ContinueGate.evaluate()', () => {
+ let gate: ContinueGate;
+
+ beforeEach(() => {
+ gate = new ContinueGate();
+ });
+
+ // --- continue ---
+
+ it('should return continue when all checks pass', () => {
+ const decision = gate.evaluate(makeContext());
+ expect(decision.decision).toBe('continue');
+ expect(decision.reasons).toContain('All checks passed');
+ });
+
+ // --- stop: coherence ---
+
+ it('should return stop when coherence is below threshold', () => {
+ const decision = gate.evaluate(makeContext({ coherenceScore: 0.2 }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('Coherence below threshold');
+ expect(decision.metrics.coherenceLevel).toBe('critical');
+ });
+
+ it('should return stop when coherence is exactly at threshold', () => {
+ // 0.4 is minCoherenceForContinue; < 0.4 → stop
+ const decision = gate.evaluate(makeContext({ coherenceScore: 0.39 }));
+ expect(decision.decision).toBe('stop');
+ });
+
+ it('should continue when coherence equals the threshold', () => {
+ const decision = gate.evaluate(makeContext({ coherenceScore: 0.4 }));
+ expect(decision.decision).not.toBe('stop');
+ });
+
+ // --- stop: step limit ---
+
+ it('should return stop when step limit exceeded without recent checkpoint', () => {
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 100,
+ lastCheckpointStep: 50, // 100 - 50 = 50 >= 25
+ }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('Step limit exceeded');
+ });
+
+ it('should not stop at step limit if recent checkpoint exists', () => {
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 100,
+ lastCheckpointStep: 90, // 100 - 90 = 10 < 25
+ }));
+ expect(decision.decision).not.toBe('stop');
+ });
+
+ // --- stop: budget exhausted ---
+
+ it('should return stop when token budget is exhausted', () => {
+ const decision = gate.evaluate(makeContext({
+ budgetRemaining: { tokens: 0, toolCalls: 500, timeMs: 600000 },
+ }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('Budget exhausted');
+ expect(decision.reasons[0]).toContain('tokens');
+ });
+
+ it('should return stop when tool call budget is exhausted', () => {
+ const decision = gate.evaluate(makeContext({
+ budgetRemaining: { tokens: 100000, toolCalls: 0, timeMs: 600000 },
+ }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('tool calls');
+ });
+
+ it('should return stop when time budget is exhausted', () => {
+ const decision = gate.evaluate(makeContext({
+ budgetRemaining: { tokens: 100000, toolCalls: 500, timeMs: 0 },
+ }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('time');
+ });
+
+ it('should list all exhausted budgets', () => {
+ const decision = gate.evaluate(makeContext({
+ budgetRemaining: { tokens: 0, toolCalls: 0, timeMs: 0 },
+ }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('tokens');
+ expect(decision.reasons[0]).toContain('tool calls');
+ expect(decision.reasons[0]).toContain('time');
+ });
+
+ // --- pause: rework ratio ---
+
+ it('should return pause when rework ratio exceeds threshold', () => {
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 10,
+ reworkCount: 4, // 4/10 = 0.4 > 0.3
+ }));
+ expect(decision.decision).toBe('pause');
+ expect(decision.reasons[0]).toContain('Rework ratio too high');
+ expect(decision.metrics.reworkRatio).toBeCloseTo(0.4);
+ });
+
+ it('should not pause when rework ratio is at threshold', () => {
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 10,
+ reworkCount: 3, // 3/10 = 0.3, not > 0.3
+ }));
+ expect(decision.decision).not.toBe('pause');
+ });
+
+ // --- pause: uncertainty ---
+
+ it('should return pause when uncertainty exceeds threshold', () => {
+ const decision = gate.evaluate(makeContext({ uncertaintyScore: 0.9 }));
+ expect(decision.decision).toBe('pause');
+ expect(decision.reasons[0]).toContain('Uncertainty too high');
+ expect(decision.metrics.uncertaintyLevel).toBe('extreme');
+ });
+
+ it('should not pause when uncertainty is at threshold', () => {
+ const decision = gate.evaluate(makeContext({ uncertaintyScore: 0.8 }));
+ expect(decision.decision).not.toBe('pause');
+ });
+
+ // --- throttle: budget acceleration ---
+
+ it('should return throttle when budget slope is accelerating', () => {
+ const gate = new ContinueGate({ maxBudgetSlopePerStep: 0.02 });
+
+ // Feed increasing token usage to build up slope
+ for (let step = 1; step <= 10; step++) {
+ gate.evaluate(makeContext({
+ stepNumber: step,
+ totalTokensUsed: step * step * 1000, // quadratic growth
+ }));
+ }
+
+ // The 11th step should detect the high slope
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 11,
+ totalTokensUsed: 11 * 11 * 1000,
+ }));
+
+ // With quadratic growth, the slope should be positive and large
+ expect(decision.metrics.budgetSlope).toBeGreaterThan(0);
+ });
+
+ // --- checkpoint ---
+
+ it('should return checkpoint when interval reached', () => {
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 25,
+ lastCheckpointStep: 0, // 25 - 0 = 25 >= 25
+ }));
+ expect(decision.decision).toBe('checkpoint');
+ expect(decision.reasons[0]).toContain('Checkpoint interval reached');
+ });
+
+ it('should not checkpoint before interval', () => {
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 24,
+ lastCheckpointStep: 0, // 24 - 0 = 24 < 25
+ }));
+ expect(decision.decision).toBe('continue');
+ });
+
+ // --- priority order ---
+
+ it('should prioritize stop over pause', () => {
+ // Both coherence below threshold (stop) and high rework (pause)
+ const decision = gate.evaluate(makeContext({
+ coherenceScore: 0.2,
+ reworkCount: 5,
+ stepNumber: 10,
+ }));
+ expect(decision.decision).toBe('stop');
+ });
+
+ it('should prioritize pause over throttle', () => {
+ // High rework (pause) with some budget acceleration
+ const gate = new ContinueGate();
+ // Build slope data
+ for (let step = 1; step <= 10; step++) {
+ gate.evaluate(makeContext({
+ stepNumber: step,
+ totalTokensUsed: step * step * 1000,
+ reworkCount: 0,
+ }));
+ }
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 11,
+ totalTokensUsed: 11 * 11 * 1000,
+ reworkCount: 5, // 5/11 > 0.3
+ }));
+ expect(decision.decision).toBe('pause');
+ });
+
+ // --- metrics ---
+
+ it('should compute coherence level correctly', () => {
+ expect(gate.evaluate(makeContext({ coherenceScore: 0.8 })).metrics.coherenceLevel).toBe('healthy');
+ expect(gate.evaluate(makeContext({ coherenceScore: 0.5 })).metrics.coherenceLevel).toBe('degraded');
+ });
+
+ it('should compute uncertainty level correctly', () => {
+ expect(gate.evaluate(makeContext({ uncertaintyScore: 0.2 })).metrics.uncertaintyLevel).toBe('low');
+ expect(gate.evaluate(makeContext({ uncertaintyScore: 0.5 })).metrics.uncertaintyLevel).toBe('moderate');
+ expect(gate.evaluate(makeContext({ uncertaintyScore: 0.7 })).metrics.uncertaintyLevel).toBe('high');
+ });
+
+ it('should compute rework ratio correctly', () => {
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 20,
+ reworkCount: 4,
+ }));
+ expect(decision.metrics.reworkRatio).toBeCloseTo(0.2);
+ });
+
+ it('should handle zero step number (division by zero)', () => {
+ const decision = gate.evaluate(makeContext({ stepNumber: 0, reworkCount: 0 }));
+ expect(decision.metrics.reworkRatio).toBe(0);
+ });
+
+ it('should include recommended action for non-continue decisions', () => {
+ const decision = gate.evaluate(makeContext({ coherenceScore: 0.1 }));
+ expect(decision.recommendedAction).toBeDefined();
+ expect(decision.recommendedAction).toContain('coherence');
+ });
+});
+
+// ============================================================================
+// ContinueGate.evaluateWithHistory() - History and Cooldown
+// ============================================================================
+
+describe('ContinueGate.evaluateWithHistory()', () => {
+ it('should record evaluations in history', () => {
+ const gate = new ContinueGate({ cooldownMs: 0 });
+ gate.evaluateWithHistory(makeContext({ stepNumber: 1 }));
+ gate.evaluateWithHistory(makeContext({ stepNumber: 2 }));
+
+ const history = gate.getHistory();
+ expect(history).toHaveLength(2);
+ expect(history[0].step).toBe(1);
+ expect(history[1].step).toBe(2);
+ expect(history[0].timestamp).toBeLessThanOrEqual(history[1].timestamp);
+ });
+
+ it('should return continue with minimal metrics during cooldown', () => {
+ const gate = new ContinueGate({ cooldownMs: 60000 });
+
+ // First call proceeds normally
+ const first = gate.evaluateWithHistory(makeContext());
+ expect(first.decision).toBe('continue');
+
+ // Second call within cooldown
+ const second = gate.evaluateWithHistory(makeContext());
+ expect(second.decision).toBe('continue');
+ expect(second.reasons[0]).toContain('Cooldown');
+ });
+
+ it('should still stop during cooldown if coherence collapses', () => {
+ const gate = new ContinueGate({ cooldownMs: 60000 });
+
+ // First call to set cooldown timer
+ gate.evaluateWithHistory(makeContext());
+
+ // Second call during cooldown with coherence collapse
+ const decision = gate.evaluateWithHistory(makeContext({
+ coherenceScore: 0.1,
+ }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('Coherence below threshold');
+ });
+
+ it('should still stop during cooldown if budget exhausted', () => {
+ const gate = new ContinueGate({ cooldownMs: 60000 });
+
+ // First call to set cooldown timer
+ gate.evaluateWithHistory(makeContext());
+
+ // Second call during cooldown with budget exhausted
+ const decision = gate.evaluateWithHistory(makeContext({
+ budgetRemaining: { tokens: 0, toolCalls: 500, timeMs: 600000 },
+ }));
+ expect(decision.decision).toBe('stop');
+ expect(decision.reasons[0]).toContain('Budget exhausted');
+ });
+
+ it('should evict oldest entries when history exceeds max size', () => {
+ const gate = new ContinueGate({ cooldownMs: 0 });
+
+ // Fill beyond max (10000)
+ for (let i = 0; i < 10002; i++) {
+ gate.evaluateWithHistory(makeContext({ stepNumber: i }));
+ }
+
+ const history = gate.getHistory();
+ expect(history.length).toBeLessThanOrEqual(10000);
+ // Oldest entries should be evicted
+ expect(history[0].step).toBeGreaterThan(0);
+ });
+});
+
+// ============================================================================
+// ContinueGate.getStats()
+// ============================================================================
+
+describe('ContinueGate.getStats()', () => {
+ it('should return zero stats when no evaluations', () => {
+ const gate = new ContinueGate();
+ const stats = gate.getStats();
+ expect(stats.totalEvaluations).toBe(0);
+ expect(stats.decisions.continue).toBe(0);
+ expect(stats.averageBudgetSlope).toBe(0);
+ });
+
+ it('should count decisions by type', () => {
+ const gate = new ContinueGate({ cooldownMs: 0 });
+
+ // 2 continues (reworkCount: 0 to avoid pause)
+ gate.evaluateWithHistory(makeContext({ stepNumber: 1, reworkCount: 0 }));
+ gate.evaluateWithHistory(makeContext({ stepNumber: 2, reworkCount: 0 }));
+
+ // 1 stop (coherence collapse)
+ gate.evaluateWithHistory(makeContext({ stepNumber: 3, reworkCount: 0, coherenceScore: 0.1 }));
+
+ // 1 checkpoint
+ gate.evaluateWithHistory(makeContext({
+ stepNumber: 25,
+ reworkCount: 0,
+ lastCheckpointStep: 0,
+ }));
+
+ const stats = gate.getStats();
+ expect(stats.totalEvaluations).toBe(4);
+ expect(stats.decisions.continue).toBe(2);
+ expect(stats.decisions.stop).toBe(1);
+ expect(stats.decisions.checkpoint).toBe(1);
+ });
+});
+
+// ============================================================================
+// ContinueGate.reset()
+// ============================================================================
+
+describe('ContinueGate.reset()', () => {
+ it('should clear all internal state', () => {
+ const gate = new ContinueGate({ cooldownMs: 0 });
+ gate.evaluateWithHistory(makeContext({ stepNumber: 1 }));
+ gate.evaluateWithHistory(makeContext({ stepNumber: 2 }));
+ expect(gate.getHistory()).toHaveLength(2);
+
+ gate.reset();
+
+ expect(gate.getHistory()).toHaveLength(0);
+ expect(gate.getStats().totalEvaluations).toBe(0);
+ });
+});
+
+// ============================================================================
+// Budget Slope Detection (Linear Regression)
+// ============================================================================
+
+describe('Budget slope detection', () => {
+ it('should detect zero slope with constant token usage', () => {
+ const gate = new ContinueGate({ cooldownMs: 0 });
+
+ // Constant token usage: 1000 per step
+ for (let step = 1; step <= 10; step++) {
+ gate.evaluate(makeContext({
+ stepNumber: step,
+ totalTokensUsed: 1000, // same every step
+ }));
+ }
+
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 11,
+ totalTokensUsed: 1000,
+ }));
+ expect(decision.metrics.budgetSlope).toBeCloseTo(0, 1);
+ });
+
+ it('should detect positive slope with increasing token usage', () => {
+ const gate = new ContinueGate({ cooldownMs: 0 });
+
+ for (let step = 1; step <= 10; step++) {
+ gate.evaluate(makeContext({
+ stepNumber: step,
+ totalTokensUsed: step * 500, // linear growth
+ }));
+ }
+
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 11,
+ totalTokensUsed: 5500,
+ }));
+ expect(decision.metrics.budgetSlope).toBeGreaterThan(0);
+ });
+
+ it('should return zero slope with fewer than 2 data points', () => {
+ const gate = new ContinueGate({ cooldownMs: 0 });
+ const decision = gate.evaluate(makeContext({ stepNumber: 1, totalTokensUsed: 1000 }));
+ expect(decision.metrics.budgetSlope).toBe(0);
+ });
+});
+
+// ============================================================================
+// ContinueGate.getConfig()
+// ============================================================================
+
+describe('ContinueGate.getConfig()', () => {
+ it('should return a copy of the config', () => {
+ const gate = new ContinueGate({ maxConsecutiveSteps: 50 });
+ const config = gate.getConfig();
+ expect(config.maxConsecutiveSteps).toBe(50);
+
+ // Should be a copy, not the internal object
+ config.maxConsecutiveSteps = 999;
+ expect(gate.getConfig().maxConsecutiveSteps).toBe(50);
+ });
+});
+
+// ============================================================================
+// Edge Cases
+// ============================================================================
+
+describe('Edge cases', () => {
+ it('should handle negative budget remaining', () => {
+ const gate = new ContinueGate();
+ const decision = gate.evaluate(makeContext({
+ budgetRemaining: { tokens: -100, toolCalls: 500, timeMs: 600000 },
+ }));
+ expect(decision.decision).toBe('stop');
+ });
+
+ it('should handle coherence score of exactly 0', () => {
+ const gate = new ContinueGate();
+ const decision = gate.evaluate(makeContext({ coherenceScore: 0 }));
+ expect(decision.decision).toBe('stop');
+ });
+
+ it('should handle uncertainty score of exactly 1', () => {
+ const gate = new ContinueGate();
+ const decision = gate.evaluate(makeContext({ uncertaintyScore: 1.0 }));
+ expect(decision.decision).toBe('pause');
+ });
+
+ it('should handle very large step numbers', () => {
+ const gate = new ContinueGate();
+ const decision = gate.evaluate(makeContext({
+ stepNumber: 1000000,
+ lastCheckpointStep: 999990,
+ }));
+ // Should stop due to step limit, but has recent checkpoint
+ // so it depends on checkpoint interval check
+ expect(decision.decision).toBeDefined();
+ });
+});