loopwright 0.1.2 → 0.1.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/content/agents/implementer.md +1 -0
- package/content/agents/tester.md +2 -1
- package/content/hooks/guard-locked-tests.mjs +19 -7
- package/content/hooks/lib.mjs +60 -11
- package/content/hooks/lw.mjs +28 -7
- package/content/hooks/reinject-context.mjs +9 -4
- package/content/hooks/stop-gate.mjs +22 -3
- package/content/prompts/reviewer.md +2 -0
- package/content/skills/loopwright-build/SKILL.md +1 -0
- package/content/skills/loopwright-done/SKILL.md +3 -1
- package/content/skills/loopwright-test/SKILL.md +1 -0
- package/dist/cli.js +70 -9
- package/package.json +6 -3
|
@@ -14,6 +14,7 @@ The main thread gives you the task file path, and on a fix round the reviewer's
|
|
|
14
14
|
- Do not edit, move, delete or reformat locked test files (`node "{{LW_HOME}}/hooks/lw.mjs" status` lists them under `lockedTests`). Do not change test config to skip or weaken them. Review re-hashes them; any change fails the review.
|
|
15
15
|
- Stay in scope: implement what the decisions and ACs require. No unrelated refactors, renames or cleanups.
|
|
16
16
|
- Do not commit, push, or change git state other than the working tree.
|
|
17
|
+
- Never write task ids (AC, D, S shorthands) into tests, code, comments, commit messages or PR drafts. Task ids live only in the task file.
|
|
17
18
|
- You cannot ask the user questions. If a decision is contradictory, a locked test looks wrong, or the task cannot be done as decided, stop and report it under "Deviations"; do not work around it silently.
|
|
18
19
|
|
|
19
20
|
## Standards
|
package/content/agents/tester.md
CHANGED
|
@@ -14,11 +14,12 @@ The main thread gives you the task file path. Read its acceptance criteria (`AC
|
|
|
14
14
|
- Read implementation files only as far as needed to import public interfaces (module paths, exported names, signatures). Do not read or test internals.
|
|
15
15
|
- Write only test files (and fixtures they need). Do not create or edit implementation code, not even stubs.
|
|
16
16
|
- Use the repo's existing test runner, file layout and test style. Do not add a test framework or dependency.
|
|
17
|
+
- Never write task ids (AC, D, S shorthands) into tests, code, comments, commit messages or PR drafts. Task ids live only in the task file.
|
|
17
18
|
- You cannot ask the user questions. If an AC is ambiguous, untestable, or needs a runner that does not exist, stop and report what you need; do not guess.
|
|
18
19
|
|
|
19
20
|
## Steps
|
|
20
21
|
|
|
21
|
-
1. For each AC, write one or more tests
|
|
22
|
+
1. For each AC, write one or more tests. Name each test by the behavior it checks (e.g. `empty input returns []`), not by an id. Test observable behavior through the public interface, not implementation details.
|
|
22
23
|
2. Run the new tests with the task's test command, targeting only your files.
|
|
23
24
|
3. Confirm each test fails for the expected reason: the behavior is missing or wrong (assertion failure, missing export). A syntax error, a typo, an import path the task file does not name, or a crashing setup is the wrong reason; fix the test and rerun.
|
|
24
25
|
When the module or interface the task file names does not exist yet, import it from that path. "Module not found" or a missing export at that path is the expected failure; one file-level excerpt covers every AC in that file. If the task file names no module path or interface for an AC, stop and report it under Needs.
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// PreToolUse guard: block edits to locked acceptance tests during the build stage.
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
allow,
|
|
5
|
+
exitIfHooksOff,
|
|
6
|
+
deny,
|
|
7
|
+
failOpen,
|
|
8
|
+
loopwrightDir,
|
|
9
|
+
readState,
|
|
10
|
+
readStdinJson,
|
|
11
|
+
repoRelative,
|
|
12
|
+
worktreeId,
|
|
13
|
+
} from './lib.mjs';
|
|
4
14
|
|
|
5
15
|
function patchPaths(command) {
|
|
6
16
|
const out = [];
|
|
@@ -24,15 +34,17 @@ try {
|
|
|
24
34
|
if (!state || state.stage !== 'build' || !Array.isArray(state.lockedTests) || state.lockedTests.length === 0) allow();
|
|
25
35
|
|
|
26
36
|
const ti = input.tool_input ?? {};
|
|
27
|
-
const raw =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
const raw =
|
|
38
|
+
input.tool_name === 'apply_patch'
|
|
39
|
+
? patchPaths(ti.patch ?? ti.command)
|
|
40
|
+
: ti.file_path && input.tool_name !== 'Read' // Junie fires PreToolUse for reads too; only edits are guarded
|
|
41
|
+
? [ti.file_path]
|
|
42
|
+
: [];
|
|
32
43
|
const locked = new Set(state.lockedTests.map((l) => l.path));
|
|
33
44
|
for (const p of raw) {
|
|
34
45
|
const rel = repoRelative(cwd, p);
|
|
35
|
-
if (locked.has(rel))
|
|
46
|
+
if (locked.has(rel))
|
|
47
|
+
deny('', '', `loopwright: ${rel} is a locked acceptance test. Ask the user before changing it.`);
|
|
36
48
|
}
|
|
37
49
|
allow();
|
|
38
50
|
} catch (err) {
|
package/content/hooks/lib.mjs
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
// Loopwright hook runtime library. Only node:* imports.
|
|
2
2
|
import { execFileSync } from 'node:child_process';
|
|
3
3
|
import { createHash } from 'node:crypto';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
existsSync,
|
|
6
|
+
lstatSync,
|
|
7
|
+
mkdirSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
readlinkSync,
|
|
10
|
+
realpathSync,
|
|
11
|
+
renameSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from 'node:fs';
|
|
5
14
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
6
15
|
|
|
7
16
|
/** Mirror of PROFILES in src/core/profiles.ts (a test pins them equal). */
|
|
@@ -19,12 +28,13 @@ function git(cwd, args) {
|
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
export function gitTopLevel(cwd) {
|
|
22
|
-
|
|
31
|
+
// Git prints forward slashes on Windows; resolve() gives the platform form.
|
|
32
|
+
return resolve(git(cwd, ['rev-parse', '--show-toplevel']));
|
|
23
33
|
}
|
|
24
34
|
|
|
25
|
-
function mainWorktreeRoot(cwd) {
|
|
35
|
+
export function mainWorktreeRoot(cwd) {
|
|
26
36
|
const common = git(cwd, ['rev-parse', '--path-format=absolute', '--git-common-dir']);
|
|
27
|
-
if (basename(common) === '.git') return dirname(common);
|
|
37
|
+
if (basename(common) === '.git') return resolve(dirname(common));
|
|
28
38
|
return gitTopLevel(cwd);
|
|
29
39
|
}
|
|
30
40
|
|
|
@@ -72,7 +82,16 @@ export function writeState(dir, id, state) {
|
|
|
72
82
|
mkdirSync(dirname(path), { recursive: true });
|
|
73
83
|
const tmp = `${path}.${process.pid}.tmp`;
|
|
74
84
|
writeFileSync(tmp, JSON.stringify(state, null, 2) + '\n');
|
|
75
|
-
|
|
85
|
+
// Windows: rename fails with EPERM/EBUSY/EACCES while another process briefly holds the target.
|
|
86
|
+
for (let attempt = 0; ; attempt++) {
|
|
87
|
+
try {
|
|
88
|
+
renameSync(tmp, path);
|
|
89
|
+
return;
|
|
90
|
+
} catch (err) {
|
|
91
|
+
if (attempt >= 5 || !['EPERM', 'EBUSY', 'EACCES'].includes(err?.code)) throw err;
|
|
92
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20 * (attempt + 1));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
76
95
|
}
|
|
77
96
|
|
|
78
97
|
export function sha256File(path) {
|
|
@@ -82,9 +101,28 @@ export function sha256File(path) {
|
|
|
82
101
|
export function fingerprint(cwd, baseCommit) {
|
|
83
102
|
const h = createHash('sha256');
|
|
84
103
|
const top = gitTopLevel(cwd);
|
|
85
|
-
const diffArgs = [
|
|
104
|
+
const diffArgs = [
|
|
105
|
+
'-c',
|
|
106
|
+
'core.quotePath=false',
|
|
107
|
+
'diff',
|
|
108
|
+
'--binary',
|
|
109
|
+
'--no-color',
|
|
110
|
+
'--no-ext-diff',
|
|
111
|
+
'--no-textconv',
|
|
112
|
+
'--no-renames',
|
|
113
|
+
'--full-index',
|
|
114
|
+
'--no-relative',
|
|
115
|
+
'--src-prefix=a/',
|
|
116
|
+
'--dst-prefix=b/',
|
|
117
|
+
baseCommit,
|
|
118
|
+
];
|
|
86
119
|
h.update(execFileSync('git', diffArgs, { cwd: top, maxBuffer: MAX_BUFFER, stdio: ['ignore', 'pipe', 'pipe'] }));
|
|
87
|
-
const out = execFileSync('git', ['-c', 'core.quotePath=false', 'ls-files', '--others', '--exclude-standard', '-z'], {
|
|
120
|
+
const out = execFileSync('git', ['-c', 'core.quotePath=false', 'ls-files', '--others', '--exclude-standard', '-z'], {
|
|
121
|
+
cwd: top,
|
|
122
|
+
maxBuffer: MAX_BUFFER,
|
|
123
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
124
|
+
encoding: 'utf8',
|
|
125
|
+
});
|
|
88
126
|
const files = out.split('\0').filter(Boolean).sort();
|
|
89
127
|
for (const f of files) {
|
|
90
128
|
h.update(f + '\0');
|
|
@@ -131,13 +169,22 @@ export function failOpen(err) {
|
|
|
131
169
|
process.exit(0);
|
|
132
170
|
}
|
|
133
171
|
|
|
172
|
+
/** Native realpath expands Windows 8.3 short names (RUNNER~1) the way git reports them; JS realpath keeps them. Falls back so a native throw cannot fail the guard open. */
|
|
173
|
+
export function realpathResolve(path, impl = realpathSync.native) {
|
|
174
|
+
try {
|
|
175
|
+
return impl(path);
|
|
176
|
+
} catch {
|
|
177
|
+
return realpathSync(path);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
134
181
|
/** realpath of the nearest existing ancestor, with the not-yet-existing tail re-appended. */
|
|
135
182
|
export function realpathLoose(abs) {
|
|
136
183
|
const tail = [];
|
|
137
184
|
let cur = abs;
|
|
138
185
|
for (;;) {
|
|
139
186
|
try {
|
|
140
|
-
return join(
|
|
187
|
+
return join(realpathResolve(cur), ...tail.reverse());
|
|
141
188
|
} catch {
|
|
142
189
|
const parent = dirname(cur);
|
|
143
190
|
if (parent === cur) return abs;
|
|
@@ -148,8 +195,10 @@ export function realpathLoose(abs) {
|
|
|
148
195
|
}
|
|
149
196
|
|
|
150
197
|
/** Worktree-top-relative POSIX path of p (relative to cwd if not absolute), symlinks resolved. May start with '..' if outside. */
|
|
151
|
-
export function repoRelative(cwd, p) {
|
|
152
|
-
const top =
|
|
198
|
+
export function repoRelative(cwd, p, platform = process.platform) {
|
|
199
|
+
const top = realpathResolve(gitTopLevel(cwd));
|
|
153
200
|
const abs = realpathLoose(isAbsolute(p) ? resolve(p) : resolve(cwd, p));
|
|
154
|
-
|
|
201
|
+
const rel = relative(top, abs).split(sep).join('/');
|
|
202
|
+
// Windows paths are case-insensitive: fold so Test/T.test.ts matches a locked test/t.test.ts.
|
|
203
|
+
return platform === 'win32' ? rel.toLowerCase() : rel;
|
|
155
204
|
}
|
package/content/hooks/lw.mjs
CHANGED
|
@@ -5,7 +5,19 @@ import { homedir } from 'node:os';
|
|
|
5
5
|
import { isAbsolute, join, resolve } from 'node:path';
|
|
6
6
|
import { execFileSync } from 'node:child_process';
|
|
7
7
|
import { fileURLToPath } from 'node:url';
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
PROFILES,
|
|
10
|
+
fingerprint,
|
|
11
|
+
gitTopLevel,
|
|
12
|
+
loopwrightDir,
|
|
13
|
+
loopwrightPath,
|
|
14
|
+
readState,
|
|
15
|
+
repoRelative,
|
|
16
|
+
sha256File,
|
|
17
|
+
statePath,
|
|
18
|
+
worktreeId,
|
|
19
|
+
writeState,
|
|
20
|
+
} from './lib.mjs';
|
|
9
21
|
|
|
10
22
|
const STAGES = ['plan', 'test', 'build', 'review', 'done'];
|
|
11
23
|
const cwd = process.cwd();
|
|
@@ -61,7 +73,8 @@ function requireState() {
|
|
|
61
73
|
|
|
62
74
|
/** Effort for a role: the task header's `profile:`, else the manifest's recorded profile, else balanced. */
|
|
63
75
|
function effortFor(role) {
|
|
64
|
-
if (!Object.hasOwn(PROFILES.balanced, role))
|
|
76
|
+
if (!Object.hasOwn(PROFILES.balanced, role))
|
|
77
|
+
fail(`loopwright: usage: effort <${Object.keys(PROFILES.balanced).join('|')}>`);
|
|
65
78
|
// Works before `start` (Plan's scout spawn): no task or repo state just means no header.
|
|
66
79
|
let name;
|
|
67
80
|
let dir = null;
|
|
@@ -71,16 +84,21 @@ function effortFor(role) {
|
|
|
71
84
|
if (state?.task && existsSync(join(dir, state.task))) {
|
|
72
85
|
name = /^profile:\s*(\S+)\s*$/m.exec(readFileSync(join(dir, state.task), 'utf8').split(/^## /m)[0])?.[1];
|
|
73
86
|
}
|
|
74
|
-
} catch {
|
|
87
|
+
} catch {
|
|
88
|
+
// Unreadable task or state: fall through to the manifest profile.
|
|
89
|
+
}
|
|
75
90
|
// Repo manifest first, then the global one (global installs record it under ~/.config/loopwright).
|
|
76
91
|
for (const m of [dir && join(dir, 'manifest.json'), join(homedir(), '.config', 'loopwright', 'manifest.json')]) {
|
|
77
92
|
if (name || !m || !existsSync(m)) continue;
|
|
78
93
|
try {
|
|
79
94
|
name = JSON.parse(readFileSync(m, 'utf8')).options?.profile;
|
|
80
|
-
} catch {
|
|
95
|
+
} catch {
|
|
96
|
+
// Unreadable manifest: try the next one.
|
|
97
|
+
}
|
|
81
98
|
}
|
|
82
99
|
name ??= 'balanced';
|
|
83
|
-
if (!Object.hasOwn(PROFILES, name))
|
|
100
|
+
if (!Object.hasOwn(PROFILES, name))
|
|
101
|
+
fail(`loopwright: unknown profile '${name}' (allowed: ${Object.keys(PROFILES).join(', ')})`);
|
|
84
102
|
return PROFILES[name][role];
|
|
85
103
|
}
|
|
86
104
|
|
|
@@ -192,7 +210,8 @@ try {
|
|
|
192
210
|
if (result !== 'pass' && result !== 'fail') fail('loopwright: usage: verdict <pass|fail> --round <n>');
|
|
193
211
|
const roundRaw = flag(rest, '--round');
|
|
194
212
|
const round = Number(roundRaw);
|
|
195
|
-
if (roundRaw === undefined || !Number.isInteger(round) || round < 0)
|
|
213
|
+
if (roundRaw === undefined || !Number.isInteger(round) || round < 0)
|
|
214
|
+
fail('loopwright: verdict requires --round <n>');
|
|
196
215
|
const { dir, id, state } = requireState();
|
|
197
216
|
state.verdict = { pass: result === 'pass', fingerprint: fingerprint(cwd, state.baseCommit), round };
|
|
198
217
|
delete state.awaitingUser;
|
|
@@ -236,7 +255,9 @@ try {
|
|
|
236
255
|
break;
|
|
237
256
|
}
|
|
238
257
|
default:
|
|
239
|
-
fail(
|
|
258
|
+
fail(
|
|
259
|
+
'loopwright: usage: lw.mjs <dir|start|stage|lock|check-locks|fingerprint|verdict|await-user|resume|status|effort|done>',
|
|
260
|
+
);
|
|
240
261
|
}
|
|
241
262
|
} catch (err) {
|
|
242
263
|
fail(`loopwright: ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -13,7 +13,7 @@ const NEXT = {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
function goalParagraph(text) {
|
|
16
|
-
const lines = text.split(
|
|
16
|
+
const lines = text.split(/\r?\n/);
|
|
17
17
|
const i = lines.findIndex((l) => /^#{1,6}\s*goal\b/i.test(l.trim()));
|
|
18
18
|
if (i === -1) return [];
|
|
19
19
|
const out = [];
|
|
@@ -45,18 +45,23 @@ try {
|
|
|
45
45
|
const text = readFileSync(file, 'utf8');
|
|
46
46
|
lines.push(...goalParagraph(text));
|
|
47
47
|
lines.push(`Stage: ${state.stage}`);
|
|
48
|
-
lines.push(...text.split(
|
|
48
|
+
lines.push(...text.split(/\r?\n/).filter((l) => l.startsWith('- D')));
|
|
49
49
|
} else {
|
|
50
50
|
lines.push(`Stage: ${state.stage}`);
|
|
51
51
|
}
|
|
52
|
-
if (state.awaitingUser)
|
|
52
|
+
if (state.awaitingUser)
|
|
53
|
+
lines.push(
|
|
54
|
+
`Waiting on the user: ${state.awaitingUser} — do not proceed until they answer; then run lw.mjs resume.`,
|
|
55
|
+
);
|
|
53
56
|
else lines.push(NEXT[state.stage] ?? NEXT.plan);
|
|
54
57
|
const body = lines.slice(0, 40);
|
|
55
58
|
if (lines.length > 40) body[39] = lines[lines.length - 1];
|
|
56
59
|
const out = body.join('\n');
|
|
57
60
|
|
|
58
61
|
if (tool === 'codex') {
|
|
59
|
-
process.stdout.write(
|
|
62
|
+
process.stdout.write(
|
|
63
|
+
JSON.stringify({ hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: out } }) + '\n',
|
|
64
|
+
);
|
|
60
65
|
} else {
|
|
61
66
|
process.stdout.write(out + '\n');
|
|
62
67
|
}
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Stop hook: block stopping in build/review without a passing review for the current code.
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
allow,
|
|
5
|
+
exitIfHooksOff,
|
|
6
|
+
deny,
|
|
7
|
+
failOpen,
|
|
8
|
+
fingerprint,
|
|
9
|
+
loopwrightDir,
|
|
10
|
+
readState,
|
|
11
|
+
readStdinJson,
|
|
12
|
+
worktreeId,
|
|
13
|
+
writeState,
|
|
14
|
+
} from './lib.mjs';
|
|
4
15
|
|
|
5
16
|
const CAP = 3;
|
|
6
17
|
|
|
@@ -29,9 +40,17 @@ try {
|
|
|
29
40
|
allow();
|
|
30
41
|
}
|
|
31
42
|
state.stopBlocks = blocks + 1;
|
|
32
|
-
(state.history ??= []).push({
|
|
43
|
+
(state.history ??= []).push({
|
|
44
|
+
event: 'stop-blocked',
|
|
45
|
+
detail: String(state.stopBlocks),
|
|
46
|
+
at: new Date().toISOString(),
|
|
47
|
+
});
|
|
33
48
|
writeState(dir, id, state);
|
|
34
|
-
deny(
|
|
49
|
+
deny(
|
|
50
|
+
'',
|
|
51
|
+
'',
|
|
52
|
+
'loopwright: no passing review for the current code. Run /loopwright-review (or finish the fix loop) before stopping.',
|
|
53
|
+
);
|
|
35
54
|
} catch (err) {
|
|
36
55
|
failOpen(err);
|
|
37
56
|
}
|
|
@@ -48,6 +48,8 @@ Second pass: only when the caller hands you the implementer's report. Verify eac
|
|
|
48
48
|
|
|
49
49
|
## Findings
|
|
50
50
|
|
|
51
|
+
Never write task ids (AC, D, S shorthands) into tests, code, comments, commit messages or PR drafts. A task id in the diff is a finding; your own findings may cite them.
|
|
52
|
+
|
|
51
53
|
Every reported finding has:
|
|
52
54
|
- `location` — `path:line` (or `path` when file-level, or the command run);
|
|
53
55
|
- `expectation` — the violated expectation, citing its source (`D3`, `AC2`, `AGENTS.md`, the comment rule, an existing pattern at `path:line`);
|
|
@@ -31,5 +31,6 @@ The implementer subagent writes the code. This thread dispatches it and hands th
|
|
|
31
31
|
|
|
32
32
|
## Rules
|
|
33
33
|
|
|
34
|
+
- Never write task ids (AC, D, S shorthands) into tests, code, comments, commit messages or PR drafts.
|
|
34
35
|
- Never edit locked tests or ask the implementer to. A locked test that looks wrong goes to the user.
|
|
35
36
|
- Do not run `lw.mjs verdict` here; only Review records verdicts.
|
|
@@ -27,9 +27,11 @@ If `CLAUDE.md` exists and has no line `@AGENTS.md`, propose adding that import l
|
|
|
27
27
|
|
|
28
28
|
## 2. Commit message and PR description
|
|
29
29
|
|
|
30
|
+
Never write task ids (AC, D, S shorthands) into tests, code, comments, commit messages or PR drafts. Describe decisions in words.
|
|
31
|
+
|
|
30
32
|
Draft both and show them to the user:
|
|
31
33
|
- Commit: imperative subject ≤ 72 chars; body says why, the key decisions, and the rejected options with the reason.
|
|
32
|
-
- PR: summary of intent; decisions and rejected options
|
|
34
|
+
- PR: summary of intent; decisions and rejected options; notable assumptions the reviewer should know; how it was verified (check commands, acceptance tests, review rounds from the log). Do not list the diff file by file.
|
|
33
35
|
|
|
34
36
|
## 3. Close
|
|
35
37
|
|
|
@@ -29,5 +29,6 @@ The tester writes acceptance tests from the confirmed ACs and locks them. This t
|
|
|
29
29
|
|
|
30
30
|
## Rules
|
|
31
31
|
|
|
32
|
+
- Never write task ids (AC, D, S shorthands) into tests, code, comments, commit messages or PR drafts.
|
|
32
33
|
- Lock only in this stage, and only the tests for this stage's scope (the task, or the current slice). Never re-lock an existing locked test; if one is wrong, tell the user and let them decide.
|
|
33
34
|
- Do not read the implementation to judge the tests; the ACs are the contract.
|
package/dist/cli.js
CHANGED
|
@@ -64,7 +64,7 @@ function git(cwd, args) {
|
|
|
64
64
|
}).trim();
|
|
65
65
|
}
|
|
66
66
|
function gitTopLevel(cwd) {
|
|
67
|
-
return git(cwd, ["rev-parse", "--show-toplevel"]);
|
|
67
|
+
return resolve(git(cwd, ["rev-parse", "--show-toplevel"]));
|
|
68
68
|
}
|
|
69
69
|
function mainWorktreeRoot(cwd) {
|
|
70
70
|
const common = git(cwd, [
|
|
@@ -72,7 +72,7 @@ function mainWorktreeRoot(cwd) {
|
|
|
72
72
|
"--path-format=absolute",
|
|
73
73
|
"--git-common-dir"
|
|
74
74
|
]);
|
|
75
|
-
if (basename(common) === ".git") return dirname(common);
|
|
75
|
+
if (basename(common) === ".git") return resolve(dirname(common));
|
|
76
76
|
return gitTopLevel(cwd);
|
|
77
77
|
}
|
|
78
78
|
// src/core/targets.ts
|
|
@@ -182,7 +182,7 @@ function readConfig(path, profile) {
|
|
|
182
182
|
try {
|
|
183
183
|
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
184
184
|
} catch (e) {
|
|
185
|
-
throw new Error(`Invalid JSON in ${path}: ${e.message}
|
|
185
|
+
throw new Error(`Invalid JSON in ${path}: ${e.message}`, { cause: e });
|
|
186
186
|
}
|
|
187
187
|
config = merge(config, parsed);
|
|
188
188
|
}
|
|
@@ -540,6 +540,12 @@ function symlinkTarget(path) {
|
|
|
540
540
|
return null;
|
|
541
541
|
}
|
|
542
542
|
}
|
|
543
|
+
/** Windows readlink may return backslashes for a link we created with slashes. */
|
|
544
|
+
function sameLinkTarget(actual, expected, platform = process.platform) {
|
|
545
|
+
if (actual === null) return false;
|
|
546
|
+
if (platform === "win32") return actual.replaceAll("\\", "/") === expected.replaceAll("\\", "/");
|
|
547
|
+
return actual === expected;
|
|
548
|
+
}
|
|
543
549
|
function isEmptyDir(path) {
|
|
544
550
|
try {
|
|
545
551
|
return lstatSync(path).isDirectory() && readdirSync$1(path).length === 0;
|
|
@@ -616,7 +622,7 @@ function detectConflict(claudeSkillsPath, expectedTarget) {
|
|
|
616
622
|
if (!existsSync$1(claudeSkillsPath) && !isSymlink(claudeSkillsPath)) return { kind: "none" };
|
|
617
623
|
if (isSymlink(claudeSkillsPath)) {
|
|
618
624
|
const target = symlinkTarget(claudeSkillsPath);
|
|
619
|
-
if (target
|
|
625
|
+
if (sameLinkTarget(target, expectedTarget)) return {
|
|
620
626
|
kind: "correct-symlink",
|
|
621
627
|
currentTarget: target ?? void 0
|
|
622
628
|
};
|
|
@@ -671,10 +677,35 @@ async function planLink(opts) {
|
|
|
671
677
|
default: return "skip";
|
|
672
678
|
}
|
|
673
679
|
}
|
|
680
|
+
var SymlinkUnavailableError = class extends Error {
|
|
681
|
+
constructor(cause) {
|
|
682
|
+
super(`cannot create symlinks here: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
683
|
+
this.name = "SymlinkUnavailableError";
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
/** Create and remove a throwaway link so a failure surfaces before anything is deleted. */
|
|
687
|
+
function probeSymlink(root) {
|
|
688
|
+
const probe = join(root, ".claude", `.lw-link-probe-${process.pid}`);
|
|
689
|
+
try {
|
|
690
|
+
createSymlink("probe-target", probe);
|
|
691
|
+
} catch (e) {
|
|
692
|
+
throw new SymlinkUnavailableError(e);
|
|
693
|
+
}
|
|
694
|
+
try {
|
|
695
|
+
unlinkSync(probe);
|
|
696
|
+
} catch {}
|
|
697
|
+
}
|
|
674
698
|
/** Carry out a planned action. Returns whether anything changed. */
|
|
675
699
|
function applyLink(root, action, dryRun) {
|
|
676
700
|
const claudeSkillsPath = join(root, ".claude", "skills");
|
|
677
701
|
const agentsSkillsPath = join(root, ".agents", "skills");
|
|
702
|
+
if (!dryRun && [
|
|
703
|
+
"create",
|
|
704
|
+
"repoint",
|
|
705
|
+
"replace-empty",
|
|
706
|
+
"merge",
|
|
707
|
+
"replace"
|
|
708
|
+
].includes(action)) probeSymlink(root);
|
|
678
709
|
switch (action) {
|
|
679
710
|
case "create":
|
|
680
711
|
if (dryRun) {
|
|
@@ -709,7 +740,13 @@ function applyLink(root, action, dryRun) {
|
|
|
709
740
|
case "merge":
|
|
710
741
|
case "replace":
|
|
711
742
|
if (dryRun) return false;
|
|
712
|
-
if (action === "merge")
|
|
743
|
+
if (action === "merge") {
|
|
744
|
+
const { copied, conflicts } = mergeIntoAgents(claudeSkillsPath, agentsSkillsPath);
|
|
745
|
+
if (conflicts.length > 0) {
|
|
746
|
+
log.warn(`Kept .claude/skills/ (differs from .agents/skills/: ${conflicts.join(", ")}); symlink skipped`);
|
|
747
|
+
return copied > 0;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
713
750
|
removeDir(claudeSkillsPath);
|
|
714
751
|
createSymlink(RELATIVE_TARGET, claudeSkillsPath);
|
|
715
752
|
log.success(action === "merge" ? "Merged existing skills and created symlink" : "Replaced .claude/skills/ with symlink");
|
|
@@ -721,14 +758,35 @@ function applyLink(root, action, dryRun) {
|
|
|
721
758
|
}
|
|
722
759
|
function mergeIntoAgents(source, dest) {
|
|
723
760
|
ensureDir(dest);
|
|
761
|
+
let copied = 0;
|
|
762
|
+
const conflicts = [];
|
|
724
763
|
for (const entry of readdirSync$1(source)) {
|
|
725
764
|
const srcPath = join(source, entry);
|
|
726
765
|
const destPath = join(dest, entry);
|
|
727
|
-
if (!
|
|
766
|
+
if (!lstatSync(destPath, { throwIfNoEntry: false })) {
|
|
728
767
|
cpSync(srcPath, destPath, { recursive: true });
|
|
768
|
+
copied++;
|
|
729
769
|
log.dim(` Merged ${entry}`);
|
|
730
|
-
} else
|
|
770
|
+
} else if (!sameEntry(srcPath, destPath)) conflicts.push(entry);
|
|
731
771
|
}
|
|
772
|
+
return {
|
|
773
|
+
copied,
|
|
774
|
+
conflicts
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
/** Structural equality without following symlinks. */
|
|
778
|
+
function sameEntry(a, b) {
|
|
779
|
+
const sa = lstatSync(a, { throwIfNoEntry: false });
|
|
780
|
+
const sb = lstatSync(b, { throwIfNoEntry: false });
|
|
781
|
+
if (!sa || !sb) return false;
|
|
782
|
+
if (sa.isSymbolicLink() || sb.isSymbolicLink()) return sa.isSymbolicLink() && sb.isSymbolicLink() && readlinkSync(a) === readlinkSync(b);
|
|
783
|
+
if (sa.isDirectory() && sb.isDirectory()) {
|
|
784
|
+
const na = readdirSync$1(a).sort();
|
|
785
|
+
const nb = readdirSync$1(b).sort();
|
|
786
|
+
return na.length === nb.length && na.every((n, i) => n === nb[i] && sameEntry(join(a, n), join(b, n)));
|
|
787
|
+
}
|
|
788
|
+
if (sa.isFile() && sb.isFile()) return readFileSync(a).equals(readFileSync(b));
|
|
789
|
+
return false;
|
|
732
790
|
}
|
|
733
791
|
// src/install/install.ts
|
|
734
792
|
/** Compute hook wiring results without writing. Throws SettingsParseError on malformed files. */
|
|
@@ -802,8 +860,11 @@ async function installAll(targets, writer, config, opts, link = "skip") {
|
|
|
802
860
|
const raw = readContent(`${sub}/${f}`);
|
|
803
861
|
await put(join(targets.lwHome, sub, f), sub === "hooks" ? raw : renderTemplate(raw, vars));
|
|
804
862
|
}
|
|
805
|
-
if (targets.claudeSkillsMode === "symlink") {
|
|
863
|
+
if (targets.claudeSkillsMode === "symlink") try {
|
|
806
864
|
if (applyLink(targets.root, link, opts.dryRun)) changed = true;
|
|
865
|
+
} catch (e) {
|
|
866
|
+
if (!(e instanceof SymlinkUnavailableError)) throw e;
|
|
867
|
+
log.warn(`${e.message}; leaving .claude/skills as-is (enable Developer Mode or run elevated on Windows)`);
|
|
807
868
|
}
|
|
808
869
|
if (!opts.hook) writer.carryPriorConfig(["claude-hooks", "codex-block"]);
|
|
809
870
|
for (const w of wiring) {
|
|
@@ -1007,7 +1068,7 @@ async function runUninstall(opts) {
|
|
|
1007
1068
|
writeFileSync(file, kind !== "codex-block" && out.trim() === "{}" ? "{}\n" : out, "utf-8");
|
|
1008
1069
|
}
|
|
1009
1070
|
const link = targets.claudeSkillsDir;
|
|
1010
|
-
if (lstatSync(link, { throwIfNoEntry: false })?.isSymbolicLink() &&
|
|
1071
|
+
if (lstatSync(link, { throwIfNoEntry: false })?.isSymbolicLink() && sameLinkTarget(symlinkTarget(link), "../.agents/skills")) {
|
|
1011
1072
|
log.dim(` ${would} ${link}`);
|
|
1012
1073
|
if (!dry) {
|
|
1013
1074
|
unlinkSync(link);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "loopwright",
|
|
3
3
|
"author": "Xuan Son Trinh",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"description": "Agent workflow installer for Claude Code, Codex and Junie",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
"pretest": "npm run build",
|
|
18
18
|
"test": "vitest run",
|
|
19
19
|
"dev": "npm run build && node dist/cli.js",
|
|
20
|
-
"prepublishOnly": "npm run build"
|
|
20
|
+
"prepublishOnly": "npm run build",
|
|
21
|
+
"format": "prettier --write .",
|
|
22
|
+
"format:check": "prettier --check ."
|
|
21
23
|
},
|
|
22
24
|
"files": [
|
|
23
25
|
"dist/",
|
|
@@ -35,10 +37,11 @@
|
|
|
35
37
|
"license": "MIT",
|
|
36
38
|
"devDependencies": {
|
|
37
39
|
"@types/node": "^22.20.5",
|
|
40
|
+
"prettier": "^3.9.10",
|
|
38
41
|
"smol-toml": "^1.9.1",
|
|
39
42
|
"tsdown": "^0.23.7",
|
|
40
43
|
"typescript": "^7.0.2",
|
|
41
44
|
"unrun": "^0.3.1",
|
|
42
|
-
"vitest": "^5.0
|
|
45
|
+
"vitest": "^5.1.0"
|
|
43
46
|
}
|
|
44
47
|
}
|