@agi-cli/sdk 0.1.88 → 0.1.89
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/package.json +1 -1
- package/src/core/src/tools/builtin/patch/apply.ts +6 -0
- package/src/prompts/src/agents/build.txt +37 -1
- package/src/prompts/src/base.txt +2 -1
- package/src/prompts/src/providers/anthropic.txt +22 -0
- package/src/prompts/src/providers/default.txt +32 -0
- package/src/prompts/src/providers/openai.txt +31 -0
package/package.json
CHANGED
|
@@ -259,6 +259,12 @@ function applyHunkToLines(
|
|
|
259
259
|
.map((line) => line.content);
|
|
260
260
|
|
|
261
261
|
const removals = hunk.lines.filter((line) => line.kind === 'remove');
|
|
262
|
+
const additions = hunk.lines
|
|
263
|
+
.filter((line) => line.kind === 'add')
|
|
264
|
+
.map((line) => line.content);
|
|
265
|
+
const contextLines = hunk.lines
|
|
266
|
+
.filter((line) => line.kind === 'context')
|
|
267
|
+
.map((line) => line.content);
|
|
262
268
|
|
|
263
269
|
const hasExpected = expected.length > 0;
|
|
264
270
|
const initialHint =
|
|
@@ -29,9 +29,45 @@ You help with coding and build tasks.
|
|
|
29
29
|
actual line from file ← Context (space prefix) - REQUIRED
|
|
30
30
|
-line to remove ← Remove this line
|
|
31
31
|
+line to add ← Add this line
|
|
32
|
-
|
|
32
|
+
more context ← More context (space prefix)
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
## ⚠️ Why Patches Fail (Common Mistakes)
|
|
36
|
+
|
|
37
|
+
**Mistake 1: Patching from Memory** (Most Common)
|
|
38
|
+
- ❌ Creating patches based on what you remember from earlier
|
|
39
|
+
- ✅ ALWAYS read the file FIRST in this same turn, then create patch
|
|
40
|
+
|
|
41
|
+
**Mistake 2: Context Lines Don't Match File**
|
|
42
|
+
- ❌ Guessing or inventing what context lines look like
|
|
43
|
+
- ✅ Copy context lines EXACTLY character-for-character from the file you just read
|
|
44
|
+
- Context lines (space prefix) must exist in the actual file
|
|
45
|
+
|
|
46
|
+
**Mistake 3: Wrong Indentation**
|
|
47
|
+
- ❌ File uses 2 spaces; patch uses tabs or 4 spaces
|
|
48
|
+
- ✅ Match indentation exactly: if file uses spaces, patch uses spaces (same count)
|
|
49
|
+
|
|
50
|
+
**Mistake 4: Missing Markers**
|
|
51
|
+
- ❌ Forgot `*** End Patch` or malformed `*** Begin Patch`
|
|
52
|
+
- ✅ Always wrap: `*** Begin Patch` ... hunks ... `*** End Patch`
|
|
53
|
+
|
|
54
|
+
**Mistake 5: Hallucinated Code**
|
|
55
|
+
- ❌ Adding lines that "should" be there but aren't
|
|
56
|
+
- ✅ Only use lines that actually exist in the file
|
|
57
|
+
|
|
58
|
+
**Success Formula:**
|
|
59
|
+
1. Read file with `read` tool
|
|
60
|
+
2. Note exact indentation (spaces/tabs), line content
|
|
61
|
+
3. Extract 2-3 context lines before/after your change
|
|
62
|
+
4. Copy them EXACTLY into patch with space prefix
|
|
63
|
+
5. Add your `-old` and `+new` lines
|
|
64
|
+
6. Verify markers: `*** Begin Patch` and `*** End Patch`
|
|
65
|
+
|
|
66
|
+
**When Patch Fails:**
|
|
67
|
+
- Error means context didn't match or file changed
|
|
68
|
+
- Solution: Read the file AGAIN, check character-for-character
|
|
69
|
+
- If still failing repeatedly, use `edit` tool instead
|
|
70
|
+
|
|
35
71
|
**Using the `edit` Tool** (Alternative):
|
|
36
72
|
- Specify the file path and a list of operations
|
|
37
73
|
- Operations are applied sequentially to the latest file state
|
package/src/prompts/src/base.txt
CHANGED
|
@@ -15,7 +15,8 @@ You MUST call the `finish` tool at the end of every response to signal completio
|
|
|
15
15
|
**IMPORTANT**: Do NOT call `finish` before streaming your response. Always stream your message first, then call `finish`. If you forget to call `finish`, the system will hang and not complete properly.
|
|
16
16
|
|
|
17
17
|
File Editing Best Practices:
|
|
18
|
-
- ALWAYS read a file immediately before using apply_patch
|
|
18
|
+
- ⚠️ CRITICAL: ALWAYS read a file immediately before using apply_patch - never patch from memory
|
|
19
|
+
- Read the file in THIS turn, not from previous context or memory
|
|
19
20
|
- When making multiple edits to the same file, combine them into a single edit operation with multiple ops
|
|
20
21
|
- Each edit operation re-reads the file, so ops within a single edit call are applied sequentially to the latest content
|
|
21
22
|
- If you need to make edits based on previous edits, ensure they're in the same edit call or re-read the file between calls
|
|
@@ -210,6 +210,28 @@ NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTAN
|
|
|
210
210
|
|
|
211
211
|
IMPORTANT: Always use the update_plan tool to plan and track tasks throughout the conversation.
|
|
212
212
|
|
|
213
|
+
# Apply Patch Tool - Critical Guidelines for Claude
|
|
214
|
+
|
|
215
|
+
**⚠️ Claude-Specific Patch Failures:**
|
|
216
|
+
|
|
217
|
+
You (Claude/Sonnet) generally excel at using patches, but even you can fail when:
|
|
218
|
+
- Relying on memory from earlier in the conversation instead of fresh file reads
|
|
219
|
+
- Making assumptions about unchanged file state between operations
|
|
220
|
+
- Mixing tabs/spaces when file indentation isn't verified first
|
|
221
|
+
|
|
222
|
+
**Your Success Pattern (Follow This):**
|
|
223
|
+
1. Read file with `read` tool immediately before patching
|
|
224
|
+
2. Extract exact context lines (space prefix) from what you just read
|
|
225
|
+
3. Match indentation character-for-character
|
|
226
|
+
4. Use multiple `@@` hunks for multiple edits in same file
|
|
227
|
+
5. Wrap in `*** Begin Patch` and `*** End Patch`
|
|
228
|
+
|
|
229
|
+
**If Patch Fails:**
|
|
230
|
+
- Don't retry with same context - read file AGAIN first
|
|
231
|
+
- Check that context lines exist exactly as written
|
|
232
|
+
- Verify indentation matches (spaces vs tabs)
|
|
233
|
+
- If failing 2+ times, switch to `edit` tool instead
|
|
234
|
+
|
|
213
235
|
# Code References
|
|
214
236
|
|
|
215
237
|
When referencing specific functions or pieces of code include the pattern `file_path:line_number` to allow the user to easily navigate to the source code location.
|
|
@@ -427,6 +427,38 @@ When using the shell, you must adhere to the following guidelines:
|
|
|
427
427
|
- When searching for text or files, prefer using `rg` or `rg --files` respectively because `rg` is much faster than alternatives like `grep`. (If the `rg` command is not found, then use alternatives.)
|
|
428
428
|
- Read files in chunks with a max chunk size of 250 lines. Do not use python scripts to attempt to output larger chunks of a file. Command line output will be truncated after 10 kilobytes or 256 lines of output, regardless of the command used.
|
|
429
429
|
|
|
430
|
+
## Apply Patch Tool - Critical Guidelines
|
|
431
|
+
|
|
432
|
+
**⚠️ Common Patch Failures Across All Models:**
|
|
433
|
+
|
|
434
|
+
Most patch failures happen because:
|
|
435
|
+
- Patches created from memory instead of reading file first
|
|
436
|
+
- Context lines guessed or invented rather than copied exactly
|
|
437
|
+
- Indentation mismatches (tabs vs spaces)
|
|
438
|
+
- Missing or malformed markers (`*** Begin Patch` / `*** End Patch`)
|
|
439
|
+
- Hallucinated code in context lines
|
|
440
|
+
|
|
441
|
+
**Universal Pre-Flight Checklist:**
|
|
442
|
+
Before calling `apply_patch`, verify ALL of these:
|
|
443
|
+
- [ ] File was read with `read` tool in THIS turn (not from memory)
|
|
444
|
+
- [ ] Context lines (space prefix) copied EXACTLY character-for-character
|
|
445
|
+
- [ ] Indentation verified (if file uses spaces, patch uses spaces)
|
|
446
|
+
- [ ] Wrapped in `*** Begin Patch` and `*** End Patch` markers
|
|
447
|
+
- [ ] Used correct directive: `*** Add/Update/Delete File: path`
|
|
448
|
+
|
|
449
|
+
**Success Formula:**
|
|
450
|
+
1. Use `read` tool on target file
|
|
451
|
+
2. Identify exact location to edit
|
|
452
|
+
3. Extract 2-3 lines before/after as context (space prefix)
|
|
453
|
+
4. Add `-old` lines to remove, `+new` lines to add
|
|
454
|
+
5. Wrap in `*** Begin Patch` ... `*** End Patch`
|
|
455
|
+
6. Verify all context matches file exactly
|
|
456
|
+
|
|
457
|
+
**If Patch Fails:**
|
|
458
|
+
- Error = context didn't match OR file content changed
|
|
459
|
+
- Solution: Read file AGAIN, verify context character-by-character
|
|
460
|
+
- After 2+ failures: use `edit` tool instead (more forgiving)
|
|
461
|
+
|
|
430
462
|
## `update_plan`
|
|
431
463
|
|
|
432
464
|
A tool named `update_plan` is available to you. You can use it to keep an up‑to‑date, step‑by‑step plan for the task.
|
|
@@ -366,6 +366,37 @@ When using the shell, you must adhere to the following guidelines:
|
|
|
366
366
|
- Shell commands should be reserved for execution, not discovery
|
|
367
367
|
- Read files in chunks with a max chunk size of 250 lines. Do not use python scripts to attempt to output larger chunks of a file. Command line output will be truncated after 10 kilobytes or 256 lines of output, regardless of the command used.
|
|
368
368
|
|
|
369
|
+
## Apply Patch Tool - Critical for GPT-4 Models
|
|
370
|
+
|
|
371
|
+
**⚠️ GPT-4 Common Patch Failures:**
|
|
372
|
+
|
|
373
|
+
GPT-4 models (especially GPT-4o) often fail patches by:
|
|
374
|
+
- Creating patches from memory instead of reading the file first
|
|
375
|
+
- Guessing at context lines that don't match the actual file
|
|
376
|
+
- Missing or malformed `*** End Patch` markers
|
|
377
|
+
- Mixing tabs/spaces without checking file's indentation
|
|
378
|
+
|
|
379
|
+
**Mandatory Pre-Flight Checklist (Check EVERY time):**
|
|
380
|
+
- [ ] Read the target file with `read` tool in THIS turn
|
|
381
|
+
- [ ] Copy context lines EXACTLY from what you just read
|
|
382
|
+
- [ ] Verify indentation (spaces vs tabs) matches the file
|
|
383
|
+
- [ ] Include `*** Begin Patch` and `*** End Patch` markers
|
|
384
|
+
- [ ] Use space prefix for context lines (NOT `@@` line - that's just a hint)
|
|
385
|
+
|
|
386
|
+
**GPT-4 Success Formula:**
|
|
387
|
+
1. Call `read` on the file you want to patch
|
|
388
|
+
2. Identify the exact lines to change
|
|
389
|
+
3. Copy 2-3 surrounding lines AS-IS (space prefix)
|
|
390
|
+
4. Add your `-removal` and `+addition` lines
|
|
391
|
+
5. Wrap in `*** Begin Patch` ... `*** End Patch`
|
|
392
|
+
6. Double-check markers and indentation before calling tool
|
|
393
|
+
|
|
394
|
+
**If Your Patch Fails:**
|
|
395
|
+
- You didn't read the file first, OR
|
|
396
|
+
- Context lines don't match file character-for-character
|
|
397
|
+
- Solution: Read file AGAIN, copy exact lines
|
|
398
|
+
- After 2 failures: switch to `edit` tool instead
|
|
399
|
+
|
|
369
400
|
## `update_plan`
|
|
370
401
|
|
|
371
402
|
A tool named `update_plan` is available to you. You can use it to keep an up‑to‑date, step‑by‑step plan for the task.
|