tokencostguard / scripts /test-hermes-evolution.ts
lixiaowww
CSP: allow huggingface.co frame-ancestors
249c849
Raw
History Blame Contribute Delete
4.02 kB
import axios from "axios";
import "dotenv/config";
const API_BASE = "http://localhost:3000/api/v1";
const API_KEY = process.env.API_KEYS?.split(",")[0] || "";
async function runHermesStressTest() {
console.log("🚀 Starting HERMES Closed-Loop Evolution Stress Test...");
try {
// Phase 0: Get current policy
console.log("\n--- Phase 0: Fetching Current Policy ---");
const authRes = await axios.get(`${API_BASE}/auth/session`, {
headers: { Authorization: `Bearer ${API_KEY}` }
});
console.log("Authenticated as:", authRes.data.tenant.name);
// Phase 1: Generate Successful Baseline Outcomes
console.log("\n--- Phase 1: Generating Successful Baseline Outcomes ---");
for (let i = 0; i < 5; i++) {
const traceId = `trace_success_${i}_${Date.now()}`;
// Record a mock decision audit first so Hermes can correlate it
await axios.post(`${API_BASE}/optimize`, {
logs: [{
step: "test",
agent: "TestAgent",
model: "gpt-4",
input: "test",
output: "test output",
prompt_tokens: 100,
completion_tokens: 50
}],
mode: "fast",
decision: { mode: "recommend" }
}, {
headers: { Authorization: `Bearer ${API_KEY}`, "X-TCG-Trace-Id": traceId }
});
await axios.post(`${API_BASE}/feedback`, {
trace_id: traceId,
success: true,
score: 0.95,
feedback: "Perfect optimization"
}, {
headers: { Authorization: `Bearer ${API_KEY}` }
});
process.stdout.write(".");
}
console.log("\nDone.");
// Phase 2: Generate Critical Failure Outcomes (Simulation)
console.log("\n--- Phase 2: Generating Critical Failure Outcomes (Simulation) ---");
console.log("Simulating a high failure rate...");
for (let i = 0; i < 20; i++) {
const traceId = `trace_fail_${i}_${Date.now()}`;
// Record a mock decision audit
await axios.post(`${API_BASE}/optimize`, {
logs: [{
step: "test",
agent: "TestAgent",
model: "gpt-4",
input: "test",
output: "test output",
prompt_tokens: 100,
completion_tokens: 50
}],
mode: "fast",
decision: { mode: "recommend" }
}, {
headers: { Authorization: `Bearer ${API_KEY}`, "X-TCG-Trace-Id": traceId }
});
await axios.post(`${API_BASE}/feedback`, {
trace_id: traceId,
success: false,
score: 0.2,
feedback: "Context lost during pruning"
}, {
headers: { Authorization: `Bearer ${API_KEY}` }
});
process.stdout.write("X");
}
console.log("\nDone.");
// Phase 3: Trigger Hermes Evolution
console.log("\n--- Phase 3: Triggering Hermes Evolution ---");
const evolveRes = await axios.post(`${API_BASE}/hermes/evolve`, {}, {
headers: { Authorization: `Bearer ${API_KEY}` }
});
console.log("Status:", evolveRes.status);
console.log("Rationale:", evolveRes.data.proposal.rationale);
const newWeights = evolveRes.data.proposal.newWeights;
const newThresholds = evolveRes.data.proposal.newThresholds;
console.log("\nNew Weights:", JSON.stringify(newWeights, null, 2));
console.log("New Thresholds:", JSON.stringify(newThresholds, null, 2));
// Assertions
if (evolveRes.data.proposal.rationale.includes("CRITICAL: Low success rate")) {
console.log("\n✅ SUCCESS: Hermes detected the degradation and pivoted to Quality Restoration.");
} else {
console.log("\n❌ FAILURE: Hermes did not detect the degradation correctly.");
}
} catch (error: any) {
console.error("\n❌ Test Failed:", error.response?.data || error.message);
}
}
runHermesStressTest();