architecture-harness 0.23.0 → 0.25.0
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/LICENSE +21 -0
- package/package.json +20 -5
- package/src/cli/bash-command-guard.mjs +44 -5
- package/src/cli/bash-command-guard.test.mjs +35 -1
- package/src/cli/budget-guard.mjs +1 -0
- package/src/cli/budget-guard.test.mjs +7 -1
- package/src/cli/budget.mjs +3 -4
- package/src/cli/budget.test.mjs +5 -5
- package/src/cli/check-commands.mjs +82 -0
- package/src/cli/check-commands.test.mjs +190 -0
- package/src/cli/check.mjs +34 -13
- package/src/cli/ci-template.test.mjs +17 -15
- package/src/cli/cli.test.mjs +33 -2
- package/src/cli/commit-msg.mjs +12 -12
- package/src/cli/commit-msg.test.mjs +22 -7
- package/src/cli/generated-file-guard.mjs +11 -2
- package/src/cli/generated-file-guard.test.mjs +14 -2
- package/src/cli/init.mjs +70 -25
- package/src/cli/lint-changed.mjs +157 -0
- package/src/cli/lint-changed.test.mjs +102 -0
- package/src/cli/plugin-agents.test.mjs +1 -1
- package/src/cli/post-edit-gate.test.mjs +12 -3
- package/src/cli/pr-check.mjs +15 -2
- package/src/cli/pr-check.test.mjs +19 -9
- package/src/cli/qc.mjs +38 -7
- package/src/cli/scaffold.mjs +23 -2
- package/src/cli/scaffold.test.mjs +51 -0
- package/src/cli/shell-command.mjs +3 -1
- package/src/cli/stop-gate.test.mjs +49 -128
- package/src/cli/typecheck.mjs +46 -0
- package/src/cli/typecheck.test.mjs +43 -0
- package/src/cli/user-config-package.mjs +16 -0
- package/src/cli/user-config-toml.mjs +144 -0
- package/src/cli/user-config.mjs +155 -0
- package/src/cli/user-config.test.mjs +274 -0
- package/src/cli/work-order-guard.mjs +2 -2
- package/src/cli/worktree-gate.mjs +11 -12
- package/src/cli/worktree-gate.test.mjs +28 -29
- package/src/cli/worktree-isolation.mjs +3 -3
- package/src/cli/worktree-isolation.test.mjs +1 -1
- package/src/cli/worktree.mjs +9 -1
- package/src/cli/worktree.test.mjs +16 -1
- package/src/config.mjs +28 -11
- package/src/config.test.mjs +33 -1
- package/src/eslint/preset.mjs +8 -3
- package/src/eslint/preset.test.mjs +15 -8
- package/src/eslint/rules/no-raw-fetch.mjs +42 -2
- package/src/eslint/rules.test.mjs +15 -0
- package/src/gates/audit-append-only.mjs +18 -4
- package/src/gates/audit-append-only.test.mjs +19 -0
- package/src/gates/tenant-predicate.mjs +147 -7
- package/src/gates/tenant-predicate.test.mjs +55 -0
- package/src/gates/tenant-tables.mjs +5 -2
- package/src/gates/tenant-tables.test.mjs +8 -0
- package/src/gates/threshold-ratchet.mjs +12 -9
- package/src/gates/threshold-ratchet.test.mjs +16 -0
- package/src/plugin.test.mjs +28 -0
- package/src/screens/auth-run.test.mjs +20 -17
- package/src/screens/capture-idle.test.mjs +2 -1
- package/src/screens/catalog.test.mjs +3 -3
- package/src/screens/chrome.mjs +21 -0
- package/src/vale-style.test.mjs +5 -5
- package/templates/CLAUDE.md +2 -2
- package/templates/githooks/pre-commit +7 -10
- package/templates/github/workflows/ci.yml +25 -84
- package/templates/github/workflows/pr-policy.yml +36 -0
- package/user-config/claude/settings.shared.json +74 -0
- package/user-config/claude/statusline.js +323 -0
- package/user-config/codex/config.shared.toml +19 -0
- package/src/cli/edit-batch-guard.mjs +0 -77
- package/src/cli/edit-batch-guard.test.mjs +0 -131
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// Three-line powerline status line for Claude Code.
|
|
2
|
+
// Needs a Nerd Font in the terminal for the branch, arrow and building
|
|
3
|
+
// glyphs (U+E0A0, U+E0B0, U+F1AD); every other icon is plain Unicode.
|
|
4
|
+
'use strict';
|
|
5
|
+
|
|
6
|
+
const { execFileSync } = require('child_process');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const os = require('os');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
|
|
11
|
+
const ARROW = '';
|
|
12
|
+
const BRANCH_ICON = '';
|
|
13
|
+
const BUILDING_ICON = '';
|
|
14
|
+
|
|
15
|
+
const WHITE = [255, 255, 255];
|
|
16
|
+
const ORANGE_BROWN = [166, 88, 39];
|
|
17
|
+
const DARK_GREY = [58, 58, 58];
|
|
18
|
+
const SLATE = [92, 105, 138];
|
|
19
|
+
const RED = [178, 34, 34];
|
|
20
|
+
const NAVY = [16, 26, 58];
|
|
21
|
+
const GREEN = [30, 120, 60];
|
|
22
|
+
const BLUE = [30, 80, 170];
|
|
23
|
+
const AMBER = [196, 110, 0];
|
|
24
|
+
|
|
25
|
+
function fg(rgb) { return `\x1b[38;2;${rgb[0]};${rgb[1]};${rgb[2]}m`; }
|
|
26
|
+
function bg(rgb) { return `\x1b[48;2;${rgb[0]};${rgb[1]};${rgb[2]}m`; }
|
|
27
|
+
const RESET = '\x1b[0m';
|
|
28
|
+
|
|
29
|
+
// Renders one powerline row: each segment gets a solid background and white
|
|
30
|
+
// text, and an arrow that shades into the next segment's background (or
|
|
31
|
+
// fades to the terminal background after the last segment in the row).
|
|
32
|
+
function renderLine(segments) {
|
|
33
|
+
const visible = segments.filter(Boolean);
|
|
34
|
+
if (visible.length === 0) return null;
|
|
35
|
+
let out = '';
|
|
36
|
+
for (let i = 0; i < visible.length; i++) {
|
|
37
|
+
const seg = visible[i];
|
|
38
|
+
const next = visible[i + 1];
|
|
39
|
+
out += `${fg(WHITE)}${bg(seg.color)} ${seg.text} `;
|
|
40
|
+
out += next
|
|
41
|
+
? `${fg(seg.color)}${bg(next.color)}${ARROW}`
|
|
42
|
+
: `${RESET}${fg(seg.color)}${ARROW}${RESET}`;
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Replaces the home directory prefix with `~`, the way a shell prompt does.
|
|
48
|
+
function tildePath(dir) {
|
|
49
|
+
const home = os.homedir().replace(/\\/g, '/').replace(/\/$/, '');
|
|
50
|
+
const norm = dir.replace(/\\/g, '/');
|
|
51
|
+
if (norm.toLowerCase() === home.toLowerCase()) return '~';
|
|
52
|
+
if (norm.toLowerCase().startsWith(`${home.toLowerCase()}/`)) return `~${norm.slice(home.length)}`;
|
|
53
|
+
return norm;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// One git call carries branch, ahead/behind and dirty status together,
|
|
57
|
+
// instead of running three separate processes on every render.
|
|
58
|
+
function gitInfo(dir) {
|
|
59
|
+
let raw;
|
|
60
|
+
try {
|
|
61
|
+
raw = execFileSync(
|
|
62
|
+
'git',
|
|
63
|
+
['-C', dir, '--no-optional-locks', 'status', '--porcelain=v2', '--branch'],
|
|
64
|
+
{ stdio: ['ignore', 'pipe', 'ignore'] }
|
|
65
|
+
).toString();
|
|
66
|
+
} catch {
|
|
67
|
+
return null; // Not a repository, or git is missing.
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let branch = '';
|
|
71
|
+
let ahead = 0;
|
|
72
|
+
let behind = 0;
|
|
73
|
+
let dirty = false;
|
|
74
|
+
for (const line of raw.split('\n')) {
|
|
75
|
+
if (line.startsWith('# branch.head ')) {
|
|
76
|
+
branch = line.slice('# branch.head '.length).trim();
|
|
77
|
+
} else if (line.startsWith('# branch.ab ')) {
|
|
78
|
+
const m = line.match(/\+(\d+) -(\d+)/);
|
|
79
|
+
if (m) { ahead = Number(m[1]); behind = Number(m[2]); }
|
|
80
|
+
} else if (line && !line.startsWith('#')) {
|
|
81
|
+
dirty = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!branch || branch === '(detached)') {
|
|
86
|
+
try {
|
|
87
|
+
branch = execFileSync(
|
|
88
|
+
'git',
|
|
89
|
+
['-C', dir, '--no-optional-locks', 'rev-parse', '--short', 'HEAD'],
|
|
90
|
+
{ stdio: ['ignore', 'pipe', 'ignore'] }
|
|
91
|
+
).toString().trim();
|
|
92
|
+
} catch { /* leave branch as whatever branch.head reported */ }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return { branch, ahead, behind, dirty };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function gitSegmentText(info) {
|
|
99
|
+
let text = `${BRANCH_ICON} ${info.branch}`;
|
|
100
|
+
if (info.ahead > 0) text += ` ↑${info.ahead}`;
|
|
101
|
+
if (info.behind > 0) text += ` ↓${info.behind}`;
|
|
102
|
+
if (info.dirty) text += ' ●';
|
|
103
|
+
return text;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Formats a duration as "1h 52m" or "6d 13h", matching the two rate-limit
|
|
107
|
+
// reset examples: minutes only under an hour, hours dropped past a day.
|
|
108
|
+
function formatDuration(totalSeconds) {
|
|
109
|
+
const s = Math.max(0, Math.floor(totalSeconds));
|
|
110
|
+
const days = Math.floor(s / 86400);
|
|
111
|
+
const hours = Math.floor((s % 86400) / 3600);
|
|
112
|
+
const minutes = Math.floor((s % 3600) / 60);
|
|
113
|
+
if (days > 0) return `${days}d ${hours}h`;
|
|
114
|
+
if (hours > 0) return `${hours}h ${minutes}m`;
|
|
115
|
+
return `${minutes}m`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function dotBar(pct) {
|
|
119
|
+
const filled = Math.max(0, Math.min(10, Math.round(pct / 10)));
|
|
120
|
+
return '●'.repeat(filled) + '○'.repeat(10 - filled);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function boxBar(pct) {
|
|
124
|
+
const filled = Math.max(0, Math.min(10, Math.round(pct / 10)));
|
|
125
|
+
return '▮'.repeat(filled) + '▯'.repeat(10 - filled);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Context against the auto-compact window (autoCompactWindow in settings.json), not the model's 1M window.
|
|
129
|
+
const COMPACT_WINDOW = 233000;
|
|
130
|
+
function contextText(pct, size) {
|
|
131
|
+
if (typeof size !== 'number' || size <= 0) return `◔ ${Math.round(pct)}%`;
|
|
132
|
+
const used = (pct / 100) * size;
|
|
133
|
+
return `◔ ${Math.round(used / 1000)}K/${COMPACT_WINDOW / 1000}K (${Math.round((used / COMPACT_WINDOW) * 100)}%)`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The fraction of the seven-day window elapsed so far, as a percentage, so
|
|
137
|
+
// the pace segment can compare actual usage against the expected usage.
|
|
138
|
+
function weeklyExpectedPct(resetsAt, nowSec) {
|
|
139
|
+
const WINDOW = 7 * 86400;
|
|
140
|
+
const windowStart = resetsAt - WINDOW;
|
|
141
|
+
const elapsed = Math.min(WINDOW, Math.max(0, nowSec - windowStart));
|
|
142
|
+
return (elapsed / WINDOW) * 100;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function configDir() {
|
|
146
|
+
return process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// The account email lives in .claude.json, next to (or above) the settings
|
|
150
|
+
// the status line command itself is read from, never in the stdin payload.
|
|
151
|
+
function readAccountEmail() {
|
|
152
|
+
const candidates = [];
|
|
153
|
+
if (process.env.CLAUDE_CONFIG_DIR) candidates.push(path.join(process.env.CLAUDE_CONFIG_DIR, '.claude.json'));
|
|
154
|
+
candidates.push(path.join(os.homedir(), '.claude.json'));
|
|
155
|
+
for (const file of candidates) {
|
|
156
|
+
try {
|
|
157
|
+
const email = JSON.parse(fs.readFileSync(file, 'utf8'))?.oauthAccount?.emailAddress;
|
|
158
|
+
if (email) return email;
|
|
159
|
+
} catch { /* try the next candidate */ }
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---- Codex usage ----------------------------------------------------------
|
|
165
|
+
const CODEX_SESSIONS = path.join(process.env.CODEX_HOME || path.join(os.homedir(), '.codex'), 'sessions');
|
|
166
|
+
const CODEX_CACHE = path.join(configDir(), 'codex-usage-cache.json');
|
|
167
|
+
const CODEX_TTL_SEC = 60;
|
|
168
|
+
const CODEX_TAIL_BYTES = 256 * 1024;
|
|
169
|
+
|
|
170
|
+
function dayDir(d) {
|
|
171
|
+
const p = (n) => String(n).padStart(2, '0');
|
|
172
|
+
return path.join(CODEX_SESSIONS, String(d.getFullYear()), p(d.getMonth() + 1), p(d.getDate()));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function newestCodexFiles(count) {
|
|
176
|
+
const now = new Date();
|
|
177
|
+
const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
|
|
178
|
+
const files = [];
|
|
179
|
+
for (const d of [now, yesterday]) {
|
|
180
|
+
const dir = dayDir(d);
|
|
181
|
+
let names;
|
|
182
|
+
try { names = fs.readdirSync(dir); } catch { continue; }
|
|
183
|
+
for (const n of names) {
|
|
184
|
+
if (!n.endsWith('.jsonl')) continue;
|
|
185
|
+
const full = path.join(dir, n);
|
|
186
|
+
try { files.push({ full, mtime: fs.statSync(full).mtimeMs }); } catch { /* skip */ }
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return files.sort((a, b) => b.mtime - a.mtime).slice(0, count);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function lastRateLimits(file) {
|
|
193
|
+
const fd = fs.openSync(file, 'r');
|
|
194
|
+
try {
|
|
195
|
+
const size = fs.fstatSync(fd).size;
|
|
196
|
+
const len = Math.min(size, CODEX_TAIL_BYTES);
|
|
197
|
+
const buf = Buffer.alloc(len);
|
|
198
|
+
fs.readSync(fd, buf, 0, len, size - len);
|
|
199
|
+
const lines = buf.toString('utf8').split('\n');
|
|
200
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
201
|
+
if (!lines[i].includes('"rate_limits":')) continue;
|
|
202
|
+
try {
|
|
203
|
+
const obj = JSON.parse(lines[i]);
|
|
204
|
+
const rl = obj?.payload?.rate_limits ?? obj?.rate_limits ?? obj?.payload?.info?.rate_limits;
|
|
205
|
+
if (rl && typeof rl === 'object' && (rl.primary || rl.secondary)) return rl;
|
|
206
|
+
} catch { /* partial first line or unrelated JSON: keep looking */ }
|
|
207
|
+
}
|
|
208
|
+
} finally {
|
|
209
|
+
fs.closeSync(fd);
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function codexWindows(nowSec) {
|
|
215
|
+
let cached;
|
|
216
|
+
try {
|
|
217
|
+
const c = JSON.parse(fs.readFileSync(CODEX_CACHE, 'utf8'));
|
|
218
|
+
if (typeof c.at === 'number' && nowSec - c.at < CODEX_TTL_SEC) cached = c;
|
|
219
|
+
} catch { /* no usable cache */ }
|
|
220
|
+
if (cached) return cached.windows;
|
|
221
|
+
|
|
222
|
+
let windows = null;
|
|
223
|
+
for (const f of newestCodexFiles(3)) {
|
|
224
|
+
let rl = null;
|
|
225
|
+
try { rl = lastRateLimits(f.full); } catch { /* try the next file */ }
|
|
226
|
+
if (rl) {
|
|
227
|
+
windows = [rl.primary, rl.secondary]
|
|
228
|
+
.filter((w) => w && typeof w.used_percent === 'number' && typeof w.resets_at === 'number')
|
|
229
|
+
.map((w) => ({ used: w.used_percent, minutes: w.window_minutes, resetsAt: w.resets_at }));
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
try { fs.writeFileSync(CODEX_CACHE, JSON.stringify({ at: nowSec, windows })); } catch { /* cache is optional */ }
|
|
234
|
+
return windows;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function codexSegment(nowSec) {
|
|
238
|
+
try {
|
|
239
|
+
const windows = codexWindows(nowSec);
|
|
240
|
+
if (!windows || windows.length === 0) return null;
|
|
241
|
+
const parts = [];
|
|
242
|
+
let minLeft = 100;
|
|
243
|
+
windows.forEach((w, i) => {
|
|
244
|
+
const label = i === 0 ? 'codex' : `codex ${w.minutes === 300 ? '5h' : w.minutes ? `${Math.round(w.minutes / 60)}h` : '2nd'}`;
|
|
245
|
+
if (w.resetsAt <= nowSec) { parts.push(`${label} reset`); return; }
|
|
246
|
+
const left = Math.max(0, Math.min(100, Math.round(100 - w.used)));
|
|
247
|
+
minLeft = Math.min(minLeft, left);
|
|
248
|
+
const d = new Date(w.resetsAt * 1000);
|
|
249
|
+
const day = d.toLocaleDateString('en-US', { weekday: 'short' });
|
|
250
|
+
const hm = `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
|
251
|
+
const sameDay = d.toDateString() === new Date(nowSec * 1000).toDateString();
|
|
252
|
+
parts.push(`${label} ${left}% left · resets ${sameDay ? hm : `${day} ${hm}`}`);
|
|
253
|
+
});
|
|
254
|
+
const color = minLeft > 50 ? GREEN : minLeft >= 20 ? AMBER : RED;
|
|
255
|
+
return { text: parts.join(' · '), color };
|
|
256
|
+
} catch {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
let raw = '';
|
|
262
|
+
process.stdin.on('data', (d) => { raw += d; }).on('end', () => {
|
|
263
|
+
let j = {};
|
|
264
|
+
try { j = JSON.parse(raw); } catch { /* render what defaults allow */ }
|
|
265
|
+
|
|
266
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
267
|
+
const dir = j.workspace?.current_dir || j.cwd || process.cwd();
|
|
268
|
+
const git = gitInfo(dir);
|
|
269
|
+
const ctxPct = j.context_window?.used_percentage;
|
|
270
|
+
const model = j.model?.display_name;
|
|
271
|
+
const fiveHour = j.rate_limits?.five_hour;
|
|
272
|
+
const sevenDay = j.rate_limits?.seven_day;
|
|
273
|
+
const email = readAccountEmail();
|
|
274
|
+
|
|
275
|
+
// Leave the weekly usage where an agent can read it, since no tool exposes it.
|
|
276
|
+
if (sevenDay) {
|
|
277
|
+
try {
|
|
278
|
+
require('fs').writeFileSync(
|
|
279
|
+
require('path').join(process.env.CLAUDE_CONFIG_DIR || require('os').homedir() + '/.claude', 'weekly-usage.json'),
|
|
280
|
+
JSON.stringify({ used_percentage: sevenDay.used_percentage, resets_at: sevenDay.resets_at, at: nowSec }),
|
|
281
|
+
);
|
|
282
|
+
} catch { /* the status line must render even if the write fails */ }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const line1 = [
|
|
286
|
+
{ text: tildePath(dir), color: ORANGE_BROWN },
|
|
287
|
+
git ? { text: gitSegmentText(git), color: DARK_GREY } : null,
|
|
288
|
+
typeof ctxPct === 'number' ? { text: contextText(ctxPct, j.context_window?.context_window_size), color: SLATE } : null,
|
|
289
|
+
];
|
|
290
|
+
|
|
291
|
+
const line2 = [
|
|
292
|
+
model ? { text: `✱ ${model}`, color: RED } : null,
|
|
293
|
+
fiveHour ? {
|
|
294
|
+
text: `◱ ${dotBar(fiveHour.used_percentage)} ${Math.round(fiveHour.used_percentage)}% (${formatDuration(fiveHour.resets_at - nowSec)})`,
|
|
295
|
+
color: NAVY,
|
|
296
|
+
} : null,
|
|
297
|
+
sevenDay ? {
|
|
298
|
+
text: `◑ ${boxBar(sevenDay.used_percentage)} ${Math.round(sevenDay.used_percentage)}% (${formatDuration(sevenDay.resets_at - nowSec)})`,
|
|
299
|
+
color: GREEN,
|
|
300
|
+
} : null,
|
|
301
|
+
];
|
|
302
|
+
|
|
303
|
+
let weeklyPace = null;
|
|
304
|
+
if (sevenDay) {
|
|
305
|
+
const used = Math.round(sevenDay.used_percentage);
|
|
306
|
+
const expected = Math.round(weeklyExpectedPct(sevenDay.resets_at, nowSec));
|
|
307
|
+
const onPace = used <= expected;
|
|
308
|
+
weeklyPace = {
|
|
309
|
+
text: `${onPace ? '✓' : '⚠'} weekly ${used}% ${onPace ? '≤' : '>'} ${expected}%`,
|
|
310
|
+
color: onPace ? GREEN : AMBER,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const line3 = [
|
|
315
|
+
email ? { text: `⚙ ${email}`, color: DARK_GREY } : null,
|
|
316
|
+
{ text: `${BUILDING_ICON} ${path.basename(configDir())}`, color: BLUE },
|
|
317
|
+
weeklyPace,
|
|
318
|
+
codexSegment(nowSec),
|
|
319
|
+
];
|
|
320
|
+
|
|
321
|
+
const out = [renderLine(line1), renderLine(line2), renderLine(line3)].filter(Boolean).join('\n');
|
|
322
|
+
process.stdout.write(out);
|
|
323
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
approval_policy = "never"
|
|
2
|
+
default_permissions = ":danger-full-access"
|
|
3
|
+
model = "gpt-6.1-sol"
|
|
4
|
+
model_reasoning_effort = "medium"
|
|
5
|
+
model_auto_compact_token_limit = 300000
|
|
6
|
+
model_post_turn_compact_threshold_percent = 40
|
|
7
|
+
service_tier = "default"
|
|
8
|
+
|
|
9
|
+
[plugins."superpowers@claude-plugins-official"]
|
|
10
|
+
enabled = true
|
|
11
|
+
|
|
12
|
+
[plugins."cocoindex-code@cocoindex-code"]
|
|
13
|
+
enabled = false
|
|
14
|
+
|
|
15
|
+
[plugins."architecture@architecture-kit"]
|
|
16
|
+
enabled = false
|
|
17
|
+
|
|
18
|
+
[plugins."frontend-design@claude-plugins-official"]
|
|
19
|
+
enabled = true
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// The trigger for swarm.md's one edit per file per parallel block. PreToolUse claims the file for the
|
|
3
|
-
// batch and denies a second claim. PostToolBatch ends the batch, and a claim expires as a last resort.
|
|
4
|
-
|
|
5
|
-
import { existsSync } from "node:fs";
|
|
6
|
-
import os from "node:os";
|
|
7
|
-
import path from "node:path";
|
|
8
|
-
import { fileURLToPath } from "node:url";
|
|
9
|
-
import { CONFIG_FILE } from "../config.mjs";
|
|
10
|
-
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
11
|
-
import { batchDirOf, claimPath, endBatch, isBatchAware, releasePath } from "./edit-batch-state.mjs";
|
|
12
|
-
|
|
13
|
-
const EDIT_TOOLS = new Set(["Write", "Edit", "MultiEdit"]);
|
|
14
|
-
export const CLAIM_TTL_MS = 60_000;
|
|
15
|
-
|
|
16
|
-
/** One file as one key: absolute, forward slashes, and lowercase on Windows. */
|
|
17
|
-
export function pathKey(file, cwd, platform = process.platform) {
|
|
18
|
-
const paths = platform === "win32" ? path.win32 : path.posix;
|
|
19
|
-
const slashed = paths.resolve(cwd, file).split(paths.sep).join("/");
|
|
20
|
-
return platform === "win32" ? slashed.toLowerCase() : slashed;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const deny = (file) => ({
|
|
24
|
-
hookSpecificOutput: {
|
|
25
|
-
hookEventName: "PreToolUse",
|
|
26
|
-
permissionDecision: "deny",
|
|
27
|
-
permissionDecisionReason:
|
|
28
|
-
`Edit batch guard: one edit per file per parallel block: send it after this result. Another edit ` +
|
|
29
|
-
`of ${file} is in this block, and two edits of one file race and lose all but the last.`,
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
/** The verdict on one hook event. @returns the hook output, or null to let the call run with no message. */
|
|
34
|
-
export function decide(call, tmp = os.tmpdir(), now = Date.now()) {
|
|
35
|
-
const agentKey = typeof call.agent_id === "string" && call.agent_id !== "" ? call.agent_id : "main";
|
|
36
|
-
const dir = batchDirOf(call.session_id ?? "session", agentKey, tmp);
|
|
37
|
-
try {
|
|
38
|
-
if (call.hook_event_name === "PostToolBatch") {
|
|
39
|
-
// Only a claim makes the folder, so a repository with no config gets no file here.
|
|
40
|
-
if (existsSync(dir)) endBatch(dir);
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
const file = call.tool_input?.file_path;
|
|
44
|
-
if (!EDIT_TOOLS.has(call.tool_name) || typeof file !== "string" || file === "") return null;
|
|
45
|
-
const key = pathKey(file, call.cwd ?? process.cwd());
|
|
46
|
-
if (call.hook_event_name === "PostToolUse") {
|
|
47
|
-
// A client with no batch event ends the claim with the result, so a later edit is never refused.
|
|
48
|
-
if (!isBatchAware(dir)) releasePath(dir, key);
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
if (call.hook_event_name !== "PreToolUse") return null;
|
|
52
|
-
const root = checkoutRootOf(path.dirname(path.resolve(call.cwd ?? process.cwd(), file)));
|
|
53
|
-
if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
|
|
54
|
-
return claimPath(dir, key, now, CLAIM_TTL_MS) ? null : deny(file);
|
|
55
|
-
} catch {
|
|
56
|
-
// A busy or read-only temp folder is the hook's own problem, never a reason to refuse an edit.
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async function readStdin() {
|
|
62
|
-
let data = "";
|
|
63
|
-
for await (const chunk of process.stdin) data += chunk;
|
|
64
|
-
return data;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const isEntryPoint = process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1];
|
|
68
|
-
if (isEntryPoint) {
|
|
69
|
-
let call = null;
|
|
70
|
-
try {
|
|
71
|
-
call = JSON.parse(await readStdin());
|
|
72
|
-
} catch {
|
|
73
|
-
// Not hook input, so there is nothing to judge.
|
|
74
|
-
}
|
|
75
|
-
const output = call ? decide(call) : null;
|
|
76
|
-
if (output) process.stdout.write(JSON.stringify(output));
|
|
77
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { strict as assert } from "node:assert";
|
|
2
|
-
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
-
import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
-
import os from "node:os";
|
|
5
|
-
import path from "node:path";
|
|
6
|
-
import { test } from "node:test";
|
|
7
|
-
import { fileURLToPath } from "node:url";
|
|
8
|
-
import { CLAIM_TTL_MS, decide, pathKey } from "./edit-batch-guard.mjs";
|
|
9
|
-
|
|
10
|
-
const GUARD = fileURLToPath(new URL("./edit-batch-guard.mjs", import.meta.url));
|
|
11
|
-
|
|
12
|
-
/** An adopted checkout, a checkout with no config, and a temp folder for the batch state. */
|
|
13
|
-
function workspace(t) {
|
|
14
|
-
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-edit-batch-")));
|
|
15
|
-
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
16
|
-
const adopted = path.join(base, "adopted");
|
|
17
|
-
const plain = path.join(base, "plain");
|
|
18
|
-
const tmp = path.join(base, "tmp");
|
|
19
|
-
for (const dir of [adopted, plain, tmp]) mkdirSync(dir);
|
|
20
|
-
execFileSync("git", ["init", "-q"], { cwd: adopted, stdio: "pipe" });
|
|
21
|
-
execFileSync("git", ["init", "-q"], { cwd: plain, stdio: "pipe" });
|
|
22
|
-
writeFileSync(path.join(adopted, "qc.config.json"), "{}");
|
|
23
|
-
return { adopted, plain, tmp, file: path.join(adopted, "src", "a.ts") };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const callOf = (hook_event_name, file, { agent, tool = "Edit" } = {}) => ({
|
|
27
|
-
session_id: "session-aaaa",
|
|
28
|
-
...(agent ? { agent_id: agent, agent_type: "general-purpose" } : {}),
|
|
29
|
-
cwd: path.dirname(path.dirname(file)),
|
|
30
|
-
hook_event_name,
|
|
31
|
-
tool_name: tool,
|
|
32
|
-
tool_input: { file_path: file, old_string: "a", new_string: "b" },
|
|
33
|
-
});
|
|
34
|
-
const batchEnd = (agent) => ({
|
|
35
|
-
session_id: "session-aaaa",
|
|
36
|
-
...(agent ? { agent_id: agent } : {}),
|
|
37
|
-
hook_event_name: "PostToolBatch",
|
|
38
|
-
tool_calls: [],
|
|
39
|
-
});
|
|
40
|
-
const isDenied = (output) => output?.hookSpecificOutput?.permissionDecision === "deny";
|
|
41
|
-
|
|
42
|
-
test("two edits to one path in one batch: the second is denied and told to wait for the result", (t) => {
|
|
43
|
-
const ws = workspace(t);
|
|
44
|
-
assert.equal(decide(callOf("PreToolUse", ws.file), ws.tmp), null);
|
|
45
|
-
const output = decide(callOf("PreToolUse", ws.file, { tool: "Write" }), ws.tmp);
|
|
46
|
-
assert.ok(isDenied(output));
|
|
47
|
-
assert.equal(output.hookSpecificOutput.hookEventName, "PreToolUse");
|
|
48
|
-
assert.match(output.hookSpecificOutput.permissionDecisionReason, /one edit per file per parallel block: send it after this result/);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("a sequential pair passes: the batch event ends the claim", (t) => {
|
|
52
|
-
const ws = workspace(t);
|
|
53
|
-
assert.equal(decide(callOf("PreToolUse", ws.file, { tool: "MultiEdit" }), ws.tmp), null);
|
|
54
|
-
decide(callOf("PostToolUse", ws.file), ws.tmp);
|
|
55
|
-
decide(batchEnd(), ws.tmp);
|
|
56
|
-
assert.equal(decide(callOf("PreToolUse", ws.file), ws.tmp), null);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test("a batch event before any claim writes nothing", (t) => {
|
|
60
|
-
const ws = workspace(t);
|
|
61
|
-
decide(batchEnd(), ws.tmp);
|
|
62
|
-
assert.equal(existsSync(path.join(ws.tmp, "architecture-kit", "edit-batch")), false);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test("once the agent has seen a batch event, its result alone does not end the claim", (t) => {
|
|
66
|
-
const ws = workspace(t);
|
|
67
|
-
decide(callOf("PreToolUse", ws.file), ws.tmp);
|
|
68
|
-
decide(batchEnd(), ws.tmp);
|
|
69
|
-
assert.equal(decide(callOf("PreToolUse", ws.file), ws.tmp), null);
|
|
70
|
-
decide(callOf("PostToolUse", ws.file), ws.tmp);
|
|
71
|
-
assert.ok(isDenied(decide(callOf("PreToolUse", ws.file), ws.tmp)));
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("before any batch event, the result of the edit ends the claim", (t) => {
|
|
75
|
-
const ws = workspace(t);
|
|
76
|
-
assert.equal(decide(callOf("PreToolUse", ws.file, { agent: "agent-1" }), ws.tmp), null);
|
|
77
|
-
decide(callOf("PostToolUse", ws.file, { agent: "agent-1" }), ws.tmp);
|
|
78
|
-
assert.equal(decide(callOf("PreToolUse", ws.file, { agent: "agent-1" }), ws.tmp), null);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test("a claim left behind expires", (t) => {
|
|
82
|
-
const ws = workspace(t);
|
|
83
|
-
decide(batchEnd(), ws.tmp);
|
|
84
|
-
assert.equal(decide(callOf("PreToolUse", ws.file), ws.tmp), null);
|
|
85
|
-
assert.ok(isDenied(decide(callOf("PreToolUse", ws.file), ws.tmp)));
|
|
86
|
-
assert.equal(decide(callOf("PreToolUse", ws.file), ws.tmp, Date.now() + CLAIM_TTL_MS + 1000), null);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test("different agents, and the main session, do not block each other", (t) => {
|
|
90
|
-
const ws = workspace(t);
|
|
91
|
-
assert.equal(decide(callOf("PreToolUse", ws.file, { agent: "agent-1" }), ws.tmp), null);
|
|
92
|
-
assert.equal(decide(callOf("PreToolUse", ws.file, { agent: "agent-2" }), ws.tmp), null);
|
|
93
|
-
assert.equal(decide(callOf("PreToolUse", ws.file), ws.tmp), null);
|
|
94
|
-
assert.ok(isDenied(decide(callOf("PreToolUse", ws.file, { agent: "agent-2" }), ws.tmp)));
|
|
95
|
-
decide(batchEnd("agent-2"), ws.tmp);
|
|
96
|
-
assert.equal(decide(callOf("PreToolUse", ws.file, { agent: "agent-2" }), ws.tmp), null);
|
|
97
|
-
assert.ok(isDenied(decide(callOf("PreToolUse", ws.file, { agent: "agent-1" }), ws.tmp)));
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test("a repository with no qc.config.json is never guarded, and other tools are ignored", (t) => {
|
|
101
|
-
const ws = workspace(t);
|
|
102
|
-
const file = path.join(ws.plain, "src", "a.ts");
|
|
103
|
-
decide(callOf("PreToolUse", file), ws.tmp);
|
|
104
|
-
assert.equal(decide(callOf("PreToolUse", file), ws.tmp), null);
|
|
105
|
-
decide(callOf("PreToolUse", ws.file, { tool: "Read" }), ws.tmp);
|
|
106
|
-
assert.equal(decide(callOf("PreToolUse", ws.file), ws.tmp), null);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test("a path key is absolute with forward slashes, and case-blind on Windows only", () => {
|
|
110
|
-
assert.equal(pathKey("C:\\Repo\\Src\\A.ts", "C:\\", "win32"), "c:/repo/src/a.ts");
|
|
111
|
-
assert.equal(pathKey("src/A.ts", "C:\\Repo", "win32"), "c:/repo/src/a.ts");
|
|
112
|
-
assert.equal(pathKey("/repo/Src/A.ts", "/", "linux"), "/repo/Src/A.ts");
|
|
113
|
-
assert.equal(pathKey("A.ts", "/repo", "linux"), "/repo/A.ts");
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test("the hook process prints a deny for the second claim and nothing for the batch event", (t) => {
|
|
117
|
-
const ws = workspace(t);
|
|
118
|
-
const run = (call) => {
|
|
119
|
-
const result = spawnSync(process.execPath, [GUARD], {
|
|
120
|
-
input: JSON.stringify(call),
|
|
121
|
-
env: { ...process.env, TEMP: ws.tmp, TMP: ws.tmp, TMPDIR: ws.tmp },
|
|
122
|
-
encoding: "utf8",
|
|
123
|
-
});
|
|
124
|
-
assert.equal(result.status, 0, result.stderr);
|
|
125
|
-
return result.stdout.trim() === "" ? null : JSON.parse(result.stdout);
|
|
126
|
-
};
|
|
127
|
-
assert.equal(run(callOf("PreToolUse", ws.file)), null);
|
|
128
|
-
assert.ok(isDenied(run(callOf("PreToolUse", ws.file))));
|
|
129
|
-
assert.equal(run(batchEnd()), null);
|
|
130
|
-
assert.equal(run(callOf("PreToolUse", ws.file)), null);
|
|
131
|
-
});
|