-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmini_coding_agent.py
More file actions
1017 lines (908 loc) · 37.8 KB
/
mini_coding_agent.py
File metadata and controls
1017 lines (908 loc) · 37.8 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
import argparse
import json
import os
import re
import shutil
import subprocess
import sys
import textwrap
import urllib.error
import urllib.request
import uuid
from datetime import datetime, timezone
from pathlib import Path
DOC_NAMES = ("AGENTS.md", "README.md", "pyproject.toml", "package.json")
HELP_TEXT = "/help, /memory, /session, /reset, /exit"
WELCOME_ART = (
"/\\ /\\\\",
"{ `---' }",
"{ O O }",
"~~> V <~~",
"\\\\ \\|/ /",
"`-----'__",
)
HELP_DETAILS = textwrap.dedent(
"""\
Commands:
/help Show this help message.
/memory Show the agent's distilled working memory.
/session Show the path to the saved session file.
/reset Clear the current session history and memory.
/exit Exit the agent.
"""
).strip()
MAX_TOOL_OUTPUT = 4000
MAX_HISTORY = 12000
IGNORED_PATH_NAMES = {".git", ".mini-coding-agent", "__pycache__", ".pytest_cache", ".ruff_cache", ".venv", "venv"}
##############################
#### Six Agent Components ####
##############################
# 1) Live Repo Context -> WorkspaceContext
# 2) Prompt Shape And Cache Reuse -> build_prefix, memory_text, prompt
# 3) Structured Tools, Validation, And Permissions -> build_tools, run_tool, validate_tool, approve, parse, path, tool_*
# 4) Context Reduction And Output Management -> clip, history_text
# 5) Transcripts, Memory, And Resumption -> SessionStore, record, note_tool, ask, reset
# 6) Delegation And Bounded Subagents -> tool_delegate
def now():
return datetime.now(timezone.utc).isoformat()
# Supporting helper for component 4 (context reduction and output management).
def clip(text, limit=MAX_TOOL_OUTPUT):
text = str(text)
if len(text) <= limit:
return text
return text[:limit] + f"\n...[truncated {len(text) - limit} chars]"
def middle(text, limit):
text = str(text).replace("\n", " ")
if len(text) <= limit:
return text
if limit <= 3:
return text[:limit]
left = (limit - 3) // 2
right = limit - 3 - left
return text[:left] + "..." + text[-right:]
##############################
#### 1) Live Repo Context ####
##############################
class WorkspaceContext:
def __init__(self, cwd, repo_root, branch, default_branch, status, recent_commits, project_docs):
self.cwd = cwd
self.repo_root = repo_root
self.branch = branch
self.default_branch = default_branch
self.status = status
self.recent_commits = recent_commits
self.project_docs = project_docs
@classmethod
def build(cls, cwd):
cwd = Path(cwd).resolve()
def git(args, fallback=""):
try:
result = subprocess.run(
["git", *args],
cwd=cwd,
capture_output=True,
text=True,
check=True,
timeout=5,
)
return result.stdout.strip() or fallback
except Exception:
return fallback
repo_root = Path(git(["rev-parse", "--show-toplevel"], str(cwd))).resolve()
docs = {}
for base in (repo_root, cwd):
for name in DOC_NAMES:
path = base / name
if not path.exists():
continue
key = str(path.relative_to(repo_root))
if key in docs:
continue
docs[key] = clip(path.read_text(encoding="utf-8", errors="replace"), 1200)
return cls(
cwd=str(cwd),
repo_root=str(repo_root),
branch=git(["branch", "--show-current"], "-") or "-",
default_branch=(git(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], "origin/main") or "origin/main").removeprefix("origin/"),
status=clip(git(["status", "--short"], "clean") or "clean", 1500),
recent_commits=[line for line in git(["log", "--oneline", "-5"]).splitlines() if line],
project_docs=docs,
)
def text(self):
commits = "\n".join(f"- {line}" for line in self.recent_commits) or "- none"
docs = "\n".join(f"- {path}\n{snippet}" for path, snippet in self.project_docs.items()) or "- none"
return textwrap.dedent(
f"""\
Workspace:
- cwd: {self.cwd}
- repo_root: {self.repo_root}
- branch: {self.branch}
- default_branch: {self.default_branch}
- status:
{self.status}
- recent_commits:
{commits}
- project_docs:
{docs}
"""
).strip()
##############################
#### 5) Session Memory #######
##############################
class SessionStore:
def __init__(self, root):
self.root = Path(root)
self.root.mkdir(parents=True, exist_ok=True)
def path(self, session_id):
return self.root / f"{session_id}.json"
def save(self, session):
path = self.path(session["id"])
path.write_text(json.dumps(session, indent=2), encoding="utf-8")
return path
def load(self, session_id):
return json.loads(self.path(session_id).read_text(encoding="utf-8"))
def latest(self):
files = sorted(self.root.glob("*.json"), key=lambda path: path.stat().st_mtime)
return files[-1].stem if files else None
class FakeModelClient:
def __init__(self, outputs):
self.outputs = list(outputs)
self.prompts = []
def complete(self, prompt, max_new_tokens):
self.prompts.append(prompt)
if not self.outputs:
raise RuntimeError("fake model ran out of outputs")
return self.outputs.pop(0)
class OllamaModelClient:
def __init__(self, model, host, temperature, top_p, timeout):
self.model = model
self.host = host.rstrip("/")
self.temperature = temperature
self.top_p = top_p
self.timeout = timeout
def complete(self, prompt, max_new_tokens):
payload = {
"model": self.model,
"prompt": prompt,
"stream": False,
"raw": False,
"think": False,
"options": {
"num_predict": max_new_tokens,
"temperature": self.temperature,
"top_p": self.top_p,
},
}
request = urllib.request.Request(
self.host + "/api/generate",
data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=self.timeout) as response:
data = json.loads(response.read().decode("utf-8"))
except urllib.error.HTTPError as exc:
body = exc.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Ollama request failed with HTTP {exc.code}: {body}") from exc
except urllib.error.URLError as exc:
raise RuntimeError(
"Could not reach Ollama.\n"
"Make sure `ollama serve` is running and the model is available.\n"
f"Host: {self.host}\n"
f"Model: {self.model}"
) from exc
if data.get("error"):
raise RuntimeError(f"Ollama error: {data['error']}")
return data.get("response", "")
class MiniAgent:
def __init__(
self,
model_client,
workspace,
session_store,
session=None,
approval_policy="ask",
max_steps=6,
max_new_tokens=512,
depth=0,
max_depth=1,
read_only=False,
):
self.model_client = model_client
self.workspace = workspace
self.root = Path(workspace.repo_root)
self.session_store = session_store
self.approval_policy = approval_policy
self.max_steps = max_steps
self.max_new_tokens = max_new_tokens
self.depth = depth
self.max_depth = max_depth
self.read_only = read_only
self.session = session or {
"id": datetime.now().strftime("%Y%m%d-%H%M%S") + "-" + uuid.uuid4().hex[:6],
"created_at": now(),
"workspace_root": workspace.repo_root,
"history": [],
"memory": {"task": "", "files": [], "notes": []},
}
self.tools = self.build_tools()
self.prefix = self.build_prefix()
self.session_path = self.session_store.save(self.session)
@classmethod
def from_session(cls, model_client, workspace, session_store, session_id, **kwargs):
return cls(
model_client=model_client,
workspace=workspace,
session_store=session_store,
session=session_store.load(session_id),
**kwargs,
)
@staticmethod
def remember(bucket, item, limit):
if not item:
return
if item in bucket:
bucket.remove(item)
bucket.append(item)
del bucket[:-limit]
###############################################
#### 3) Structured Tools And Permissions ######
###############################################
def build_tools(self):
tools = {
"list_files": {
"schema": {"path": "str='.'"},
"risky": False,
"description": "List files in the workspace.",
"run": self.tool_list_files,
},
"read_file": {
"schema": {"path": "str", "start": "int=1", "end": "int=200"},
"risky": False,
"description": "Read a UTF-8 file by line range.",
"run": self.tool_read_file,
},
"search": {
"schema": {"pattern": "str", "path": "str='.'"},
"risky": False,
"description": "Search the workspace with rg or a simple fallback.",
"run": self.tool_search,
},
"run_shell": {
"schema": {"command": "str", "timeout": "int=20"},
"risky": True,
"description": "Run a shell command in the repo root.",
"run": self.tool_run_shell,
},
"write_file": {
"schema": {"path": "str", "content": "str"},
"risky": True,
"description": "Write a text file.",
"run": self.tool_write_file,
},
"patch_file": {
"schema": {"path": "str", "old_text": "str", "new_text": "str"},
"risky": True,
"description": "Replace one exact text block in a file.",
"run": self.tool_patch_file,
},
}
if self.depth < self.max_depth:
tools["delegate"] = {
"schema": {"task": "str", "max_steps": "int=3"},
"risky": False,
"description": "Ask a bounded read-only child agent to investigate.",
"run": self.tool_delegate,
}
return tools
############################################
#### 2) Prompt Shape And Cache Reuse #######
############################################
def build_prefix(self):
tool_lines = []
for name, tool in self.tools.items():
fields = ", ".join(f"{key}: {value}" for key, value in tool["schema"].items())
risk = "approval required" if tool["risky"] else "safe"
tool_lines.append(f"- {name}({fields}) [{risk}] {tool['description']}")
tool_text = "\n".join(tool_lines)
examples = "\n".join(
[
'<tool>{"name":"list_files","args":{"path":"."}}</tool>',
'<tool>{"name":"read_file","args":{"path":"README.md","start":1,"end":80}}</tool>',
'<tool name="write_file" path="binary_search.py"><content>def binary_search(nums, target):\n return -1\n</content></tool>',
'<tool name="patch_file" path="binary_search.py"><old_text>return -1</old_text><new_text>return mid</new_text></tool>',
'<tool>{"name":"run_shell","args":{"command":"uv run --with pytest python -m pytest -q","timeout":20}}</tool>',
"<final>Done.</final>",
]
)
return textwrap.dedent(
f"""\
You are Mini-Coding-Agent, a small local coding agent running through Ollama.
Rules:
- Use tools instead of guessing about the workspace.
- Return exactly one <tool>...</tool> or one <final>...</final>.
- Tool calls must look like:
<tool>{{"name":"tool_name","args":{{...}}}}</tool>
- For write_file and patch_file with multi-line text, prefer XML style:
<tool name="write_file" path="file.py"><content>...</content></tool>
- Final answers must look like:
<final>your answer</final>
- Never invent tool results.
- Keep answers concise and concrete.
- If the user asks you to create or update a specific file and the path is clear, use write_file or patch_file instead of repeatedly listing files.
- Before writing tests for existing code, read the implementation first.
- When writing tests, match the current implementation unless the user explicitly asked you to change the code.
- New files should be complete and runnable, including obvious imports.
- Do not repeat the same tool call with the same arguments if it did not help. Choose a different tool or return a final answer.
- Required tool arguments must not be empty. Do not call read_file, write_file, patch_file, run_shell, or delegate with args={{}}.
Tools:
{tool_text}
Valid response examples:
{examples}
{self.workspace.text()}
"""
).strip()
def memory_text(self):
memory = self.session["memory"]
return textwrap.dedent(
f"""\
Memory:
- task: {memory['task'] or "-"}
- files: {", ".join(memory["files"]) or "-"}
- notes:
{chr(10).join(f"- {note}" for note in memory["notes"]) or "- none"}
"""
).strip()
#####################################################
#### 4) Context Reduction And Output Management #####
#####################################################
def history_text(self):
history = self.session["history"]
if not history:
return "- empty"
lines = []
seen_reads = set()
recent_start = max(0, len(history) - 6)
for index, item in enumerate(history):
recent = index >= recent_start
if item["role"] == "tool" and item["name"] == "read_file" and not recent:
path = str(item["args"].get("path", ""))
if path in seen_reads:
continue
seen_reads.add(path)
if item["role"] == "tool":
limit = 900 if recent else 180
lines.append(f"[tool:{item['name']}] {json.dumps(item['args'], sort_keys=True)}")
lines.append(clip(item["content"], limit))
else:
limit = 900 if recent else 220
lines.append(f"[{item['role']}] {clip(item['content'], limit)}")
return clip("\n".join(lines), MAX_HISTORY)
########################################################
#### 2) Prompt Shape And Cache Reuse (Continued) #######
########################################################
def prompt(self, user_message):
return textwrap.dedent(
f"""\
{self.prefix}
{self.memory_text()}
Transcript:
{self.history_text()}
Current user request:
{user_message}
"""
).strip()
###############################################
#### 5) Session Memory (Continued) ###########
###############################################
def record(self, item):
self.session["history"].append(item)
self.session_path = self.session_store.save(self.session)
def note_tool(self, name, args, result):
memory = self.session["memory"]
path = args.get("path")
if name in {"read_file", "write_file", "patch_file"} and path:
self.remember(memory["files"], str(path), 8)
note = f"{name}: {clip(str(result).replace(chr(10), ' '), 220)}"
self.remember(memory["notes"], note, 5)
def ask(self, user_message):
memory = self.session["memory"]
if not memory["task"]:
memory["task"] = clip(user_message.strip(), 300)
self.record({"role": "user", "content": user_message, "created_at": now()})
tool_steps = 0
attempts = 0
max_attempts = max(self.max_steps * 3, self.max_steps + 4)
while tool_steps < self.max_steps and attempts < max_attempts:
attempts += 1
raw = self.model_client.complete(self.prompt(user_message), self.max_new_tokens)
kind, payload = self.parse(raw)
if kind == "tool":
tool_steps += 1
name = payload.get("name", "")
args = payload.get("args", {})
result = self.run_tool(name, args)
self.record(
{
"role": "tool",
"name": name,
"args": args,
"content": result,
"created_at": now(),
}
)
self.note_tool(name, args, result)
continue
if kind == "retry":
self.record({"role": "assistant", "content": payload, "created_at": now()})
continue
final = (payload or raw).strip()
self.record({"role": "assistant", "content": final, "created_at": now()})
self.remember(memory["notes"], clip(final, 220), 5)
return final
if attempts >= max_attempts and tool_steps < self.max_steps:
final = "Stopped after too many malformed model responses without a valid tool call or final answer."
else:
final = "Stopped after reaching the step limit without a final answer."
self.record({"role": "assistant", "content": final, "created_at": now()})
return final
#############################################################
#### 3) Structured Tools, Validation, And Permissions #######
#############################################################
def run_tool(self, name, args):
tool = self.tools.get(name)
if tool is None:
return f"error: unknown tool '{name}'"
try:
self.validate_tool(name, args)
except Exception as exc:
example = self.tool_example(name)
message = f"error: invalid arguments for {name}: {exc}"
if example:
message += f"\nexample: {example}"
return message
if self.repeated_tool_call(name, args):
return f"error: repeated identical tool call for {name}; choose a different tool or return a final answer"
if tool["risky"] and not self.approve(name, args):
return f"error: approval denied for {name}"
try:
return clip(tool["run"](args))
except Exception as exc:
return f"error: tool {name} failed: {exc}"
def repeated_tool_call(self, name, args):
tool_events = [item for item in self.session["history"] if item["role"] == "tool"]
if len(tool_events) < 2:
return False
recent = tool_events[-2:]
return all(item["name"] == name and item["args"] == args for item in recent)
def tool_example(self, name):
examples = {
"list_files": '<tool>{"name":"list_files","args":{"path":"."}}</tool>',
"read_file": '<tool>{"name":"read_file","args":{"path":"README.md","start":1,"end":80}}</tool>',
"search": '<tool>{"name":"search","args":{"pattern":"binary_search","path":"."}}</tool>',
"run_shell": '<tool>{"name":"run_shell","args":{"command":"uv run --with pytest python -m pytest -q","timeout":20}}</tool>',
"write_file": '<tool name="write_file" path="binary_search.py"><content>def binary_search(nums, target):\n return -1\n</content></tool>',
"patch_file": '<tool name="patch_file" path="binary_search.py"><old_text>return -1</old_text><new_text>return mid</new_text></tool>',
"delegate": '<tool>{"name":"delegate","args":{"task":"inspect README.md","max_steps":3}}</tool>',
}
return examples.get(name, "")
def validate_tool(self, name, args):
args = args or {}
if name == "list_files":
path = self.path(args.get("path", "."))
if not path.is_dir():
raise ValueError("path is not a directory")
return
if name == "read_file":
path = self.path(args["path"])
if not path.is_file():
raise ValueError("path is not a file")
start = int(args.get("start", 1))
end = int(args.get("end", 200))
if start < 1 or end < start:
raise ValueError("invalid line range")
return
if name == "search":
pattern = str(args.get("pattern", "")).strip()
if not pattern:
raise ValueError("pattern must not be empty")
self.path(args.get("path", "."))
return
if name == "run_shell":
command = str(args.get("command", "")).strip()
if not command:
raise ValueError("command must not be empty")
timeout = int(args.get("timeout", 20))
if timeout < 1 or timeout > 120:
raise ValueError("timeout must be in [1, 120]")
return
if name == "write_file":
path = self.path(args["path"])
if path.exists() and path.is_dir():
raise ValueError("path is a directory")
if "content" not in args:
raise ValueError("missing content")
return
if name == "patch_file":
path = self.path(args["path"])
if not path.is_file():
raise ValueError("path is not a file")
old_text = str(args.get("old_text", ""))
if not old_text:
raise ValueError("old_text must not be empty")
if "new_text" not in args:
raise ValueError("missing new_text")
text = path.read_text(encoding="utf-8")
count = text.count(old_text)
if count != 1:
raise ValueError(f"old_text must occur exactly once, found {count}")
return
if name == "delegate":
if self.depth >= self.max_depth:
raise ValueError("delegate depth exceeded")
task = str(args.get("task", "")).strip()
if not task:
raise ValueError("task must not be empty")
return
def approve(self, name, args):
if self.read_only:
return False
if self.approval_policy == "auto":
return True
if self.approval_policy == "never":
return False
try:
answer = input(f"approve {name} {json.dumps(args, ensure_ascii=True)}? [y/N] ")
except EOFError:
return False
return answer.strip().lower() in {"y", "yes"}
@staticmethod
def parse(raw):
raw = str(raw)
if "<tool>" in raw and ("<final>" not in raw or raw.find("<tool>") < raw.find("<final>")):
body = MiniAgent.extract(raw, "tool")
try:
payload = json.loads(body)
except Exception:
return "retry", MiniAgent.retry_notice("model returned malformed tool JSON")
if not isinstance(payload, dict):
return "retry", MiniAgent.retry_notice("tool payload must be a JSON object")
if not str(payload.get("name", "")).strip():
return "retry", MiniAgent.retry_notice("tool payload is missing a tool name")
args = payload.get("args", {})
if args is None:
payload["args"] = {}
elif not isinstance(args, dict):
return "retry", MiniAgent.retry_notice()
return "tool", payload
if "<tool" in raw and ("<final>" not in raw or raw.find("<tool") < raw.find("<final>")):
payload = MiniAgent.parse_xml_tool(raw)
if payload is not None:
return "tool", payload
return "retry", MiniAgent.retry_notice()
if "<final>" in raw:
final = MiniAgent.extract(raw, "final").strip()
if final:
return "final", final
return "retry", MiniAgent.retry_notice("model returned an empty <final> answer")
raw = raw.strip()
if raw:
return "final", raw
return "retry", MiniAgent.retry_notice("model returned an empty response")
@staticmethod
def retry_notice(problem=None):
prefix = "Runtime notice"
if problem:
prefix += f": {problem}"
else:
prefix += ": model returned malformed tool output"
return (
f"{prefix}. Reply with a valid <tool> call or a non-empty <final> answer. "
'For multi-line files, prefer <tool name="write_file" path="file.py"><content>...</content></tool>.'
)
@staticmethod
def parse_xml_tool(raw):
match = re.search(r"<tool(?P<attrs>[^>]*)>(?P<body>.*?)</tool>", raw, re.S)
if not match:
return None
attrs = MiniAgent.parse_attrs(match.group("attrs"))
name = str(attrs.pop("name", "")).strip()
if not name:
return None
body = match.group("body")
args = dict(attrs)
for key in ("content", "old_text", "new_text", "command", "task", "pattern", "path"):
if f"<{key}>" in body:
args[key] = MiniAgent.extract_raw(body, key)
body_text = body.strip("\n")
if name == "write_file" and "content" not in args and body_text:
args["content"] = body_text
if name == "delegate" and "task" not in args and body_text:
args["task"] = body_text.strip()
return {"name": name, "args": args}
@staticmethod
def parse_attrs(text):
attrs = {}
for match in re.finditer(r"""([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(?:"([^"]*)"|'([^']*)')""", text):
attrs[match.group(1)] = match.group(2) if match.group(2) is not None else match.group(3)
return attrs
@staticmethod
def extract(text, tag):
start_tag = f"<{tag}>"
end_tag = f"</{tag}>"
start = text.find(start_tag)
if start == -1:
return text
start += len(start_tag)
end = text.find(end_tag, start)
if end == -1:
return text[start:].strip()
return text[start:end].strip()
@staticmethod
def extract_raw(text, tag):
start_tag = f"<{tag}>"
end_tag = f"</{tag}>"
start = text.find(start_tag)
if start == -1:
return text
start += len(start_tag)
end = text.find(end_tag, start)
if end == -1:
return text[start:]
return text[start:end]
def reset(self):
self.session["history"] = []
self.session["memory"] = {"task": "", "files": [], "notes": []}
self.session_store.save(self.session)
def path(self, raw_path):
path = Path(raw_path)
path = path if path.is_absolute() else self.root / path
resolved = path.resolve()
if os.path.commonpath([str(self.root), str(resolved)]) != str(self.root):
raise ValueError(f"path escapes workspace: {raw_path}")
return resolved
def tool_list_files(self, args):
path = self.path(args.get("path", "."))
if not path.is_dir():
raise ValueError("path is not a directory")
entries = [
item for item in sorted(path.iterdir(), key=lambda item: (item.is_file(), item.name.lower()))
if item.name not in IGNORED_PATH_NAMES
]
lines = []
for entry in entries[:200]:
kind = "[D]" if entry.is_dir() else "[F]"
lines.append(f"{kind} {entry.relative_to(self.root)}")
return "\n".join(lines) or "(empty)"
def tool_read_file(self, args):
path = self.path(args["path"])
if not path.is_file():
raise ValueError("path is not a file")
start = int(args.get("start", 1))
end = int(args.get("end", 200))
if start < 1 or end < start:
raise ValueError("invalid line range")
lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
body = "\n".join(f"{number:>4}: {line}" for number, line in enumerate(lines[start - 1:end], start=start))
return f"# {path.relative_to(self.root)}\n{body}"
def tool_search(self, args):
pattern = str(args.get("pattern", "")).strip()
if not pattern:
raise ValueError("pattern must not be empty")
path = self.path(args.get("path", "."))
if shutil.which("rg"):
result = subprocess.run(
["rg", "-n", "--smart-case", "--max-count", "200", pattern, str(path)],
cwd=self.root,
capture_output=True,
text=True,
)
return result.stdout.strip() or result.stderr.strip() or "(no matches)"
matches = []
files = [path] if path.is_file() else [
item for item in path.rglob("*")
if item.is_file() and not any(part in IGNORED_PATH_NAMES for part in item.relative_to(self.root).parts)
]
for file_path in files:
for number, line in enumerate(file_path.read_text(encoding="utf-8", errors="replace").splitlines(), start=1):
if pattern.lower() in line.lower():
matches.append(f"{file_path.relative_to(self.root)}:{number}:{line}")
if len(matches) >= 200:
return "\n".join(matches)
return "\n".join(matches) or "(no matches)"
def tool_run_shell(self, args):
command = str(args.get("command", "")).strip()
if not command:
raise ValueError("command must not be empty")
timeout = int(args.get("timeout", 20))
if timeout < 1 or timeout > 120:
raise ValueError("timeout must be in [1, 120]")
result = subprocess.run(
command,
cwd=self.root,
shell=True,
capture_output=True,
text=True,
timeout=timeout,
)
return textwrap.dedent(
f"""\
exit_code: {result.returncode}
stdout:
{result.stdout.strip() or "(empty)"}
stderr:
{result.stderr.strip() or "(empty)"}
"""
).strip()
def tool_write_file(self, args):
path = self.path(args["path"])
content = str(args["content"])
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
return f"wrote {path.relative_to(self.root)} ({len(content)} chars)"
def tool_patch_file(self, args):
path = self.path(args["path"])
if not path.is_file():
raise ValueError("path is not a file")
old_text = str(args.get("old_text", ""))
if not old_text:
raise ValueError("old_text must not be empty")
if "new_text" not in args:
raise ValueError("missing new_text")
text = path.read_text(encoding="utf-8")
count = text.count(old_text)
if count != 1:
raise ValueError(f"old_text must occur exactly once, found {count}")
path.write_text(text.replace(old_text, str(args["new_text"]), 1), encoding="utf-8")
return f"patched {path.relative_to(self.root)}"
###################################################
#### 6) Delegation And Bounded Subagents ##########
###################################################
def tool_delegate(self, args):
if self.depth >= self.max_depth:
raise ValueError("delegate depth exceeded")
task = str(args.get("task", "")).strip()
if not task:
raise ValueError("task must not be empty")
child = MiniAgent(
model_client=self.model_client,
workspace=self.workspace,
session_store=self.session_store,
approval_policy="never",
max_steps=int(args.get("max_steps", 3)),
max_new_tokens=self.max_new_tokens,
depth=self.depth + 1,
max_depth=self.max_depth,
read_only=True,
)
child.session["memory"]["task"] = task
child.session["memory"]["notes"] = [clip(self.history_text(), 300)]
return "delegate_result:\n" + child.ask(task)
def build_welcome(agent, model, host):
width = max(68, min(shutil.get_terminal_size((80, 20)).columns, 84))
inner = width - 4
gap = 3
left_width = (inner - gap) // 2
right_width = inner - gap - left_width
def row(text):
body = middle(text, width - 4)
return f"| {body.ljust(width - 4)} |"
def divider(char="-"):
return "+" + char * (width - 2) + "+"
def center(text):
body = middle(text, inner)
return f"| {body.center(inner)} |"
def cell(label, value, size):
body = middle(f"{label:<9} {value}", size)
return body.ljust(size)
def pair(left_label, left_value, right_label, right_value):
left = cell(left_label, left_value, left_width)
right = cell(right_label, right_value, right_width)
return f"| {left}{' ' * gap}{right} |"
line = divider("=")
rows = [center(text) for text in WELCOME_ART]
rows.extend(
[
center("MINI CODING AGENT"),
divider("-"),
row(""),
row("WORKSPACE " + middle(agent.workspace.cwd, inner - 11)),
pair("MODEL", model, "BRANCH", agent.workspace.branch),
pair("APPROVAL", agent.approval_policy, "SESSION", agent.session["id"]),
row(""),
]
)
return "\n".join([line, *rows, line])
def build_agent(args):
workspace = WorkspaceContext.build(args.cwd)
store = SessionStore(Path(workspace.repo_root) / ".mini-coding-agent" / "sessions")
model = OllamaModelClient(
model=args.model,
host=args.host,
temperature=args.temperature,
top_p=args.top_p,
timeout=args.ollama_timeout,
)
session_id = args.resume
if session_id == "latest":
session_id = store.latest()
if session_id:
return MiniAgent.from_session(
model_client=model,
workspace=workspace,
session_store=store,
session_id=session_id,
approval_policy=args.approval,
max_steps=args.max_steps,
max_new_tokens=args.max_new_tokens,
)
return MiniAgent(
model_client=model,
workspace=workspace,
session_store=store,
approval_policy=args.approval,
max_steps=args.max_steps,
max_new_tokens=args.max_new_tokens,
)
def build_arg_parser():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Minimal coding agent for Ollama models.",
)
parser.add_argument("prompt", nargs="*", help="Optional one-shot prompt.")
parser.add_argument("--cwd", default=".", help="Workspace directory.")
parser.add_argument("--model", default="qwen3.5:4b", help="Ollama model name.")
parser.add_argument("--host", default="http://127.0.0.1:11434", help="Ollama server URL.")
parser.add_argument("--ollama-timeout", type=int, default=300, help="Ollama request timeout in seconds.")
parser.add_argument("--resume", default=None, help="Session id to resume or 'latest'.")
parser.add_argument("--approval", choices=("ask", "auto", "never"), default="ask", help="Approval policy for risky tools.")
parser.add_argument("--max-steps", type=int, default=6, help="Maximum tool/model iterations per request.")
parser.add_argument("--max-new-tokens", type=int, default=512, help="Maximum model output tokens per step.")
parser.add_argument("--temperature", type=float, default=0.2, help="Sampling temperature sent to Ollama.")
parser.add_argument("--top-p", type=float, default=0.9, help="Top-p sampling value sent to Ollama.")
return parser
def main(argv=None):
args = build_arg_parser().parse_args(argv)
agent = build_agent(args)
print(build_welcome(agent, model=args.model, host=args.host))
if args.prompt:
prompt = " ".join(args.prompt).strip()
if prompt:
print()
try:
print(agent.ask(prompt))
except RuntimeError as exc:
print(str(exc), file=sys.stderr)
return 1
return 0
while True:
try:
user_input = input("\nmini-coding-agent> ").strip()
except (EOFError, KeyboardInterrupt):
print("")
return 0
if not user_input:
continue
if user_input in {"/exit", "/quit"}:
return 0
if user_input == "/help":
print(HELP_DETAILS)
continue
if user_input == "/memory":
print(agent.memory_text())
continue