@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
49 lines • 2.3 kB
JavaScript
import { resolveDetection } from "./coordinator.js";
import { collapseDetector, createCollapseState } from "./detectors/collapse.js";
import { corroboratesControlLeak, createControlLeakDetector } from "./detectors/control-leak.js";
import { COLLAPSE_RULE_NAME, CONTROL_LEAK_RULE_NAME } from "./prompts.js";
export class StreamWatcher {
#manager;
#leakDetector = createControlLeakDetector();
#disabledBuiltin;
#tracks = new Map();
constructor(manager, disabledBuiltinRules) {
this.#manager = manager;
this.#disabledBuiltin = new Set(disabledBuiltinRules);
}
reset() {
this.#tracks.clear();
this.#manager.resetBuffers();
}
handleDelta(source, streamKey, delta, generation, toolName) {
const track = this.#trackFor(source, streamKey);
const detectorCtx = { source, streamKey, generation };
// Tool arguments are watched too; the detector ignores angle-bracket markup there
// (`source: "tool"`, omo#10789) and still catches `<|...|>` and `[UPPER]` leaks.
const leakMatch = this.#disabledBuiltin.has(CONTROL_LEAK_RULE_NAME)
? null
: this.#leakDetector.checkDelta(track.leak, delta, detectorCtx);
const collapseMatch = this.#disabledBuiltin.has(COLLAPSE_RULE_NAME)
? null
: collapseDetector.checkDelta(track.collapse, delta, detectorCtx);
const evidence = track.leak.pendingEvidence;
const corroborated = collapseMatch !== null &&
evidence !== undefined &&
corroboratesControlLeak(evidence, collapseMatch.anomalyStartOffset, track.leak.currentOffset)
? collapseMatch
: null;
const resolution = resolveDetection(leakMatch, collapseMatch, corroborated);
const ruleMatches = this.#manager.checkDelta(delta, { source, streamKey, toolName });
return { resolution, ruleMatches };
}
#trackFor(source, streamKey) {
const key = `${source}:${streamKey}`;
let track = this.#tracks.get(key);
if (track === undefined) {
track = { collapse: createCollapseState(), leak: this.#leakDetector.createState() };
this.#tracks.set(key, track);
}
return track;
}
}
//# sourceMappingURL=watch.js.map