-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
1301 lines (1194 loc) · 50.6 KB
/
agent.py
File metadata and controls
1301 lines (1194 loc) · 50.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import base64
import io
import json
import os
import re
import subprocess
import time
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any
import anthropic
from PIL import Image
from claude_print_client import ClaudePrintClient
from claude_print_runtime import (
DEFAULT_LLM_BACKEND,
LLM_BACKENDS,
assistant_blocks_from_claude_print_payload,
build_claude_print_env,
clip_text,
extract_first_json_object,
extract_stream_json_result,
normalize_claude_print_effort,
normalize_llm_backend,
render_message_history_for_claude_print,
resolve_claude_print_model,
)
from config import CortexConfig
from fl_state import (
EXTRACT_FL_STATE_TOOL_NAME,
extract_fl_state_from_image,
fl_state_tool_param,
resolve_reference_images,
)
from fl_visual_judge import VisualJudgeResult, judge_fl_visual
from learning import generate_lessons, load_relevant_lessons, store_lessons
from memory import ensure_session, write_event, write_metrics
from run_eval import evaluate_drum_run
from self_improve import (
SkillUpdate,
apply_skill_updates,
auto_promote_queued_candidates,
parse_reflection_response,
queue_skill_update_candidates,
skill_digest,
)
from skill_routing import (
SkillManifestEntry,
build_skill_manifest,
manifest_summaries_text,
resolve_skill_content,
route_manifest_entries,
)
if TYPE_CHECKING:
from computer_use import ToolResult
BASE_SYSTEM_PROMPT = """You are controlling FL Studio Desktop on macOS via screenshots and mouse/keyboard.
Rules:
- Keyboard first. Prefer shortcuts over clicking whenever possible.
- Keep verification lightweight. Confirm ambiguous targets once, then act.
- Do not loop on inspection actions. If two inspections fail to increase confidence, switch to a decisive action.
- After every action, verify the UI changed as expected. If not, try one alternative and move on.
- Use extract_fl_state to convert screenshots into structured UI facts before repeating zooms.
- Use app-specific skills for UI conventions and domain workflows.
- Keep the run safe: do not interact with anything outside FL Studio.
- Never use OS-level shortcuts: do not press Command+Q, Command+Tab, Command+W, Command+M, or anything intended to quit/switch apps.
"""
def _supports_computer_20251124(model: str) -> bool:
"""
Map model id -> computer tool generation.
Anthropic currently documents `computer_20251124` (zoom-enabled) for Opus
4.6/4.5, while Sonnet 4.6/4.5 stay on `computer_20250124`.
"""
model_id = model.strip().lower()
return ("opus-4-6" in model_id) or ("opus-4-5" in model_id)
def build_system_prompt(*, tool_api_type: str) -> str:
# zoom exists only on computer_20251124
zoom_line = ""
no_zoom_line = ""
if tool_api_type == "computer_20251124":
zoom_line = "- Use the zoom action when UI elements are small/dense.\n"
else:
no_zoom_line = (
"- Zoom action is unavailable in this run. Do not attempt keyboard zoom aliases "
"(minus, plus, kp_subtract, kp_add), pinch-style behavior, or exploratory scroll "
"as a zoom substitute.\n"
"- If an unsupported key name fails once, do not retry key-name variants. "
"Switch to another supported action.\n"
)
return BASE_SYSTEM_PROMPT + "\n" + zoom_line + no_zoom_line
PROMPT_CACHING_BETA_FLAG = "prompt-caching-2024-07-31"
READ_SKILL_TOOL_NAME = "read_skill"
NON_PRODUCTIVE_ACTIONS = {"zoom", "mouse_move"}
RESET_NON_PRODUCTIVE_ACTIONS = {"left_click", "key"}
MAX_SAME_STEP_RETRIES = 2
def _read_skill_tool_param() -> dict[str, Any]:
return {
"name": READ_SKILL_TOOL_NAME,
"description": (
"Read full contents of a skill document by stable skill_ref. "
"Use this only when title/description metadata is not enough."
),
"input_schema": {
"type": "object",
"properties": {
"skill_ref": {
"type": "string",
"description": "Stable skill reference from skill metadata list.",
}
},
"required": ["skill_ref"],
"additionalProperties": False,
},
}
def _inject_prompt_caching(messages: list[dict[str, Any]], *, breakpoints: int = 3) -> None:
"""
Put cache breakpoints on the most recent user turns so repeated loops are cheap.
Mirrors Anthropic quickstart behavior.
"""
remaining = breakpoints
for msg in reversed(messages):
if msg.get("role") != "user":
continue
content = msg.get("content")
if not isinstance(content, list) or not content:
continue
if remaining > 0:
remaining -= 1
last = content[-1]
if isinstance(last, dict):
last["cache_control"] = {"type": "ephemeral"}
else:
last = content[-1]
if isinstance(last, dict) and "cache_control" in last:
del last["cache_control"]
break
def _tool_result_block(tool_use_id: str, result: ToolResult) -> dict[str, Any]:
content: list[dict[str, Any]] = []
if result.output:
content.append({"type": "text", "text": result.output})
if result.error:
content.append({"type": "text", "text": result.error})
if result.base64_image_png:
content.append(
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": result.base64_image_png,
},
}
)
return {
"type": "tool_result",
"tool_use_id": tool_use_id,
"is_error": result.is_error(),
"content": content or "",
}
def _normalize_llm_backend(value: str) -> str:
return normalize_llm_backend(value)
def _clip_text(text: str, *, max_chars: int = 4000) -> str:
return clip_text(text, max_chars=max_chars)
def _render_message_history_for_claude_print(messages: list[dict[str, Any]]) -> str:
return render_message_history_for_claude_print(
messages,
max_messages=12,
include_tool_result_image_hint=True,
)
def _extract_latest_tool_result_image(messages: list[dict[str, Any]]) -> dict[str, Any] | None:
"""
Return the latest tool_result image block as a compact JPEG image block.
"""
for msg in reversed(messages):
if str(msg.get("role")) != "user":
continue
content = msg.get("content")
if not isinstance(content, list):
continue
for block in reversed(content):
if not (isinstance(block, dict) and str(block.get("type")) == "tool_result"):
continue
parts = block.get("content")
if not isinstance(parts, list):
continue
for part in reversed(parts):
if not (isinstance(part, dict) and str(part.get("type")) == "image"):
continue
src = part.get("source", {})
if not isinstance(src, dict):
continue
data = src.get("data")
media_type = str(src.get("media_type", "image/png")).strip() or "image/png"
if not isinstance(data, str) or not data:
continue
compact = _compact_image_block_for_prompt(data_b64=data, media_type=media_type)
if compact is not None:
return compact
return None
def _compact_image_block_for_prompt(*, data_b64: str, media_type: str) -> dict[str, Any] | None:
"""
Downscale/compress screenshot blocks so claude -p prompt size stays tractable.
"""
try:
raw = base64.b64decode(data_b64.encode("ascii"), validate=True)
with Image.open(io.BytesIO(raw)) as img:
working = img.convert("RGB")
working.thumbnail((640, 480))
out = io.BytesIO()
working.save(out, format="JPEG", quality=55, optimize=True)
compact_b64 = base64.b64encode(out.getvalue()).decode("ascii")
except Exception:
return None
return {
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": compact_b64,
},
}
def _extract_first_json_object(raw: str) -> dict[str, Any]:
return extract_first_json_object(raw, max_error_chars=600)
def _assistant_blocks_from_claude_print_payload(
*,
payload: dict[str, Any],
allowed_tool_names: set[str],
) -> list[dict[str, Any]]:
return assistant_blocks_from_claude_print_payload(
payload=payload,
allowed_tool_names=allowed_tool_names,
)
def _create_executor_response_via_claude_print(
*,
model: str,
effort: str | None,
system_blocks: list[dict[str, Any]],
tools: list[dict[str, Any]],
messages: list[dict[str, Any]],
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
"""
Run one FL executor turn via claude -p using stream-json input.
We include the latest screenshot as a compact image block so the model
still has visual grounding.
"""
allowed_tool_names = {
str(tool.get("name", "")).strip()
for tool in tools
if isinstance(tool, dict) and str(tool.get("name", "")).strip()
}
tools_for_prompt: list[dict[str, Any]] = []
for tool in tools:
if not isinstance(tool, dict):
continue
name = str(tool.get("name", "")).strip()
if not name:
continue
tools_for_prompt.append(
{
"name": name,
"description": str(tool.get("description", "")).strip(),
"input_schema": tool.get("input_schema", {}),
}
)
system_text = "\n\n".join(
str(block.get("text", "")).strip()
for block in system_blocks
if isinstance(block, dict) and str(block.get("type", "")).strip() == "text"
).strip()
history_text = _render_message_history_for_claude_print(messages)
prompt = (
"You are the planner for a tool-using FL Studio loop.\n"
"Return exactly one JSON object with this shape:\n"
"{\n"
' "assistant_text": "short reasoning",\n'
' "tool_calls": [{"name":"tool_name","input":{...}}]\n'
"}\n"
"Rules:\n"
"- Use only listed tools.\n"
"- Prefer one decisive tool call at a time.\n"
"- tool_calls may be empty if task is complete.\n"
"- input must satisfy tool input_schema.\n"
"- For computer tool calls, input.action must exactly match the schema enum.\n"
"- Never use shorthand actions like click/drag/type/press; use exact names such as left_click, left_click_drag, mouse_move, key, screenshot, scroll.\n"
"- For left_click_drag, provide both start_coordinate and coordinate.\n"
"- For scroll, provide both scroll_direction and scroll_amount.\n"
"- Output strict JSON only (no markdown).\n\n"
f"SYSTEM_PROMPT:\n{system_text}\n\n"
f"TOOLS:\n{json.dumps(tools_for_prompt, ensure_ascii=True, indent=2, sort_keys=True)}\n\n"
f"MESSAGE_HISTORY:\n{history_text}\n"
)
content_blocks: list[dict[str, Any]] = [{"type": "text", "text": prompt}]
latest_image = _extract_latest_tool_result_image(messages)
if latest_image is not None:
content_blocks.append({"type": "text", "text": "LATEST_SCREENSHOT:"})
content_blocks.append(latest_image)
input_line = {
"type": "user",
"message": {
"role": "user",
"content": content_blocks,
},
}
timeout_s = max(15, int(os.getenv("CORTEX_CLAUDE_PRINT_TIMEOUT_S", "120")))
requested_model, effective_model = resolve_claude_print_model(
model,
fallback_model="claude-opus-4-6",
)
requested_effort = normalize_claude_print_effort(effort, default="high")
cmd = [
"claude",
"-p",
"--input-format",
"stream-json",
"--output-format",
"stream-json",
"--verbose",
"--tools",
"",
"--effort",
requested_effort,
]
cmd.extend(["--model", effective_model])
cmd_env = build_claude_print_env()
try:
proc = subprocess.run(
cmd,
input=json.dumps(input_line, ensure_ascii=True) + "\n",
capture_output=True,
text=True,
timeout=timeout_s,
check=False,
env=cmd_env,
)
except subprocess.TimeoutExpired as exc:
raise RuntimeError(
f"claude -p executor turn timed out after {timeout_s}s. "
"Try increasing CORTEX_CLAUDE_PRINT_TIMEOUT_S or reducing max steps."
) from exc
stdout = str(proc.stdout or "")
stderr = str(proc.stderr or "")
if proc.returncode != 0:
raise RuntimeError(
"claude -p executor turn failed "
f"(code={proc.returncode}): {_clip_text(stderr or stdout, max_chars=800)}"
)
result_text, usage_payload = extract_stream_json_result(stdout)
if not result_text:
raise RuntimeError(f"claude -p produced no result payload: {_clip_text(stdout, max_chars=800)}")
payload = _extract_first_json_object(result_text)
assistant_blocks = _assistant_blocks_from_claude_print_payload(
payload=payload,
allowed_tool_names=allowed_tool_names,
)
usage_payload = {
"backend": "claude_print",
"model": effective_model,
"requested_model": requested_model,
"effort": requested_effort,
"stdout_chars": len(stdout),
"stderr_chars": len(stderr),
**usage_payload,
}
return assistant_blocks, usage_payload
def _save_png_b64(session_dir: Path, *, name: str, b64: str) -> Path:
out = session_dir / name
out.write_bytes(base64.b64decode(b64))
return out
def _image_block_from_file(path: Path) -> dict[str, Any] | None:
try:
data = base64.b64encode(path.read_bytes()).decode("ascii")
except Exception:
return None
return {
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": data,
},
}
def _select_reflection_screenshots(
events: list[dict[str, Any]],
*,
max_images: int = 3,
) -> list[tuple[int, Path]]:
candidates: list[tuple[int, Path]] = []
for ev in events:
shot = ev.get("screenshot")
step = ev.get("step")
if not isinstance(shot, str) or not shot:
continue
if not isinstance(step, int):
continue
p = Path(shot)
if p.exists():
candidates.append((step, p))
if not candidates:
return []
# Pick representative frames: early, middle, late.
picks: list[tuple[int, Path]] = []
indexes = sorted({0, len(candidates) // 2, len(candidates) - 1})
for idx in indexes:
picks.append(candidates[idx])
# Keep stable ordering and max bound.
seen: set[str] = set()
deduped: list[tuple[int, Path]] = []
for step, p in picks:
key = str(p)
if key in seen:
continue
seen.add(key)
deduped.append((step, p))
return deduped[:max_images]
def _load_fl_reference_snippet(*, max_chars: int = 1600) -> str:
p = Path("docs/FL-STUDIO-REFERENCE.md")
if not p.exists():
return ""
try:
text = p.read_text(encoding="utf-8")
except Exception:
return ""
text = " ".join(text.split())
return text[:max_chars]
def _read_session_events(events_path: Path) -> list[dict[str, Any]]:
events: list[dict[str, Any]] = []
try:
lines = events_path.read_text(encoding="utf-8").splitlines()
except Exception:
return events
for line in lines:
try:
parsed = json.loads(line)
except Exception:
continue
if isinstance(parsed, dict):
events.append(parsed)
return events
def _latest_screenshot_from_events(events: list[dict[str, Any]]) -> Path | None:
latest_step = -1
latest_path: Path | None = None
for ev in events:
raw_path = ev.get("screenshot")
raw_step = ev.get("step")
if not isinstance(raw_path, str) or not raw_path.strip():
continue
if not isinstance(raw_step, int):
continue
path = Path(raw_path)
if not path.exists():
continue
if raw_step >= latest_step:
latest_step = raw_step
latest_path = path
return latest_path
def _build_fallback_updates(
*,
eval_result: dict[str, Any],
allowed_skill_refs: set[str],
skill_digests: dict[str, str],
) -> tuple[list[SkillUpdate], float]:
reasons = eval_result.get("reasons")
if not isinstance(reasons, list):
return [], 0.0
reason_set = {r for r in reasons if isinstance(r, str)}
if not reason_set:
return [], 0.0
target_ref = "fl-studio/drum-pattern"
if target_ref not in allowed_skill_refs:
return [], 0.0
digest = skill_digests.get(target_ref, "")
if not digest:
return [], 0.0
evidence_steps: list[int] = []
clicks = eval_result.get("clicks", [])
if isinstance(clicks, list):
for item in clicks[:4]:
if isinstance(item, dict):
s = item.get("step")
if isinstance(s, int) and s > 0:
evidence_steps.append(s)
if not evidence_steps:
evidence_steps = [1]
bullets: list[str] = []
replace_rules = []
root_parts: list[str] = []
if "selector_zone_misclick" in reason_set:
root_parts.append("First step clicks entered selector strip instead of the step-button band.")
bullets.append(
"When clicking kick steps, avoid the selector strip near channel name; if Hint Bar shows Select/UpDown, move right and retry."
)
if "inspection_loop" in reason_set:
root_parts.append("Run spent too many inspection actions before decisive clicks.")
replace_rules.append(
{
"find": "Do not spam zoom on the step row. At most one zoom is allowed before the click sequence.",
"replace": "Do at most one zoom before the click sequence; then execute the four clicks without additional zoom.",
}
)
if "insufficient_step_clicks" in reason_set:
root_parts.append("Run ended before four kick-step clicks were completed.")
bullets.append("Do not stop early: complete exactly four kick-step clicks (1,5,9,13) before any final verification.")
if not bullets and not replace_rules:
return [], 0.0
rr_objs = []
from self_improve import ReplaceRule # local import to avoid widening global import list
for rr in replace_rules[:2]:
rr_objs.append(ReplaceRule(find=rr["find"], replace=rr["replace"]))
update = SkillUpdate(
skill_ref=target_ref,
skill_digest=digest,
root_cause=" ".join(root_parts)[:400],
evidence_steps=sorted(set(evidence_steps))[:6],
replace_rules=rr_objs,
append_bullets=bullets[:3],
)
return [update], 0.8
@dataclass
class RunResult:
messages: list[dict[str, Any]]
metrics: dict[str, Any]
def run_agent(
*,
cfg: CortexConfig,
task: str,
session_id: int,
max_steps: int = 80,
model: str,
allowed_actions: set[str] | None = None,
load_skills: bool = True,
posttask_learn: bool = True,
posttask_mode: str = "direct",
llm_backend: str = DEFAULT_LLM_BACKEND,
claude_print_effort: str | None = None,
verbose: bool = False,
) -> RunResult:
# Quartz-backed computer tool import is delayed so module-level utilities/tests
# can run on environments where Quartz bindings are unavailable.
from computer_use import ComputerTool, ToolResult
llm_backend = _normalize_llm_backend(llm_backend)
api_key = str(getattr(cfg, "anthropic_api_key", "") or "").strip()
client: Any | None = None
if llm_backend == "anthropic":
if not api_key:
raise RuntimeError("ANTHROPIC_API_KEY is required when llm_backend=anthropic.")
client = anthropic.Anthropic(api_key=api_key, max_retries=3)
else:
client = ClaudePrintClient(default_effort=claude_print_effort)
if claude_print_effort is not None:
normalized_effort = str(claude_print_effort).strip().lower()
if normalized_effort not in {"low", "medium", "high"}:
raise ValueError(
f"Invalid claude_print_effort={claude_print_effort!r}. Expected one of: low, medium, high."
)
claude_print_effort = normalized_effort
# Tool version + beta flag must match the chosen model's computer-use support.
# Do not rely on "heavy vs decider" naming because users may run Sonnet as the
# primary model for speed/cost.
if _supports_computer_20251124(model):
computer_api_type = cfg.computer_tool_type_heavy
computer_beta = cfg.computer_use_beta_heavy
else:
computer_api_type = cfg.computer_tool_type_decider
computer_beta = cfg.computer_use_beta_decider
computer = ComputerTool(
api_type=computer_api_type,
display_width_px=cfg.display_width_px,
display_height_px=cfg.display_height_px,
enable_zoom=(computer_api_type == "computer_20251124"),
)
paths = ensure_session(session_id)
messages: list[dict[str, Any]] = [
{
"role": "user",
"content": [{"type": "text", "text": task}],
}
]
# System is split so we can put the cache breakpoint after stable "skills" text.
# This mirrors Anthropic prompt caching guidance: keep the prefix stable.
base_system_block: dict[str, Any] = {"type": "text", "text": build_system_prompt(tool_api_type=computer_api_type)}
skill_manifest_entries: list[SkillManifestEntry] = []
routed_skill_entries: list[SkillManifestEntry] = []
if load_skills:
skill_manifest_entries = build_skill_manifest()
routed_skill_entries = route_manifest_entries(task=task, entries=skill_manifest_entries, top_k=3)
skills_text = manifest_summaries_text(routed_skill_entries)
else:
skills_text = "No skills loaded."
skills_system_block: dict[str, Any] = {"type": "text", "text": skills_text}
if posttask_learn:
lessons_text, lessons_loaded = load_relevant_lessons(task, max_lessons=10, max_sessions=5)
else:
lessons_text, lessons_loaded = ("No prior lessons loaded.", 0)
lessons_system_block: dict[str, Any] = {"type": "text", "text": lessons_text}
skill_usage_block: dict[str, Any] = {
"type": "text",
"text": (
"Skills policy:\n"
"- You only have skill titles and descriptions at start.\n"
"- If a listed skill may help, call read_skill with that exact skill_ref to fetch full steps.\n"
"- Do not assume full skill contents without calling read_skill.\n"
"- Keep read_skill calls targeted; avoid reading every skill."
"\n"
"State policy:\n"
+ (
"- Use extract_fl_state after screenshot when UI is ambiguous.\n"
"- Prefer extracting structured state (rows/active steps) over repeated zoom loops.\n"
if client is not None
else "- extract_fl_state is unavailable in this run; use screenshot + direct action/verification.\n"
)
),
}
system_blocks = [base_system_block, skills_system_block, lessons_system_block, skill_usage_block]
tools: list[dict[str, Any]] = [computer.to_tool_param()]
if client is not None:
tools.append(fl_state_tool_param())
if load_skills:
tools.append(_read_skill_tool_param())
betas = [computer_beta]
if cfg.enable_prompt_caching:
betas.append(PROMPT_CACHING_BETA_FLAG)
# Cache after stable context blocks so repeated runs can reuse prefix tokens.
skills_system_block["cache_control"] = {"type": "ephemeral"}
lessons_system_block["cache_control"] = {"type": "ephemeral"}
# Anthropic limit: max 4 cache_control blocks total in a request.
# We currently use 2 on system blocks (skills + lessons), so user-turn cache
# breakpoints must be capped to keep requests valid.
system_cache_blocks = int("cache_control" in skills_system_block) + int("cache_control" in lessons_system_block)
user_cache_breakpoints = max(0, 4 - system_cache_blocks)
# Reduce screenshot/tool payload overhead when supported.
# If unsupported by the model, the API will 400; we'll disable if that happens.
betas.append(cfg.token_efficient_tools_beta)
metrics: dict[str, Any] = {
"session_id": session_id,
"task": task,
"model": model,
"llm_backend": llm_backend,
"time_start": time.time(),
"steps": 0,
"load_skills": load_skills,
"tool_actions": 0,
"skill_reads": 0,
"tool_errors": 0,
"loop_guard_blocks": 0,
"posttask_learn": posttask_learn,
"posttask_patch_attempted": False,
"posttask_patch_applied": 0,
"posttask_candidates_queued": 0,
"posttask_allowed_source": None,
"posttask_allowed_skill_refs": [],
"posttask_skip_reason": None,
"lessons_loaded": lessons_loaded,
"lessons_generated": 0,
"auto_promotion_applied": 0,
"auto_promotion_reason": None,
"eval_passed": None,
"eval_score": None,
"eval_reasons": [],
"eval_final_verdict": "unknown",
"eval_source": "none",
"eval_disagreement": False,
"eval_det_passed": None,
"eval_det_score": None,
"eval_det_reasons": [],
"judge_model": cfg.model_visual_judge,
"judge_passed": None,
"judge_score": None,
"judge_confidence": None,
"judge_reasons": [],
"judge_reference_images": [],
"judge_observed_steps": [],
"usage": [],
}
# Hard guardrail for Opus path: stop inspection loops and force decisive actions.
non_productive_streak = 0
loop_guard_enabled = computer_api_type == "computer_20251124"
read_skill_refs: set[str] = set()
step = 1
same_step_retries = 0
while step <= max_steps:
metrics["steps"] = step
if cfg.enable_prompt_caching and llm_backend == "anthropic":
_inject_prompt_caching(messages, breakpoints=user_cache_breakpoints)
if llm_backend == "anthropic":
if client is None:
raise RuntimeError("Anthropic client unavailable while llm_backend=anthropic.")
try:
resp = client.beta.messages.create(
model=model,
max_tokens=2048,
system=system_blocks,
tools=tools,
messages=messages,
betas=betas,
)
except anthropic.BadRequestError as e:
# If the token-efficient-tools beta isn't supported, retry once without it.
msg = str(getattr(e, "message", "")) + " " + str(getattr(e, "body", ""))
if cfg.token_efficient_tools_beta in msg and cfg.token_efficient_tools_beta in betas:
betas = [b for b in betas if b != cfg.token_efficient_tools_beta]
resp = client.beta.messages.create(
model=model,
max_tokens=2048,
system=system_blocks,
tools=tools,
messages=messages,
betas=betas,
)
else:
raise
# Usage accounting (incl. prompt caching fields when enabled)
try:
usage = resp.usage.model_dump() # type: ignore[attr-defined]
except Exception:
usage = getattr(resp, "usage", None)
usage = usage.model_dump() if usage is not None and hasattr(usage, "model_dump") else {}
assistant_blocks = [b.model_dump() for b in resp.content] # type: ignore[attr-defined]
else:
assistant_blocks, usage = _create_executor_response_via_claude_print(
model=model,
effort=claude_print_effort,
system_blocks=system_blocks,
tools=tools,
messages=messages,
)
metrics["usage"].append(usage)
messages.append({"role": "assistant", "content": assistant_blocks})
tool_results: list[dict[str, Any]] = []
retry_same_step = False
decisive_action_succeeded = False
for block in assistant_blocks:
if not (isinstance(block, dict) and block.get("type") == "tool_use"):
continue
tool_use_id = block.get("id", "")
tool_name = block.get("name", "")
tool_input = block.get("input", {})
if tool_name == computer.name:
metrics["tool_actions"] += 1
try:
tool_in = tool_input if isinstance(tool_input, dict) else {}
action = tool_in.get("action")
if loop_guard_enabled and action in NON_PRODUCTIVE_ACTIONS and non_productive_streak >= 2:
result = ToolResult(
error=(
"Loop guard: too many consecutive zoom/mouse_move actions without progress. "
"Next action must be decisive: left_click or key."
)
)
metrics["loop_guard_blocks"] += 1
retry_same_step = True
elif allowed_actions is not None:
if not isinstance(action, str) or action not in allowed_actions:
result = ToolResult(error=f"Action not allowed in this run: {action!r}")
else:
result = computer.run(tool_in)
else:
result = computer.run(tool_in)
if action in NON_PRODUCTIVE_ACTIONS and not result.is_error():
non_productive_streak += 1
elif action in RESET_NON_PRODUCTIVE_ACTIONS and not result.is_error():
non_productive_streak = 0
decisive_action_succeeded = True
except Exception as e:
# Don't crash the loop on unexpected local tool errors; surface it to the model.
result = ToolResult(error=f"Local tool exception: {type(e).__name__}: {e}")
if result.is_error():
metrics["tool_errors"] += 1
elif tool_name == EXTRACT_FL_STATE_TOOL_NAME:
tool_in = tool_input if isinstance(tool_input, dict) else {}
goal = str(tool_in.get("goal", "")).strip()
task_hint = str(tool_in.get("task_hint", "")).strip() or task
if client is None:
result = ToolResult(error="extract_fl_state is unavailable because no LLM client is configured")
metrics["tool_errors"] += 1
else:
try:
shot = computer.run({"action": "screenshot"})
if shot.is_error() or not shot.base64_image_png:
result = ToolResult(error=shot.error or "extract_fl_state could not capture screenshot")
metrics["tool_errors"] += 1
else:
state = extract_fl_state_from_image(
client=client,
model=cfg.model_decider,
screenshot_b64=shot.base64_image_png,
goal=goal,
task_hint=task_hint,
)
result = ToolResult(
output=json.dumps(state, ensure_ascii=True),
base64_image_png=shot.base64_image_png,
)
except Exception as e:
result = ToolResult(error=f"extract_fl_state exception: {type(e).__name__}: {e}")
metrics["tool_errors"] += 1
elif tool_name == READ_SKILL_TOOL_NAME:
metrics["skill_reads"] += 1
tool_in = tool_input if isinstance(tool_input, dict) else {}
skill_ref = tool_in.get("skill_ref")
if not isinstance(skill_ref, str):
result = ToolResult(error=f"read_skill requires string skill_ref, got: {skill_ref!r}")
metrics["tool_errors"] += 1
else:
content, err = resolve_skill_content(skill_manifest_entries, skill_ref)
if err:
result = ToolResult(error=err)
metrics["tool_errors"] += 1
else:
read_skill_refs.add(skill_ref)
result = ToolResult(output=f"skill_ref: {skill_ref}\n\n{content}")
else:
result = ToolResult(error=f"Unknown tool requested: {tool_name!r}")
if result.base64_image_png:
img_path = _save_png_b64(paths.session_dir, name=f"step-{step:03d}.png", b64=result.base64_image_png)
else:
img_path = None
write_event(
paths.jsonl_path,
{
"step": step,
"tool": tool_name,
"tool_input": tool_input,
"ok": not result.is_error(),
"error": result.error,
"output": result.output,
"screenshot": str(img_path) if img_path else None,
"usage": usage,
},
)
if verbose:
action = tool_input.get("action") if isinstance(tool_input, dict) else None
print(
f"[step {step:03d}] tool={tool_name} action={action!r} ok={not result.is_error()} error={result.error!r}",
flush=True,
)
tool_results.append(_tool_result_block(tool_use_id, result))
if not tool_results:
# No tool calls => model claims it's done / can't proceed.
if verbose:
print(f"[step {step:03d}] no tool call; model stopped.", flush=True)
break
messages.append({"role": "user", "content": tool_results})
if retry_same_step and not decisive_action_succeeded and same_step_retries < MAX_SAME_STEP_RETRIES:
same_step_retries += 1
if verbose:
print(
f"[step {step:03d}] governor retry without step burn ({same_step_retries}/{MAX_SAME_STEP_RETRIES})",
flush=True,
)
continue
same_step_retries = 0
step += 1
# End-of-run evaluation: deterministic contract + independent visual judge.
all_events: list[dict[str, Any]] = _read_session_events(paths.jsonl_path)
tail_events: list[dict[str, Any]] = []
for ev in all_events[-20:]:
tail_events.append(
{
"step": ev.get("step"),
"tool": ev.get("tool"),
"tool_input": ev.get("tool_input"),
"ok": ev.get("ok"),
"error": ev.get("error"),
}
)
drum_eval = evaluate_drum_run(task, all_events).to_dict()
det_passed = bool(drum_eval.get("passed"))
try:
det_score = float(drum_eval.get("score", 0.0))
except (TypeError, ValueError):
det_score = 0.0
det_reasons = drum_eval.get("reasons")
if not isinstance(det_reasons, list):
det_reasons = []
visual_judge: VisualJudgeResult | None = None
final_shot = _latest_screenshot_from_events(all_events)
if final_shot is not None and client is not None:
try:
screenshot_b64 = base64.b64encode(final_shot.read_bytes()).decode("ascii")
judge_refs = resolve_reference_images()
visual_judge = judge_fl_visual(
client=client,
model=cfg.model_visual_judge,
final_screenshot_b64=screenshot_b64,
task=task,
rubric=(
"Pass only if FL Studio kick row uses 4-on-the-floor pattern with active steps 1,5,9,13 "
"and without obvious step-index mismatches."
),
reference_images=judge_refs,
)
write_event(
paths.jsonl_path,
{
"step": metrics["steps"],
"tool": "visual_judge",
"tool_input": {
"model": cfg.model_visual_judge,
"final_screenshot": str(final_shot),
"reference_images": [str(p) for p in judge_refs],
},
"ok": True,
"error": None,
"output": visual_judge.to_dict(),