rigcode 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rigcode/__init__.py +4 -0
- rigcode/__main__.py +4 -0
- rigcode/agent/__init__.py +0 -0
- rigcode/agent/loop.py +627 -0
- rigcode/agent/mentions.py +70 -0
- rigcode/agent/prompts.py +195 -0
- rigcode/agent/runtime.py +535 -0
- rigcode/agent/session.py +165 -0
- rigcode/agent/state.py +161 -0
- rigcode/cli.py +283 -0
- rigcode/config.py +284 -0
- rigcode/doctor.py +158 -0
- rigcode/extensions.py +275 -0
- rigcode/hooks.py +142 -0
- rigcode/llm/__init__.py +0 -0
- rigcode/llm/client.py +259 -0
- rigcode/llm/probe.py +229 -0
- rigcode/llm/stream.py +240 -0
- rigcode/llm/textmode.py +161 -0
- rigcode/llm/types.py +92 -0
- rigcode/llm/wire.py +76 -0
- rigcode/mcp.py +329 -0
- rigcode/permissions.py +221 -0
- rigcode/prompts/agents/coder.md +13 -0
- rigcode/prompts/agents/debugger.md +14 -0
- rigcode/prompts/agents/explore.md +22 -0
- rigcode/prompts/agents/plan.md +21 -0
- rigcode/prompts/agents/researcher.md +14 -0
- rigcode/prompts/agents/reviewer.md +18 -0
- rigcode/prompts/agents/verifier.md +15 -0
- rigcode/prompts/commit.md +5 -0
- rigcode/prompts/compact.md +24 -0
- rigcode/prompts/init.md +10 -0
- rigcode/prompts/system.md +27 -0
- rigcode/tools/__init__.py +0 -0
- rigcode/tools/base.py +245 -0
- rigcode/tools/bashsafety.py +262 -0
- rigcode/tools/editmatch.py +367 -0
- rigcode/tools/files.py +534 -0
- rigcode/tools/interact.py +69 -0
- rigcode/tools/search.py +309 -0
- rigcode/tools/shell.py +424 -0
- rigcode/tools/skill.py +37 -0
- rigcode/tools/task.py +54 -0
- rigcode/tools/todo.py +62 -0
- rigcode/tools/web.py +346 -0
- rigcode/ui/__init__.py +0 -0
- rigcode/ui/base.py +69 -0
- rigcode/ui/commands.py +581 -0
- rigcode/ui/plain.py +321 -0
- rigcode/ui/render.py +207 -0
- rigcode/ui/terminal.py +1154 -0
- rigcode/utils/__init__.py +0 -0
- rigcode/utils/files.py +176 -0
- rigcode/utils/frontmatter.py +38 -0
- rigcode/utils/globmatch.py +91 -0
- rigcode/utils/jsonrepair.py +110 -0
- rigcode/utils/paths.py +83 -0
- rigcode/utils/text.py +105 -0
- rigcode-0.1.0.dist-info/METADATA +203 -0
- rigcode-0.1.0.dist-info/RECORD +64 -0
- rigcode-0.1.0.dist-info/WHEEL +4 -0
- rigcode-0.1.0.dist-info/entry_points.txt +2 -0
- rigcode-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""@path mentions in a user message: attach the file (or directory listing) to the
|
|
2
|
+
message up front, which saves the model a slow tool round trip."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
from rigcode.utils.files import is_binary_file, read_text_file
|
|
8
|
+
from rigcode.utils.paths import display_path, resolve_path
|
|
9
|
+
from rigcode.utils.text import number_lines
|
|
10
|
+
|
|
11
|
+
MENTION_RE = re.compile(r"(?<![\w/\\@])@(\"[^\"\n]+\"|[^\s\"'`<>|]+)")
|
|
12
|
+
MAX_FILES = 8
|
|
13
|
+
MAX_DIR_ENTRIES = 200
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def expand_mentions(text: str, rt) -> tuple[str, list[str]]:
|
|
17
|
+
"""Returns (message with attachments appended, notes for the UI)."""
|
|
18
|
+
budget = int(rt.settings.get("context.read_max_chars", 30000))
|
|
19
|
+
attachments: list[str] = []
|
|
20
|
+
notes: list[str] = []
|
|
21
|
+
seen: set[str] = set()
|
|
22
|
+
for m in MENTION_RE.finditer(text):
|
|
23
|
+
raw = m.group(1).strip('"').rstrip(".,;:!?)]}")
|
|
24
|
+
if not raw:
|
|
25
|
+
continue
|
|
26
|
+
path = resolve_path(raw, rt.cwd)
|
|
27
|
+
key = str(path).lower()
|
|
28
|
+
if key in seen or not path.exists():
|
|
29
|
+
continue
|
|
30
|
+
seen.add(key)
|
|
31
|
+
if len(attachments) >= MAX_FILES:
|
|
32
|
+
notes.append(f"Skipped @{raw}: at most {MAX_FILES} attachments per message")
|
|
33
|
+
continue
|
|
34
|
+
shown = display_path(path, rt.cwd)
|
|
35
|
+
if path.is_dir():
|
|
36
|
+
try:
|
|
37
|
+
entries = sorted(path.iterdir(), key=lambda p: (not p.is_dir(), p.name.lower()))
|
|
38
|
+
except OSError as e:
|
|
39
|
+
notes.append(f"Could not list @{raw}: {e}")
|
|
40
|
+
continue
|
|
41
|
+
listing = [e.name + ("/" if e.is_dir() else "") for e in entries[:MAX_DIR_ENTRIES]]
|
|
42
|
+
more = f"\n... {len(entries) - MAX_DIR_ENTRIES} more" if len(entries) > MAX_DIR_ENTRIES else ""
|
|
43
|
+
attachments.append(f'<directory path="{shown}">\n' + "\n".join(listing) + more + "\n</directory>")
|
|
44
|
+
notes.append(f"Listed {shown}/ ({len(entries)} entries)")
|
|
45
|
+
continue
|
|
46
|
+
if is_binary_file(path):
|
|
47
|
+
notes.append(f"Not attaching @{raw}: binary file")
|
|
48
|
+
continue
|
|
49
|
+
try:
|
|
50
|
+
tf = read_text_file(path)
|
|
51
|
+
except OSError as e:
|
|
52
|
+
notes.append(f"Could not read @{raw}: {e}")
|
|
53
|
+
continue
|
|
54
|
+
lines = tf.text.split("\n")
|
|
55
|
+
if lines and lines[-1] == "":
|
|
56
|
+
lines.pop()
|
|
57
|
+
body = number_lines(lines)
|
|
58
|
+
truncated = ""
|
|
59
|
+
if len(body) > budget:
|
|
60
|
+
body = body[:budget].rsplit("\n", 1)[0]
|
|
61
|
+
shown_lines = body.count("\n") + 1
|
|
62
|
+
truncated = f"\n[Truncated: showing lines 1-{shown_lines} of {len(lines)}. Use Read with offset for more.]"
|
|
63
|
+
else:
|
|
64
|
+
rt.file_state.record(path) # the model has the full current content: allow direct edits
|
|
65
|
+
attachments.append(f'<file path="{shown}">\n{body}{truncated}\n</file>')
|
|
66
|
+
notes.append(f"Attached {shown} ({len(lines)} lines)")
|
|
67
|
+
budget = max(2000, budget - len(body))
|
|
68
|
+
if not attachments:
|
|
69
|
+
return text, notes
|
|
70
|
+
return text + "\n\nAttached by the user:\n" + "\n\n".join(attachments), notes
|
rigcode/agent/prompts.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Builds the prompt pieces.
|
|
2
|
+
|
|
3
|
+
Layout chosen for prompt-cache reuse on the server:
|
|
4
|
+
[tools][system: CORE + PROJECT CONTEXT] <- fixed for the whole session, shared by every agent
|
|
5
|
+
[user: <env snapshot reminder> + first message] ... <- append-only from here on
|
|
6
|
+
|
|
7
|
+
llama.cpp can only rewind a hybrid model's cache to a checkpoint, and it creates
|
|
8
|
+
them at the start of user messages. The first one is never evicted, so everything
|
|
9
|
+
before the first user message (tools, system prompt, project context) is reusable
|
|
10
|
+
for /clear, compaction and subagents. That is why the stable project context lives
|
|
11
|
+
in the system message, while volatile facts (date, git status) go in the first user
|
|
12
|
+
message: they would break the shared prefix if they lived in the system message.
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import platform
|
|
17
|
+
import subprocess
|
|
18
|
+
import time
|
|
19
|
+
from collections import Counter
|
|
20
|
+
from importlib import resources
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
from rigcode.utils.files import list_project_files
|
|
24
|
+
from rigcode.utils.paths import find_git_root
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def load_prompt(name: str) -> str:
|
|
28
|
+
return (resources.files("rigcode") / "prompts" / f"{name}.md").read_text(encoding="utf-8")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _git(root: Path, *args: str, timeout: float = 5) -> str | None:
|
|
32
|
+
try:
|
|
33
|
+
proc = subprocess.run(["git", "-C", str(root), *args], capture_output=True, timeout=timeout)
|
|
34
|
+
except (OSError, subprocess.SubprocessError):
|
|
35
|
+
return None
|
|
36
|
+
if proc.returncode != 0:
|
|
37
|
+
return None
|
|
38
|
+
return proc.stdout.decode("utf-8", errors="replace").strip()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def git_snapshot(root: Path) -> dict | None:
|
|
42
|
+
git_root = find_git_root(root)
|
|
43
|
+
if not git_root:
|
|
44
|
+
return None
|
|
45
|
+
branch = _git(root, "rev-parse", "--abbrev-ref", "HEAD") or "?"
|
|
46
|
+
status = _git(root, "status", "--porcelain=v1") or ""
|
|
47
|
+
lines = [l for l in status.splitlines() if l.strip()]
|
|
48
|
+
log = _git(root, "log", "--oneline", "-5", "--no-decorate") or ""
|
|
49
|
+
return {"root": str(git_root), "branch": branch, "changes": lines, "log": log}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _about(n: int) -> str:
|
|
53
|
+
"""Rounded file count, so adding a file rarely changes the prompt (and its cache)."""
|
|
54
|
+
if n < 10:
|
|
55
|
+
return str(n)
|
|
56
|
+
digits = len(str(n)) - 1
|
|
57
|
+
return f"~{round(n, -digits):,}"
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def repo_overview(root: Path, max_lines: int = 80) -> str:
|
|
61
|
+
try:
|
|
62
|
+
files = list_project_files(root, max_files=30_000)
|
|
63
|
+
except Exception: # noqa: BLE001
|
|
64
|
+
return ""
|
|
65
|
+
if not files:
|
|
66
|
+
return "(empty directory)"
|
|
67
|
+
top_dirs: Counter = Counter()
|
|
68
|
+
sub_dirs: dict[str, Counter] = {}
|
|
69
|
+
top_files = []
|
|
70
|
+
for f in files:
|
|
71
|
+
parts = f.split("/")
|
|
72
|
+
if len(parts) == 1:
|
|
73
|
+
top_files.append(f)
|
|
74
|
+
continue
|
|
75
|
+
top_dirs[parts[0]] += 1
|
|
76
|
+
if len(parts) > 2:
|
|
77
|
+
sub_dirs.setdefault(parts[0], Counter())[parts[1]] += 1
|
|
78
|
+
lines: list[str] = []
|
|
79
|
+
for d in sorted(top_dirs):
|
|
80
|
+
lines.append(f"{d}/ ({_about(top_dirs[d])} files)")
|
|
81
|
+
subs = sub_dirs.get(d)
|
|
82
|
+
if subs and len(subs) <= 12:
|
|
83
|
+
lines.append(" " + ", ".join(f"{s}/" for s in sorted(subs)))
|
|
84
|
+
elif subs:
|
|
85
|
+
shown = ", ".join(f"{s}/" for s in sorted(s for s, _ in subs.most_common(8)))
|
|
86
|
+
lines.append(f" {len(subs)} subdirectories, largest: {shown}")
|
|
87
|
+
files_line = sorted(top_files)
|
|
88
|
+
if len(files_line) > 30:
|
|
89
|
+
files_line = files_line[:30] + ["... more files"]
|
|
90
|
+
lines.extend(files_line)
|
|
91
|
+
if len(lines) > max_lines:
|
|
92
|
+
lines = lines[:max_lines] + ["... (truncated)"]
|
|
93
|
+
return f"(about {_about(len(files))} files, .gitignore respected)\n" + "\n".join(lines)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def build_project_context(rt) -> str:
|
|
97
|
+
"""Stable facts about the project. Sent in the system message, so keep anything
|
|
98
|
+
that changes during a session (date, git status) out of here."""
|
|
99
|
+
s = rt.settings
|
|
100
|
+
parts: list[str] = []
|
|
101
|
+
os_name = f"{platform.system()} {platform.release()}"
|
|
102
|
+
env = [
|
|
103
|
+
f"- Working directory: {rt.cwd}",
|
|
104
|
+
f"- Project root: {rt.project_root}",
|
|
105
|
+
f"- Platform: {os_name}; Bash tool shell: {rt.shell.name}",
|
|
106
|
+
f"- Model: {rt.client.model} ({rt.server.kind} server)",
|
|
107
|
+
"- The current date and git status are in a reminder in the first user message.",
|
|
108
|
+
]
|
|
109
|
+
parts.append("# Environment\n" + "\n".join(env))
|
|
110
|
+
if s.get("context.repo_map", True):
|
|
111
|
+
overview = repo_overview(rt.project_root, int(s.get("context.repo_map_max_lines", 80)))
|
|
112
|
+
if overview:
|
|
113
|
+
parts.append("# Project layout\n" + overview)
|
|
114
|
+
if rt.memory:
|
|
115
|
+
mem = ["# Project instructions",
|
|
116
|
+
"Instructions from the user's memory files. Follow them; they override default behavior."]
|
|
117
|
+
for m in rt.memory:
|
|
118
|
+
mem.append(f"\n## {m.path}\n{m.content}")
|
|
119
|
+
parts.append("\n".join(mem))
|
|
120
|
+
if rt.skills:
|
|
121
|
+
lines = ["# Skills", "Specialized instructions you can load with the Skill tool when a task matches:"]
|
|
122
|
+
for sk in sorted(rt.skills.values(), key=lambda x: x.name):
|
|
123
|
+
lines.append(f"- {sk.name}: {sk.description}")
|
|
124
|
+
parts.append("\n".join(lines))
|
|
125
|
+
return "\n\n".join(parts)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def build_env_snapshot(rt, resumed: bool = False) -> str:
|
|
129
|
+
"""Volatile facts for the first user message of a conversation."""
|
|
130
|
+
head = "This session was resumed. Current environment:" if resumed else "Environment at the start of this conversation:"
|
|
131
|
+
lines = [head, f"- Date: {time.strftime('%Y-%m-%d %H:%M')} ({time.strftime('%A')})"]
|
|
132
|
+
git = git_snapshot(rt.project_root)
|
|
133
|
+
if not git:
|
|
134
|
+
lines.append("- Git: not a git repository")
|
|
135
|
+
return "\n".join(lines)
|
|
136
|
+
n = len(git["changes"])
|
|
137
|
+
lines.append(f"- Git branch: {git['branch']}; {n} uncommitted change{'s' if n != 1 else ''}")
|
|
138
|
+
if git["changes"]:
|
|
139
|
+
shown = git["changes"][:15]
|
|
140
|
+
if n > 15:
|
|
141
|
+
shown.append(f"... {n - 15} more")
|
|
142
|
+
lines.append("\nUncommitted changes (git status --porcelain):\n" + "\n".join(shown))
|
|
143
|
+
if git["log"]:
|
|
144
|
+
lines.append("\nRecent commits:\n" + git["log"])
|
|
145
|
+
return "\n".join(lines)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def subagent_brief(agent_def, description: str, prompt: str, *, forked: bool = False, env: str | None = None) -> str:
|
|
149
|
+
"""The first message a subagent sees. A forked subagent gets it as the result of the
|
|
150
|
+
Task call, right after the main agent's conversation; a fresh one as its first user
|
|
151
|
+
message, after the shared system prompt."""
|
|
152
|
+
tools_note = ", ".join(agent_def.tools) if agent_def.tools else "all tools except Task"
|
|
153
|
+
mode = (" You are read-only: do not modify files, and use Bash only for read-only commands."
|
|
154
|
+
if agent_def.read_only else "")
|
|
155
|
+
if forked:
|
|
156
|
+
intro = (f"You are now running as the \"{agent_def.name}\" subagent, forked from the main agent's "
|
|
157
|
+
"conversation above. That conversation is background only: do not continue the main agent's "
|
|
158
|
+
"work, do not reply to the user, and do not repeat work that is already done. Work only on the "
|
|
159
|
+
"task below.")
|
|
160
|
+
else:
|
|
161
|
+
intro = f"You are running as the \"{agent_def.name}\" subagent, working for the main agent."
|
|
162
|
+
parts = [
|
|
163
|
+
f"<system-reminder>\n{intro}{mode} Tools you may use: {tools_note}. You cannot ask the user questions: if "
|
|
164
|
+
"something is ambiguous, make a reasonable assumption and mention it in your report. Your final message is "
|
|
165
|
+
"returned to the main agent as your report; make it self-contained and concise.\n</system-reminder>",
|
|
166
|
+
]
|
|
167
|
+
if env:
|
|
168
|
+
parts.append(reminder(env))
|
|
169
|
+
parts.append(f"<role>\n{agent_def.prompt}\n</role>")
|
|
170
|
+
parts.append(f"# Task: {description}\n\n{prompt}")
|
|
171
|
+
return "\n\n".join(parts)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def compact_instruction(extra: str | None = None) -> str:
|
|
175
|
+
extra_text = f"\nAdditional focus requested by the user: {extra}" if extra else ""
|
|
176
|
+
return load_prompt("compact").replace("{extra}", extra_text).strip()
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def post_compact_message(summary: str, recent_user: list[str], todos_text: str | None, files: list[str]) -> str:
|
|
180
|
+
parts = ["<system-reminder>\nThis session was compacted to save context. The summary below replaces the "
|
|
181
|
+
"earlier conversation. Continue the work from where it left off without recapping it to the user.\n"
|
|
182
|
+
"</system-reminder>",
|
|
183
|
+
f"<conversation-summary>\n{summary.strip()}\n</conversation-summary>"]
|
|
184
|
+
if recent_user:
|
|
185
|
+
parts.append("Most recent user messages (verbatim, oldest first):\n" +
|
|
186
|
+
"\n".join(f"<user-message>\n{u}\n</user-message>" for u in recent_user))
|
|
187
|
+
if todos_text:
|
|
188
|
+
parts.append(f"Current todo list:\n{todos_text}")
|
|
189
|
+
if files:
|
|
190
|
+
parts.append("Files recently read or edited: " + ", ".join(files))
|
|
191
|
+
return "\n\n".join(parts)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def reminder(text: str) -> str:
|
|
195
|
+
return f"<system-reminder>\n{text.strip()}\n</system-reminder>"
|