62 lines
1.1 KiB
JavaScript
62 lines
1.1 KiB
JavaScript
/**
|
|
* Identical tool-call stationarity. No Bare imports.
|
|
* Same name+args 4 times → nudge; 8 times → stuck.
|
|
*/
|
|
|
|
const NUDGE_AFTER = 4;
|
|
const STOP_AFTER = 8;
|
|
|
|
function fingerprint(name, args) {
|
|
let a = args;
|
|
try {
|
|
a = JSON.stringify(args || {});
|
|
} catch (_) {
|
|
a = String(args);
|
|
}
|
|
return String(name || '') + ':' + a;
|
|
}
|
|
|
|
function create() {
|
|
return { last: null, count: 0, nudged: false };
|
|
}
|
|
|
|
function observe(st, name, args) {
|
|
if (!st) return 0;
|
|
const fp = fingerprint(name, args);
|
|
if (st.last === fp) st.count += 1;
|
|
else {
|
|
st.last = fp;
|
|
st.count = 1;
|
|
st.nudged = false;
|
|
}
|
|
return st.count;
|
|
}
|
|
|
|
function shouldNudge(st) {
|
|
return !!(st && st.count >= NUDGE_AFTER && st.count < STOP_AFTER && !st.nudged);
|
|
}
|
|
|
|
function shouldStop(st) {
|
|
return !!(st && st.count >= STOP_AFTER);
|
|
}
|
|
|
|
function nudgeText() {
|
|
return 'You are repeating the same tool call. Try a different approach, a different path, or finish.';
|
|
}
|
|
|
|
function markNudged(st) {
|
|
if (st) st.nudged = true;
|
|
}
|
|
|
|
module.exports = {
|
|
NUDGE_AFTER,
|
|
STOP_AFTER,
|
|
fingerprint,
|
|
create,
|
|
observe,
|
|
shouldNudge,
|
|
shouldStop,
|
|
nudgeText,
|
|
markNudged,
|
|
};
|