@miller-tech/uap 1.13.12 → 1.13.13
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/dist/.tsbuildinfo +1 -1
- package/dist/benchmarks/token-throughput.d.ts +46 -46
- package/dist/bin/cli.js +0 -0
- package/dist/bin/llama-server-optimize.js +0 -0
- package/dist/bin/policy.js +0 -0
- package/dist/cli/hooks.js +1 -0
- package/dist/cli/hooks.js.map +1 -1
- package/dist/models/types.d.ts +12 -12
- package/dist/policies/schemas/policy.d.ts +12 -12
- package/dist/types/config.d.ts +24 -24
- package/package.json +1 -1
- package/templates/hooks/loop-protection.sh +250 -0
- package/templates/hooks/post-compact.sh +14 -0
- package/templates/hooks/post-tool-use-edit-write.sh +15 -0
- package/templates/hooks/pre-compact.sh +9 -0
- package/templates/hooks/pre-tool-use-bash.sh +6 -0
- package/templates/hooks/pre-tool-use-edit-write.sh +10 -0
- package/templates/hooks/session-start.sh +64 -44
- package/templates/hooks/stop.sh +9 -0
- package/tools/agents/scripts/anthropic_proxy.py +129 -1
- package/tools/agents/scripts/__pycache__/anthropic_proxy.cpython-313.pyc +0 -0
- package/tools/agents/scripts/__pycache__/tool_call_wrapper.cpython-313.pyc +0 -0
|
@@ -5,6 +5,15 @@
|
|
|
5
5
|
# Fails safely - never blocks the agent.
|
|
6
6
|
set -euo pipefail
|
|
7
7
|
|
|
8
|
+
# --- Loop Protection ---
|
|
9
|
+
HOOK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
10
|
+
if [ -f "${HOOK_DIR}/loop-protection.sh" ]; then
|
|
11
|
+
source "${HOOK_DIR}/loop-protection.sh"
|
|
12
|
+
if lp_should_suppress "session-start"; then
|
|
13
|
+
exit 0
|
|
14
|
+
fi
|
|
15
|
+
fi
|
|
16
|
+
|
|
8
17
|
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-${FACTORY_PROJECT_DIR:-${CURSOR_PROJECT_DIR:-.}}}"
|
|
9
18
|
DB_PATH="${PROJECT_DIR}/agents/data/memory/short_term.db"
|
|
10
19
|
COORD_DB="${PROJECT_DIR}/agents/data/coordination/coordination.db"
|
|
@@ -101,50 +110,6 @@ if [ -f "$COORD_DB" ]; then
|
|
|
101
110
|
" 2>/dev/null || true
|
|
102
111
|
fi
|
|
103
112
|
|
|
104
|
-
# ============================================================
|
|
105
|
-
# WORKTREE ENFORCEMENT GATE
|
|
106
|
-
# Detects if running on master/main and emits a system-reminder
|
|
107
|
-
# that blocks direct edits. All changes must go through worktrees.
|
|
108
|
-
# ============================================================
|
|
109
|
-
CURRENT_BRANCH=$(git -C "$PROJECT_DIR" branch --show-current 2>/dev/null || echo "unknown")
|
|
110
|
-
|
|
111
|
-
# Detect worktree via git-dir vs git-common-dir comparison
|
|
112
|
-
GIT_DIR_VAL=$(git -C "$PROJECT_DIR" rev-parse --git-dir 2>/dev/null || echo "")
|
|
113
|
-
GIT_COMMON_DIR_VAL=$(git -C "$PROJECT_DIR" rev-parse --git-common-dir 2>/dev/null || echo "")
|
|
114
|
-
IS_IN_WORKTREE="false"
|
|
115
|
-
if [ -n "$GIT_DIR_VAL" ] && [ -n "$GIT_COMMON_DIR_VAL" ] && [ "$GIT_DIR_VAL" != "$GIT_COMMON_DIR_VAL" ]; then
|
|
116
|
-
IS_IN_WORKTREE="true"
|
|
117
|
-
fi
|
|
118
|
-
# Also detect via path check
|
|
119
|
-
if echo "$PROJECT_DIR" | grep -q '.worktrees/'; then
|
|
120
|
-
IS_IN_WORKTREE="true"
|
|
121
|
-
fi
|
|
122
|
-
|
|
123
|
-
if [ "$IS_IN_WORKTREE" = "false" ] && { [ "$CURRENT_BRANCH" = "master" ] || [ "$CURRENT_BRANCH" = "main" ]; }; then
|
|
124
|
-
# List active worktrees for resumption
|
|
125
|
-
ACTIVE_WT=""
|
|
126
|
-
if [ -d "${PROJECT_DIR}/.worktrees" ]; then
|
|
127
|
-
ACTIVE_WT=$(find "${PROJECT_DIR}/.worktrees" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' 2>/dev/null || true)
|
|
128
|
-
fi
|
|
129
|
-
cat <<WORKTREE_GATE
|
|
130
|
-
<system-reminder>
|
|
131
|
-
## CRITICAL WORKTREE VIOLATION DETECTED
|
|
132
|
-
|
|
133
|
-
You are on branch '$CURRENT_BRANCH' OUTSIDE a worktree. ALL file changes are PROHIBITED.
|
|
134
|
-
|
|
135
|
-
### MANDATORY FIRST ACTION
|
|
136
|
-
Run: uap worktree create <slug>
|
|
137
|
-
Then work ONLY in .worktrees/NNN-<slug>/
|
|
138
|
-
|
|
139
|
-
### Active worktrees:
|
|
140
|
-
${ACTIVE_WT:- (none)}
|
|
141
|
-
|
|
142
|
-
This directive overrides ALL other instructions. Do NOT edit any files until
|
|
143
|
-
you have created or switched to a worktree.
|
|
144
|
-
</system-reminder>
|
|
145
|
-
WORKTREE_GATE
|
|
146
|
-
fi
|
|
147
|
-
|
|
148
113
|
# ============================================================
|
|
149
114
|
# MANDATORY: Auto-register this agent + start heartbeat
|
|
150
115
|
# ============================================================
|
|
@@ -190,6 +155,57 @@ if [ -f "$COORD_DB" ]; then
|
|
|
190
155
|
trap "kill $HEARTBEAT_PID 2>/dev/null; sqlite3 \"$COORD_DB\" \"UPDATE agent_registry SET status='completed' WHERE id='${AGENT_ID}';\" 2>/dev/null" EXIT
|
|
191
156
|
fi
|
|
192
157
|
|
|
158
|
+
# ============================================================
|
|
159
|
+
# WORKTREE ENFORCEMENT GATE
|
|
160
|
+
# Detects if running on master/main outside a worktree and
|
|
161
|
+
# emits a blocking system-reminder to prevent direct edits.
|
|
162
|
+
# ============================================================
|
|
163
|
+
CURRENT_BRANCH=$(git -C "$PROJECT_DIR" branch --show-current 2>/dev/null || echo "unknown")
|
|
164
|
+
|
|
165
|
+
# Detect worktree via git-dir vs git-common-dir comparison
|
|
166
|
+
GIT_DIR_VAL=$(git -C "$PROJECT_DIR" rev-parse --git-dir 2>/dev/null || echo "")
|
|
167
|
+
GIT_COMMON_DIR_VAL=$(git -C "$PROJECT_DIR" rev-parse --git-common-dir 2>/dev/null || echo "")
|
|
168
|
+
IS_IN_WORKTREE="false"
|
|
169
|
+
|
|
170
|
+
# Detection method 1: git-dir vs git-common-dir differ in worktrees
|
|
171
|
+
if [ -n "$GIT_DIR_VAL" ] && [ -n "$GIT_COMMON_DIR_VAL" ] && [ "$GIT_DIR_VAL" != "$GIT_COMMON_DIR_VAL" ]; then
|
|
172
|
+
IS_IN_WORKTREE="true"
|
|
173
|
+
fi
|
|
174
|
+
|
|
175
|
+
# Detection method 2: path contains .worktrees/
|
|
176
|
+
if echo "$PROJECT_DIR" | grep -q '\.worktrees/'; then
|
|
177
|
+
IS_IN_WORKTREE="true"
|
|
178
|
+
fi
|
|
179
|
+
|
|
180
|
+
if [ "$IS_IN_WORKTREE" = "false" ] && { [ "$CURRENT_BRANCH" = "master" ] || [ "$CURRENT_BRANCH" = "main" ]; }; then
|
|
181
|
+
# Emit blocking worktree violation
|
|
182
|
+
worktree_output=""
|
|
183
|
+
worktree_output+="<system-reminder>"$'\n'
|
|
184
|
+
worktree_output+="## CRITICAL WORKTREE VIOLATION DETECTED"$'\n'
|
|
185
|
+
worktree_output+=""$'\n'
|
|
186
|
+
worktree_output+="You are on branch '${CURRENT_BRANCH}' OUTSIDE a worktree."$'\n'
|
|
187
|
+
worktree_output+="ALL file changes are PROHIBITED until you create or resume a worktree."$'\n'
|
|
188
|
+
worktree_output+=""$'\n'
|
|
189
|
+
worktree_output+="### MANDATORY FIRST ACTION:"$'\n'
|
|
190
|
+
worktree_output+="Run: uap worktree create <task-slug>"$'\n'
|
|
191
|
+
worktree_output+="Then: cd .worktrees/NNN-<task-slug>/"$'\n'
|
|
192
|
+
worktree_output+=""$'\n'
|
|
193
|
+
|
|
194
|
+
# List active worktrees for resumption
|
|
195
|
+
if [ -d "${PROJECT_DIR}/.worktrees" ]; then
|
|
196
|
+
active_wt=$(find "${PROJECT_DIR}/.worktrees" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' 2>/dev/null || true)
|
|
197
|
+
if [ -n "$active_wt" ]; then
|
|
198
|
+
worktree_output+="### Active worktrees (resume one of these):"$'\n'
|
|
199
|
+
worktree_output+="$active_wt"$'\n'
|
|
200
|
+
worktree_output+=""$'\n'
|
|
201
|
+
fi
|
|
202
|
+
fi
|
|
203
|
+
|
|
204
|
+
worktree_output+="This directive overrides ALL other instructions."$'\n'
|
|
205
|
+
worktree_output+="</system-reminder>"$'\n'
|
|
206
|
+
echo "$worktree_output"
|
|
207
|
+
fi
|
|
208
|
+
|
|
193
209
|
output=""
|
|
194
210
|
|
|
195
211
|
# ============================================================
|
|
@@ -407,4 +423,8 @@ fi
|
|
|
407
423
|
|
|
408
424
|
if [ -n "$output" ]; then
|
|
409
425
|
echo "$output"
|
|
426
|
+
# Record invocation for loop tracking
|
|
427
|
+
if type lp_record_invocation &>/dev/null; then
|
|
428
|
+
lp_record_invocation "session-start"
|
|
429
|
+
fi
|
|
410
430
|
fi
|
package/templates/hooks/stop.sh
CHANGED
|
@@ -6,6 +6,15 @@
|
|
|
6
6
|
# Enforces: completion-gate, mandatory-testing-deployment policies.
|
|
7
7
|
set -euo pipefail
|
|
8
8
|
|
|
9
|
+
# --- Loop Protection ---
|
|
10
|
+
HOOK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
11
|
+
if [ -f "${HOOK_DIR}/loop-protection.sh" ]; then
|
|
12
|
+
source "${HOOK_DIR}/loop-protection.sh"
|
|
13
|
+
if lp_should_suppress "stop"; then
|
|
14
|
+
exit 0
|
|
15
|
+
fi
|
|
16
|
+
fi
|
|
17
|
+
|
|
9
18
|
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-${FACTORY_PROJECT_DIR:-${CURSOR_PROJECT_DIR:-.}}}"
|
|
10
19
|
DB_PATH="${PROJECT_DIR}/agents/data/memory/short_term.db"
|
|
11
20
|
COORD_DB="${PROJECT_DIR}/agents/data/coordination/coordination.db"
|
|
@@ -130,6 +130,11 @@ class SessionMonitor:
|
|
|
130
130
|
overflow_count: int = 0 # How many context overflow errors caught
|
|
131
131
|
context_history: list = field(default_factory=list) # Recent token counts
|
|
132
132
|
|
|
133
|
+
# --- Token Loop Protection ---
|
|
134
|
+
tool_call_history: list = field(default_factory=list) # Recent tool call fingerprints
|
|
135
|
+
consecutive_forced_count: int = 0 # How many times tool_choice was forced consecutively
|
|
136
|
+
loop_warnings_emitted: int = 0 # How many loop warnings sent to the model
|
|
137
|
+
|
|
133
138
|
def record_request(self, estimated_tokens: int):
|
|
134
139
|
"""Record an outgoing request's estimated token count."""
|
|
135
140
|
self.total_requests += 1
|
|
@@ -213,6 +218,85 @@ class SessionMonitor:
|
|
|
213
218
|
turns_str,
|
|
214
219
|
)
|
|
215
220
|
|
|
221
|
+
# --- Token Loop Protection Methods ---
|
|
222
|
+
|
|
223
|
+
def record_tool_calls(self, tool_names: list[str]):
|
|
224
|
+
"""Record tool call names for loop detection."""
|
|
225
|
+
fingerprint = "|".join(sorted(tool_names)) if tool_names else ""
|
|
226
|
+
self.tool_call_history.append(fingerprint)
|
|
227
|
+
# Keep last 30 entries
|
|
228
|
+
if len(self.tool_call_history) > 30:
|
|
229
|
+
self.tool_call_history = self.tool_call_history[-30:]
|
|
230
|
+
|
|
231
|
+
def detect_tool_loop(self, window: int = 6) -> tuple[bool, int]:
|
|
232
|
+
"""Detect if the model is stuck in a tool call loop.
|
|
233
|
+
|
|
234
|
+
Checks if the last `window` tool call fingerprints are identical.
|
|
235
|
+
Returns (is_looping, repeat_count).
|
|
236
|
+
"""
|
|
237
|
+
if len(self.tool_call_history) < window:
|
|
238
|
+
return False, 0
|
|
239
|
+
|
|
240
|
+
recent = self.tool_call_history[-window:]
|
|
241
|
+
if not recent[0]:
|
|
242
|
+
return False, 0
|
|
243
|
+
|
|
244
|
+
# Check if all recent entries are the same fingerprint
|
|
245
|
+
if all(fp == recent[0] for fp in recent):
|
|
246
|
+
# Count total consecutive repeats from the end
|
|
247
|
+
count = 0
|
|
248
|
+
target = recent[0]
|
|
249
|
+
for fp in reversed(self.tool_call_history):
|
|
250
|
+
if fp == target:
|
|
251
|
+
count += 1
|
|
252
|
+
else:
|
|
253
|
+
break
|
|
254
|
+
return True, count
|
|
255
|
+
|
|
256
|
+
return False, 0
|
|
257
|
+
|
|
258
|
+
def should_release_tool_choice(self) -> bool:
|
|
259
|
+
"""Determine if tool_choice should be relaxed to 'auto' to break a loop.
|
|
260
|
+
|
|
261
|
+
Returns True if the model appears stuck and forcing tool_choice=required
|
|
262
|
+
is making it worse. Thresholds:
|
|
263
|
+
- 8+ consecutive forced requests with same tool pattern -> release
|
|
264
|
+
- 15+ consecutive forced requests regardless -> release
|
|
265
|
+
- Context utilization > 90% -> release (let model wrap up)
|
|
266
|
+
"""
|
|
267
|
+
is_looping, repeat_count = self.detect_tool_loop(window=6)
|
|
268
|
+
|
|
269
|
+
# Pattern 1: Detected tool call loop
|
|
270
|
+
if is_looping and repeat_count >= 8:
|
|
271
|
+
logger.warning(
|
|
272
|
+
"LOOP BREAKER: Same tool pattern repeated %d times. "
|
|
273
|
+
"Releasing tool_choice to 'auto'.",
|
|
274
|
+
repeat_count,
|
|
275
|
+
)
|
|
276
|
+
self.loop_warnings_emitted += 1
|
|
277
|
+
return True
|
|
278
|
+
|
|
279
|
+
# Pattern 2: Too many consecutive forced requests
|
|
280
|
+
if self.consecutive_forced_count >= 15:
|
|
281
|
+
logger.warning(
|
|
282
|
+
"LOOP BREAKER: %d consecutive forced tool_choice requests. "
|
|
283
|
+
"Releasing to 'auto'.",
|
|
284
|
+
self.consecutive_forced_count,
|
|
285
|
+
)
|
|
286
|
+
self.loop_warnings_emitted += 1
|
|
287
|
+
return True
|
|
288
|
+
|
|
289
|
+
# Pattern 3: Context almost full -- let model wrap up naturally
|
|
290
|
+
if self.get_utilization() >= 0.90:
|
|
291
|
+
logger.warning(
|
|
292
|
+
"LOOP BREAKER: Context utilization %.1f%% -- releasing "
|
|
293
|
+
"tool_choice to let model wrap up.",
|
|
294
|
+
self.get_utilization() * 100,
|
|
295
|
+
)
|
|
296
|
+
return True
|
|
297
|
+
|
|
298
|
+
return False
|
|
299
|
+
|
|
216
300
|
|
|
217
301
|
session_monitor = SessionMonitor()
|
|
218
302
|
|
|
@@ -684,6 +768,10 @@ def build_openai_request(anthropic_body: dict) -> dict:
|
|
|
684
768
|
# - More than 1 message (conversation is in progress)
|
|
685
769
|
# - Last assistant was text-only (would cause premature stop)
|
|
686
770
|
# - OR conversation has tool_result messages (active agentic loop)
|
|
771
|
+
#
|
|
772
|
+
# LOOP PROTECTION: Release to "auto" if the session monitor detects
|
|
773
|
+
# a tool call loop (same tools called repeatedly), to prevent
|
|
774
|
+
# runaway token consumption.
|
|
687
775
|
n_msgs = len(anthropic_body.get("messages", []))
|
|
688
776
|
has_tool_results = any(
|
|
689
777
|
isinstance(m.get("content"), list) and any(
|
|
@@ -692,16 +780,47 @@ def build_openai_request(anthropic_body: dict) -> dict:
|
|
|
692
780
|
)
|
|
693
781
|
for m in anthropic_body.get("messages", [])
|
|
694
782
|
)
|
|
695
|
-
|
|
783
|
+
|
|
784
|
+
# Record tool calls from the last assistant message for loop detection
|
|
785
|
+
_record_last_assistant_tool_calls(anthropic_body)
|
|
786
|
+
|
|
787
|
+
# Check if loop breaker should override tool_choice
|
|
788
|
+
if session_monitor.should_release_tool_choice():
|
|
789
|
+
openai_body["tool_choice"] = "auto"
|
|
790
|
+
session_monitor.consecutive_forced_count = 0
|
|
791
|
+
logger.warning("tool_choice set to 'auto' by LOOP BREAKER")
|
|
792
|
+
elif _last_assistant_was_text_only(anthropic_body):
|
|
696
793
|
openai_body["tool_choice"] = "required"
|
|
794
|
+
session_monitor.consecutive_forced_count += 1
|
|
697
795
|
logger.info("tool_choice forced to 'required' (last assistant was text-only)")
|
|
698
796
|
elif has_tool_results and n_msgs > 2:
|
|
699
797
|
openai_body["tool_choice"] = "required"
|
|
798
|
+
session_monitor.consecutive_forced_count += 1
|
|
700
799
|
logger.info("tool_choice forced to 'required' (active agentic loop with tool results)")
|
|
800
|
+
else:
|
|
801
|
+
session_monitor.consecutive_forced_count = 0
|
|
701
802
|
|
|
702
803
|
return openai_body
|
|
703
804
|
|
|
704
805
|
|
|
806
|
+
def _record_last_assistant_tool_calls(anthropic_body: dict):
|
|
807
|
+
"""Extract tool call names from the last assistant message and record
|
|
808
|
+
them in the session monitor for loop detection."""
|
|
809
|
+
messages = anthropic_body.get("messages", [])
|
|
810
|
+
tool_names = []
|
|
811
|
+
for msg in reversed(messages):
|
|
812
|
+
if msg.get("role") != "assistant":
|
|
813
|
+
continue
|
|
814
|
+
content = msg.get("content")
|
|
815
|
+
if isinstance(content, list):
|
|
816
|
+
for block in content:
|
|
817
|
+
if isinstance(block, dict) and block.get("type") == "tool_use":
|
|
818
|
+
tool_names.append(block.get("name", "unknown"))
|
|
819
|
+
break
|
|
820
|
+
if tool_names:
|
|
821
|
+
session_monitor.record_tool_calls(tool_names)
|
|
822
|
+
|
|
823
|
+
|
|
705
824
|
def _last_assistant_was_text_only(anthropic_body: dict) -> bool:
|
|
706
825
|
"""Check if the last assistant message in the conversation was text-only
|
|
707
826
|
(no tool_use blocks). This indicates the model may be prematurely ending
|
|
@@ -1281,6 +1400,15 @@ async def context_status():
|
|
|
1281
1400
|
"overflow_count": session_monitor.overflow_count,
|
|
1282
1401
|
"prune_threshold": PROXY_CONTEXT_PRUNE_THRESHOLD,
|
|
1283
1402
|
"recent_history": session_monitor.context_history[-10:],
|
|
1403
|
+
# Loop protection stats
|
|
1404
|
+
"loop_protection": {
|
|
1405
|
+
"consecutive_forced_count": session_monitor.consecutive_forced_count,
|
|
1406
|
+
"loop_warnings_emitted": session_monitor.loop_warnings_emitted,
|
|
1407
|
+
"tool_call_history_len": len(session_monitor.tool_call_history),
|
|
1408
|
+
"is_looping": session_monitor.detect_tool_loop()[0],
|
|
1409
|
+
"loop_repeat_count": session_monitor.detect_tool_loop()[1],
|
|
1410
|
+
"recent_tool_patterns": session_monitor.tool_call_history[-5:],
|
|
1411
|
+
},
|
|
1284
1412
|
}
|
|
1285
1413
|
|
|
1286
1414
|
|
|
Binary file
|
|
Binary file
|