@agjs/tsforge 0.2.3 → 0.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agjs/tsforge",
3
3
  "type": "module",
4
- "version": "0.2.3",
4
+ "version": "0.2.4",
5
5
  "license": "MIT",
6
6
  "description": "TypeScript coding harness with a deterministic gate, stack-aware guardrails, and stream-level correction.",
7
7
  "repository": {
@@ -108,3 +108,22 @@ export function isValidHash(hash: string): boolean {
108
108
  export function normalizeHash(hash: string): string {
109
109
  return hash.toUpperCase();
110
110
  }
111
+
112
+ /**
113
+ * Extract the 4-hex hash from a raw value that may be a full `¶path#HASH` tag,
114
+ * a `path#HASH`, a `#HASH`, or a bare `HASH`. The model frequently pastes the
115
+ * whole header tag (what it saw on read) into the `hash` arg, which then fails
116
+ * the staleness compare against a real 4-hex hash. Returns undefined when no
117
+ * valid hash is present.
118
+ */
119
+ export function extractHash(raw: string | undefined): string | undefined {
120
+ if (raw === undefined) {
121
+ return undefined;
122
+ }
123
+
124
+ const candidate = raw.includes(HL_HASH_SEP)
125
+ ? raw.slice(raw.lastIndexOf(HL_HASH_SEP) + 1).trim()
126
+ : raw.trim();
127
+
128
+ return isValidHash(candidate) ? candidate : undefined;
129
+ }
@@ -10,6 +10,7 @@ import {
10
10
  computeFileHash,
11
11
  parseHashHeader,
12
12
  normalizeHash,
13
+ HL_HEADER_SIGIL,
13
14
  } from "./hashline-format";
14
15
 
15
16
  /**
@@ -318,26 +319,35 @@ export function parseHashlineEdit(input: string): {
318
319
 
319
320
  let i = 0;
320
321
 
321
- // Parse file header
322
- if (i < lines.length) {
323
- const headerLine = lines[i] ?? "";
324
- const parsed = parseHeaderLine(headerLine, errors);
322
+ // The `¶path#HASH` header is OPTIONAL. The model frequently sends bare ops
323
+ // (`delete 79..164`) — the path is already in the tool args and the hash
324
+ // comes from the `hash` arg — so only treat the first line as a header when
325
+ // it actually looks like one (a malformed sigil header is still an error).
326
+ const headerLine = lines[0] ?? "";
325
327
 
326
- filePath = parsed.filePath;
327
- fileHash = parsed.fileHash;
328
+ if (headerLine.startsWith(HL_HEADER_SIGIL)) {
329
+ const parsed = parseHeaderLine(headerLine, errors);
328
330
 
329
- if (filePath.length > 0) {
330
- i++;
331
- } else if (errors.length > 0) {
331
+ if (errors.length > 0) {
332
332
  return { filePath: "", fileHash: undefined, ops: [], errors };
333
- } else if (headerLine.trim() === "") {
334
- i++;
335
333
  }
334
+
335
+ filePath = parsed.filePath;
336
+ fileHash = parsed.fileHash;
337
+ i = 1;
338
+ } else if (headerLine.trim() === "") {
339
+ i = 1;
336
340
  }
337
341
 
338
342
  // Parse operations
339
343
  const ops = parseOperations(lines, i, errors);
340
344
 
345
+ if (ops.length === 0 && errors.length === 0) {
346
+ errors.push(
347
+ "No edit operations found. Provide replace/delete/insert ops (optionally after a ¶path#HASH header)."
348
+ );
349
+ }
350
+
341
351
  return { filePath, fileHash, ops, errors };
342
352
  }
343
353
 
@@ -3,6 +3,7 @@ import {
3
3
  parseHashlineEdit,
4
4
  SessionSnapshotStore,
5
5
  } from "../../files/hashline";
6
+ import { extractHash } from "../../files/hashline-format";
6
7
  import { parseOrRepair, reject, type IToolContext } from "./tool-context";
7
8
  import { toHashlineEdit } from "../../agent";
8
9
 
@@ -50,11 +51,17 @@ export async function doHashlineEdit(
50
51
  // Ensure the store exists on the context
51
52
  ctx.snapshotStore ??= new SessionSnapshotStore();
52
53
 
54
+ // Hash source priority: the `¶path#HASH` header the model wrote in `input`
55
+ // (the format it saw on read), else the `hash` arg — tolerantly extracted so
56
+ // a pasted full tag (`¶path#HASH`) still yields the bare hash. Using the raw
57
+ // arg directly caused false stale-anchor rejections on unchanged files.
58
+ const fileHash = parsed.fileHash ?? extractHash(edit.hash);
59
+
53
60
  const result = await applyHashlineEdit(
54
61
  ctx.snapshotStore,
55
62
  ctx.cwd,
56
63
  edit.file,
57
- edit.hash,
64
+ fileHash,
58
65
  parsed.ops
59
66
  );
60
67