@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
77 lines • 2.89 kB
JavaScript
import { IDENTICAL_BLOCK_NOTICE_THRESHOLD, IDENTICAL_HARD_STOP_BLOCK_THRESHOLD } from "./policy.js";
const ALLOW_DECISION = { kind: "allow" };
export class IdenticalLoopEscalation {
constructor() {
this.attempts = new Map();
}
observeAttempt(toolCallId, record) {
const patternChanged = this.episode !== undefined && this.episode.fingerprint !== record.signature;
if (patternChanged) {
this.reset();
}
this.attempts.set(toolCallId, record);
return patternChanged;
}
observeNotice(detection) {
switch (detection.kind) {
case "similar":
case "cycle":
return;
case "identical": {
if (this.episode === undefined || this.episode.fingerprint !== detection.fingerprint) {
this.episode = {
fingerprint: detection.fingerprint,
toolName: detection.toolName,
admittedNoticeCount: 0,
activateBlockAfterAttempt: false,
blockActive: false,
blockedCallCount: 0,
hardStopAnnounced: false,
};
}
this.episode.admittedNoticeCount++;
if (this.episode.admittedNoticeCount >= IDENTICAL_BLOCK_NOTICE_THRESHOLD) {
this.episode.activateBlockAfterAttempt = true;
}
}
}
}
finishTurn() {
this.attempts.clear();
}
consumeToolCall(toolCallId) {
const attempt = this.attempts.get(toolCallId);
this.attempts.delete(toolCallId);
if (attempt === undefined || this.episode === undefined || this.episode.fingerprint !== attempt.signature) {
return ALLOW_DECISION;
}
if (!this.episode.blockActive) {
if (this.episode.activateBlockAfterAttempt) {
this.episode.activateBlockAfterAttempt = false;
this.episode.blockActive = true;
}
return ALLOW_DECISION;
}
this.episode.blockedCallCount++;
if (this.episode.blockedCallCount >= IDENTICAL_HARD_STOP_BLOCK_THRESHOLD) {
const announce = !this.episode.hardStopAnnounced;
this.episode.hardStopAnnounced = true;
return {
kind: "hardStop",
toolName: this.episode.toolName,
blockedCallCount: this.episode.blockedCallCount,
announce,
};
}
return {
kind: "block",
toolName: this.episode.toolName,
blockedCallCount: this.episode.blockedCallCount,
};
}
reset() {
this.episode = undefined;
this.attempts.clear();
}
}
//# sourceMappingURL=escalation.js.map