@agjs/tsforge 0.2.3 → 0.2.5

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.5",
5
5
  "license": "MIT",
6
6
  "description": "TypeScript coding harness with a deterministic gate, stack-aware guardrails, and stream-level correction.",
7
7
  "repository": {
@@ -478,6 +478,47 @@ export function buildCoreFix(): string {
478
478
  return `${lintFix} ; ${format}`;
479
479
  }
480
480
 
481
+ /**
482
+ * Auto-format ONE just-written file in place: `eslint --fix` (squashes the
483
+ * auto-fixable mechanical rules — padding-line, curly, prefer-template, quotes)
484
+ * then `prettier --write` (whitespace/quotes/width). Run at WRITE time (in the
485
+ * write guard) so the model never sees — nor hand-chases — formatting noise.
486
+ * Deferring all of this to the settle-time gate let the model self-run eslint
487
+ * mid-build, see the un-squashed mechanical lint, and spiral fixing blank lines
488
+ * and braces by hand to the turn cap. Best-effort + per-file (cheap): any failure
489
+ * is swallowed and the settle gate stays the authority.
490
+ */
491
+ export async function formatFile(cwd: string, file: string): Promise<void> {
492
+ const abs = join(cwd, file);
493
+
494
+ try {
495
+ await Bun.spawn(
496
+ [
497
+ "bun",
498
+ ESLINT_BIN,
499
+ "--no-config-lookup",
500
+ "-c",
501
+ STRICT_CONFIG,
502
+ "--fix",
503
+ abs,
504
+ ],
505
+ { cwd, stdout: "ignore", stderr: "ignore" }
506
+ ).exited;
507
+ } catch {
508
+ // best-effort — the settle gate still fixes + validates
509
+ }
510
+
511
+ try {
512
+ await Bun.spawn(["bun", PRETTIER_BIN, "--write", abs], {
513
+ cwd,
514
+ stdout: "ignore",
515
+ stderr: "ignore",
516
+ }).exited;
517
+ } catch {
518
+ // best-effort
519
+ }
520
+ }
521
+
481
522
  async function ensureFile(
482
523
  cwd: string,
483
524
  name: string,
@@ -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
 
@@ -11,6 +11,7 @@ export const SYSTEM = [
11
11
  "Tools: `read` (inspect a file), `edit` (replace an exact, unique snippet), `create` (a new file), `run` (execute any shell command and see its output).",
12
12
  "Lead with action: write the implementation FIRST (one `create`/`edit`) — do NOT deliberate at length before writing any code.",
13
13
  "After every edit the harness AUTOMATICALLY runs the gate and gives you the result (the errors + fix guidance for the failing rules). You do NOT need to run the acceptance command yourself — read that result and fix exactly what it reports, then edit again. Keep going until it reports green; the harness ends the task at that point.",
14
+ "The harness also AUTO-FIXES mechanical formatting on every file you write — blank lines, braces, quotes, semicolons, import order, `prefer-template`. NEVER hand-fix or chase those, and do NOT run `tsc`/`eslint`/the gate yourself to look for them. Fix only what the gate explicitly hands back (`as`/`any`/`!`, `I`-prefix, real type errors), then stop.",
14
15
  "Test hypotheses by RUNNING them, never by reasoning them out. Unsure about an edge case, rounding, or ordering (`Math.floor(100/3)`, largest-remainder ties)? `run` a quick `bun -e '…console.log(…)'`, or write a throwaway `scratch/check.ts` importing your impl and `run` it. `scratch/` is yours — the gate ignores it.",
15
16
  "The gate is `tsc` strict + eslint with every rule an error, so write TypeScript that satisfies it: interfaces are `I`-prefixed; `===`; no `var`; never the non-null `!` — guard index access (`const x = arr[i]; if (x === undefined) {...}`); no `any` and no `as` — type every parameter (e.g. `.reduce((acc: number, r: number) => …, 0)`); explicit boolean conditions. When the gate flags errors in read-only files (tests/types), they come from your editable file being missing or wrong-shaped and vanish once it's correct — don't edit them.",
16
17
  ].join("\n");
@@ -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
 
package/src/loop/turn.ts CHANGED
@@ -34,6 +34,7 @@ import {
34
34
  import { TsService, type ITsDiagnostic } from "../lsp";
35
35
  import type { McpRegistry } from "../mcp";
36
36
  import type { FileLinter, IFileLintProblem } from "../detect-gate";
37
+ import { formatFile } from "../detect-gate";
37
38
  import {
38
39
  buildMetaRuleContext,
39
40
  runMetaRules,
@@ -322,6 +323,12 @@ async function writeGuard(
322
323
  });
323
324
  }
324
325
 
326
+ // Auto-format this file NOW (eslint --fix + prettier) — not at the settle
327
+ // gate. Otherwise the mechanical lint (blank lines, braces, quotes) sits
328
+ // unfixed between writes, and when the model self-runs the gate it sees the
329
+ // noise and hand-chases it to the turn cap. Best-effort; gate stays authority.
330
+ await formatFile(cwd, file);
331
+
325
332
  tsService.refresh(file);
326
333
 
327
334
  const typeErrors = tsService