agent-loop-guard 0.1.0__tar.gz

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.
@@ -0,0 +1,15 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ dist/
5
+ build/
6
+ .venv/
7
+ .pytest_cache/
8
+ .ruff_cache/
9
+ *.egg
10
+ .eggs/
11
+
12
+ CLAUDE.md
13
+ PLAN.txt
14
+ .env
15
+ .env.local
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 QuartzUnit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,126 @@
1
+ Metadata-Version: 2.4
2
+ Name: agent-loop-guard
3
+ Version: 0.1.0
4
+ Summary: Framework-agnostic agent loop detection — sliding window similarity scoring to catch stuck agents
5
+ Project-URL: Homepage, https://github.com/QuartzUnit/agent-loop-guard
6
+ Project-URL: Repository, https://github.com/QuartzUnit/agent-loop-guard
7
+ Project-URL: Issues, https://github.com/QuartzUnit/agent-loop-guard/issues
8
+ Author-email: hmj <hmj@quartzunit.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: agent,ai-agent,detection,guardrail,llm,loop,safety
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.9
24
+ Description-Content-Type: text/markdown
25
+
26
+ # agent-loop-guard
27
+
28
+ Framework-agnostic agent loop detection — sliding window similarity scoring to catch stuck agents.
29
+
30
+ ## Install
31
+
32
+ ```bash
33
+ pip install agent-loop-guard
34
+ ```
35
+
36
+ ## Quick Start
37
+
38
+ ```python
39
+ from loop_guard import LoopGuard, Action
40
+
41
+ guard = LoopGuard()
42
+
43
+ for action in agent_actions:
44
+ decision = guard.check(tool=action.name, args=action.args)
45
+ if decision.action == Action.STOP:
46
+ print(f"Loop detected: {decision.reason}")
47
+ break
48
+ ```
49
+
50
+ ## Why not just `max_iter`?
51
+
52
+ | Approach | What it catches | Limitation |
53
+ |----------|----------------|------------|
54
+ | `max_iter=10` | Runaway agents | Kills long *legitimate* tasks; misses 3-step loops at step 9 |
55
+ | **agent-loop-guard** | Exact repeats, fuzzy repeats, A→B→C→A cycles, output stagnation | — |
56
+
57
+ `max_iter` is a blunt timeout. `agent-loop-guard` detects *behavioral patterns* — the agent doing the same thing over and over, even with slight variations.
58
+
59
+ ## Detection Strategies
60
+
61
+ | Strategy | What it detects | Confidence signal |
62
+ |----------|----------------|-------------------|
63
+ | **Exact Repeat** | Same `(tool, args)` called repeatedly | Consecutive identical calls |
64
+ | **Fuzzy Repeat** | Near-identical args (Jaccard + edit distance) | Similarity > threshold |
65
+ | **Cycle Detection** | A→B→C→A→B→C repeating sequences | Pattern repetition count |
66
+ | **Output Stagnation** | Tool returns same output repeatedly | Output similarity > threshold |
67
+
68
+ All four strategies run on every call. The highest confidence wins.
69
+
70
+ ## API
71
+
72
+ ```python
73
+ guard = LoopGuard(
74
+ window_size=10, # actions to keep in memory
75
+ similarity_threshold=0.85, # fuzzy match threshold
76
+ )
77
+
78
+ decision = guard.check(
79
+ tool="web_search", # tool/function name
80
+ args={"query": "python"}, # arguments (dict or str)
81
+ output="Results: ...", # optional: enables stagnation detection
82
+ )
83
+
84
+ decision.action # Action.CONTINUE | WARN | STOP | ESCALATE
85
+ decision.reason # "Cycle detected: [search → parse → search] repeated 3 times"
86
+ decision.strategy # "cycle_detection"
87
+ decision.confidence # 0.0 ~ 1.0
88
+ decision.is_loop # True if STOP or ESCALATE
89
+ decision.should_warn # True if WARN
90
+
91
+ guard.reset() # reuse for next session
92
+ ```
93
+
94
+ ## Action Escalation
95
+
96
+ Actions escalate with consecutive detections:
97
+
98
+ ```python
99
+ from loop_guard import ActionConfig
100
+
101
+ config = ActionConfig(
102
+ warn_threshold=2, # 2 consecutive hits → WARN
103
+ stop_threshold=4, # 4 consecutive hits → STOP
104
+ escalate_threshold=6, # 6 consecutive hits → ESCALATE
105
+ )
106
+
107
+ guard = LoopGuard(action_config=config)
108
+ ```
109
+
110
+ ## Generic Callback
111
+
112
+ ```python
113
+ from loop_guard.integrations.generic import LoopGuardCallback
114
+
115
+ callback = LoopGuardCallback(
116
+ on_warn=lambda d: logger.warning(f"Loop warning: {d.reason}"),
117
+ on_stop=lambda d: raise_stop_error(d),
118
+ )
119
+
120
+ # In your agent loop:
121
+ decision = callback.before_tool_call("search", {"query": "test"})
122
+ ```
123
+
124
+ ## License
125
+
126
+ MIT
@@ -0,0 +1,101 @@
1
+ # agent-loop-guard
2
+
3
+ Framework-agnostic agent loop detection — sliding window similarity scoring to catch stuck agents.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pip install agent-loop-guard
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```python
14
+ from loop_guard import LoopGuard, Action
15
+
16
+ guard = LoopGuard()
17
+
18
+ for action in agent_actions:
19
+ decision = guard.check(tool=action.name, args=action.args)
20
+ if decision.action == Action.STOP:
21
+ print(f"Loop detected: {decision.reason}")
22
+ break
23
+ ```
24
+
25
+ ## Why not just `max_iter`?
26
+
27
+ | Approach | What it catches | Limitation |
28
+ |----------|----------------|------------|
29
+ | `max_iter=10` | Runaway agents | Kills long *legitimate* tasks; misses 3-step loops at step 9 |
30
+ | **agent-loop-guard** | Exact repeats, fuzzy repeats, A→B→C→A cycles, output stagnation | — |
31
+
32
+ `max_iter` is a blunt timeout. `agent-loop-guard` detects *behavioral patterns* — the agent doing the same thing over and over, even with slight variations.
33
+
34
+ ## Detection Strategies
35
+
36
+ | Strategy | What it detects | Confidence signal |
37
+ |----------|----------------|-------------------|
38
+ | **Exact Repeat** | Same `(tool, args)` called repeatedly | Consecutive identical calls |
39
+ | **Fuzzy Repeat** | Near-identical args (Jaccard + edit distance) | Similarity > threshold |
40
+ | **Cycle Detection** | A→B→C→A→B→C repeating sequences | Pattern repetition count |
41
+ | **Output Stagnation** | Tool returns same output repeatedly | Output similarity > threshold |
42
+
43
+ All four strategies run on every call. The highest confidence wins.
44
+
45
+ ## API
46
+
47
+ ```python
48
+ guard = LoopGuard(
49
+ window_size=10, # actions to keep in memory
50
+ similarity_threshold=0.85, # fuzzy match threshold
51
+ )
52
+
53
+ decision = guard.check(
54
+ tool="web_search", # tool/function name
55
+ args={"query": "python"}, # arguments (dict or str)
56
+ output="Results: ...", # optional: enables stagnation detection
57
+ )
58
+
59
+ decision.action # Action.CONTINUE | WARN | STOP | ESCALATE
60
+ decision.reason # "Cycle detected: [search → parse → search] repeated 3 times"
61
+ decision.strategy # "cycle_detection"
62
+ decision.confidence # 0.0 ~ 1.0
63
+ decision.is_loop # True if STOP or ESCALATE
64
+ decision.should_warn # True if WARN
65
+
66
+ guard.reset() # reuse for next session
67
+ ```
68
+
69
+ ## Action Escalation
70
+
71
+ Actions escalate with consecutive detections:
72
+
73
+ ```python
74
+ from loop_guard import ActionConfig
75
+
76
+ config = ActionConfig(
77
+ warn_threshold=2, # 2 consecutive hits → WARN
78
+ stop_threshold=4, # 4 consecutive hits → STOP
79
+ escalate_threshold=6, # 6 consecutive hits → ESCALATE
80
+ )
81
+
82
+ guard = LoopGuard(action_config=config)
83
+ ```
84
+
85
+ ## Generic Callback
86
+
87
+ ```python
88
+ from loop_guard.integrations.generic import LoopGuardCallback
89
+
90
+ callback = LoopGuardCallback(
91
+ on_warn=lambda d: logger.warning(f"Loop warning: {d.reason}"),
92
+ on_stop=lambda d: raise_stop_error(d),
93
+ )
94
+
95
+ # In your agent loop:
96
+ decision = callback.before_tool_call("search", {"query": "test"})
97
+ ```
98
+
99
+ ## License
100
+
101
+ MIT
@@ -0,0 +1,52 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "agent-loop-guard"
7
+ version = "0.1.0"
8
+ description = "Framework-agnostic agent loop detection — sliding window similarity scoring to catch stuck agents"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.9"
12
+ authors = [{ name = "hmj", email = "hmj@quartzunit.com" }]
13
+ keywords = [
14
+ "agent",
15
+ "loop",
16
+ "detection",
17
+ "guardrail",
18
+ "safety",
19
+ "llm",
20
+ "ai-agent",
21
+ ]
22
+ classifiers = [
23
+ "Development Status :: 4 - Beta",
24
+ "Intended Audience :: Developers",
25
+ "License :: OSI Approved :: MIT License",
26
+ "Programming Language :: Python :: 3",
27
+ "Programming Language :: Python :: 3.9",
28
+ "Programming Language :: Python :: 3.10",
29
+ "Programming Language :: Python :: 3.11",
30
+ "Programming Language :: Python :: 3.12",
31
+ "Programming Language :: Python :: 3.13",
32
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
33
+ "Typing :: Typed",
34
+ ]
35
+
36
+ [project.urls]
37
+ Homepage = "https://github.com/QuartzUnit/agent-loop-guard"
38
+ Repository = "https://github.com/QuartzUnit/agent-loop-guard"
39
+ Issues = "https://github.com/QuartzUnit/agent-loop-guard/issues"
40
+
41
+ [tool.hatch.build.targets.wheel]
42
+ packages = ["src/loop_guard"]
43
+
44
+ [tool.pytest.ini_options]
45
+ testpaths = ["tests"]
46
+
47
+ [tool.ruff]
48
+ line-length = 120
49
+ target-version = "py39"
50
+
51
+ [tool.ruff.lint]
52
+ select = ["E", "F", "I", "W", "UP"]
@@ -0,0 +1,11 @@
1
+ """agent-loop-guard — Framework-agnostic agent loop detection.
2
+
3
+ Sliding window similarity scoring to catch stuck agents.
4
+ """
5
+
6
+ from loop_guard.decision import Action, ActionConfig, Decision
7
+ from loop_guard.guard import LoopGuard
8
+ from loop_guard.strategies import ActionRecord
9
+
10
+ __all__ = ["LoopGuard", "Action", "ActionConfig", "Decision", "ActionRecord"]
11
+ __version__ = "0.1.0"
@@ -0,0 +1,54 @@
1
+ """Decision types and action configuration."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ from enum import Enum, auto
7
+
8
+
9
+ class Action(Enum):
10
+ """What to do when a loop pattern is detected."""
11
+
12
+ CONTINUE = auto()
13
+ WARN = auto()
14
+ STOP = auto()
15
+ ESCALATE = auto()
16
+
17
+
18
+ @dataclass(frozen=True, slots=True)
19
+ class Decision:
20
+ """Result of a loop guard check."""
21
+
22
+ action: Action
23
+ reason: str = ""
24
+ strategy: str = "" # which strategy triggered
25
+ confidence: float = 0.0 # 0.0 ~ 1.0
26
+ step_number: int = 0
27
+
28
+ @property
29
+ def is_loop(self) -> bool:
30
+ return self.action in (Action.STOP, Action.ESCALATE)
31
+
32
+ @property
33
+ def should_warn(self) -> bool:
34
+ return self.action == Action.WARN
35
+
36
+
37
+ @dataclass
38
+ class ActionConfig:
39
+ """Configure how many warnings before escalation."""
40
+
41
+ warn_threshold: int = 2
42
+ stop_threshold: int = 4
43
+ escalate_threshold: int = 6
44
+ reflection_message: str = "You appear to be stuck in a loop. Try a different approach."
45
+
46
+ def resolve_action(self, consecutive_hits: int) -> Action:
47
+ """Map hit count to action."""
48
+ if consecutive_hits >= self.escalate_threshold:
49
+ return Action.ESCALATE
50
+ if consecutive_hits >= self.stop_threshold:
51
+ return Action.STOP
52
+ if consecutive_hits >= self.warn_threshold:
53
+ return Action.WARN
54
+ return Action.CONTINUE
@@ -0,0 +1,129 @@
1
+ """Core LoopGuard — orchestrates detection strategies."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from typing import Any
7
+
8
+ from loop_guard.decision import ActionConfig, Decision
9
+ from loop_guard.strategies import (
10
+ ActionRecord,
11
+ CycleDetectionStrategy,
12
+ ExactRepeatStrategy,
13
+ FuzzyRepeatStrategy,
14
+ OutputStagnationStrategy,
15
+ )
16
+
17
+
18
+ @dataclass
19
+ class LoopGuard:
20
+ """Framework-agnostic agent loop detector.
21
+
22
+ Usage:
23
+ guard = LoopGuard()
24
+
25
+ for action in agent_actions:
26
+ decision = guard.check(action.tool, action.args, action.output)
27
+ if decision.action == Action.STOP:
28
+ break
29
+ if decision.should_warn:
30
+ print(f"Warning: {decision.reason}")
31
+ """
32
+
33
+ window_size: int = 10
34
+ similarity_threshold: float = 0.85
35
+ action_config: ActionConfig = field(default_factory=ActionConfig)
36
+
37
+ # Strategy instances (auto-created)
38
+ _exact: ExactRepeatStrategy = field(init=False, repr=False)
39
+ _fuzzy: FuzzyRepeatStrategy = field(init=False, repr=False)
40
+ _cycle: CycleDetectionStrategy = field(init=False, repr=False)
41
+ _stagnation: OutputStagnationStrategy = field(init=False, repr=False)
42
+
43
+ # State
44
+ _step: int = field(default=0, init=False, repr=False)
45
+ _consecutive_hits: int = field(default=0, init=False, repr=False)
46
+
47
+ def __post_init__(self) -> None:
48
+ self._exact = ExactRepeatStrategy(window_size=self.window_size)
49
+ self._fuzzy = FuzzyRepeatStrategy(
50
+ window_size=self.window_size,
51
+ similarity_threshold=self.similarity_threshold,
52
+ )
53
+ self._cycle = CycleDetectionStrategy(max_cycle_length=min(self.window_size, 5))
54
+ self._stagnation = OutputStagnationStrategy(
55
+ window_size=self.window_size,
56
+ similarity_threshold=self.similarity_threshold,
57
+ )
58
+
59
+ def check(
60
+ self,
61
+ tool: str,
62
+ args: dict[str, Any] | str | None = None,
63
+ output: str | None = None,
64
+ ) -> Decision:
65
+ """Check an agent action for loop patterns.
66
+
67
+ Args:
68
+ tool: Name of the tool/function being called.
69
+ args: Arguments passed to the tool.
70
+ output: Output/result of the tool call (optional, enables stagnation detection).
71
+
72
+ Returns:
73
+ Decision with action (CONTINUE/WARN/STOP/ESCALATE), reason, and confidence.
74
+ """
75
+ self._step += 1
76
+ record = ActionRecord(tool=tool, args=args, output=output, step=self._step)
77
+
78
+ # Run all strategies and take the highest confidence
79
+ results = [
80
+ ("exact_repeat", *self._exact.check(record)),
81
+ ("fuzzy_repeat", *self._fuzzy.check(record)),
82
+ ("cycle_detection", *self._cycle.check(record)),
83
+ ("output_stagnation", *self._stagnation.check(record)),
84
+ ]
85
+
86
+ # Find highest confidence detection
87
+ best_strategy = ""
88
+ best_confidence = 0.0
89
+ best_reason = ""
90
+
91
+ for strategy_name, confidence, reason in results:
92
+ if confidence > best_confidence:
93
+ best_strategy = strategy_name
94
+ best_confidence = confidence
95
+ best_reason = reason
96
+
97
+ # Update consecutive hit counter
98
+ if best_confidence > 0.3:
99
+ self._consecutive_hits += 1
100
+ else:
101
+ self._consecutive_hits = 0
102
+
103
+ # Determine action based on consecutive hits
104
+ action = self.action_config.resolve_action(self._consecutive_hits)
105
+
106
+ return Decision(
107
+ action=action,
108
+ reason=best_reason,
109
+ strategy=best_strategy,
110
+ confidence=best_confidence,
111
+ step_number=self._step,
112
+ )
113
+
114
+ def reset(self) -> None:
115
+ """Reset all state for reuse."""
116
+ self._step = 0
117
+ self._consecutive_hits = 0
118
+ self._exact.reset()
119
+ self._fuzzy.reset()
120
+ self._cycle.reset()
121
+ self._stagnation.reset()
122
+
123
+ @property
124
+ def step_count(self) -> int:
125
+ return self._step
126
+
127
+ @property
128
+ def consecutive_hits(self) -> int:
129
+ return self._consecutive_hits
@@ -0,0 +1,56 @@
1
+ """Generic callback adapter for any agent framework."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any, Callable
6
+
7
+ from loop_guard.decision import Action, Decision
8
+ from loop_guard.guard import LoopGuard
9
+
10
+
11
+ class LoopGuardCallback:
12
+ """Wrap LoopGuard as a callback for any agent framework.
13
+
14
+ Usage:
15
+ callback = LoopGuardCallback(
16
+ on_warn=lambda d: print(f"Warning: {d.reason}"),
17
+ on_stop=lambda d: raise StopIteration(),
18
+ )
19
+
20
+ # In your agent loop:
21
+ decision = callback.before_tool_call("search", {"query": "test"})
22
+ """
23
+
24
+ def __init__(
25
+ self,
26
+ guard: LoopGuard | None = None,
27
+ on_warn: Callable[[Decision], None] | None = None,
28
+ on_stop: Callable[[Decision], Any] | None = None,
29
+ on_escalate: Callable[[Decision], Any] | None = None,
30
+ **guard_kwargs: Any,
31
+ ):
32
+ self.guard = guard or LoopGuard(**guard_kwargs)
33
+ self._on_warn = on_warn
34
+ self._on_stop = on_stop
35
+ self._on_escalate = on_escalate
36
+
37
+ def before_tool_call(
38
+ self,
39
+ tool: str,
40
+ args: dict | str | None = None,
41
+ output: str | None = None,
42
+ ) -> Decision:
43
+ """Check and dispatch callbacks. Returns the Decision."""
44
+ decision = self.guard.check(tool, args, output)
45
+
46
+ if decision.action == Action.WARN and self._on_warn:
47
+ self._on_warn(decision)
48
+ elif decision.action == Action.STOP and self._on_stop:
49
+ self._on_stop(decision)
50
+ elif decision.action == Action.ESCALATE and self._on_escalate:
51
+ self._on_escalate(decision)
52
+
53
+ return decision
54
+
55
+ def reset(self) -> None:
56
+ self.guard.reset()
File without changes
@@ -0,0 +1,83 @@
1
+ """Similarity functions for comparing agent actions."""
2
+
3
+ from __future__ import annotations
4
+
5
+
6
+ def jaccard_similarity(a: set, b: set) -> float:
7
+ """Jaccard index between two sets. Returns 0.0 ~ 1.0."""
8
+ if not a and not b:
9
+ return 1.0
10
+ if not a or not b:
11
+ return 0.0
12
+ return len(a & b) / len(a | b)
13
+
14
+
15
+ def token_jaccard(s1: str, s2: str) -> float:
16
+ """Jaccard similarity on whitespace-split tokens."""
17
+ return jaccard_similarity(set(s1.split()), set(s2.split()))
18
+
19
+
20
+ def normalized_edit_distance(s1: str, s2: str) -> float:
21
+ """Normalized Levenshtein distance. Returns 0.0 (identical) ~ 1.0 (totally different).
22
+
23
+ Uses O(min(m,n)) space dynamic programming.
24
+ """
25
+ if s1 == s2:
26
+ return 0.0
27
+ n, m = len(s1), len(s2)
28
+ if n == 0 or m == 0:
29
+ return 1.0
30
+
31
+ # Ensure s1 is the shorter string for O(min) space
32
+ if n > m:
33
+ s1, s2, n, m = s2, s1, m, n
34
+
35
+ prev = list(range(n + 1))
36
+ curr = [0] * (n + 1)
37
+
38
+ for j in range(1, m + 1):
39
+ curr[0] = j
40
+ for i in range(1, n + 1):
41
+ if s1[i - 1] == s2[j - 1]:
42
+ curr[i] = prev[i - 1]
43
+ else:
44
+ curr[i] = 1 + min(prev[i - 1], prev[i], curr[i - 1])
45
+ prev, curr = curr, prev
46
+
47
+ return prev[n] / max(n, m)
48
+
49
+
50
+ def edit_similarity(s1: str, s2: str) -> float:
51
+ """Edit-distance-based similarity. Returns 0.0 (different) ~ 1.0 (identical)."""
52
+ return 1.0 - normalized_edit_distance(s1, s2)
53
+
54
+
55
+ def args_similarity(args1: dict | str | None, args2: dict | str | None) -> float:
56
+ """Compare two action args. Supports dict and str."""
57
+ s1 = _normalize_args(args1)
58
+ s2 = _normalize_args(args2)
59
+ if s1 == s2:
60
+ return 1.0
61
+ # Blend Jaccard (fast, structural) and edit (precise, sequential)
62
+ j = token_jaccard(s1, s2)
63
+ # Only compute edit distance for shorter strings (performance)
64
+ if max(len(s1), len(s2)) <= 2000:
65
+ e = edit_similarity(s1, s2)
66
+ return 0.5 * j + 0.5 * e
67
+ return j
68
+
69
+
70
+ def _normalize_args(args: dict | str | None) -> str:
71
+ """Convert args to a normalized string for comparison."""
72
+ if args is None:
73
+ return ""
74
+ if isinstance(args, str):
75
+ return args
76
+ if isinstance(args, dict):
77
+ # Sort keys for deterministic comparison
78
+ parts = []
79
+ for k in sorted(args.keys()):
80
+ v = args[k]
81
+ parts.append(f"{k}={v}")
82
+ return " ".join(parts)
83
+ return str(args)