Repository navigation
Expand file tree
/
Copy pathagent.py
More file actions
1555 lines (1408 loc) · 53.2 KB
/
Copy pathagent.py
File metadata and controls
1555 lines (1408 loc) · 53.2 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
"""Workspace tools for ``deepfold chat --agent``.
v2 surface: search, patch, git read, allowlisted argv, todo. ``web_search``
defaults to free Tavily (keyless) whenever agent tools are on, unless the
user passed ``--no-agent-web`` or ``DEEPFOLD_AGENT_WEB=0``. Brave and Google
CSE are optional if those keys are set. Paths stay under ``--workspace``.
Writes, tests, commands, and web follow ``--agent-trust``. Not an
unrestricted shell. Session KV lives in ``chat`` (TokenLoop and Decode V2).
This module is CPU-only.
"""
from __future__ import annotations
import fnmatch
import json
import os
import re
import shutil
import subprocess
import sys
import urllib.error
import urllib.parse
import urllib.request
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
from difflib import unified_diff
from pathlib import Path
from typing import Any, Iterable, Sequence
from .paths import deepfold_home
__all__ = [
"EDIT_TOOLS",
"NET_TOOLS",
"READ_TOOLS",
"RUN_TOOLS",
"SCHEMAS",
"TOOL_END",
"TOOL_START",
"TRUST_LEVELS",
"AgentSession",
"PathEscape",
"clip_result",
"confirm_accepted",
"degenerate_tool_text",
"drop_agent_system",
"edit_preview",
"ensure_agent_system",
"execute",
"execute_calls",
"flatten_history",
"format_call",
"load_workspace_rules",
"looks_like_small_agent_model",
"needs_confirm",
"parse_tool_calls",
"preview",
"resolve_under",
"run_read_only_parallel",
"strip_tool_xml",
"suffix_after",
"plan_session_prefill",
"tool_call_closed",
"tool_schemas",
"truncated_tool_call",
"visible_stream_text",
]
SYSTEM_MARK = "deepfold-agent-workspace:"
TOOL_START = "<tool_call>"
TOOL_END = "</tool_call>"
_START = TOOL_START
_END = TOOL_END
_SKIP_DIR = frozenset(
{".git", "__pycache__", ".venv", "node_modules", ".pytest_cache", ".mypy_cache"}
)
TRUST_LEVELS = ("ask", "write", "workspace")
READ_TOOLS = frozenset(
{"list_dir", "read_file", "glob", "grep", "git_status", "git_diff"}
)
EDIT_TOOLS = frozenset({"write_file", "str_replace", "delete_file"})
RUN_TOOLS = frozenset({"run_tests", "run_argv"})
NET_TOOLS = frozenset({"web_search"})
_MAX_LIST = 200
_MAX_READ_BYTES = 1_000_000
_MAX_READ_LINES = 400
_MAX_WRITE_CHARS = 256 * 1024
_MAX_RESULT = 12_000
_MAX_RULES = 8 * 1024
_TEST_TIMEOUT_S = 120
_GREP_FILES = 50
_GREP_MATCHES = 80
_GREP_LINE = 200
_GIT_BLOCK = frozenset({"push", "fetch", "pull", "clone", "remote"})
_GIT_READ = frozenset({"status", "diff", "log", "show", "rev-parse"})
_PYTHON_STEMS = frozenset({"python", "python3"})
_ALLOW_STEMS = _PYTHON_STEMS | frozenset({"pytest", "ruff", "go", "git", "deepfold"})
_WEB_QUERY_MAX = 200
_WEB_NUM_DEFAULT = 5
_WEB_NUM_MAX = 8
_WEB_TIMEOUT_S = 15
_CSE_ENDPOINT = "https://www.googleapis.com/customsearch/v1"
_BRAVE_ENDPOINT = "https://api.search.brave.com/res/v1/web/search"
_TAVILY_ENDPOINT = "https://api.tavily.com/search"
_CSE_ENV_KEY = "DEEPFOLD_GOOGLE_CSE_KEY"
_CSE_ENV_CX = "DEEPFOLD_GOOGLE_CSE_CX"
_BRAVE_ENV = "DEEPFOLD_BRAVE_KEY"
_BRAVE_ENV_ALT = "BRAVE_API_KEY"
_TAVILY_ENV = "DEEPFOLD_TAVILY_KEY"
_TAVILY_ENV_ALT = "TAVILY_API_KEY"
_CSE_FILE = "cse.env"
_SECRET_KEYS = frozenset(
{
_CSE_ENV_KEY,
_CSE_ENV_CX,
_BRAVE_ENV,
_BRAVE_ENV_ALT,
_TAVILY_ENV,
_TAVILY_ENV_ALT,
}
)
SCHEMAS: list[dict[str, Any]] = [
{
"name": "list_dir",
"description": "List files and directories under a workspace path.",
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Relative path inside the workspace. Default: .",
}
},
},
},
{
"name": "read_file",
"description": "Read a UTF-8 text file. Returns numbered lines.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "File path inside the workspace"},
"offset": {"type": "integer", "description": "1-based start line (default 1)"},
"limit": {
"type": "integer",
"description": "Max lines to return (default 200, max 400)",
},
},
"required": ["path"],
},
},
{
"name": "glob",
"description": "List workspace paths matching a glob (e.g. gpu/cli/*.py).",
"parameters": {
"type": "object",
"properties": {
"pattern": {"type": "string", "description": "Glob relative to the workspace"},
},
"required": ["pattern"],
},
},
{
"name": "grep",
"description": "Regex search in workspace files. Caps: 50 files, 80 matches.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Python/ripgrep regex"},
"path": {"type": "string", "description": "File or directory to search. Default: ."},
"glob": {"type": "string", "description": "Optional filename glob, e.g. *.py"},
},
"required": ["query"],
},
},
{
"name": "git_status",
"description": "git status --porcelain in the workspace. Read-only.",
"parameters": {"type": "object", "properties": {}},
},
{
"name": "git_diff",
"description": "git diff for an optional workspace path. Read-only.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Optional path inside the workspace"},
},
},
},
{
"name": "str_replace",
"description": (
"Replace one unique old_string with new_string in a file. "
"Prefer this over write_file. Needs confirmation at trust=ask."
),
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"old_string": {"type": "string"},
"new_string": {"type": "string"},
},
"required": ["path", "old_string", "new_string"],
},
},
{
"name": "write_file",
"description": "Create or overwrite a UTF-8 file. Prefer str_replace for edits.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "File path inside the workspace"},
"content": {"type": "string", "description": "Full file contents"},
},
"required": ["path", "content"],
},
},
{
"name": "delete_file",
"description": "Delete one file in the workspace (not a directory).",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
},
},
{
"name": "run_tests",
"description": "Run pytest on one workspace path. No extra pytest flags.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Test file or directory"},
},
"required": ["path"],
},
},
{
"name": "run_argv",
"description": (
"Run an allowlisted command as an argv array (no shell). "
"Allowed: python, pytest, ruff, go, git, deepfold. git push is refused."
),
"parameters": {
"type": "object",
"properties": {
"argv": {
"type": "array",
"items": {"type": "string"},
"description": "Argument vector, e.g. [\"pytest\", \"-q\", \"tests\"]",
}
},
"required": ["argv"],
},
},
{
"name": "web_search",
"description": (
"Web search. Default is free Tavily (no API key). Brave or Google "
"CSE if those keys are set. On with --agent unless --no-agent-web. "
"Returns titles, URLs, and snippets. Not a browser."
),
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"},
"num": {
"type": "integer",
"description": "Hits to return (default 5, max 8)",
},
},
"required": ["query"],
},
},
{
"name": "todo",
"description": "Replace the in-session plan. status: pending, in_progress, done.",
"parameters": {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "string"},
"content": {"type": "string"},
"status": {"type": "string"},
},
},
}
},
"required": ["items"],
},
},
]
@dataclass
class AgentSession:
"""Per-chat agent state. Not persisted except as JSON history text."""
trust: str = "ask"
web: bool = False
todos: list[dict[str, str]] = field(default_factory=list)
class PathEscape(ValueError):
"""A tool path resolved outside the workspace."""
def looks_like_small_agent_model(cfg: dict[str, Any] | None) -> bool:
"""Qwen2.5-3B-class: tool JSON is unreliable. 14B is the agent plate."""
if not cfg:
return False
try:
hidden = int(cfg.get("hidden_size") or 0)
layers = int(cfg.get("num_hidden_layers") or 0)
except (TypeError, ValueError):
return False
return hidden <= 2048 and 1 <= layers <= 36
def load_workspace_rules(workspace: Path) -> str:
"""``AGENTS.md`` or ``.deepfold/instructions.md``, first hit, capped."""
for rel in ("AGENTS.md", ".deepfold/instructions.md"):
path = workspace / rel
if not path.is_file():
continue
try:
text = path.read_text(encoding="utf-8", errors="replace")
except OSError:
continue
if len(text) > _MAX_RULES:
text = text[:_MAX_RULES] + "\n... truncated"
return text.strip()
return ""
def tool_schemas(*, web: bool = False) -> list[dict[str, Any]]:
"""Schemas advertised to the model. ``web_search`` only when ``web`` is on."""
if web:
return list(SCHEMAS)
return [item for item in SCHEMAS if item["name"] != "web_search"]
def agent_system(workspace: Path, *, web: bool = False) -> str:
rules = load_workspace_rules(workspace)
extra = f"\nProject rules:\n{rules}\n" if rules else ""
web_line = ""
if web:
web_line = (
"web_search looks up current public facts (free Tavily by default; "
"Brave or Google CSE if those keys are set). Cite the returned URLs "
"and do not invent links. Prefer glob/grep for this workspace.\n"
)
return (
f"{SYSTEM_MARK} {workspace}\n"
"You work only in that workspace. Use tools to search (glob, grep), read, "
"patch (str_replace), and run tests. Prefer str_replace over write_file. "
"Do not claim you cannot access files. Read a file before rewriting it. "
"After a tool result, continue until the user's request is done. "
"Do not invent file contents you have not read. Keep a todo list for "
"multi-step work. There is no unrestricted shell; run_argv is allowlisted.\n"
f"{web_line}"
f"{extra}"
"A tool call is one complete JSON object, then you stop:\n"
"<tool_call>\n"
'{"name": "grep", "arguments": {"query": "TokenLoop", "glob": "*.py"}}\n'
"</tool_call>\n"
"Never fill a tool call with punctuation. Always close </tool_call>."
)
def ensure_agent_system(
history: list[dict[str, Any]], workspace: Path, *, web: bool = False
) -> None:
msg = {"role": "system", "content": agent_system(workspace, web=web)}
if (
history
and history[0].get("role") == "system"
and SYSTEM_MARK in str(history[0].get("content") or "")
):
history[0] = msg
return
history.insert(0, msg)
def drop_agent_system(history: list[dict[str, Any]]) -> None:
if (
history
and history[0].get("role") == "system"
and SYSTEM_MARK in str(history[0].get("content") or "")
):
history.pop(0)
def parse_tool_calls(text: str) -> list[dict[str, Any]]:
"""Pull ``<tool_call>{...}</tool_call>`` blocks. Unknown JSON is skipped."""
out: list[dict[str, Any]] = []
pos = 0
while True:
start = text.find(_START, pos)
if start < 0:
break
end = text.find(_END, start + len(_START))
if end < 0:
break
raw = text[start + len(_START) : end].strip()
pos = end + len(_END)
try:
obj = json.loads(raw)
except ValueError:
continue
if not isinstance(obj, dict):
continue
fn = obj.get("function") if isinstance(obj.get("function"), dict) else {}
name = obj.get("name") or fn.get("name")
args = obj.get("arguments", fn.get("arguments", {}))
if isinstance(args, str):
try:
args = json.loads(args)
except ValueError:
args = {"_raw": args}
if not isinstance(args, dict):
args = {"value": args}
if isinstance(name, str) and name.strip():
out.append({"name": name.strip(), "arguments": args})
return out
def tool_call_closed(text: str) -> bool:
"""Decode can stop: at least one complete ``</tool_call>`` with parseable JSON."""
return bool(parse_tool_calls(text))
def truncated_tool_call(text: str) -> bool:
"""Opened ``<tool_call>`` but JSON did not parse and it is not punctuation spam."""
if _START not in text or parse_tool_calls(text) or degenerate_tool_text(text):
return False
return True
def degenerate_tool_text(text: str) -> bool:
"""True when the model opened a tool call and then spammed junk (``!!!!``)."""
if _START not in text:
compact = "".join(text.split())
return len(compact) >= 16 and len(set(compact)) == 1
if parse_tool_calls(text):
return False
after = text.split(_START, 1)[1].replace(_END, "")
compact = "".join(after.split())
if not compact:
return True
if len(compact) >= 8 and not any(ch.isalnum() for ch in compact):
return True
return len(compact) >= 12 and len(set(compact)) <= 2
def strip_tool_xml(text: str) -> str:
pos = 0
bits: list[str] = []
while True:
start = text.find(_START, pos)
if start < 0:
bits.append(text[pos:])
break
bits.append(text[pos:start])
end = text.find(_END, start + len(_START))
if end < 0:
bits.append(text[start:])
break
pos = end + len(_END)
return "".join(bits).strip()
def visible_stream_text(text: str) -> str:
"""Prose only: hide complete tool blocks and an unclosed ``<tool_call>``."""
out: list[str] = []
pos = 0
while True:
start = text.find(_START, pos)
if start < 0:
rest = text[pos:]
lt = rest.rfind("<")
if lt >= 0:
tail = rest[lt:]
if _START.startswith(tail) or tail.startswith("<tool"):
rest = rest[:lt]
out.append(rest)
break
out.append(text[pos:start])
end = text.find(_END, start)
if end < 0:
break
pos = end + len(_END)
return "".join(out)
def suffix_after(old: Sequence[int], new: Sequence[int]) -> list[int] | None:
"""Token suffix for Wave B session KV. None means full prefill."""
prev = [int(x) for x in old]
cur = [int(x) for x in new]
n = len(prev)
if len(cur) < n:
return None
if prev != cur[:n]:
return None
return cur[n:]
def plan_session_prefill(
prefix_ids: Sequence[int] | None,
kv_len: int,
new_ids: Sequence[int],
) -> tuple[str, list[int]]:
"""How to extend KV. ``full`` / ``suffix`` / ``repeat``.
``repeat`` means the new template is identical to the ids already in KV
(need last-slot logits, no new tokens). ``full`` is reset + prefill.
"""
cur = [int(x) for x in new_ids]
if not cur:
return "full", []
if prefix_ids is None or int(kv_len) != len(prefix_ids):
return "full", cur
delta = suffix_after(prefix_ids, cur)
if delta is None:
return "full", cur
if not delta:
return "repeat", []
return "suffix", delta
def render_tool_calls(calls: list[dict[str, Any]]) -> str:
chunks: list[str] = []
for call in calls:
payload = {"name": call.get("name"), "arguments": call.get("arguments") or {}}
chunks.append(_START + "\n" + json.dumps(payload, ensure_ascii=False) + "\n" + _END)
return "\n".join(chunks)
def flatten_history(history: list[dict[str, Any]]) -> list[dict[str, str]]:
"""Tokenizer-safe turns: tool XML in content, tool results as ``<tool_response>``."""
out: list[dict[str, str]] = []
for row in history:
role = str(row.get("role") or "user")
content = str(row.get("content") or "")
calls = row.get("tool_calls")
if role == "assistant" and isinstance(calls, list) and calls:
xml = render_tool_calls(calls)
body = (content.rstrip() + "\n" + xml).strip() if content.strip() else xml
out.append({"role": "assistant", "content": body})
elif role == "tool":
if "<tool_response>" in content:
body = content
else:
body = f"<tool_response>\n{content}\n</tool_response>"
out.append({"role": "user", "content": body})
elif role in ("user", "assistant", "system"):
out.append({"role": role, "content": content})
return out
def _realpath(path: Path) -> Path:
return Path(os.path.realpath(path))
def resolve_under(root: Path, rel: str) -> Path:
root = _realpath(root)
text = (rel or ".").strip() or "."
if text.startswith("~"):
raise PathEscape("home paths are not allowed")
raw = Path(text)
target = _realpath(raw if raw.is_absolute() else root / raw)
root_s = os.path.normcase(str(root))
target_s = os.path.normcase(str(target))
if target_s != root_s and not target_s.startswith(root_s + os.sep):
raise PathEscape(f"{text!r} is outside the workspace")
return target
def _in_git(root: Path, target: Path) -> bool:
rel = _rel_posix(root, target)
if rel in (".", ""):
return False
return ".git" in Path(rel).parts
def _rel_posix(root: Path, target: Path) -> str:
rel = os.path.relpath(str(_realpath(target)), str(_realpath(root)))
return Path(rel).as_posix()
def _normalize_trust(trust: str) -> str:
text = (trust or "ask").strip().lower()
return text if text in TRUST_LEVELS else "ask"
def _argv_list(arguments: dict[str, Any]) -> list[str]:
raw = arguments.get("argv")
if not isinstance(raw, list):
return []
return [str(x) for x in raw]
def _exe_stem(argv0: str) -> str:
name = Path(argv0).name
lower = name.lower()
if lower.endswith(".exe"):
lower = lower[:-4]
return lower
def _git_mutating(arguments: dict[str, Any]) -> bool:
argv = _argv_list(arguments)
if not argv or _exe_stem(argv[0]) != "git":
return False
sub = argv[1].lstrip("-") if len(argv) > 1 else ""
return sub not in _GIT_READ
def needs_confirm(
name: str,
arguments: dict[str, Any] | None = None,
trust: str = "ask",
) -> bool:
arguments = arguments if isinstance(arguments, dict) else {}
level = _normalize_trust(trust)
if name in READ_TOOLS or name == "todo":
return False
if name == "run_argv" and _git_mutating(arguments):
return True
if name in EDIT_TOOLS:
return level == "ask"
if name in RUN_TOOLS or name in NET_TOOLS:
return level != "workspace"
return True
def confirm_accepted(line: str) -> bool:
return line.strip().lower() in ("y", "yes", "д", "да")
def format_call(name: str, arguments: dict[str, Any]) -> str:
if name == "write_file":
path = arguments.get("path", "")
content = arguments.get("content", "")
n = len(content) if isinstance(content, str) else 0
return f"write_file {path} ({n} chars)"
if name == "str_replace":
return f"str_replace {arguments.get('path', '')}"
if name == "delete_file":
return f"delete_file {arguments.get('path', '')}"
if name == "run_tests":
return f"run_tests {arguments.get('path', '')}"
if name == "run_argv":
return "run_argv " + " ".join(_argv_list(arguments))
if name == "read_file":
return f"read_file {arguments.get('path', '')}"
if name == "list_dir":
return f"list_dir {arguments.get('path', '.')}"
if name == "glob":
return f"glob {arguments.get('pattern', '')}"
if name == "grep":
return f"grep {arguments.get('query', '')}"
if name == "git_diff":
return f"git_diff {arguments.get('path', '.')}"
if name == "todo":
items = arguments.get("items")
n = len(items) if isinstance(items, list) else 0
return f"todo {n} items"
if name == "web_search":
return f"web_search {arguments.get('query', '')}"
return f"{name} {json.dumps(arguments, ensure_ascii=False)}"
def clip_result(text: str, *, limit: int = _MAX_RESULT) -> str:
if len(text) <= limit:
return text
return text[:limit] + f"\n... truncated {len(text) - limit} chars"
def preview(text: str, *, limit: int = 240) -> str:
one = " ".join(text.split())
if len(one) <= limit:
return one
return one[: limit - 1] + "…"
def _unified(path: str, before: str, after: str) -> str:
diff = unified_diff(
before.splitlines(keepends=True),
after.splitlines(keepends=True),
fromfile=f"a/{path}",
tofile=f"b/{path}",
)
return "".join(diff) or "(no textual diff)"
def edit_preview(name: str, arguments: dict[str, Any], workspace: Path) -> str | None:
"""Unified diff for confirm. None if there is nothing to show."""
if name not in EDIT_TOOLS:
return None
try:
if name == "str_replace":
path = str(arguments.get("path") or "")
old = arguments.get("old_string")
new = arguments.get("new_string")
if not path or not isinstance(old, str) or not isinstance(new, str):
return None
target = resolve_under(workspace, path)
if not target.is_file():
return None
before = target.read_text(encoding="utf-8")
if before.count(old) != 1:
return None
return _unified(_rel_posix(workspace, target), before, before.replace(old, new, 1))
if name == "write_file":
path = str(arguments.get("path") or "")
content = arguments.get("content")
if not path or not isinstance(content, str):
return None
target = resolve_under(workspace, path)
before = target.read_text(encoding="utf-8") if target.is_file() else ""
return _unified(path.replace("\\", "/"), before, content)
if name == "delete_file":
return f"delete {arguments.get('path', '')}"
except (OSError, PathEscape, UnicodeError):
return None
return None
def execute(
name: str,
arguments: dict[str, Any],
workspace: Path,
*,
session: AgentSession | None = None,
) -> str:
if not isinstance(arguments, dict):
return "error: arguments must be a JSON object"
try:
if name == "list_dir":
return _list_dir(workspace, str(arguments.get("path") or "."))
if name == "read_file":
path = arguments.get("path")
if not path:
return "error: path is required"
return _read_file(
workspace,
str(path),
offset=arguments.get("offset", 1),
limit=arguments.get("limit", 200),
)
if name == "glob":
pattern = arguments.get("pattern")
if not pattern:
return "error: pattern is required"
return _glob(workspace, str(pattern))
if name == "grep":
query = arguments.get("query")
if not query:
return "error: query is required"
return _grep(
workspace,
str(query),
path=str(arguments.get("path") or "."),
glob_pat=arguments.get("glob"),
)
if name == "git_status":
return _git_status(workspace)
if name == "git_diff":
return _git_diff(workspace, str(arguments.get("path") or ""))
if name == "str_replace":
path = arguments.get("path")
if not path:
return "error: path is required"
return _str_replace(
workspace,
str(path),
arguments.get("old_string"),
arguments.get("new_string"),
)
if name == "write_file":
path = arguments.get("path")
if not path:
return "error: path is required"
if "content" not in arguments:
return "error: content is required"
return _write_file(workspace, str(path), arguments.get("content"))
if name == "delete_file":
path = arguments.get("path")
if not path:
return "error: path is required"
return _delete_file(workspace, str(path))
if name == "run_tests":
path = arguments.get("path")
if not path:
return "error: path is required"
return _run_tests(workspace, str(path))
if name == "run_argv":
return _run_argv(workspace, arguments.get("argv"))
if name == "web_search":
return _web_search(arguments, session=session)
if name == "todo":
return _todo(session, arguments.get("items"))
names = ", ".join(item["name"] for item in SCHEMAS)
return f"error: unknown tool {name!r}. available: {names}"
except PathEscape as exc:
return f"error: {exc}"
except OSError as exc:
return f"error: {exc}"
def execute_calls(
calls: list[dict[str, Any]],
workspace: Path,
*,
session: AgentSession | None = None,
confirm: Any = None,
progress: Any = None,
) -> list[tuple[str, dict[str, Any], str]]:
"""Run one assistant's tool list. ``confirm(name, args) -> bool`` or None=allow."""
def _one(call: dict[str, Any]) -> tuple[str, dict[str, Any], str]:
name = str(call.get("name") or "")
arguments = call.get("arguments") if isinstance(call.get("arguments"), dict) else {}
allowed = True if confirm is None else bool(confirm(name, arguments))
if allowed and progress is not None:
progress(name, arguments)
result = (
execute(name, arguments, workspace, session=session)
if allowed
else "denied by user"
)
return name, arguments, clip_result(result)
if calls and all(str(c.get("name") or "") in READ_TOOLS for c in calls) and len(calls) > 1:
if progress is not None:
progress("_parallel", {"n": len(calls)})
return run_read_only_parallel(calls, workspace, session=session)
return [_one(call) for call in calls]
def run_read_only_parallel(
calls: list[dict[str, Any]],
workspace: Path,
*,
session: AgentSession | None = None,
) -> list[tuple[str, dict[str, Any], str]]:
"""Host-side fan-out for read tools. Order matches ``calls``."""
del session
indexed = list(enumerate(calls))
out: list[tuple[str, dict[str, Any], str] | None] = [None] * len(calls)
def _job(item: tuple[int, dict[str, Any]]) -> tuple[int, str, dict[str, Any], str]:
i, call = item
name = str(call.get("name") or "")
arguments = call.get("arguments") if isinstance(call.get("arguments"), dict) else {}
result = clip_result(execute(name, arguments, workspace))
return i, name, arguments, result
workers = min(4, len(calls))
with ThreadPoolExecutor(max_workers=workers) as pool:
futs = [pool.submit(_job, item) for item in indexed]
for fut in as_completed(futs):
i, name, arguments, result = fut.result()
out[i] = (name, arguments, result)
return [row for row in out if row is not None]
def _as_int(value: Any, default: int) -> int:
try:
return int(value)
except (TypeError, ValueError):
return default
def _list_dir(root: Path, rel: str) -> str:
target = resolve_under(root, rel)
if _in_git(root, target):
return "error: .git paths are not listable"
if not target.exists():
return f"error: {rel} does not exist"
if target.is_file():
return f"error: {rel} is a file"
try:
entries = sorted(target.iterdir(), key=lambda p: (not p.is_dir(), p.name.lower()))
except OSError as exc:
return f"error: {exc}"
rows: list[str] = []
shown = 0
for item in entries:
if item.name in _SKIP_DIR:
continue
shown += 1
if shown > _MAX_LIST:
rows.append("... truncated")
break
if item.is_dir():
rows.append(f"dir\t{item.name}")
continue
try:
size = item.stat().st_size
except OSError:
size = 0
rows.append(f"file\t{item.name}\t{size}B")
return "\n".join(rows) if rows else "(empty)"
def _read_file(root: Path, rel: str, *, offset: Any, limit: Any) -> str:
target = resolve_under(root, rel)
if _in_git(root, target):
return "error: .git paths are not readable"
if not target.is_file():
return f"error: {rel} is not a file"
try:
size = target.stat().st_size
except OSError as exc:
return f"error: {exc}"
if size > _MAX_READ_BYTES:
return f"error: file is {size} bytes; too large to read"
data = target.read_bytes()
if b"\x00" in data[:4096]:
return "error: binary file"
text = data.decode("utf-8", errors="replace")
lines = text.splitlines()
start = max(1, _as_int(offset, 1))
n = min(_MAX_READ_LINES, max(1, _as_int(limit, 200)))
chunk = lines[start - 1 : start - 1 + n]
numbered = [f"{i}: {line}" for i, line in enumerate(chunk, start=start)]
body = "\n".join(numbered) if numbered else "(empty)"
leftover = len(lines) - (start - 1 + len(chunk))
if leftover > 0:
body += f"\n... {leftover} more lines"
return body
def _glob(root: Path, pattern: str) -> str:
text = pattern.strip().replace("\\", "/")
if not text or text.startswith("/") or text.startswith("~") or ":" in text[:3]:
return "error: pattern must be a relative workspace glob"
hits: list[str] = []
try:
matches: Iterable[Path] = root.glob(text)
except ValueError as exc:
return f"error: {exc}"
for item in sorted(matches, key=lambda p: str(p).lower()):
try:
rel = _rel_posix(root, item)
resolve_under(root, rel)
except (OSError, PathEscape, ValueError):
continue
if _in_git(root, item) or any(part in _SKIP_DIR for part in Path(rel).parts):
continue
kind = "dir" if item.is_dir() else "file"
hits.append(f"{kind}\t{rel}")
if len(hits) >= _MAX_LIST:
hits.append("... truncated")
break
return "\n".join(hits) if hits else "(no matches)"
def _iter_grep_files(start: Path, root: Path, glob_pat: str | None) -> Iterable[Path]:
if start.is_file():
yield start
return
if not start.is_dir():
return
for dirpath, dirnames, filenames in os.walk(start):
dirnames[:] = [d for d in dirnames if d not in _SKIP_DIR]
base = Path(dirpath)
if _in_git(root, base):
dirnames[:] = []
continue
for name in filenames:
path = base / name
if _in_git(root, path):
continue
if glob_pat and not fnmatch.fnmatch(name, str(glob_pat)):
continue
yield path
def _grep(root: Path, query: str, *, path: str, glob_pat: Any) -> str:
try:
rx = re.compile(query)
except re.error as exc:
return f"error: bad regex: {exc}"
start = resolve_under(root, path or ".")
if _in_git(root, start):
return "error: .git paths are not searchable"
glob_text = str(glob_pat) if glob_pat else None
rows: list[str] = []
files_hit = 0
matches = 0
for file in _iter_grep_files(start, root, glob_text):