dsh-hashline-edit 0.4.1 → 0.4.3
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 +5 -6
- package/cordis.patch.yml +4 -3
- package/lib/index.js +48 -74
- package/lib/types/dsh/guidance.d.ts +2 -2
- package/lib/types/dsh/index.d.ts +11 -7
- package/lib/types/src/hashline/resolve.d.ts +2 -1
- package/lib/types/src/payload-contract.d.ts +5 -1
- package/package.json +2 -3
- package/prompts/apply-patch-guidelines.md +10 -0
- package/prompts/apply-patch.md +1 -0
- package/prompts/edit-guidelines.md +8 -0
- package/prompts/edit.md +1 -0
- package/prompts/read-description.md +1 -0
- package/prompts/read-guidelines.md +5 -0
- package/prompts/write-guidelines.md +3 -0
- package/src/hashline/resolve.ts +8 -4
- package/src/payload-contract.ts +45 -20
- package/src/prompts.ts +6 -21
- package/lib/types/src/prompts-bundled.d.ts +0 -11
- package/src/prompts-bundled.ts +0 -44
package/README.md
CHANGED
|
@@ -57,7 +57,7 @@ pnpm pack
|
|
|
57
57
|
dsh plugin add ./dsh-hashline-edit-0.2.1.tgz
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-
The bundle ships `lib/index.js` plus `lib/types/**`; `prompts/` must sit next to `lib/` in the installed package (it is in `files`).
|
|
60
|
+
The bundle ships `lib/index.js` plus `lib/types/**`; `prompts/` must sit next to `lib/` in the installed package (it is in `files`). Prompt prose is read from `prompts/*.md` when the plugin loads, so editing those files and loading the rebuilt plugin changes what the model reads — there is no separate override file.
|
|
61
61
|
|
|
62
62
|
## The anchored model
|
|
63
63
|
|
|
@@ -73,7 +73,7 @@ p9r│ return total;
|
|
|
73
73
|
a1b│}
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
Copy only the build-width characters before `│` (3 by default; 4 in a 4-char build). `edit` passes them as `anchor_from
|
|
76
|
+
Copy only the build-width characters before `│` (3 by default; 4 in a 4-char build). `edit` passes them as `anchor_from`, plus `anchor_to` only when the range spans more than one line; `apply_patch` writes them as `@@<anchor>` for one row or `@@<from>-<to>` for a span. Both forms target an inclusive served range; a successful call returns a diff whose modified and context rows carry fresh anchors:
|
|
77
77
|
|
|
78
78
|
```text
|
|
79
79
|
- szJ│ let total = 0;
|
|
@@ -177,17 +177,16 @@ The anchored range writer. One call targets one file and carries one ordered `ed
|
|
|
177
177
|
{
|
|
178
178
|
"file": "src/config.ts",
|
|
179
179
|
"edits": [
|
|
180
|
-
{ "anchor_from": "szJ", "
|
|
180
|
+
{ "anchor_from": "szJ", "text": " retries: 5," },
|
|
181
181
|
{
|
|
182
182
|
"anchor_from": "d2x",
|
|
183
|
-
"anchor_to": "d2x",
|
|
184
183
|
"text_ref": { "anchor_from": "a1b", "anchor_to": "a1b", "file": "src/defaults.ts", "mode": "copy" }
|
|
185
184
|
}
|
|
186
185
|
]
|
|
187
186
|
}
|
|
188
187
|
```
|
|
189
188
|
|
|
190
|
-
Each item names
|
|
189
|
+
Each item names one inclusive bare anchor, `anchor_from`, and exactly one payload: `text` is the replacement content (`""` deletes the range), or `text_ref` is a served span read in `copy` or `cut` mode. A multi-line range adds the optional second anchor `anchor_to` (the last line, inclusive), so a single-line edit omits it and the target is the one line `anchor_from` names — spelling `anchor_from === anchor_to` still validates. A `text_ref` span always names both bounds. Optional `at` is `in-place` by default; `before` / `after` insert at the boundary and require a single-line target. A `text_ref.file` may name another served file as the copy/cut source, and `cut` also retires those source lines; the call's own target remains one file. The retired item-level `file` field is not part of the payload — an old multi-file item shape is refused with `[E_BAD_PAYLOAD]`, and a different target file is a different call. The call commits as one transaction and writes one undo record for recovery; undo is not exposed as a model tool.
|
|
191
190
|
|
|
192
191
|
## Shell view
|
|
193
192
|
|
|
@@ -278,7 +277,7 @@ Every refusal carries a bracketed code; `src/domain-errors.ts` is the source of
|
|
|
278
277
|
| `[W_SERVED_PREFIX_MISMATCH]` | A served prefix does not match; fresh rows were served. |
|
|
279
278
|
| `[W_UNICODE_LITERAL]` | A Unicode literal in the payload looks like a copied anchor. |
|
|
280
279
|
|
|
281
|
-
Range-family rejections render their current-row evidence exactly once: the headline plus one `Current range (fresh read):` block. A single-line anchor pair
|
|
280
|
+
Range-family rejections render their current-row evidence exactly once: the headline plus one `Current range (fresh read):` block. A single-line target — whether written as one anchor or as a pair with `anchor_from` equal to `anchor_to` — is listed once, and the distinct-anchor lists de-duplicate. `[E_UNKNOWN_ANCHOR]` and `[E_FOREIGN_ANCHOR]` also name each distinct anchor once.
|
|
282
281
|
|
|
283
282
|
## Current limitations
|
|
284
283
|
|
package/cordis.patch.yml
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
# Installed via `dsh plugin --profile <name> add dsh-hashline-edit` (or a
|
|
3
3
|
# profile's bundle list). The row carries three feature switches, edited on the
|
|
4
4
|
# Plugins page: `read` shadows the built-in read, `applyPatch` exposes the
|
|
5
|
-
# `apply_patch` tool, `edit` exposes the anchored range `edit` tool.
|
|
6
|
-
#
|
|
7
|
-
# `
|
|
5
|
+
# `apply_patch` tool, `edit` exposes the anchored range `edit` tool. `edit` hides
|
|
6
|
+
# the native `edit`/`str_replace_editor` (and, in an edit-only profile, the native
|
|
7
|
+
# `apply_patch`); `apply_patch` hides only the native `apply_patch`; the built-in
|
|
8
|
+
# `write` stays visible;
|
|
8
9
|
# both may be on at once. Each toggles live: the host reloads this row and the
|
|
9
10
|
# plugin re-mounts every live agent. Defaults: read on, apply_patch on, edit off.
|
|
10
11
|
- insert:
|
package/lib/index.js
CHANGED
|
@@ -1427,6 +1427,10 @@ function hiddenNames(config) {
|
|
|
1427
1427
|
names.push(name2);
|
|
1428
1428
|
}
|
|
1429
1429
|
}
|
|
1430
|
+
if (readBooleanSource(config.hideNativeWriteTools, true) && config.toolNames.includes("edit") && !config.toolNames.includes("apply_patch") && !seen.has("apply_patch")) {
|
|
1431
|
+
seen.add("apply_patch");
|
|
1432
|
+
names.push("apply_patch");
|
|
1433
|
+
}
|
|
1430
1434
|
for (const name2 of sanitizeHiddenToolNames(config)) {
|
|
1431
1435
|
if (seen.has(name2)) continue;
|
|
1432
1436
|
seen.add(name2);
|
|
@@ -1910,42 +1914,8 @@ function commandOf(args) {
|
|
|
1910
1914
|
return typeof command === "string" && command.length > 0 ? command : void 0;
|
|
1911
1915
|
}
|
|
1912
1916
|
|
|
1913
|
-
// src/prompts-bundled.ts
|
|
1914
|
-
var BUNDLED_PROMPTS = {
|
|
1915
|
-
"apply-patch-guidelines.md": `- apply_patch: one call = one transaction; batch related changes and split only when an earlier RESULT decides the next.
|
|
1916
|
-
- apply_patch: an identity hunk is \`@@<anchor>\` or \`@@<from>-<to>\` followed by \`+\` lines; an empty body deletes every row of the span. Minimal shape: \`*** Begin Patch\` / \`*** Update File: <path>\` / \`@@<anchor>\` / \`+<new line>\` / \`*** End Patch\`.
|
|
1917
|
-
- apply_patch: an identity anchor is the bare \`{{HASH_LEN}}\` characters before \`\u2502\` in a served row \u2014 never the whole row, never a line number; syntax is \`@@\` plus the anchor with no space between them.
|
|
1918
|
-
- apply_patch: served beats text \u2014 when this session served the file, an identity hunk is REQUIRED, not preferred; a fragment hunk is limited to a file or region this session never served, and a text match never licenses one.
|
|
1919
|
-
- apply_patch: anchors in hand mean no re-read \u2014 apply the identity hunk to the served rows instead of reading the file again; a rejection already serves the current rows back.
|
|
1920
|
-
- apply_patch: change only what the task changes \u2014 an identity hunk carries \`+\` lines for the edited region and no unchanged rows, so a small edit stays a small patch.
|
|
1921
|
-
- apply_patch: for a one-line or single-region change to a served file, one identity hunk is the whole call; do not switch to \`write\` because the file content is already in context.
|
|
1922
|
-
- apply_patch: hunks are validated against the pre-call file in order (overlapping ranges reject the batch); a fragment hunk must match exactly once \u2014 \`E_UNIQUE_MATCH\` writes nothing \u2014 while a stale or unknown anchor is rejected; both serve the current rows back.
|
|
1923
|
-
- apply_patch: a rejection writes nothing; retry from the served \`HASH\u2502content\` rows without another \`read\`.
|
|
1924
|
-
- apply_patch: success returns fresh anchors for the changed lines; a patch carries only the changed rows, so a new file or an every-line rewrite stays with \`write\`.`,
|
|
1925
|
-
"apply-patch.md": `Change an existing file with \`apply_patch\`; it is the default for every change to an existing file, including a one-line change. \`write\` is only for a new file or for a rewrite where every line changes; a partial change is never a whole-file replacement. Pass the complete patch as \`input\`. When this session already served the file \u2014 through \`read\`, \`grep\`, a \`bash\` view, or a previous \`apply_patch\` result \u2014 every hunk MUST be an identity hunk on a served row: reuse the bare anchor before \`\u2502\` (\`@@<anchor>\` to replace that row, \`@@<from>-<to>\` to replace a span whose anchors you hold), and add only the changed \`+\` lines rather than re-emitting rows you are not changing. Anchors already in hand need no re-read: patch them directly instead of calling \`read\` again. A fragment hunk (\`@@\` or \`@@ <context>\` matched by file text) is legal only for a file or region this session never served; if the file was served, a fragment hunk is the wrong tool even when the text matches, because it discards the anchor check that proves you edited the version you actually saw.`,
|
|
1926
|
-
"edit-guidelines.md": `- edit: one call = one file and one transaction; N changes in that file are N items in the same call, and a different file is a different call.
|
|
1927
|
-
- edit: every item carries exactly one payload \u2014 \`text\` (literal content; \`""\` deletes the range) or \`text_ref\` (\`mode: "copy"\` or \`"cut"\`); never both, never neither.
|
|
1928
|
-
- edit: anchors are the bare \`{{HASH_LEN}}\` characters before \`\u2502\` in served output \u2014 never the whole row, never a line number.
|
|
1929
|
-
- edit: both anchors are inclusive; \`at\` is \`in-place\` by default, while \`before\`/\`after\` insert at the boundary and require a single-line resolved target.
|
|
1930
|
-
- edit: \`text_ref\` copies a served span's bytes; \`"cut"\` also retires the source lines, and \`text_ref.file\` may name another served file as that source.
|
|
1931
|
-
- edit: anchors already in hand need no re-read \u2014 a rejection serves the current rows back, so repair from the \`HASH\u2502content\` rows instead of guessing.
|
|
1932
|
-
- edit: change only what the task changes \u2014 one item per intended range, and unchanged rows stay out of the payload.
|
|
1933
|
-
- edit: a stale or unknown anchor, an overlapping range, or a payload reproducing served rows in \`general\` mode rejects the whole call and writes nothing; \`mode: "literal"\` declares reproduced rows as intended content.
|
|
1934
|
-
- edit: \`write\` is the whole-file path \u2014 a new file or an every-line rewrite stays with \`write\`, never an edit item.`,
|
|
1935
|
-
"edit.md": `Change a range of lines in one text file with hash anchors: pass \`file\` plus \`edits\`, one item per change. Each item names two inclusive bare anchors from served rows (\`anchor_from\`, \`anchor_to\`), exactly one payload \u2014 \`text\` for the replacement content (\`""\` deletes the range) or \`text_ref\` for a served span (\`mode: "copy"\` re-inserts its bytes, \`"cut"\` also retires them) \u2014 and optional \`at\` (\`in-place\` by default, \`before\`/\`after\` for a single-line target). One call edits one file; \`text_ref.file\` may name another served file as the copy/cut source. Anchors are the bare \`{{HASH_LEN}}\` characters before \`\u2502\` in served output. A rejected call writes nothing.`,
|
|
1936
|
-
"read-guidelines.md": `- read: call it for content the tool has never served you; a bash view or an auto-read already served the file, so \`apply_patch\` can use those rows.
|
|
1937
|
-
- read: batch several disjoint ranges in ONE call with \`windows\` \u2014 an offset/limit range, or \`around_anchor\` plus \`radius\` around a line this session served.
|
|
1938
|
-
- read: pass \`mode: "verbatim"\` when you will not \`apply_patch\` the file (config, docs, a README, skill files); otherwise omit it so the rows carry anchors.
|
|
1939
|
-
- read: a window of 61 lines or fewer may widen to its enclosing block; widened rows carry a \`[widened \u2026]\` note. Caps are 80 lines for code/Markdown and 40 for plain text.
|
|
1940
|
-
- read: read several files with parallel \`read\` calls, one file per call.
|
|
1941
|
-
- read: PNG/JPG/GIF/WebP files return as an image attachment for multimodal models; binary files and directories are refused.`,
|
|
1942
|
-
"read.md": `Reads a UTF-8 file as \`HASH\u2502content\` rows in served mode; each row's anchor is the \`apply_patch\` identity anchor for that line, and only the \`{{HASH_LEN}}\` characters before \`\u2502\` are the anchor. Pass \`mode: "verbatim"\` for plain text with no anchors when you will not apply_patch; verbatim is not capped by line budget. Batch disjoint ranges in one call with \`windows\` (offset/limit or \`around_anchor\`).`,
|
|
1943
|
-
"write-guidelines.md": `- write: the content is the complete file; a partial change stays an \`apply_patch\` identity hunk, even when the whole file is already in context.
|
|
1944
|
-
- write: never paste served \`HASH\u2502\` rows into the content; a line that reproduces a served row is refused as a served hash echo.`,
|
|
1945
|
-
"write.md": `Read a file before overwriting it with \`write\`. \`write\` replaces the whole file: use it for a new file, or for a rewrite where every line changes. For a change to part of an existing file, use \`apply_patch\` identity hunks on the served rows instead.`
|
|
1946
|
-
};
|
|
1947
|
-
|
|
1948
1917
|
// src/prompts.ts
|
|
1918
|
+
import { readFileSync } from "node:fs";
|
|
1949
1919
|
function applyReplacements(content, replacements) {
|
|
1950
1920
|
let out = content;
|
|
1951
1921
|
if (replacements) {
|
|
@@ -1955,27 +1925,19 @@ function applyReplacements(content, replacements) {
|
|
|
1955
1925
|
}
|
|
1956
1926
|
return out.split("{{HASH_LEN}}").join(String(HASH_LEN));
|
|
1957
1927
|
}
|
|
1958
|
-
function bundled(relativePath2) {
|
|
1959
|
-
const name2 = relativePath2.slice(relativePath2.lastIndexOf("/") + 1);
|
|
1960
|
-
const body = BUNDLED_PROMPTS[name2];
|
|
1961
|
-
if (body === void 0) {
|
|
1962
|
-
throw new Error(
|
|
1963
|
-
`unknown bundled prompt: ${relativePath2} (bundled: ${Object.keys(BUNDLED_PROMPTS).join(", ")})`
|
|
1964
|
-
);
|
|
1965
|
-
}
|
|
1966
|
-
return body;
|
|
1967
|
-
}
|
|
1968
1928
|
function loadP(relativePath2, replacements) {
|
|
1969
|
-
|
|
1929
|
+
const content = readFileSync(new URL(relativePath2, import.meta.url), "utf-8").trim();
|
|
1930
|
+
return applyReplacements(content, replacements);
|
|
1970
1931
|
}
|
|
1971
1932
|
function loadGuide(relativePath2, replacements) {
|
|
1972
|
-
|
|
1933
|
+
const content = readFileSync(new URL(relativePath2, import.meta.url), "utf-8");
|
|
1934
|
+
return applyReplacements(content, replacements).split("\n").map((line) => line.trim()).filter((line) => line.startsWith("- ")).map((line) => line.slice(2));
|
|
1973
1935
|
}
|
|
1974
1936
|
|
|
1975
1937
|
// dsh/guidance.ts
|
|
1976
1938
|
function guidanceText(intro, lines) {
|
|
1977
1939
|
const bullets = lines.map((line) => `- ${line}`).join("\n");
|
|
1978
|
-
return `${intro}
|
|
1940
|
+
return intro.trim() === "" ? bullets : `${intro}
|
|
1979
1941
|
|
|
1980
1942
|
${bullets}`;
|
|
1981
1943
|
}
|
|
@@ -1984,28 +1946,25 @@ var GUIDANCE_SECTIONS = [
|
|
|
1984
1946
|
name: "tool:read",
|
|
1985
1947
|
defaultOrder: 1100,
|
|
1986
1948
|
orderName: "TOOL_READ",
|
|
1987
|
-
renderDefault: () => guidanceText(
|
|
1949
|
+
renderDefault: () => guidanceText("", loadGuide("../prompts/read-guidelines.md"))
|
|
1988
1950
|
},
|
|
1989
1951
|
{
|
|
1990
1952
|
name: "tool:write",
|
|
1991
1953
|
defaultOrder: 1300,
|
|
1992
1954
|
orderName: "TOOL_WRITE",
|
|
1993
|
-
renderDefault: () => guidanceText(
|
|
1955
|
+
renderDefault: () => guidanceText("", loadGuide("../prompts/write-guidelines.md"))
|
|
1994
1956
|
},
|
|
1995
1957
|
{
|
|
1996
1958
|
name: "tool:apply_patch",
|
|
1997
1959
|
defaultOrder: 1350,
|
|
1998
1960
|
orderName: "TOOL_APPLY_PATCH",
|
|
1999
|
-
renderDefault: () => guidanceText(
|
|
2000
|
-
loadP("../prompts/apply-patch.md"),
|
|
2001
|
-
loadGuide("../prompts/apply-patch-guidelines.md")
|
|
2002
|
-
)
|
|
1961
|
+
renderDefault: () => guidanceText("", loadGuide("../prompts/apply-patch-guidelines.md"))
|
|
2003
1962
|
},
|
|
2004
1963
|
{
|
|
2005
1964
|
name: "tool:edit",
|
|
2006
1965
|
defaultOrder: 1360,
|
|
2007
1966
|
orderName: "TOOL_EDIT",
|
|
2008
|
-
renderDefault: () => guidanceText(
|
|
1967
|
+
renderDefault: () => guidanceText("", loadGuide("../prompts/edit-guidelines.md"))
|
|
2009
1968
|
}
|
|
2010
1969
|
];
|
|
2011
1970
|
var SECTION_BY_NAME = new Map(GUIDANCE_SECTIONS.map((section) => [section.name, section]));
|
|
@@ -6476,9 +6435,9 @@ function assertItem(edit) {
|
|
|
6476
6435
|
message: '"text" must be a string with \\n line separators, not an array. Multiple lines belong in one string, such as "line1\\nline2". An empty string deletes a range. Nothing was written.'
|
|
6477
6436
|
});
|
|
6478
6437
|
}
|
|
6479
|
-
if (typeof edit.anchor_from !== "string"
|
|
6438
|
+
if (typeof edit.anchor_from !== "string") {
|
|
6480
6439
|
throw new DomainError("E_BAD_PAYLOAD", {
|
|
6481
|
-
message: `The edit requires "anchor_from"
|
|
6440
|
+
message: `The edit requires an "anchor_from" anchor string (a bare ${HASH_LEN}-char hash from served output). Nothing was written.`
|
|
6482
6441
|
});
|
|
6483
6442
|
}
|
|
6484
6443
|
if ("placement" in edit && !isEditPlacement(edit.placement)) {
|
|
@@ -6508,7 +6467,7 @@ function firstHashFromBlock(block) {
|
|
|
6508
6467
|
function resEdit(edit) {
|
|
6509
6468
|
assertItem(edit);
|
|
6510
6469
|
const editLines = parseText(edit.text);
|
|
6511
|
-
const bounds = [edit.anchor_from, edit.anchor_to].map((ref) => {
|
|
6470
|
+
const bounds = [edit.anchor_from, edit.anchor_to ?? edit.anchor_from].map((ref) => {
|
|
6512
6471
|
const trimmed = ref.trim();
|
|
6513
6472
|
if (trimmed.includes("\n")) {
|
|
6514
6473
|
const hash = firstHashFromBlock(trimmed);
|
|
@@ -8419,7 +8378,7 @@ function findEnclosingScope(root, requestedStart, requestedEnd, scopeKinds) {
|
|
|
8419
8378
|
|
|
8420
8379
|
// src/read-expansion/grammar-cache.ts
|
|
8421
8380
|
import { createHash } from "node:crypto";
|
|
8422
|
-
import { existsSync as existsSync3, mkdirSync as mkdirSync5, readFileSync, renameSync as renameSync4, rmSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
8381
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync5, readFileSync as readFileSync2, renameSync as renameSync4, rmSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
8423
8382
|
import { dirname as dirname7, join as join8 } from "node:path";
|
|
8424
8383
|
|
|
8425
8384
|
// src/read-expansion/grammar-manifest.ts
|
|
@@ -8514,7 +8473,7 @@ function readVerifiedCache(path, expected) {
|
|
|
8514
8473
|
if (!existsSync3(path)) return void 0;
|
|
8515
8474
|
let bytes;
|
|
8516
8475
|
try {
|
|
8517
|
-
bytes =
|
|
8476
|
+
bytes = readFileSync2(path);
|
|
8518
8477
|
} catch {
|
|
8519
8478
|
discardCachedGrammar(path);
|
|
8520
8479
|
return void 0;
|
|
@@ -9794,7 +9753,7 @@ import {
|
|
|
9794
9753
|
constants as constants5,
|
|
9795
9754
|
existsSync as existsSync4,
|
|
9796
9755
|
mkdirSync as mkdirSync6,
|
|
9797
|
-
readFileSync as
|
|
9756
|
+
readFileSync as readFileSync3,
|
|
9798
9757
|
renameSync as renameSync5,
|
|
9799
9758
|
unlinkSync as unlinkSync2,
|
|
9800
9759
|
writeFileSync as writeFileSync3
|
|
@@ -9866,7 +9825,7 @@ function reasonOf2(error) {
|
|
|
9866
9825
|
function readConfigObject(path, onWarn) {
|
|
9867
9826
|
let raw;
|
|
9868
9827
|
try {
|
|
9869
|
-
raw =
|
|
9828
|
+
raw = readFileSync3(path, "utf-8");
|
|
9870
9829
|
} catch (error) {
|
|
9871
9830
|
if (!isMissingFile(error)) onWarn?.(`cannot read config ${path}: ${reasonOf2(error)}`);
|
|
9872
9831
|
return void 0;
|
|
@@ -10017,7 +9976,7 @@ async function publishPluginWrites(deps, requestPaths, ctx, exec) {
|
|
|
10017
9976
|
|
|
10018
9977
|
// dsh/tools/read.ts
|
|
10019
9978
|
var READ_TOOL_NAME = "read";
|
|
10020
|
-
var READ_DESCRIPTION =
|
|
9979
|
+
var READ_DESCRIPTION = loadP("../prompts/read-description.md");
|
|
10021
9980
|
var RENDER_MODES = ["served", "verbatim"];
|
|
10022
9981
|
function createDefaultReadSpanProvider(io) {
|
|
10023
9982
|
const readTextFile = io === void 0 ? void 0 : async (path) => {
|
|
@@ -15137,8 +15096,8 @@ function isRec(value) {
|
|
|
15137
15096
|
function isNormalizedEdit(input) {
|
|
15138
15097
|
return isRec(input) && input[normalizedEdit] === true;
|
|
15139
15098
|
}
|
|
15140
|
-
var ITEM_SHAPE = "an item is exactly { anchor_from, anchor_to
|
|
15141
|
-
var EDIT_PAYLOAD_HINT = `Edit must be called with exactly one payload per item, and each call edits ONE file. Use the canonical payload {"file": file, "edits": [{ "anchor_from": anchor_from, "anchor_to": anchor_to, "text": text }, ...], "mode"?: "general" | "literal"}: "file" is the text file to edit (a non-empty string, never a directory); each item names
|
|
15099
|
+
var ITEM_SHAPE = "an item is exactly { anchor_from, anchor_to?, text[, at] } or { anchor_from, anchor_to?, text_ref[, at] }";
|
|
15100
|
+
var EDIT_PAYLOAD_HINT = `Edit must be called with exactly one payload per item, and each call edits ONE file. Use the canonical payload {"file": file, "edits": [{ "anchor_from": anchor_from, "anchor_to": anchor_to, "text": text }, ...], "mode"?: "general" | "literal"}: "file" is the text file to edit (a non-empty string, never a directory); each item names the bare-${HASH_LEN}-char anchor "anchor_from" (the first line of the range, and the whole range of a single-line edit), an optional second bare anchor "anchor_to" (the last line of a multi-line range; omit it to edit "anchor_from" alone) and exactly one payload \u2014 "text" (bare replacement content; an empty string deletes the range) or "text_ref" ({ anchor_from, anchor_to, file?, mode (required): "copy" | "cut" } \u2014 the served span's bytes, whose two bounds are always required, and whose "file" may name another served file as the copy/cut source); optional "at" is "in-place" (default), "before" or "after" (single-line resolved target only); optional "mode" is "general" (default, reproduced served rows are refused) or "literal" (declared literal content).`;
|
|
15142
15101
|
function describeReceived(input) {
|
|
15143
15102
|
if (input === void 0) return "Received no arguments.";
|
|
15144
15103
|
if (input === null) return "Received null.";
|
|
@@ -15188,9 +15147,13 @@ function sanitizePath(value) {
|
|
|
15188
15147
|
}
|
|
15189
15148
|
var ITEM_KEY_SETS = [
|
|
15190
15149
|
["anchor_from", "anchor_to", "text"],
|
|
15150
|
+
["anchor_from", "text"],
|
|
15191
15151
|
["anchor_from", "anchor_to", "at", "text"],
|
|
15152
|
+
["anchor_from", "at", "text"],
|
|
15192
15153
|
["anchor_from", "anchor_to", "text_ref"],
|
|
15193
|
-
["anchor_from", "
|
|
15154
|
+
["anchor_from", "text_ref"],
|
|
15155
|
+
["anchor_from", "anchor_to", "at", "text_ref"],
|
|
15156
|
+
["anchor_from", "at", "text_ref"]
|
|
15194
15157
|
];
|
|
15195
15158
|
function keySetEquals(keys, shape) {
|
|
15196
15159
|
if (keys.size !== shape.length) return false;
|
|
@@ -15205,7 +15168,7 @@ function isAbsentValue(value) {
|
|
|
15205
15168
|
}
|
|
15206
15169
|
function foldAbsentSlots(item) {
|
|
15207
15170
|
const folded = { ...item };
|
|
15208
|
-
for (const key of ["text", "text_ref", "at"]) {
|
|
15171
|
+
for (const key of ["text", "text_ref", "at", "anchor_to"]) {
|
|
15209
15172
|
if (isAbsentValue(folded[key])) delete folded[key];
|
|
15210
15173
|
}
|
|
15211
15174
|
if (isRec(folded.text_ref)) {
|
|
@@ -15228,7 +15191,7 @@ function analyzeItem(value, index) {
|
|
|
15228
15191
|
}
|
|
15229
15192
|
const payloadKeys = /* @__PURE__ */ new Set(["anchor_from", "anchor_to", "at", "text", "text_ref"]);
|
|
15230
15193
|
const offending = [...keys].filter((key) => !payloadKeys.has(key));
|
|
15231
|
-
const missing = ["anchor_from"
|
|
15194
|
+
const missing = ["anchor_from"].filter((key) => !keys.has(key));
|
|
15232
15195
|
const clauses = [];
|
|
15233
15196
|
if (offending.length > 0) clauses.push(`unsupported field(s) ${quoted(offending)}`);
|
|
15234
15197
|
if (missing.length > 0) clauses.push(`missing required field(s) ${quoted(missing)}`);
|
|
@@ -15239,8 +15202,11 @@ function analyzeItem(value, index) {
|
|
|
15239
15202
|
return `edit[${index}] has ${clauses.join(";")} (${ITEM_SHAPE}).`;
|
|
15240
15203
|
}
|
|
15241
15204
|
const { anchor_from, anchor_to, at, text, text_ref } = value;
|
|
15242
|
-
if (typeof anchor_from !== "string"
|
|
15243
|
-
return `edit[${index}] "anchor_from"
|
|
15205
|
+
if (typeof anchor_from !== "string") {
|
|
15206
|
+
return `edit[${index}] "anchor_from" must be a bare ${HASH_LEN}-char hash anchor copied from served output (before \u2502): it is the one anchor every item needs \u2014 an omitted "anchor_to" makes it the whole single-line target.`;
|
|
15207
|
+
}
|
|
15208
|
+
if (anchor_to !== void 0 && typeof anchor_to !== "string") {
|
|
15209
|
+
return `edit[${index}] "anchor_to" must be a bare ${HASH_LEN}-char hash anchor copied from served output (before \u2502) or omitted to target the single line "anchor_from" names.`;
|
|
15244
15210
|
}
|
|
15245
15211
|
if (!isAbsentValue(at)) {
|
|
15246
15212
|
if (at === "in_place") {
|
|
@@ -15349,8 +15315,12 @@ function analyzeRequest(input) {
|
|
|
15349
15315
|
function normalizedItemFrom(item) {
|
|
15350
15316
|
const at = item.at ?? "in-place";
|
|
15351
15317
|
const payload = item.text_ref !== void 0 ? { kind: "reference", span: item.text_ref, mode: item.text_ref.mode } : item.text === "" ? { kind: "empty" } : { kind: "literal", text: item.text ?? "" };
|
|
15318
|
+
const target = {
|
|
15319
|
+
anchor_from: item.anchor_from,
|
|
15320
|
+
anchor_to: item.anchor_to ?? item.anchor_from
|
|
15321
|
+
};
|
|
15352
15322
|
return {
|
|
15353
|
-
target
|
|
15323
|
+
target,
|
|
15354
15324
|
at,
|
|
15355
15325
|
payload
|
|
15356
15326
|
};
|
|
@@ -15620,9 +15590,13 @@ var EDIT_PARAMETERS = {
|
|
|
15620
15590
|
description: "Bare hash anchor of the first range line (inclusive)"
|
|
15621
15591
|
},
|
|
15622
15592
|
anchor_to: {
|
|
15623
|
-
|
|
15624
|
-
|
|
15625
|
-
|
|
15593
|
+
oneOf: [
|
|
15594
|
+
{
|
|
15595
|
+
type: "string",
|
|
15596
|
+
description: "Bare hash anchor of the last range line (inclusive); omit it to edit the single line anchor_from names"
|
|
15597
|
+
},
|
|
15598
|
+
{ type: "null" }
|
|
15599
|
+
]
|
|
15626
15600
|
},
|
|
15627
15601
|
at: {
|
|
15628
15602
|
oneOf: [
|
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
* pi-side guidance (`prompts/*.md`) loaded through `src/prompts.ts`, so the dsh
|
|
8
8
|
* and pi hosts cannot drift about the tool contract.
|
|
9
9
|
*
|
|
10
|
-
* Injection is STATIC: each section registers its
|
|
10
|
+
* Injection is STATIC: each section registers its prompt text once, the
|
|
11
11
|
* same way the agent-teams plugin registers its constant `systemPrompt` section.
|
|
12
12
|
* There are no runtime user-override files and no per-step re-resolution — the
|
|
13
|
-
* guidance a session sees is exactly what the installed
|
|
13
|
+
* guidance a session sees is exactly what the installed package ships, so the
|
|
14
14
|
* single control point is `prompts/*.md` + a rebuild/upgrade.
|
|
15
15
|
*
|
|
16
16
|
* @module dsh-hashline-edit/guidance
|
package/lib/types/dsh/index.d.ts
CHANGED
|
@@ -4,10 +4,11 @@
|
|
|
4
4
|
* On every `agent/created`, install the hashline tools on the AGENT's
|
|
5
5
|
* own scope layer so they shadow the preset's built-in `read` for that
|
|
6
6
|
* agent (nearest layer wins in dsh's tool registry) and unwind automatically
|
|
7
|
-
* when the agent is disposed. The native `edit
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* when the agent is disposed. The native `edit`/`str_replace_editor` are hidden
|
|
8
|
+
* only while this plugin's own `edit` registers, and the native `apply_patch`
|
|
9
|
+
* only while this plugin's own `apply_patch` registers — in an edit-only profile
|
|
10
|
+
* the native `apply_patch` is hidden too — so the plugin's own tools own precise
|
|
11
|
+
* and batch text changes; the host's built-in `write` stays visible and
|
|
11
12
|
* is integrated by the write hook.
|
|
12
13
|
*
|
|
13
14
|
* `inject` deliberately lists only `tools` and `systemPrompt`: the read-path
|
|
@@ -49,9 +50,12 @@ export interface PluginConfig {
|
|
|
49
50
|
*/
|
|
50
51
|
read?: boolean;
|
|
51
52
|
/**
|
|
52
|
-
* Expose the hashline `apply_patch` tool. Defaults to true.
|
|
53
|
-
*
|
|
54
|
-
*
|
|
53
|
+
* Expose the hashline `apply_patch` tool. Defaults to true. Registering it
|
|
54
|
+
* hides only the native `apply_patch` (by the same-name probe); the native
|
|
55
|
+
* `edit`/`str_replace_editor` stay visible unless this plugin's `edit` is on.
|
|
56
|
+
* With it off while `edit` is on, the hide layer also denies the native
|
|
57
|
+
* `apply_patch`, so the edit-only profile has no apply_patch surface. The
|
|
58
|
+
* host's `write` is never hidden.
|
|
55
59
|
*/
|
|
56
60
|
applyPatch?: boolean;
|
|
57
61
|
/**
|
|
@@ -120,7 +120,8 @@ export interface NEdit {
|
|
|
120
120
|
export type HTEdit = {
|
|
121
121
|
text: string;
|
|
122
122
|
anchor_from: string;
|
|
123
|
-
|
|
123
|
+
/** Last line of the target range; omitted means a single-line target — the line `anchor_from` names. */
|
|
124
|
+
anchor_to?: string;
|
|
124
125
|
placement?: EditPlacement;
|
|
125
126
|
source?: {
|
|
126
127
|
anchor_from: string;
|
|
@@ -28,7 +28,11 @@ export declare const _placementVocabularyAgreement: [
|
|
|
28
28
|
Placement extends EditPlacement ? true : never,
|
|
29
29
|
EditPlacement extends Placement ? true : never
|
|
30
30
|
];
|
|
31
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* The anchor pair an item targets — placement and payload are relative to its resolved span. A
|
|
33
|
+
* single-line target names its one line in both bounds (`anchor_from === anchor_to`): the engine
|
|
34
|
+
* keeps the closed pair so nothing downstream has to branch on an absent bound.
|
|
35
|
+
*/
|
|
32
36
|
type AnchorSpan = {
|
|
33
37
|
anchor_from: string;
|
|
34
38
|
anchor_to: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-hashline-edit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Hash-anchored read/apply_patch/edit tools for DeepSeek Harness (dsh). Every line gets a unique 3- or 4-char hash (A-Za-z0-9, configurable) that stays stable across edits; apply_patch and the anchored range edit tool are the file-mutation entry points, each committing its whole batch as one transaction.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"anchors",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"files": [
|
|
31
31
|
"lib",
|
|
32
32
|
"cordis.patch.yml",
|
|
33
|
+
"prompts",
|
|
33
34
|
"src",
|
|
34
35
|
"README.md",
|
|
35
36
|
"LICENSE"
|
|
@@ -60,8 +61,6 @@
|
|
|
60
61
|
"format:fix": "oxfmt .",
|
|
61
62
|
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.dsh.json",
|
|
62
63
|
"build": "node scripts/build-dsh.mjs",
|
|
63
|
-
"prompts:gen": "node scripts/gen-prompts.mjs",
|
|
64
|
-
"prompts:check": "node scripts/gen-prompts.mjs --check",
|
|
65
64
|
"prepublishOnly": "node scripts/build-dsh.mjs && pnpm run typecheck && pnpm run lint && pnpm run test:win",
|
|
66
65
|
"canon:audit": "node scripts/canon-churn-audit.mjs",
|
|
67
66
|
"prepack": "node scripts/build-dsh.mjs",
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
- apply_patch: one call = one transaction; batch related changes and split only when an earlier RESULT decides the next.
|
|
2
|
+
- apply_patch: an identity hunk is `@@<anchor>` or `@@<from>-<to>` followed by `+` lines; an empty body deletes every row of the span. Minimal shape: `*** Begin Patch` / `*** Update File: <path>` / `@@<anchor>` / `+<new line>` / `*** End Patch`.
|
|
3
|
+
- apply_patch: an identity anchor is the bare `{{HASH_LEN}}` characters before `│` in a served row — never the whole row, never a line number; syntax is `@@` plus the anchor with no space between them.
|
|
4
|
+
- apply_patch: served beats text — when this session served the file, an identity hunk is REQUIRED, not preferred; a fragment hunk is limited to a file or region this session never served, and a text match never licenses one.
|
|
5
|
+
- apply_patch: anchors in hand mean no re-read — apply the identity hunk to the served rows instead of reading the file again; a rejection already serves the current rows back.
|
|
6
|
+
- apply_patch: change only what the task changes — an identity hunk carries `+` lines for the edited region and no unchanged rows, so a small edit stays a small patch.
|
|
7
|
+
- apply_patch: for a one-line or single-region change to a served file, one identity hunk is the whole call; do not switch to `write` because the file content is already in context.
|
|
8
|
+
- apply_patch: hunks are validated against the pre-call file in order (overlapping ranges reject the batch); a fragment hunk must match exactly once — `E_UNIQUE_MATCH` writes nothing — while a stale or unknown anchor is rejected; both serve the current rows back.
|
|
9
|
+
- apply_patch: a rejection writes nothing; retry from the served `HASH│content` rows without another `read`.
|
|
10
|
+
- apply_patch: success returns fresh anchors for the changed lines; a patch carries only the changed rows, so a new file or an every-line rewrite stays with `write`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Change an existing file with `apply_patch`; it is the default for every change to an existing file, including a one-line change. `write` is only for a new file or for a rewrite where every line changes; a partial change is never a whole-file replacement. Pass the complete patch as `input`. When this session already served the file — through `read`, `grep`, a `bash` view, or a previous `apply_patch` result — every hunk MUST be an identity hunk on a served row: reuse the bare anchor before `│` (`@@<anchor>` to replace that row, `@@<from>-<to>` to replace a span whose anchors you hold), and add only the changed `+` lines rather than re-emitting rows you are not changing. Anchors already in hand need no re-read: patch them directly instead of calling `read` again. A fragment hunk (`@@` or `@@ <context>` matched by file text) is legal only for a file or region this session never served; if the file was served, a fragment hunk is the wrong tool even when the text matches, because it discards the anchor check that proves you edited the version you actually saw.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
- edit: one call targets one `file` in one transaction — batch all N changes to that file as N items in the same call, and use parallel `edit` calls for different target files.
|
|
2
|
+
- edit: every item carries exactly one payload — `text` (literal content; `""` deletes the range when `at` is `"in-place"`, and is a no-op for `"before"`/`"after"`) or `text_ref` (with required non-null `mode`: `"copy"` re-inserts the served span's bytes, `"cut"` also deletes the source lines); never both, never neither.
|
|
3
|
+
- edit: when `text_ref` uses `mode: "cut"` (same file or another served file via `text_ref.file`), the source lines are deleted automatically in the same correlated transaction — never send a second `edit` to delete the source span.
|
|
4
|
+
- edit: anchors are strictly the bare `{{HASH_LEN}}` characters before `│` in served `HASH│content` rows — never a line number, full row, or null. `anchor_from` is always required and names the first line of the range; `anchor_to` is optional and names the last line — omit it to edit the single line `anchor_from` names (the range is single-line without it, which is mandatory when `at` is `"before"` or `"after"`), and a `text_ref` span always names both bounds.
|
|
5
|
+
- edit: change only what the task changes — one item per intended range, keeping unchanged boundary rows and `HASH│` prefixes out of `text`.
|
|
6
|
+
- edit: a stale or unknown anchor, or an overlapping range, rejects the whole call and writes nothing. A `text` payload echoing served rows in default top-level `mode: "general"` is refused (`E_SUSPICIOUS_TEXT`); prefer `text_ref` to duplicate or move served spans, or set top-level `mode: "literal"` when `text` must intentionally reproduce served lines.
|
|
7
|
+
- edit: anchors already in context (from `read`, an auto-read, or an intercepted shell view that output `HASH│content` rows) need no re-read; an anchor rejection serves the current `HASH│content` rows back, so repair directly from those rows instead of re-reading or guessing.
|
|
8
|
+
- edit: `write` is the whole-file path — use `write` to create a new file or replace every line, never `edit`.
|
package/prompts/edit.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Change one or more line ranges in one target text file with hash anchors: pass `file`, `edits` (one item per change), and optional top-level `mode` (`"general"` by default, or `"literal"`). Each item names one inclusive bare `{{HASH_LEN}}`-character anchor from before `│` in served rows (`anchor_from`), an optional second one closing a multi-line range (`anchor_to` — omit it to edit the single line `anchor_from` names; pass the identical hash to spell a single line explicitly), optional `at` (`"in-place"` by default; `"before"` or `"after"` inserts at a single-line target), and exactly one payload — `text` for replacement content (`""` deletes the span when `at` is `"in-place"`, and is a no-op with `"before"`/`"after"`) or `text_ref` (`{ anchor_from, anchor_to, file?, mode: "copy" | "cut" }`, whose two bounds are always required, where required `mode` `"copy"` re-inserts the served span's bytes and `"cut"` also deletes the source lines). One call targets one `file`; only `text_ref.file` may reference another served file as the copy/cut source (with `"cut"` deleting those source lines in the same correlated transaction). A rejected call writes nothing.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Read a UTF-8 text file as hash-anchored `HASH│content` rows. Each anchor is the `edit` identity for that line, and only the bare `{{HASH_LEN}}` characters before `│` are the anchor. Served mode is the default; `mode: "verbatim"` returns plain text with no anchors and no line-budget cap (and does not register anchors for `edit`). Supports `offset`/`limit` paging and `windows` to batch up to 16 disjoint ranges (`offset`/`limit`, or `around_anchor` + `radius` for an anchor already served in this session) in one call. Image, binary, and directory paths are refused (`E_UNSUPPORTED_FILE`).
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
- read: call it only for spans not yet served as `HASH│content` rows in this session; an intercepted shell view (`cat`, `rg`, `Get-Content`, `Select-String`, etc.) or write-hook auto-read that output `HASH│content` rows already served the file for `edit`, whereas raw unanchored stdout is not served.
|
|
2
|
+
- read: batch up to 16 disjoint ranges of a file in ONE call with `windows` — each using `offset`/`limit`, or `around_anchor` plus `radius` around an anchor already served in this session (unserved anchors are omitted with a warning). Read multiple files with parallel `read` calls, one file per call.
|
|
3
|
+
- read: keep requested windows compact (61 lines or fewer) — the tool automatically widens compact windows to their enclosing syntax block (capped at 80 lines for code/Markdown and 40 for plain text, noted `[widened …]`), so do not manually pad `offset`/`limit` margins. Single-page output is also bounded by a byte budget.
|
|
4
|
+
- read: omit `mode` for any file you might later edit (code, configs, READMEs) so rows carry anchors; pass `mode: "verbatim"` only for strictly read-only reference files (e.g., skill files), as `verbatim` is uncapped and records no anchors for `edit`.
|
|
5
|
+
- read: `read` accepts UTF-8 text files only — images, binary files, and directories are refused (`E_UNSUPPORTED_FILE`).
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
- write: read the file before overwriting it; `write` replaces the whole file, so use it for a new file or a rewrite where every line changes.
|
|
2
|
+
- write: a partial change is not a whole-file rewrite even when the whole file is already in context — use `edit` on the served rows instead.
|
|
3
|
+
- write: never paste served `HASH│` rows into the content; a line that reproduces a served row is refused as a served hash echo.
|
package/src/hashline/resolve.ts
CHANGED
|
@@ -207,7 +207,8 @@ export interface NEdit {
|
|
|
207
207
|
export type HTEdit = {
|
|
208
208
|
text: string;
|
|
209
209
|
anchor_from: string;
|
|
210
|
-
|
|
210
|
+
/** Last line of the target range; omitted means a single-line target — the line `anchor_from` names. */
|
|
211
|
+
anchor_to?: string;
|
|
211
212
|
placement?: EditPlacement;
|
|
212
213
|
source?: { anchor_from: string; anchor_to: string; retire: boolean };
|
|
213
214
|
};
|
|
@@ -371,9 +372,9 @@ function assertItem(edit: Record<string, unknown>): void {
|
|
|
371
372
|
'"text" must be a string with \\n line separators, not an array. Multiple lines belong in one string, such as "line1\\nline2". An empty string deletes a range. Nothing was written.',
|
|
372
373
|
});
|
|
373
374
|
}
|
|
374
|
-
if (typeof edit.anchor_from !== "string"
|
|
375
|
+
if (typeof edit.anchor_from !== "string") {
|
|
375
376
|
throw new DomainError("E_BAD_PAYLOAD", {
|
|
376
|
-
message: `The edit requires "anchor_from"
|
|
377
|
+
message: `The edit requires an "anchor_from" anchor string (a bare ${HASH_LEN}-char hash from served output). Nothing was written.`,
|
|
377
378
|
});
|
|
378
379
|
}
|
|
379
380
|
// SAFETY: (ticket-04 item (iii)) this is the ONLY fail-closed check on the internal placement
|
|
@@ -425,7 +426,10 @@ export function resEdit(edit: HTEdit): HEdit {
|
|
|
425
426
|
assertItem(edit as Record<string, unknown>);
|
|
426
427
|
|
|
427
428
|
const editLines = parseText(edit.text);
|
|
428
|
-
|
|
429
|
+
// WHY: `anchor_to` is optional: an absent bound makes the target the single line `anchor_from`
|
|
430
|
+
// WHY: names, which is what a single-line edit always meant — the closed pair is built here so
|
|
431
|
+
// WHY: `hash_bounds` stays a total two-element tuple for every consumer.
|
|
432
|
+
const bounds = [edit.anchor_from, edit.anchor_to ?? edit.anchor_from].map((ref) => {
|
|
429
433
|
const trimmed = ref.trim();
|
|
430
434
|
if (trimmed.includes("\n")) {
|
|
431
435
|
const hash = firstHashFromBlock(trimmed);
|
package/src/payload-contract.ts
CHANGED
|
@@ -26,7 +26,8 @@ type SpanRef = {
|
|
|
26
26
|
*/
|
|
27
27
|
type EditItem = {
|
|
28
28
|
anchor_from: string;
|
|
29
|
-
|
|
29
|
+
/** Last line of the target range. Omitted (or null) means a single-line target: the range is "anchor_from" alone. */
|
|
30
|
+
anchor_to?: string;
|
|
30
31
|
at?: "in-place" | "before" | "after";
|
|
31
32
|
text?: string;
|
|
32
33
|
text_ref?: SpanRef;
|
|
@@ -54,7 +55,11 @@ export const _placementVocabularyAgreement: [
|
|
|
54
55
|
EditPlacement extends Placement ? true : never,
|
|
55
56
|
] = [true, true];
|
|
56
57
|
|
|
57
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* The anchor pair an item targets — placement and payload are relative to its resolved span. A
|
|
60
|
+
* single-line target names its one line in both bounds (`anchor_from === anchor_to`): the engine
|
|
61
|
+
* keeps the closed pair so nothing downstream has to branch on an absent bound.
|
|
62
|
+
*/
|
|
58
63
|
type AnchorSpan = { anchor_from: string; anchor_to: string };
|
|
59
64
|
|
|
60
65
|
type DesiredContent =
|
|
@@ -97,16 +102,18 @@ function isNormalizedEdit(input: unknown): input is Record<string, unknown> {
|
|
|
97
102
|
}
|
|
98
103
|
|
|
99
104
|
const ITEM_SHAPE =
|
|
100
|
-
"an item is exactly { anchor_from, anchor_to
|
|
105
|
+
"an item is exactly { anchor_from, anchor_to?, text[, at] } or { anchor_from, anchor_to?, text_ref[, at] }";
|
|
101
106
|
|
|
102
107
|
const EDIT_PAYLOAD_HINT =
|
|
103
108
|
"Edit must be called with exactly one payload per item, and each call edits ONE file. Use the canonical payload " +
|
|
104
109
|
'{"file": file, "edits": [{ "anchor_from": anchor_from, "anchor_to": anchor_to, "text": text }, ...], "mode"?: "general" | "literal"}: ' +
|
|
105
|
-
'"file" is the text file to edit (a non-empty string, never a directory); each item names
|
|
106
|
-
|
|
107
|
-
'
|
|
108
|
-
'
|
|
109
|
-
'
|
|
110
|
+
'"file" is the text file to edit (a non-empty string, never a directory); each item names the bare-' +
|
|
111
|
+
`${HASH_LEN}-char anchor "anchor_from" (the first line of the range, and the whole range of a single-line edit), an optional ` +
|
|
112
|
+
'second bare anchor "anchor_to" (the last line of a multi-line range; omit it to edit "anchor_from" alone) and exactly one ' +
|
|
113
|
+
'payload — "text" (bare replacement content; an empty string deletes the range) or "text_ref" ' +
|
|
114
|
+
'({ anchor_from, anchor_to, file?, mode (required): "copy" | "cut" } — the served span\'s bytes, whose two bounds are ' +
|
|
115
|
+
'always required, and whose "file" may name another served file as the copy/cut source); optional "at" is "in-place" ' +
|
|
116
|
+
'(default), "before" or "after" (single-line resolved target only); optional "mode" is "general" (default, reproduced ' +
|
|
110
117
|
'served rows are refused) or "literal" (declared literal content).';
|
|
111
118
|
function describeReceived(input: unknown): string {
|
|
112
119
|
if (input === undefined) return "Received no arguments.";
|
|
@@ -165,15 +172,21 @@ function sanitizePath(value: unknown): string | null {
|
|
|
165
172
|
}
|
|
166
173
|
|
|
167
174
|
// WHY: (ticket-04 §3) the finite key-set gate: an item is legal only when its key set is EXACTLY
|
|
168
|
-
// WHY: one of
|
|
169
|
-
// WHY: text_ref},
|
|
170
|
-
// WHY: both payloads or neither payload all refuse with the offending keys named.
|
|
171
|
-
// WHY:
|
|
175
|
+
// WHY: one of the eight shapes — { anchor_from, anchor_to?, text } and { anchor_from, anchor_to?,
|
|
176
|
+
// WHY: text_ref }, each optionally plus `at` — set EQUALITY, never superset, so a legacy key, an
|
|
177
|
+
// WHY: `op` field, both payloads or neither payload all refuse with the offending keys named.
|
|
178
|
+
// WHY: (ADR-0024) `anchor_to` is optional in every shape: one anchor means a single-line target.
|
|
179
|
+
// WHY: The vocabulary stays "anchor_from plus an optional closing anchor_to, one payload, optional
|
|
180
|
+
// WHY: placement" and one call edits one file.
|
|
172
181
|
const ITEM_KEY_SETS: readonly (readonly string[])[] = [
|
|
173
182
|
["anchor_from", "anchor_to", "text"],
|
|
183
|
+
["anchor_from", "text"],
|
|
174
184
|
["anchor_from", "anchor_to", "at", "text"],
|
|
185
|
+
["anchor_from", "at", "text"],
|
|
175
186
|
["anchor_from", "anchor_to", "text_ref"],
|
|
187
|
+
["anchor_from", "text_ref"],
|
|
176
188
|
["anchor_from", "anchor_to", "at", "text_ref"],
|
|
189
|
+
["anchor_from", "at", "text_ref"],
|
|
177
190
|
];
|
|
178
191
|
|
|
179
192
|
function keySetEquals(keys: Set<string>, shape: readonly string[]): boolean {
|
|
@@ -190,9 +203,9 @@ const AT_SPELLINGS = ["in-place", "before", "after"] as const satisfies readonly
|
|
|
190
203
|
|
|
191
204
|
// WHY: (ticket-67) strict structured-output harnesses spell "absent" as an explicit `null`
|
|
192
205
|
// WHY: (every property required). Key existence alone then lies: `{ text: "…", text_ref: null }`
|
|
193
|
-
// WHY: was refused as carrying both. `null`/`undefined` in the
|
|
194
|
-
// WHY: `text_ref`, `at`, root `mode`) reads as ABSENT wherever presence is decided —
|
|
195
|
-
// WHY: predicate, so the gate, the validators and the normalizers cannot disagree.
|
|
206
|
+
// WHY: was refused as carrying both. `null`/`undefined` in the optional slots (`text`,
|
|
207
|
+
// WHY: `text_ref`, `at`, `anchor_to`, root `mode`) reads as ABSENT wherever presence is decided —
|
|
208
|
+
// WHY: one predicate, so the gate, the validators and the normalizers cannot disagree.
|
|
196
209
|
// WHY: (ticket-75) the nested reference's optional `file` is the same slot one level down: a
|
|
197
210
|
// WHY: harness that must spell an omitted optional property emits `"file": null`, and reading it
|
|
198
211
|
// WHY: as present refused a call no retry could satisfy — the #67 class, one object deeper.
|
|
@@ -205,7 +218,7 @@ function isAbsentValue(value: unknown): value is null | undefined {
|
|
|
205
218
|
// WHY: shape `analyzeRequest` stores and nothing downstream can see an absent value as present.
|
|
206
219
|
function foldAbsentSlots(item: Record<string, unknown>): Record<string, unknown> {
|
|
207
220
|
const folded: Record<string, unknown> = { ...item };
|
|
208
|
-
for (const key of ["text", "text_ref", "at"] as const) {
|
|
221
|
+
for (const key of ["text", "text_ref", "at", "anchor_to"] as const) {
|
|
209
222
|
if (isAbsentValue(folded[key])) delete folded[key];
|
|
210
223
|
}
|
|
211
224
|
if (isRec(folded.text_ref)) {
|
|
@@ -237,7 +250,9 @@ function analyzeItem(value: unknown, index: number): string | undefined {
|
|
|
237
250
|
// WHY: list alone renders blank there, so the refusal must name the missing keys too.
|
|
238
251
|
const payloadKeys = new Set(["anchor_from", "anchor_to", "at", "text", "text_ref"]);
|
|
239
252
|
const offending = [...keys].filter((key) => !payloadKeys.has(key));
|
|
240
|
-
|
|
253
|
+
// WHY: `anchor_to` is optional at the wire — a single-line target needs no second bound — so a
|
|
254
|
+
// WHY: missing `anchor_to` is not a refusal; the normalizer folds it onto `anchor_from`.
|
|
255
|
+
const missing = ["anchor_from"].filter((key) => !keys.has(key));
|
|
241
256
|
const clauses: string[] = [];
|
|
242
257
|
if (offending.length > 0) clauses.push(`unsupported field(s) ${quoted(offending)}`);
|
|
243
258
|
if (missing.length > 0) clauses.push(`missing required field(s) ${quoted(missing)}`);
|
|
@@ -251,8 +266,11 @@ function analyzeItem(value: unknown, index: number): string | undefined {
|
|
|
251
266
|
return `edit[${index}] has ${clauses.join(";")} (${ITEM_SHAPE}).`;
|
|
252
267
|
}
|
|
253
268
|
const { anchor_from, anchor_to, at, text, text_ref } = value;
|
|
254
|
-
if (typeof anchor_from !== "string"
|
|
255
|
-
return `edit[${index}] "anchor_from"
|
|
269
|
+
if (typeof anchor_from !== "string") {
|
|
270
|
+
return `edit[${index}] "anchor_from" must be a bare ${HASH_LEN}-char hash anchor copied from served output (before │): it is the one anchor every item needs — an omitted "anchor_to" makes it the whole single-line target.`;
|
|
271
|
+
}
|
|
272
|
+
if (anchor_to !== undefined && typeof anchor_to !== "string") {
|
|
273
|
+
return `edit[${index}] "anchor_to" must be a bare ${HASH_LEN}-char hash anchor copied from served output (before │) or omitted to target the single line "anchor_from" names.`;
|
|
256
274
|
}
|
|
257
275
|
if (!isAbsentValue(at)) {
|
|
258
276
|
if (at === "in_place") {
|
|
@@ -390,8 +408,15 @@ function normalizedItemFrom(item: EditItem): NormalizedEditItem {
|
|
|
390
408
|
: item.text === ""
|
|
391
409
|
? { kind: "empty" }
|
|
392
410
|
: { kind: "literal", text: item.text ?? "" };
|
|
411
|
+
// WHY: the wire may omit `anchor_to`; the target is then the single line `anchor_from` names.
|
|
412
|
+
// WHY: The engine always receives a closed pair, so this one defaulting point keeps every
|
|
413
|
+
// WHY: downstream consumer shape-stable and nothing has to test for an absent bound.
|
|
414
|
+
const target: AnchorSpan = {
|
|
415
|
+
anchor_from: item.anchor_from,
|
|
416
|
+
anchor_to: item.anchor_to ?? item.anchor_from,
|
|
417
|
+
};
|
|
393
418
|
return {
|
|
394
|
-
target
|
|
419
|
+
target,
|
|
395
420
|
at,
|
|
396
421
|
payload,
|
|
397
422
|
};
|
package/src/prompts.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
1
3
|
import { HASH_LEN } from "./hashline/alphabet.js";
|
|
2
|
-
import { BUNDLED_PROMPTS } from "./prompts-bundled.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Resolve template variables in shipped prompt prose. Beyond caller-supplied replacements, the
|
|
@@ -16,30 +17,14 @@ function applyReplacements(content: string, replacements?: Record<string, string
|
|
|
16
17
|
return out.split("{{HASH_LEN}}").join(String(HASH_LEN));
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
/**
|
|
20
|
-
* Resolve one prompt path to its bundled body.
|
|
21
|
-
*
|
|
22
|
-
* WHY a path rather than a bare name: callers have always named the prompt by its `prompts/`
|
|
23
|
-
* path, and that argument IS the lookup key, so a typo fails at the call site instead of
|
|
24
|
-
* silently serving an empty prompt. Nothing is read from disk — see `prompts-bundled.ts`.
|
|
25
|
-
*/
|
|
26
|
-
function bundled(relativePath: string): string {
|
|
27
|
-
const name = relativePath.slice(relativePath.lastIndexOf("/") + 1);
|
|
28
|
-
const body = BUNDLED_PROMPTS[name];
|
|
29
|
-
if (body === undefined) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
`unknown bundled prompt: ${relativePath} (bundled: ${Object.keys(BUNDLED_PROMPTS).join(", ")})`,
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
return body;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
20
|
export function loadP(relativePath: string, replacements?: Record<string, string>): string {
|
|
38
|
-
|
|
21
|
+
const content = readFileSync(new URL(relativePath, import.meta.url), "utf-8").trim();
|
|
22
|
+
return applyReplacements(content, replacements);
|
|
39
23
|
}
|
|
40
24
|
|
|
41
25
|
export function loadGuide(relativePath: string, replacements?: Record<string, string>): string[] {
|
|
42
|
-
|
|
26
|
+
const content = readFileSync(new URL(relativePath, import.meta.url), "utf-8");
|
|
27
|
+
return applyReplacements(content, replacements)
|
|
43
28
|
.split("\n")
|
|
44
29
|
.map((line) => line.trim())
|
|
45
30
|
.filter((line) => line.startsWith("- "))
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* GENERATED FILE — do not edit by hand.
|
|
3
|
-
*
|
|
4
|
-
* Source of truth: `prompts/*.md`. Regenerate with `node scripts/gen-prompts.mjs`
|
|
5
|
-
* (`pnpm run build` runs it first). The prose lives inline so reinstalling the plugin is
|
|
6
|
-
* the only way — and the only needed way — to change what the model reads.
|
|
7
|
-
*
|
|
8
|
-
* @module dsh-hashline-edit/prompts-bundled
|
|
9
|
-
*/
|
|
10
|
-
/** Every shipped prompt body, keyed by its `prompts/<name>.md` basename. */
|
|
11
|
-
export declare const BUNDLED_PROMPTS: Readonly<Record<string, string>>;
|
package/src/prompts-bundled.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* GENERATED FILE — do not edit by hand.
|
|
3
|
-
*
|
|
4
|
-
* Source of truth: `prompts/*.md`. Regenerate with `node scripts/gen-prompts.mjs`
|
|
5
|
-
* (`pnpm run build` runs it first). The prose lives inline so reinstalling the plugin is
|
|
6
|
-
* the only way — and the only needed way — to change what the model reads.
|
|
7
|
-
*
|
|
8
|
-
* @module dsh-hashline-edit/prompts-bundled
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/** Every shipped prompt body, keyed by its `prompts/<name>.md` basename. */
|
|
12
|
-
export const BUNDLED_PROMPTS: Readonly<Record<string, string>> = {
|
|
13
|
-
"apply-patch-guidelines.md": `- apply_patch: one call = one transaction; batch related changes and split only when an earlier RESULT decides the next.
|
|
14
|
-
- apply_patch: an identity hunk is \`@@<anchor>\` or \`@@<from>-<to>\` followed by \`+\` lines; an empty body deletes every row of the span. Minimal shape: \`*** Begin Patch\` / \`*** Update File: <path>\` / \`@@<anchor>\` / \`+<new line>\` / \`*** End Patch\`.
|
|
15
|
-
- apply_patch: an identity anchor is the bare \`{{HASH_LEN}}\` characters before \`│\` in a served row — never the whole row, never a line number; syntax is \`@@\` plus the anchor with no space between them.
|
|
16
|
-
- apply_patch: served beats text — when this session served the file, an identity hunk is REQUIRED, not preferred; a fragment hunk is limited to a file or region this session never served, and a text match never licenses one.
|
|
17
|
-
- apply_patch: anchors in hand mean no re-read — apply the identity hunk to the served rows instead of reading the file again; a rejection already serves the current rows back.
|
|
18
|
-
- apply_patch: change only what the task changes — an identity hunk carries \`+\` lines for the edited region and no unchanged rows, so a small edit stays a small patch.
|
|
19
|
-
- apply_patch: for a one-line or single-region change to a served file, one identity hunk is the whole call; do not switch to \`write\` because the file content is already in context.
|
|
20
|
-
- apply_patch: hunks are validated against the pre-call file in order (overlapping ranges reject the batch); a fragment hunk must match exactly once — \`E_UNIQUE_MATCH\` writes nothing — while a stale or unknown anchor is rejected; both serve the current rows back.
|
|
21
|
-
- apply_patch: a rejection writes nothing; retry from the served \`HASH│content\` rows without another \`read\`.
|
|
22
|
-
- apply_patch: success returns fresh anchors for the changed lines; a patch carries only the changed rows, so a new file or an every-line rewrite stays with \`write\`.`,
|
|
23
|
-
"apply-patch.md": `Change an existing file with \`apply_patch\`; it is the default for every change to an existing file, including a one-line change. \`write\` is only for a new file or for a rewrite where every line changes; a partial change is never a whole-file replacement. Pass the complete patch as \`input\`. When this session already served the file — through \`read\`, \`grep\`, a \`bash\` view, or a previous \`apply_patch\` result — every hunk MUST be an identity hunk on a served row: reuse the bare anchor before \`│\` (\`@@<anchor>\` to replace that row, \`@@<from>-<to>\` to replace a span whose anchors you hold), and add only the changed \`+\` lines rather than re-emitting rows you are not changing. Anchors already in hand need no re-read: patch them directly instead of calling \`read\` again. A fragment hunk (\`@@\` or \`@@ <context>\` matched by file text) is legal only for a file or region this session never served; if the file was served, a fragment hunk is the wrong tool even when the text matches, because it discards the anchor check that proves you edited the version you actually saw.`,
|
|
24
|
-
"edit-guidelines.md": `- edit: one call = one file and one transaction; N changes in that file are N items in the same call, and a different file is a different call.
|
|
25
|
-
- edit: every item carries exactly one payload — \`text\` (literal content; \`""\` deletes the range) or \`text_ref\` (\`mode: "copy"\` or \`"cut"\`); never both, never neither.
|
|
26
|
-
- edit: anchors are the bare \`{{HASH_LEN}}\` characters before \`│\` in served output — never the whole row, never a line number.
|
|
27
|
-
- edit: both anchors are inclusive; \`at\` is \`in-place\` by default, while \`before\`/\`after\` insert at the boundary and require a single-line resolved target.
|
|
28
|
-
- edit: \`text_ref\` copies a served span's bytes; \`"cut"\` also retires the source lines, and \`text_ref.file\` may name another served file as that source.
|
|
29
|
-
- edit: anchors already in hand need no re-read — a rejection serves the current rows back, so repair from the \`HASH│content\` rows instead of guessing.
|
|
30
|
-
- edit: change only what the task changes — one item per intended range, and unchanged rows stay out of the payload.
|
|
31
|
-
- edit: a stale or unknown anchor, an overlapping range, or a payload reproducing served rows in \`general\` mode rejects the whole call and writes nothing; \`mode: "literal"\` declares reproduced rows as intended content.
|
|
32
|
-
- edit: \`write\` is the whole-file path — a new file or an every-line rewrite stays with \`write\`, never an edit item.`,
|
|
33
|
-
"edit.md": `Change a range of lines in one text file with hash anchors: pass \`file\` plus \`edits\`, one item per change. Each item names two inclusive bare anchors from served rows (\`anchor_from\`, \`anchor_to\`), exactly one payload — \`text\` for the replacement content (\`""\` deletes the range) or \`text_ref\` for a served span (\`mode: "copy"\` re-inserts its bytes, \`"cut"\` also retires them) — and optional \`at\` (\`in-place\` by default, \`before\`/\`after\` for a single-line target). One call edits one file; \`text_ref.file\` may name another served file as the copy/cut source. Anchors are the bare \`{{HASH_LEN}}\` characters before \`│\` in served output. A rejected call writes nothing.`,
|
|
34
|
-
"read-guidelines.md": `- read: call it for content the tool has never served you; a bash view or an auto-read already served the file, so \`apply_patch\` can use those rows.
|
|
35
|
-
- read: batch several disjoint ranges in ONE call with \`windows\` — an offset/limit range, or \`around_anchor\` plus \`radius\` around a line this session served.
|
|
36
|
-
- read: pass \`mode: "verbatim"\` when you will not \`apply_patch\` the file (config, docs, a README, skill files); otherwise omit it so the rows carry anchors.
|
|
37
|
-
- read: a window of 61 lines or fewer may widen to its enclosing block; widened rows carry a \`[widened …]\` note. Caps are 80 lines for code/Markdown and 40 for plain text.
|
|
38
|
-
- read: read several files with parallel \`read\` calls, one file per call.
|
|
39
|
-
- read: PNG/JPG/GIF/WebP files return as an image attachment for multimodal models; binary files and directories are refused.`,
|
|
40
|
-
"read.md": `Reads a UTF-8 file as \`HASH│content\` rows in served mode; each row's anchor is the \`apply_patch\` identity anchor for that line, and only the \`{{HASH_LEN}}\` characters before \`│\` are the anchor. Pass \`mode: "verbatim"\` for plain text with no anchors when you will not apply_patch; verbatim is not capped by line budget. Batch disjoint ranges in one call with \`windows\` (offset/limit or \`around_anchor\`).`,
|
|
41
|
-
"write-guidelines.md": `- write: the content is the complete file; a partial change stays an \`apply_patch\` identity hunk, even when the whole file is already in context.
|
|
42
|
-
- write: never paste served \`HASH│\` rows into the content; a line that reproduces a served row is refused as a served hash echo.`,
|
|
43
|
-
"write.md": `Read a file before overwriting it with \`write\`. \`write\` replaces the whole file: use it for a new file, or for a rewrite where every line changes. For a change to part of an existing file, use \`apply_patch\` identity hunks on the served rows instead.`,
|
|
44
|
-
};
|