@crewhaus/tool-loop-detection 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -11
- package/src/index.test.ts +35 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crewhaus/tool-loop-detection",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Detect repeated tool calls via sliding-window canonical-JSON signatures",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -12,13 +12,13 @@
|
|
|
12
12
|
"test": "bun test src"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@crewhaus/turn-state-machine": "0.
|
|
15
|
+
"@crewhaus/turn-state-machine": "0.1.2"
|
|
16
16
|
},
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"author": {
|
|
19
19
|
"name": "Max Meier",
|
|
20
|
-
"email": "max@
|
|
21
|
-
"url": "https://
|
|
20
|
+
"email": "max@crewhaus.ai",
|
|
21
|
+
"url": "https://crewhaus.ai"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -30,12 +30,7 @@
|
|
|
30
30
|
"url": "https://github.com/crewhaus/factory/issues"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
|
-
"access": "
|
|
33
|
+
"access": "public"
|
|
34
34
|
},
|
|
35
|
-
"files": [
|
|
36
|
-
"src",
|
|
37
|
-
"README.md",
|
|
38
|
-
"LICENSE",
|
|
39
|
-
"NOTICE"
|
|
40
|
-
]
|
|
35
|
+
"files": ["src", "README.md", "LICENSE", "NOTICE"]
|
|
41
36
|
}
|
package/src/index.test.ts
CHANGED
|
@@ -40,6 +40,22 @@ describe("toolCallSignature", () => {
|
|
|
40
40
|
test("undefined values collapse to null in canonical form", () => {
|
|
41
41
|
expect(toolCallSignature("X", undefined)).toBe("X:null");
|
|
42
42
|
});
|
|
43
|
+
|
|
44
|
+
test("bigint / symbol / function leaves fall through to a stable string tag", () => {
|
|
45
|
+
// These typeof branches (bigint, symbol, function) hit the final
|
|
46
|
+
// `JSON.stringify(String(value))` fall-through in canonicalJson.
|
|
47
|
+
expect(toolCallSignature("X", 10n)).toBe('X:"10"');
|
|
48
|
+
const sym = Symbol("loop");
|
|
49
|
+
expect(toolCallSignature("X", sym)).toBe(`X:${JSON.stringify(String(sym))}`);
|
|
50
|
+
const fn = function namedFn() {};
|
|
51
|
+
expect(toolCallSignature("X", fn)).toBe(`X:${JSON.stringify(String(fn))}`);
|
|
52
|
+
// Same bigint value always yields the same signature (determinism).
|
|
53
|
+
expect(toolCallSignature("X", 10n)).toBe(toolCallSignature("X", 10n));
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("bigint inside a nested object still produces a canonical signature", () => {
|
|
57
|
+
expect(toolCallSignature("X", { n: 5n })).toBe('X:{"n":"5"}');
|
|
58
|
+
});
|
|
43
59
|
});
|
|
44
60
|
|
|
45
61
|
describe("detectLoop — basic shapes", () => {
|
|
@@ -117,6 +133,25 @@ describe("detectLoop — window slicing", () => {
|
|
|
117
133
|
});
|
|
118
134
|
});
|
|
119
135
|
|
|
136
|
+
describe("detectLoop — threshold 1 (every new signature is an instant hit)", () => {
|
|
137
|
+
test("a single call with threshold 1 is detected immediately", () => {
|
|
138
|
+
// threshold === 1 takes the `counts.set` branch and returns on first sight.
|
|
139
|
+
const out = detectLoop([call("A", { k: "v" })], 10, 1);
|
|
140
|
+
expect(out).not.toBeNull();
|
|
141
|
+
expect(out?.toolName).toBe("A");
|
|
142
|
+
expect(out?.count).toBe(1);
|
|
143
|
+
expect(out?.threshold).toBe(1);
|
|
144
|
+
expect(out?.windowSize).toBe(10);
|
|
145
|
+
expect(out?.signature).toBe('A:{"k":"v"}');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("with threshold 1 the FIRST call in the window wins", () => {
|
|
149
|
+
const out = detectLoop([call("First"), call("Second"), call("First")], 10, 1);
|
|
150
|
+
expect(out?.toolName).toBe("First");
|
|
151
|
+
expect(out?.count).toBe(1);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
120
155
|
describe("detectLoop — early-exit on first hit", () => {
|
|
121
156
|
test("returns the FIRST signature to hit threshold while scanning the window", () => {
|
|
122
157
|
// Two competing loops in window: A and B. A's third occurrence should
|