@hicaru/pi-rlm 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -35
- package/README.ru.md +18 -23
- package/README.zh-CN.md +17 -28
- package/package.json +1 -1
- package/src/bridge/library.ts +61 -26
- package/src/bridge/subcall-handlers.ts +382 -0
- package/src/commands/rlm-config.ts +47 -18
- package/src/commands/rlm.ts +3 -152
- package/src/config/defaults.ts +7 -15
- package/src/config/settings.ts +8 -32
- package/src/context/library-context.ts +90 -17
- package/src/core/engine.ts +115 -360
- package/src/core/history.ts +1 -1
- package/src/core/limits.ts +5 -12
- package/src/core/resource-limits.ts +0 -2
- package/src/core/types.ts +3 -36
- package/src/index.ts +49 -10
- package/src/mode/llm-model.ts +54 -0
- package/src/mode/rlm-mode.ts +26 -57
- package/src/prompts/glossary.ts +287 -0
- package/src/prompts/native.ts +127 -0
- package/src/prompts/system.ts +14 -386
- package/src/sandbox/context-file.ts +154 -0
- package/src/sandbox/interrupts.ts +145 -0
- package/src/sandbox/protocol.ts +14 -69
- package/src/sandbox/py/guards.py +150 -0
- package/src/sandbox/py/retrieval.py +265 -0
- package/src/sandbox/py/tasks.py +116 -0
- package/src/sandbox/py/worker.py +836 -0
- package/src/sandbox/sandbox-manager.ts +33 -6
- package/src/sandbox/sandbox.ts +153 -182
- package/src/text/tokens.ts +29 -3
- package/src/tool/background-tasks.ts +95 -0
- package/src/tool/repl-details.ts +4 -2
- package/src/tool/repl-render.ts +58 -0
- package/src/tool/repl-result.ts +70 -0
- package/src/tool/repl-tool.ts +178 -216
- package/src/tool/rlm-aggregator.ts +2 -10
- package/src/tool/rlm-details.ts +0 -2
- package/src/tool/rlm-events.ts +10 -16
- package/src/tool/rlm-tool.ts +1 -12
- package/src/tool/subcall-render.ts +15 -3
- package/src/tool/subcall-store.ts +57 -1
- package/src/ui/config-panel.ts +4 -16
- package/src/ui/intro.ts +1 -2
- package/src/ui/model-picker.ts +34 -10
- package/src/ui/status.ts +3 -7
- package/src/util/concurrency.ts +91 -13
- package/src/util/trace.ts +42 -0
- package/src/bridge/fallback-todo.ts +0 -137
- package/src/bridge/interactive.ts +0 -65
- package/src/bridge/llm-query.ts +0 -156
- package/src/bridge/pi-interactive.ts +0 -41
- package/src/bridge/rlm-query.ts +0 -108
- package/src/core/artifacts.ts +0 -89
- package/src/core/critique.ts +0 -92
- package/src/core/gates.ts +0 -301
- package/src/core/pipeline-handlers.ts +0 -319
- package/src/core/pipeline.ts +0 -268
- package/src/prompts/phases.ts +0 -104
- package/src/sandbox/worker.py +0 -1078
- package/src/state/index.ts +0 -24
- package/src/state/internal.ts +0 -46
- package/src/state/paths.ts +0 -44
- package/src/state/reads.ts +0 -133
- package/src/state/resume.ts +0 -173
- package/src/state/rows.ts +0 -123
- package/src/state/writes.ts +0 -58
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The REPL vocabulary both prompts are built from.
|
|
3
|
+
*
|
|
4
|
+
* Headless (fenced ```repl``` blocks) and native (`repl({code})`) describe the same sandbox, so
|
|
5
|
+
* every line either lives here once or has an explicit condensed native twin next to it — that
|
|
6
|
+
* pairing is the whole reason this module exists. Divergence here is a bug: the model is told
|
|
7
|
+
* about functions that do not exist, or not told about ones that do.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type ContextKind = "files" | "text";
|
|
11
|
+
|
|
12
|
+
/** "str" (raw string context, e.g. rlm_query children) → text; everything else → files. */
|
|
13
|
+
export function contextKindOf(contextType: string): ContextKind {
|
|
14
|
+
return contextType === "str" ? "text" : "files";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const DEFAULT_PROMPT_CAP = 400_000;
|
|
18
|
+
|
|
19
|
+
export function promptCapTokensK(maxPromptChars: number): number {
|
|
20
|
+
return Math.round(maxPromptChars / 4_000);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Deterministic retrieval over `context` (headless + native).
|
|
25
|
+
*
|
|
26
|
+
* The paper's trajectories retrieve with hand-written regex (App. E.1); frontier models do that
|
|
27
|
+
* well, small ones guess keywords badly, and the first decomposition disproportionately decides
|
|
28
|
+
* the outcome (§5, Fig. 4a). These cost no tokens and no sub-calls.
|
|
29
|
+
*/
|
|
30
|
+
export const RETRIEVAL_GLOSSARY_LINES: readonly string[] = Object.freeze([
|
|
31
|
+
"- `search(query: str, k=10, path_glob=None)`: BM25 ranking over `context`. Returns",
|
|
32
|
+
" [{path, line, score, snippet}] — POINTERS, not bodies. **Start here.** It is free:",
|
|
33
|
+
" no sub-LLM call, no tokens. Use it before you guess at filenames or write regex.",
|
|
34
|
+
"- `grep_context(pattern, k=50, path_glob=None, before=0, after=0) -> dict`: regex over",
|
|
35
|
+
" `context`. Returns {hits: [{path, line, text}], counts: {path: n}, total, truncated} —",
|
|
36
|
+
" `counts` is complete even when `hits` is capped, so a wide pattern reports its shape",
|
|
37
|
+
" instead of flooding you. Use for exact lexical needles; use `search` for meaning.",
|
|
38
|
+
"- `outline(path) -> str`: definition/heading skeleton of one file with line numbers.",
|
|
39
|
+
" Orient in ~200 chars instead of printing 20K. Matches exact path, then suffix, then glob.",
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
/** One-line delegation helpers — orchestrating must be cheaper than solving. */
|
|
43
|
+
export const DELEGATION_GLOSSARY_LINES: readonly string[] = Object.freeze([
|
|
44
|
+
"- `map_files(files, prompt, model=None) -> dict[path, str]`: ask `prompt` of every file and",
|
|
45
|
+
" get back {path: answer}. Accepts context entries or paths, packs them into cap-sized",
|
|
46
|
+
" batched sub-calls, and splits oversized files automatically. **This is the default way to",
|
|
47
|
+
" read many files** — prefer it over hand-rolling a chunk loop.",
|
|
48
|
+
"- `llm_map_reduce(items, map_prompt, reduce_prompt, model=None) -> str`: map over items in",
|
|
49
|
+
" one batch, then reduce the partial answers with a single call. The paper's canonical",
|
|
50
|
+
" strategy (query per chunk → aggregate the buffers) as one call.",
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
/** Shared glossary entry for the chunked-query helper (headless + native). */
|
|
54
|
+
export const CHUNKED_GLOSSARY_LINES: readonly string[] = Object.freeze([
|
|
55
|
+
"- `llm_query_chunked(text: str, prompt: str, model=None) -> list[str]`: auto-splits `text` into",
|
|
56
|
+
" chunks that fit the sub-LLM prompt cap, fans them out concurrently (order preserved), and",
|
|
57
|
+
" returns one answer per chunk. Use it for ANY text too large for a single `llm_query` — a file",
|
|
58
|
+
" you open()ed, an oversized sub-result, or several concatenated context files.",
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
/** Non-blocking fan-out: spawn now, collect later (headless glossary). */
|
|
62
|
+
export const SPAWN_GLOSSARY_LINES: readonly string[] = Object.freeze([
|
|
63
|
+
"- `spawn(fn, *args) -> Task`: start `llm_query`, `llm_query_batched`, `llm_query_chunked`,",
|
|
64
|
+
" `map_files`, `rlm_query` or `rlm_query_batched` WITHOUT waiting. Returns immediately.",
|
|
65
|
+
" (Not `llm_map_reduce` — its reduce step depends on its own map results.)",
|
|
66
|
+
"- `rlm_await(task)` / `rlm_await_all(tasks) -> list`: collect results; order matches input.",
|
|
67
|
+
" Tasks survive across turns, so spawn the slow work first, keep doing useful things, and",
|
|
68
|
+
" await only when you actually need the results. `task.done` tells you if it has landed.",
|
|
69
|
+
"",
|
|
70
|
+
" ```python",
|
|
71
|
+
" # start the slow sub-agents, then keep working while they run",
|
|
72
|
+
" tasks = [spawn(rlm_query, f\"Audit {area} end to end\") for area in areas]",
|
|
73
|
+
" hits = [f for f in context if \"TODO\" in f[\"content\"]] # overlaps with the sub-agents",
|
|
74
|
+
" reports = rlm_await_all(tasks)",
|
|
75
|
+
" ```",
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What a parent must know about the child it is about to spawn. Without this the model writes
|
|
80
|
+
* referential prompts ("read lib/x/src/…") on the assumption the child can go fetch them, which
|
|
81
|
+
* is what made a missing child context degrade silently instead of failing (issue #4).
|
|
82
|
+
*/
|
|
83
|
+
export const RECURSION_CONTEXT_LINES: readonly string[] = Object.freeze([
|
|
84
|
+
"",
|
|
85
|
+
" **What a child sees:** it inherits YOUR `context` — the repository plus every library you",
|
|
86
|
+
" loaded (`lib/<id>/…`) — and runs `search` / `grep_context` / `outline` / `map_files` over the",
|
|
87
|
+
" same paths. So send instructions, never file bodies: pasting content you already share costs",
|
|
88
|
+
" your tokens twice and buys nothing. Your prompt becomes the child's question.",
|
|
89
|
+
" Narrow its world with `rlm_query(prompt, paths=['src/auth/', 'lib/x-9f3a/'])` — path PREFIXES,",
|
|
90
|
+
" not globs. Omit `paths` to hand over everything.",
|
|
91
|
+
" Inheritance is one-way: libraries the child loads, and its whole REPL, die with it — only its",
|
|
92
|
+
" final answer string returns.",
|
|
93
|
+
" At the depth cap `rlm_query` degrades to a plain sub-LLM call with NO context, which is why",
|
|
94
|
+
" this section disappears at the last recursive depth.",
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sub-RLM orientation. Emitted only at depth > 0, where `context` is the parent's world rather
|
|
99
|
+
* than a repository the run packed for itself.
|
|
100
|
+
*/
|
|
101
|
+
export const CHILD_CONTEXT_LINES: readonly string[] = Object.freeze([
|
|
102
|
+
" You are a sub-RLM. This `context` is your parent's world — the repository plus every library",
|
|
103
|
+
" it loaded (paths under `lib/<id>/…`). Answer only the question above; your REPL and anything",
|
|
104
|
+
" you load die with you, and only your final answer string returns to the parent.",
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
/** Why a file the user mentioned may be missing from `context`. */
|
|
108
|
+
export const CONTEXT_EXCLUSION_NOTE =
|
|
109
|
+
" NOTE: files larger than 1MB and gitignored files are NOT in `context` — they exist only on disk.";
|
|
110
|
+
|
|
111
|
+
/** The large-on-disk-file protocol (headless + native). */
|
|
112
|
+
export const LARGE_FILE_RULE_LINES: readonly string[] = Object.freeze([
|
|
113
|
+
"**Large on-disk files (profiles, logs, dumps, generated JSON):** files >1MB or gitignored are",
|
|
114
|
+
"absent from `context`. Protocol:",
|
|
115
|
+
'1. Load in Python: `raw = open("dhat-heap.json").read()` — loading into a variable is fine.',
|
|
116
|
+
"2. Deterministic processing in Python (`json.load`, `re`, counting, aggregation) is fine and preferred.",
|
|
117
|
+
"3. The moment you need MEANING from raw text (summarize, explain, find anomalies), do NOT read it",
|
|
118
|
+
" yourself — call `llm_query_chunked(raw, question)`, or slice + `llm_query_batched`.",
|
|
119
|
+
"4. Never print more than a small probe (~2K chars) of raw content.",
|
|
120
|
+
'Example: `parts = llm_query_chunked(raw, "Extract top allocation sites with byte totals")`, then',
|
|
121
|
+
"aggregate `parts` in Python or with one final `llm_query`.",
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
/** Concise native-mode glossary line for the chunked helper (native prompt has a 6K budget). */
|
|
125
|
+
export const CHUNKED_GLOSSARY_LINE_NATIVE =
|
|
126
|
+
"- `llm_query_chunked(text, prompt, model=None) -> list[str]` — auto-splits oversized text into cap-sized chunks, fans out concurrently; one answer per chunk.";
|
|
127
|
+
|
|
128
|
+
/** Concise native-mode large-file rule (folds in the context-exclusion note; native 6K budget). */
|
|
129
|
+
export const LARGE_FILE_RULE_NATIVE =
|
|
130
|
+
"- Files >1MB or gitignored are NOT in `context`: open() + parse deterministically in Python is fine; ANY semantic reading of the raw text goes through llm_query_chunked. Never print >2K chars raw.";
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The decomposition doctrine, ported from the RLM paper's Appendix C.3 `<env_tips>` and
|
|
134
|
+
* retargeted from competition math to repository analysis.
|
|
135
|
+
*
|
|
136
|
+
* This block is the single highest-leverage prompt intervention the paper reports: +69.5% on
|
|
137
|
+
* LongCoT-mini over the same RLM without it (Table 2). Plain RLM prompting alone actually
|
|
138
|
+
* *regressed* two of the five categories; the doctrine is what fixed them. Its purpose is to
|
|
139
|
+
* counter under-delegation — the model doing the work itself in the REPL instead of fanning out.
|
|
140
|
+
*
|
|
141
|
+
* Note the counterweight: `orchestratorAddendum` carries the anti-OVER-recursion batching rule.
|
|
142
|
+
* The paper is explicit (App. B) that one prompt does not port across models and that both
|
|
143
|
+
* guardrails are needed; keep them both.
|
|
144
|
+
*/
|
|
145
|
+
export const ENV_TIPS = [
|
|
146
|
+
"## Decomposition doctrine",
|
|
147
|
+
"",
|
|
148
|
+
"**Orchestrate; don't solve.** A single chain of thought over a large repository drifts —",
|
|
149
|
+
"you lose partials and compound mistakes. Your sub-LLMs are competent readers: given a",
|
|
150
|
+
"self-contained prompt and the text, they will extract, locate, classify, and summarize",
|
|
151
|
+
"reliably. Trust them; don't do their reading yourself.",
|
|
152
|
+
"",
|
|
153
|
+
"Your job: (1) find the relevant slice with `search` / `grep_context` / `outline`,",
|
|
154
|
+
"(2) delegate all semantic reading to `map_files` / `llm_query_batched` / `llm_map_reduce`,",
|
|
155
|
+
"(3) memoize every result you will reuse in `answers`, (4) sanity-check an answer before",
|
|
156
|
+
"another step depends on it, (5) assemble the final answer from `answers` by lookup.",
|
|
157
|
+
"Your own compute is: pointers, dict lookups, string formatting, and decisions.",
|
|
158
|
+
"",
|
|
159
|
+
"### The only state that matters",
|
|
160
|
+
"`answers` and `plan` are dicts that persist across every turn.",
|
|
161
|
+
"**If a value isn't in `answers`, it doesn't exist.** Do not trust a number from your own",
|
|
162
|
+
"earlier reasoning or from truncated stdout — context drifts. Memoize everything you reuse.",
|
|
163
|
+
"",
|
|
164
|
+
"### Shape of a run",
|
|
165
|
+
"1. Probe: `print(len(context))`, `search(<the user's question>)`. Do not print file bodies.",
|
|
166
|
+
"2. Plan: write the sub-questions into `plan`; each must be answerable from a named slice.",
|
|
167
|
+
"3. Fan out: one `map_files` / `llm_query_batched` per independent group, not one call per",
|
|
168
|
+
" file. Store results into `answers` keyed by path or sub-question.",
|
|
169
|
+
"4. Assemble: build the answer from `answers`. Delegate the aggregation too if it is large.",
|
|
170
|
+
"",
|
|
171
|
+
"### Red flags — you are off track",
|
|
172
|
+
"- Printing file bodies to read them yourself → stop, delegate to `map_files`.",
|
|
173
|
+
"- Writing regex to *infer meaning* (naming conventions, intent, correctness) → that is a",
|
|
174
|
+
" sub-LLM job. Regex is for exact lexical needles only.",
|
|
175
|
+
"- Two turns in with zero sub-LLM calls on an analysis task → you are solving it yourself.",
|
|
176
|
+
"- About to reuse a value that is not in `answers` → re-derive it and store it.",
|
|
177
|
+
"- One sub-call per file over dozens of files → batch them; fat prompts in small batches win.",
|
|
178
|
+
].join("\n");
|
|
179
|
+
|
|
180
|
+
/** Native-mode variant of the doctrine — same rules, sized for the native prompt budget. */
|
|
181
|
+
export const ENV_TIPS_CONDENSED = [
|
|
182
|
+
"### Decomposition doctrine (paper App. C.3 — worth +69.5% there)",
|
|
183
|
+
"Orchestrate; don't solve. Loop: `search`/`grep_context`/`outline` to find the slice →",
|
|
184
|
+
"`map_files` / `llm_query_batched` to read it → memoize into `answers` → assemble by lookup.",
|
|
185
|
+
"`answers` and `plan` persist across every turn: **if a value isn't in `answers`, it",
|
|
186
|
+
"doesn't exist** — never reuse a number from your own earlier reasoning or truncated stdout.",
|
|
187
|
+
"Red flags: printing file bodies to read them; regex used to infer meaning rather than match",
|
|
188
|
+
"a literal; two turns into an analysis with zero sub-LLM calls; one sub-call per file instead",
|
|
189
|
+
"of one batch. Exception — AUTHORING is not reading: you write every edit body yourself.",
|
|
190
|
+
].join("\n");
|
|
191
|
+
|
|
192
|
+
export function howToRunCode(): string {
|
|
193
|
+
return [
|
|
194
|
+
"To run Python, write a fenced ```repl``` block. The REPL **persists** across turns. Only",
|
|
195
|
+
"`print(...)` output (stdout) is returned; a bare expression on the last line is discarded, so",
|
|
196
|
+
"always wrap inspections in `print(...)`.",
|
|
197
|
+
].join(" ");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function replGlossary(
|
|
201
|
+
kind: ContextKind,
|
|
202
|
+
recursion: boolean,
|
|
203
|
+
libraryLoader: boolean,
|
|
204
|
+
child: boolean,
|
|
205
|
+
): string {
|
|
206
|
+
const lines = ["Available in the REPL:"];
|
|
207
|
+
if (kind === "text") {
|
|
208
|
+
lines.push(
|
|
209
|
+
"- `context`: str — the raw text you must analyze. Probe it with slices",
|
|
210
|
+
" (`print(context[:2000])`), split it programmatically, and delegate large chunks",
|
|
211
|
+
" to sub-LLMs — never dump the whole string into your own output.",
|
|
212
|
+
);
|
|
213
|
+
} else {
|
|
214
|
+
lines.push(
|
|
215
|
+
"- `context`: list[dict] — a pre-packed JSON array of every file in the repository. Each dict has",
|
|
216
|
+
" keys: `path` (relative file path, str), `content` (file text, str), `tokens` (estimated count, int).",
|
|
217
|
+
" For large repos, chunk `context` into batches and delegate to sub-LLMs — never dump raw file",
|
|
218
|
+
" bodies into your own output.",
|
|
219
|
+
CONTEXT_EXCLUSION_NOTE,
|
|
220
|
+
);
|
|
221
|
+
if (child) lines.push(...CHILD_CONTEXT_LINES);
|
|
222
|
+
lines.push(
|
|
223
|
+
"",
|
|
224
|
+
" Worked example — find the slice, then delegate it:",
|
|
225
|
+
" ```python",
|
|
226
|
+
' hits = search("where is the retry/backoff policy configured?", k=8)',
|
|
227
|
+
" paths = sorted({h['path'] for h in hits})",
|
|
228
|
+
' answers.update(map_files(paths, "Describe any retry/backoff policy in this file, with line numbers. Say NONE if absent."))',
|
|
229
|
+
" print({p: a[:80] for p, a in answers.items()})",
|
|
230
|
+
" ```",
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
lines.push(...RETRIEVAL_GLOSSARY_LINES);
|
|
234
|
+
lines.push(
|
|
235
|
+
"- `llm_query(prompt: str, model=None) -> str`: a single sub-LLM completion. Use for extraction,",
|
|
236
|
+
" summarization, or Q&A over a chunk of text.",
|
|
237
|
+
"- `llm_query_batched(prompts: list[str], model=None) -> list[str]`: run several sub-LLM calls",
|
|
238
|
+
" concurrently; output order matches input order.",
|
|
239
|
+
...CHUNKED_GLOSSARY_LINES,
|
|
240
|
+
...SPAWN_GLOSSARY_LINES,
|
|
241
|
+
...DELEGATION_GLOSSARY_LINES,
|
|
242
|
+
);
|
|
243
|
+
if (libraryLoader) {
|
|
244
|
+
lines.push(
|
|
245
|
+
"- `load_library(source: str) -> dict`: load an EXTERNAL library, source tree, or document and",
|
|
246
|
+
" **APPEND its files into the existing `context` list** (same shape: path/content/tokens).",
|
|
247
|
+
" `source` may be a local directory (repomix-packed), a single file path, or an https/git@ URL",
|
|
248
|
+
" (shallow-cloned, then packed). Paths are namespaced under `lib/<source_id>/…` so you can filter",
|
|
249
|
+
" by prefix. Returns metadata only:",
|
|
250
|
+
" {\"source\", \"source_id\", \"path_prefix\", \"files\", \"chars\", \"context_len\", \"already_loaded\"}",
|
|
251
|
+
" or an \"Error: ...\" string. **Never treat the return value as the file list** — always search",
|
|
252
|
+
" and chunk the single variable `context`. Do not invent `context_1` / aliases; do not call",
|
|
253
|
+
" globals()/locals(). Idempotent: re-loading the same source is a no-op.",
|
|
254
|
+
"",
|
|
255
|
+
" ```python",
|
|
256
|
+
" info = load_library(\"/path/to/other-project\")",
|
|
257
|
+
" # info is metadata; files are already in context under info[\"path_prefix\"]",
|
|
258
|
+
" lib_files = [f for f in context if f[\"path\"].startswith(info[\"path_prefix\"])]",
|
|
259
|
+
" ```",
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
if (recursion) {
|
|
263
|
+
lines.push(
|
|
264
|
+
"- `rlm_query(prompt, model=None)` / `rlm_query_batched(prompts, model=None)`: recursive RLM",
|
|
265
|
+
" sub-calls. Each child runs a full REPL loop internally — its entire conversation is PRIVATE",
|
|
266
|
+
" and never enters your history. Only the final answer (a short string) is returned.",
|
|
267
|
+
"",
|
|
268
|
+
" **Choosing between `llm_query` and `rlm_query`:**",
|
|
269
|
+
" - `llm_query` for simple one-shot tasks — summarize a chunk, extract a fact, answer a direct",
|
|
270
|
+
" question. It is a single LLM call: fast and cheap. Prefer it by default, and fan out with",
|
|
271
|
+
" `llm_query_batched` for parallel one-shots.",
|
|
272
|
+
" - `rlm_query` only when a sub-task genuinely needs iterative reasoning with its own code",
|
|
273
|
+
" execution (e.g. a sub-context large enough to need its own chunking, or a multi-step",
|
|
274
|
+
" reasoning chain). It is slower and more expensive — reserve it for cases `llm_query` cannot",
|
|
275
|
+
" handle. Avoid excessive recursive sub-calls when a batched one-shot would suffice.",
|
|
276
|
+
...RECURSION_CONTEXT_LINES,
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
lines.push(
|
|
280
|
+
"- `answers` / `plan`: two dicts that persist across turns. Memoize every",
|
|
281
|
+
" verified result in `answers` — see the decomposition doctrine below.",
|
|
282
|
+
"- `SHOW_VARS() -> str`: list every variable currently in the REPL.",
|
|
283
|
+
'- `answer`: a dict initialized to {"content": "", "ready": False}. To submit your final answer,',
|
|
284
|
+
' set `answer["content"]` to the answer text and `answer["ready"] = True`.',
|
|
285
|
+
);
|
|
286
|
+
return lines.join("\n");
|
|
287
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/** Native-mode prompts — the main Pi agent drives the sandbox through the `repl` tool. */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CHUNKED_GLOSSARY_LINE_NATIVE,
|
|
5
|
+
ENV_TIPS_CONDENSED,
|
|
6
|
+
LARGE_FILE_RULE_NATIVE,
|
|
7
|
+
DEFAULT_PROMPT_CAP,
|
|
8
|
+
promptCapTokensK,
|
|
9
|
+
} from "./glossary.ts";
|
|
10
|
+
|
|
11
|
+
/** Adapts the REPL glossary for native mode — agent calls `repl({code})` instead of writing ```repl``` blocks. */
|
|
12
|
+
function nativeReplGlossary(): string {
|
|
13
|
+
return [
|
|
14
|
+
"## RLM Native Mode — Persistent Python REPL",
|
|
15
|
+
"",
|
|
16
|
+
"Call `repl({code: \"...\"})` to execute Python in a **persistent** sandbox. Variables, imports,",
|
|
17
|
+
"State persists; only `print()` output is returned, so wrap inspections in `print(...)`.",
|
|
18
|
+
"",
|
|
19
|
+
"### REPL Environment",
|
|
20
|
+
"- `context`: list[dict] — every file in the repository. Each dict: `path` (str), `content` (str), `tokens` (int).",
|
|
21
|
+
"",
|
|
22
|
+
"Retrieval — free (no sub-LLM call, no tokens). **Start here, before guessing filenames:**",
|
|
23
|
+
"- `search(query, k=10, path_glob=None) -> [{path, line, score, snippet}]` — BM25 over `context`. Returns pointers, not bodies.",
|
|
24
|
+
"- `grep_context(pattern, k=50, path_glob=None, before=0, after=0) -> {hits, counts, total, truncated}` — regex; `counts` stays complete when `hits` is capped. Lexical needles only.",
|
|
25
|
+
"- `outline(path) -> str` — definition/heading skeleton with line numbers. Orient in ~200 chars instead of printing 20K.",
|
|
26
|
+
"",
|
|
27
|
+
"Delegation — everything semantic goes through these:",
|
|
28
|
+
"- `map_files(files, prompt, model=None) -> {path: answer}` — ask `prompt` of many files; batches and splits oversized files for you. **The default way to read many files.**",
|
|
29
|
+
"- `llm_map_reduce(items, map_prompt, reduce_prompt, model=None) -> str` — map in one batch, then reduce with one call.",
|
|
30
|
+
"- `llm_query(prompt, model=None) -> str` — one-shot sub-LLM. Use for extraction, summarization, Q&A over a chunk.",
|
|
31
|
+
"- `llm_query_batched(prompts, model=None) -> list[str]` — concurrent sub-LLM calls; output order matches input order.",
|
|
32
|
+
CHUNKED_GLOSSARY_LINE_NATIVE,
|
|
33
|
+
"- `rlm_query(prompt, model=None, paths=None) -> str` — recursive RLM with its own REPL for complex sub-tasks needing iterative reasoning. Prefer llm_query — rlm_query is slower and costlier. The child inherits your `context` (repo + loaded libraries) and takes your prompt as its question, so describe the task; never paste file text. `paths=['src/auth/']` narrows its context by prefix.",
|
|
34
|
+
"- `rlm_query_batched(prompts, model=None) -> list[str]` — concurrent recursive RLM calls.",
|
|
35
|
+
"- `spawn(fn, *args) -> Task` / `rlm_await(t)` / `rlm_await_all(ts)` — start `llm_query`, `llm_query_batched`, `llm_query_chunked`, `map_files`, `rlm_query` or `rlm_query_batched` without waiting (NOT `llm_map_reduce`); collect later, order preserved. Tasks outlive the repl() call, so spawn slow work early and await when you need it.",
|
|
36
|
+
"",
|
|
37
|
+
"",
|
|
38
|
+
"- `answers` / `plan` — dicts persisted across every repl() call. Your memo.",
|
|
39
|
+
"- `load_library(source) -> dict`: append external dir/file/git tree into `context` under `lib/<id>/…`. Return is metadata only — always use `context`.",
|
|
40
|
+
"- `SHOW_VARS() -> str` — list all variables currently in the REPL.",
|
|
41
|
+
"- `answer`: dict `{\"content\": \"\", \"ready\": False}`. Setting `answer[\"ready\"] = True` delivers it to the user; do not restate it.",
|
|
42
|
+
"",
|
|
43
|
+
ENV_TIPS_CONDENSED,
|
|
44
|
+
"",
|
|
45
|
+
"### Worked pattern",
|
|
46
|
+
"```python",
|
|
47
|
+
'hits = search("where is retry/backoff configured?", k=8)',
|
|
48
|
+
"paths = sorted({h['path'] for h in hits})",
|
|
49
|
+
'answers.update(map_files(paths, "Describe any retry/backoff policy here, with line numbers. Say NONE if absent."))',
|
|
50
|
+
"print({p: a[:80] for p, a in answers.items()})",
|
|
51
|
+
"```",
|
|
52
|
+
`- Sub-prompts cap at ${DEFAULT_PROMPT_CAP.toLocaleString()} chars (≈${promptCapTokensK(DEFAULT_PROMPT_CAP)}K tokens); ~20 prompts per batch. Fat prompts in small batches > thousands of tiny prompts.`,
|
|
53
|
+
"",
|
|
54
|
+
"### Choosing Between Tools",
|
|
55
|
+
"| Tool | When |",
|
|
56
|
+
"|------|------|",
|
|
57
|
+
"| `repl({code})` | ALL repository reading, search, and analysis; Python scripting; state across calls |",
|
|
58
|
+
"| `edit` / `write` | Change or create a file. Compose oldText/newText yourself; exact match required |",
|
|
59
|
+
"| `search` / `grep_context` / `outline` (in repl) | Locate the relevant slice — free, do this first |",
|
|
60
|
+
"| `zebra-mcp` | Semantic/embedding search when lexical `search` misses the concept |",
|
|
61
|
+
"| `map_files` / `llm_query_batched` (in repl) | Read/extract/classify that slice |",
|
|
62
|
+
"| `rlm_query` (in repl) | Sub-task needing its own iterative reasoning and REPL |",
|
|
63
|
+
"",
|
|
64
|
+
"### Task-Specific Patterns",
|
|
65
|
+
LARGE_FILE_RULE_NATIVE,
|
|
66
|
+
"- Architecture/code review: `search` for the subsystem, then `map_files` the hits.",
|
|
67
|
+
"- Bug investigation: `grep_context` for the literal symbol/message, then `map_files` the matching files.",
|
|
68
|
+
"- Finalizing: for file changes call `edit`/`write` directly so Pi validates the anchor and renders the diff; for analysis, write a normal message.",
|
|
69
|
+
"- If sub-LLM credits are exhausted, report partial results and stop — do not bypass REPL restrictions.",
|
|
70
|
+
"",
|
|
71
|
+
"Reserve your own tokens for high-level decisions: what to ask next, how to combine sub-LLM outputs, when to finalize.",
|
|
72
|
+
"Delegate everything else. Do not submit a final answer before inspecting `context`.",
|
|
73
|
+
].join("\n");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Build the native-mode system prompt for the main Pi agent. */
|
|
77
|
+
export function buildNativeSystemPrompt(): string {
|
|
78
|
+
return [
|
|
79
|
+
"╔══════════════════════════════════════════════════════════════════╗",
|
|
80
|
+
"║ NATIVE RLM MODE — YOU ARE AN ORCHESTRATOR, NOT A READER ║",
|
|
81
|
+
"╚══════════════════════════════════════════════════════════════════╝",
|
|
82
|
+
"",
|
|
83
|
+
"ENFORCED BY THE RUNTIME (not advisory):",
|
|
84
|
+
"- `read`/`grep` are blocked; bash readers (cat/sed/head/tail/awk/rg) are blocked; bash output is hard-capped at 4K chars.",
|
|
85
|
+
"- repl() stdout returned to you is hard-capped at 4K chars — printing file bodies is USELESS; the text will not reach you.",
|
|
86
|
+
"",
|
|
87
|
+
"LOCATE-THEN-DELEGATE: `search(query)` / `grep_context(pattern)` / `outline(path)` cost nothing",
|
|
88
|
+
"— run them FIRST to find the relevant slice. Then, if a step needs MEANING from more than ~4K",
|
|
89
|
+
"chars, that reading MUST be a map_files / llm_query / llm_query_batched / llm_query_chunked",
|
|
90
|
+
"call (rlm_query for iterative sub-tasks). Deterministic Python over `context` is free and",
|
|
91
|
+
"preferred for lookups. Semantic reading is always delegated.",
|
|
92
|
+
"",
|
|
93
|
+
"All file content is pre-loaded in the REPL `context` variable. Use ONLY `repl({code})`.",
|
|
94
|
+
"If sub-LLM credits are exhausted → report the error to the user and stop.",
|
|
95
|
+
"",
|
|
96
|
+
"AUTHORING RULE: sub-LLMs (`llm_query` family) READ — they extract, locate, and summarize.",
|
|
97
|
+
"They never author code you will ship. Once you know WHAT to change, compose the exact",
|
|
98
|
+
"oldText / newText yourself and apply it with `edit`. Delegated code is written by a small",
|
|
99
|
+
"model with no view of the codebase; it is a research aid, never a patch.",
|
|
100
|
+
"Never re-type file bodies or `answer[\"content\"]` in your own output.",
|
|
101
|
+
"",
|
|
102
|
+
nativeReplGlossary(),
|
|
103
|
+
].join("\n");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Soft cap on the static native prompt. Leaves headroom for per-turn context injection
|
|
107
|
+
* without bloating the root model's system prompt. Exceeded → phase-guards.ts fails.
|
|
108
|
+
* Raised from 6K when the retrieval glossary and the condensed decomposition doctrine
|
|
109
|
+
* landed; both buy far more than they cost (paper Table 2, Fig. 4a), then again for
|
|
110
|
+
* spawn/rlm_await: the async fan-out API is part of the model-visible contract, and
|
|
111
|
+
* ~50 tokens is worth the model actually using it. */
|
|
112
|
+
export const NATIVE_PROMPT_BUDGET = 7_700;
|
|
113
|
+
|
|
114
|
+
/** Exported for tests — prompt length without context metadata (which is injected separately). */
|
|
115
|
+
export const NATIVE_PROMPT_STATIC = buildNativeSystemPrompt();
|
|
116
|
+
|
|
117
|
+
/** Per-turn last-position reminder for native mode — appended to every context build. */
|
|
118
|
+
export const NATIVE_TURN_REMINDER = [
|
|
119
|
+
"[RLM orchestrator contract — enforced by the runtime, not optional:",
|
|
120
|
+
"repl() stdout to you is hard-capped at 4K chars; read/grep and bash readers are blocked.",
|
|
121
|
+
"LOCATE FIRST with search() / grep_context() / outline() — they cost nothing. Any SEMANTIC",
|
|
122
|
+
"reading MUST then go through map_files / llm_query / llm_query_batched / llm_query_chunked",
|
|
123
|
+
"(rlm_query for iterative sub-tasks). Memoize what you reuse in `answers`; a value not in",
|
|
124
|
+
"`answers` does not exist. AUTHORING IS NOT READING: you write every edit body yourself and",
|
|
125
|
+
"apply it with the native `edit` / `write` tools — never delegate code you will ship.",
|
|
126
|
+
"Keep your own output to decisions, authored edits, and aggregation.]",
|
|
127
|
+
].join("\n");
|