|
1 | 1 | # Editing with hpatch |
2 | 2 |
|
3 | | -Inspect the relevant source, then send a compact script to `hpatch translate`. Pass its |
4 | | -stdout directly to the native `apply_patch` tool inside the same wrapper operation: |
| 3 | +Inspect the relevant source before selecting it. Run `hpatch translate` from the |
| 4 | +workspace root with paths relative to that root, then pass its stdout directly to the |
| 5 | +native `apply_patch` tool inside one model tool call or equivalent host orchestration |
| 6 | +boundary: |
5 | 7 |
|
6 | 8 | ```text |
7 | | -translated = exec("hpatch translate", stdin=SCRIPT) |
8 | | -if translated failed: return its error |
9 | | -apply_patch(translated.stdout) |
| 9 | +translated = exec("hpatch translate", stdin=SCRIPT, cwd=WORKSPACE_ROOT) |
| 10 | +if translated failed: |
| 11 | + return its diagnostic |
| 12 | +
|
| 13 | +applied = native_apply_patch(translated.stdout) |
| 14 | +if applied failed: |
| 15 | + return its diagnostic |
| 16 | +
|
| 17 | +reread the intended workspace paths |
| 18 | +run focused validation |
10 | 19 | ``` |
11 | 20 |
|
12 | | -Keep the translated patch internal. Do not invoke a shell executable named |
13 | | -`apply_patch`, return the patch to the model, or ask the model to repeat it. The native |
14 | | -tool call is what applies the change and lets the harness display its diff. |
| 21 | +The boundary requirement means the translated patch stays internal: do not invoke a |
| 22 | +shell executable named `apply_patch`, return the patch to the model, or ask the model |
| 23 | +to repeat it. Use a non-PTY stdin facility when the wrapper provides one. Keep stdout |
| 24 | +patch-only and propagate diagnostics from either stage. |
| 25 | + |
| 26 | +The available native `apply_patch` resolves relative paths from its fixed workspace |
| 27 | +root and has no working-directory override. The working directory used by |
| 28 | +`hpatch translate` is not embedded in its textual patch or transferred to the native |
| 29 | +tool. Therefore: |
| 30 | + |
| 31 | +- use workspace-relative paths and normally run translation from the workspace root; |
| 32 | +- do not expect an absolute path or translator working directory to patch outside it; |
| 33 | +- for an artifact ultimately required in a system temporary directory, edit and |
| 34 | + validate it in an ignored workspace-local staging directory, then relocate the |
| 35 | + completed artifact; |
| 36 | +- treat an opaque successful native result such as `{}` as no operation summary: |
| 37 | + reread the intended files and validate behavior; |
| 38 | +- treat an absent, malformed, unrelated, or unknown/future native result as |
| 39 | + unconfirmed rather than inventing hpatch semantics. |
15 | 40 |
|
16 | 41 | ```text |
17 | 42 | in PATH select an existing file |
18 | 43 | new PATH select a new empty file at cursor 0:0 |
19 | 44 | mv PATH move the selected file |
20 | | -rm remove the selected file |
21 | | -sel LINE START:END select inclusive one-based columns |
22 | | -tsel LINE OCCURRENCE "TEXT" select a literal; -1 means the last occurrence |
23 | | -rsel START:END select inclusive complete lines |
| 45 | +rm remove the selected file and clear active state |
| 46 | +sel LINE START:END select inclusive one-based Unicode columns |
| 47 | +tsel LINE OCCURRENCE "TEXT" select a nonempty one-line literal; -1 is last |
| 48 | +rsel START:END select inclusive complete logical lines |
24 | 49 | type "TEXT" replace the selection or insert at the cursor |
25 | 50 | del delete the selection |
26 | 51 | dup duplicate the selection |
27 | 52 | ``` |
28 | 53 |
|
29 | | -Commands run sequentially and may switch files with multiple `in` commands. `type` and |
30 | | -`tsel` operands are JSON strings. |
| 54 | +Commands run sequentially against the current in-memory contents and may switch files |
| 55 | +with multiple `in` commands. Returning to a file resets its cursor to `0:0` but |
| 56 | +retains pending edits. Every selection replaces the prior cursor or selection. |
| 57 | +`type` and `del` leave the cursor immediately after the inserted text or at the |
| 58 | +deleted selection's start. `dup` selects the new copy. |
| 59 | + |
| 60 | +`rsel` owns each selected line's terminator when present. Replacing complete lines |
| 61 | +with `type` therefore requires the replacement string to include any desired final |
| 62 | +newline. Earlier edits immediately affect later line numbers: |
| 63 | + |
| 64 | +```text |
| 65 | +in example.go |
| 66 | +rsel 2:3 |
| 67 | +type "replacement line\n" |
| 68 | +tsel 3 1 "later" |
| 69 | +type "updated" |
| 70 | +``` |
| 71 | + |
| 72 | +Here the original lines 2–3, including line 3's terminator, are replaced. The later |
| 73 | +`tsel` observes the resulting current line 3, not the original file. |
| 74 | + |
| 75 | +`type` and `tsel` operands are JSON strings. Generate operands with a JSON encoder |
| 76 | +instead of hand-escaping multiline text, quotes, backslashes, template literals, or |
| 77 | +Unicode. A `type` value may contain line terminators; a `tsel` value may not. |
| 78 | + |
| 79 | +Additional examples: |
| 80 | + |
| 81 | +```text |
| 82 | +new note.txt |
| 83 | +type "foo" |
| 84 | +type " " |
| 85 | +type "bar\n" |
| 86 | +
|
| 87 | +in config.txt |
| 88 | +tsel 4 -1 "old" |
| 89 | +type "new" |
| 90 | +
|
| 91 | +in logs.txt |
| 92 | +tsel 8 1 "debug" |
| 93 | +del |
| 94 | +
|
| 95 | +in block.txt |
| 96 | +rsel 2:4 |
| 97 | +dup |
| 98 | +
|
| 99 | +in old.txt |
| 100 | +mv current.txt |
| 101 | +
|
| 102 | +in obsolete.txt |
| 103 | +rm |
| 104 | +``` |
0 commit comments