Skip to content

Commit d48be70

Browse files
donjor-agent[bot]donjor
authored andcommitted
fix(fork-hooks): the marker scanner skips regex literals (#1888)
## Problem The fork-hook marker scanner reads a regex literal's quotes as an open string. After `/^['"]|['"]$/g` in `apps/server/src/terminal/Manager.ts`, it loses 59 markers. The hook guard then flags those marked lines as unmarked, and the sync proof run can't go green. ## Change - **Scanner:** skips a regex literal when `/` follows an operator, an opening bracket, or a keyword - **Skill:** a replayed commit gains no lines - **Skill:** tip fixups iterate by amend - **Skill:** comment-only lines move into a `.fork.ts` helper ## Scope and approval Found by the fork-sync proof run for #1886; fork tooling only. Refs #1886 ## Verification - `lib/fork-hooks.test.ts`: new case fails before the fix and passes after (22/22) - Sync candidate's `terminal/Manager.ts`: missed markers 59 → 0 - `vp run fork:ci` → exit 0 Claude Opus 5.5 via Claude Code. ## Fork trailers Squashes: - 8d49eb0 fix(fork-hooks): the marker scanner skips regex literals (#1888) (#1888) - d37471a fixup! fix(fork-hooks): the marker scanner skips regex literals (#1888) (#1888) Fork-Domain: fork-meta Fork-Tier: bugfix Fork-Upstreamable: no Co-authored-by: donjor <[email protected]>
1 parent 6424f8a commit d48be70

3 files changed

Lines changed: 61 additions & 41 deletions

File tree

‎.agents/skills/fork-sync/SKILL.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Resolve each path in that worktree by verdict, then `git add` it.
8080
- **Marked fork hook:** goes back verbatim
8181
- **Net-zero fork edit:** upstream's delete stands
8282
- **Replayed commit:** never adapt; hook guard
83+
- Adds no line its original lacked
84+
- A broken build is tip work, not resolution
8385
- **[Regenerable][regen]:** regenerates on rerun
8486

8587
[regen]: ../../../docs/fork/operations/fork-sync.md#regenerable-files
@@ -104,6 +106,11 @@ To fold it sooner, run [fork-fold](../fork-fold/SKILL.md) now.
104106
Adaptation the candidate needs (build or test fixes, ports) commits at the tip of the kept worktree.
105107

106108
- **One commit per owner:** per [Reshape][reshape]
109+
- **Iterate:** amend that owner's fixup
110+
- The guard reads each commit alone
111+
- A second fixup never clears the first
112+
- **Comment-only line:** never in an upstream file
113+
- Move the code into a `.fork.ts` helper
107114
- **Owner:** newest fork commit on those lines
108115
- **Find it:** `vp run fork:fixup-fold`
109116
- **Unmarked fork line:** the fixup marks it

‎scripts/lib/fork-hooks.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ it("bounds a branch statement and template literals behind a trailing marker", (
293293
const singleLines = ["const a = 1;", "markIt(); // x"].join("\n").split("\n");
294294
const got = statementStartLine(singleLines, 2);
295295
assert.strictEqual(got, 2, JSON.stringify({ singleLines, got }));
296+
// Quotes and brackets inside a regex literal open nothing; division stays division.
297+
const regex = [
298+
'const unquoted = token.replace(/^[\'"]|[\'"]$/g, "");',
299+
"const half = total / 2 / count;",
300+
"const word = /[{(`]/.test(s) ? a : b;",
301+
"markIt(); // fork-hook: dom/x",
302+
].join("\n");
303+
assert.strictEqual(statementStartLine(regex.split("\n"), 4), 4);
296304
});
297305

298306
it("refuses a statement start it cannot prove", () => {

‎scripts/lib/fork-hooks.ts‎

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
// The fork-hook seam model. A `fork-hook:` marker in an upstream-owned source
2-
// file is the whole declaration of a seam: there is no hand-kept manifest, and
3-
// nothing outside the marked source decides whether a hook exists
4-
// (RSI-Software/t3code-hyprws#1155). `deriveForkHooks` reads the markers at the
5-
// fork tip and derives every entry the sync walk re-applies.
6-
//
7-
// Three marker forms:
8-
//
9-
// - a trailing `// fork-hook: <domain>/<name>` on a one-line hook (an import,
10-
// a call, a `const`, a property/spread);
11-
// - `{/* fork-hook: <domain>/<name> */}` … `{/* fork-hook-end *` + `/}`
12-
// (the end-marker JSX comment) around a multi-line JSX construct;
13-
// - a trailing `/* fork-hook: <domain>/<name> */` for one-line hooks in
14-
// languages where `//` is not a comment (CSS).
15-
//
16-
// A hook is exactly one construct and never removes or modifies an upstream
17-
// line; `forkHookConstructViolation` is that doctrine as code. An unmatched
18-
// JSX pair or an unknown domain fails the derivation with `path:line`, and a
19-
// marker mid-comment marks nothing.
1+
// The fork-hook seam model: a `fork-hook:` marker in an upstream-owned file is the whole seam
2+
// declaration; no manifest. Forms: a trailing `// fork-hook: <domain>/<name>` (CSS: `/* ... */`),
3+
// or `{/* fork-hook: <domain>/<name> */}` … `{/* fork-hook-end *` + `/}` around multi-line JSX.
4+
// A hook is one construct that never changes an upstream line; a marker mid-comment marks nothing.
205

216
// @effect-diagnostics nodeBuiltinImport:off
227
import * as NodeChildProcess from "node:child_process";
@@ -26,12 +11,9 @@ import * as NodePath from "node:path";
2611
import { FORK_DOMAINS } from "./fork-trailers.ts";
2712

2813
/**
29-
* Where the hook sits in its file, derived from the marked span's context at
30-
* the fork tip: the shortest run of immediately preceding lines that carry no
31-
* marker of their own and occur exactly once in the file with every marked span
32-
* removed. Re-apply puts the hook back directly after that run, so the anchor
33-
* needs no typed kinds — an import block, a collection member and a JSX child
34-
* are all "after these lines".
14+
* Where the hook sits, derived at the fork tip: the shortest run of immediately preceding unmarked
15+
* lines that occurs exactly once in the file with every marked span removed. Re-apply puts the hook
16+
* back right after that run, so the anchor needs no typed kinds.
3517
*/
3618
export interface ForkHookAnchor {
3719
readonly context: ReadonlyArray<string>;
@@ -107,6 +89,25 @@ const INITIAL_SCAN: LiteralScan = {
10789
comment: false,
10890
};
10991

92+
// A `/` at line start or after one of these characters or keywords on its line opens a regex
93+
// literal; anywhere else it divides.
94+
const REGEX_PRECEDER = "(,=:[!&|?{};+-*%<>~^";
95+
const REGEX_KEYWORD =
96+
/\b(?:return|typeof|case|do|else|in|of|void|yield|await|delete|throw|new|instanceof)\s*$/;
97+
98+
/** The index of the `/` closing a regex literal opened at `start`, or -1 when the line never closes it. */
99+
const regexLiteralEnd = (line: string, start: number): number => {
100+
let inClass = false;
101+
for (let index = start + 1; index < line.length; index += 1) {
102+
const char = line[index];
103+
if (char === "\\") index += 1;
104+
else if (char === "[") inClass = true;
105+
else if (char === "]") inClass = false;
106+
else if (char === "/" && !inClass) return index;
107+
}
108+
return -1;
109+
};
110+
110111
const scanLiteralLine = (line: string, start: LiteralScan): LiteralScan => {
111112
let { depth, quote, lastCode, comment } = start;
112113
const frames = [...start.frames];
@@ -138,6 +139,19 @@ const scanLiteralLine = (line: string, start: LiteralScan): LiteralScan => {
138139
index += 1;
139140
continue;
140141
}
142+
// A regex literal's quotes and brackets are pattern text, not literals or nesting.
143+
if (
144+
char === "/" &&
145+
(REGEX_PRECEDER.includes(line.slice(0, index).trimEnd().at(-1) ?? "(") ||
146+
REGEX_KEYWORD.test(line.slice(0, index)))
147+
) {
148+
const end = regexLiteralEnd(line, index);
149+
if (end !== -1) {
150+
index = end;
151+
lastCode = "/";
152+
continue;
153+
}
154+
}
141155
if (char === '"' || char === "'" || char === "`") {
142156
quote = char;
143157
continue;
@@ -174,17 +188,10 @@ const scanLiteralLine = (line: string, start: LiteralScan): LiteralScan => {
174188
const COMMENT_ONLY = /^(?:\/\/|\/\*|\*(?:\/|$))/;
175189

176190
/**
177-
* The first line (1-based) of the statement a trailing marker on `markerLine` closes, found by
178-
* walking backwards: while `()[]{}` brackets or string/template literals are still open, or the
179-
* previous line neither ends a statement (`;`, `{`, `,` as the last code character outside any
180-
* literal, or a balanced `}` closing a block) nor is blank or
181-
* comment-only, the statement continues upward. `null` when the start cannot be proven — the
182-
* marker sits inside a string or template text, or a stray close leaves the file unbalanced.
183-
* Pure and line-based; there is deliberately no parser dependency.
184-
*
185-
* "Open" is judged against the depth the statement itself ends at, not against the file's top
186-
* level: a hook inside a function body sits at depth 1 for its whole life, and comparing with 0
187-
* would walk the span out to the enclosing declaration and swallow upstream lines with it.
191+
* The 1-based first line of the statement a trailing marker on `markerLine` closes, walking back
192+
* while brackets or literals stay open or the previous line ends no statement; `null` if unproven.
193+
* "Open" is judged against the statement's own end depth, not depth 0, or a hook inside a function
194+
* body would swallow the enclosing declaration. Line-based; there is deliberately no parser.
188195
*/
189196
export const statementStartLine = (
190197
lines: ReadonlyArray<string>,
@@ -305,11 +312,9 @@ export const isWellFormedForkHookKey = (key: string): boolean => {
305312
);
306313
};
307314

308-
// The marker-construct doctrine (RSI-Software/t3code-hyprws#1155 folded the
309-
// authoring guard's classifier in here, next to the parser it judges). A marked
310-
// line hook must be exactly one of these shapes; the property/spread shape
311-
// additionally names a fork identifier, the others are structural. `@import` is
312-
// the CSS spelling of the import construct: a fork sheet is pulled in that way.
315+
// The marker-construct doctrine: a marked line hook must be exactly one of these shapes. The
316+
// property/spread shape also names a fork identifier; the others are structural. `@import` is the
317+
// CSS spelling of the import construct.
313318
const HOOK_IMPORT = /^\s*(?:@?import\b|export\s+\{[^}]*\}\s*from\b|export\s*\*)/;
314319
const HOOK_SINGLE_CALL =
315320
/^(?!\s*(?:if|for|while|switch|catch|return)\s*\()\s*[A-Za-z_$][\w$.]*\s*\(/;

0 commit comments

Comments
 (0)