@stupify/cli 0.0.15 → 0.1.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.
Files changed (74) hide show
  1. package/.review/CORPUS.md +73 -0
  2. package/.review/REVIEW-PROMPT.md +52 -0
  3. package/.review/RUBRIC.md +46 -0
  4. package/LICENSE +1 -1
  5. package/README.md +41 -39
  6. package/package.json +24 -25
  7. package/src/cli.ts +358 -0
  8. package/src/review-sweep.ts +492 -0
  9. package/dist/analysis.d.ts +0 -16
  10. package/dist/analysis.js +0 -165
  11. package/dist/cache.d.ts +0 -2
  12. package/dist/cache.js +0 -57
  13. package/dist/checks.d.ts +0 -4
  14. package/dist/checks.js +0 -228
  15. package/dist/command.d.ts +0 -2
  16. package/dist/command.js +0 -147
  17. package/dist/constants.d.ts +0 -4
  18. package/dist/constants.js +0 -53
  19. package/dist/counter-scout.d.ts +0 -21
  20. package/dist/counter-scout.js +0 -167
  21. package/dist/diff.d.ts +0 -1
  22. package/dist/diff.js +0 -10
  23. package/dist/doctor.d.ts +0 -4
  24. package/dist/doctor.js +0 -131
  25. package/dist/git.d.ts +0 -12
  26. package/dist/git.js +0 -298
  27. package/dist/hooks.d.ts +0 -3
  28. package/dist/hooks.js +0 -117
  29. package/dist/index.d.ts +0 -1
  30. package/dist/index.js +0 -1
  31. package/dist/model.d.ts +0 -11
  32. package/dist/model.js +0 -296
  33. package/dist/prompts.d.ts +0 -8
  34. package/dist/prompts.js +0 -89
  35. package/dist/render.d.ts +0 -3
  36. package/dist/render.js +0 -151
  37. package/dist/repomix-provider.d.ts +0 -12
  38. package/dist/repomix-provider.js +0 -196
  39. package/dist/search-bench.d.ts +0 -1
  40. package/dist/search-bench.js +0 -677
  41. package/dist/search-profile.d.ts +0 -6
  42. package/dist/search-profile.js +0 -73
  43. package/dist/sem-provider.d.ts +0 -2
  44. package/dist/sem-provider.js +0 -252
  45. package/dist/stupify.d.ts +0 -38
  46. package/dist/stupify.js +0 -474
  47. package/dist/trace.d.ts +0 -31
  48. package/dist/trace.js +0 -86
  49. package/dist/types.d.ts +0 -328
  50. package/dist/types.js +0 -6
  51. package/dist/ui.d.ts +0 -34
  52. package/dist/ui.js +0 -143
  53. package/src/analysis.ts +0 -220
  54. package/src/cache.ts +0 -63
  55. package/src/checks.ts +0 -231
  56. package/src/command.ts +0 -173
  57. package/src/constants.ts +0 -56
  58. package/src/counter-scout.ts +0 -195
  59. package/src/diff.ts +0 -9
  60. package/src/doctor.ts +0 -140
  61. package/src/git.ts +0 -306
  62. package/src/hooks.ts +0 -134
  63. package/src/index.ts +0 -1
  64. package/src/model.ts +0 -367
  65. package/src/prompts.ts +0 -100
  66. package/src/render.ts +0 -154
  67. package/src/repomix-provider.ts +0 -219
  68. package/src/search-bench.ts +0 -783
  69. package/src/search-profile.ts +0 -89
  70. package/src/sem-provider.ts +0 -297
  71. package/src/stupify.ts +0 -571
  72. package/src/trace.ts +0 -126
  73. package/src/types.ts +0 -348
  74. package/src/ui.ts +0 -187
@@ -1,89 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import path from "node:path";
3
- import { defaultChecks, searchChecks } from "./checks.ts";
4
- import type {
5
- RepomixSearchConfig,
6
- SearchProfile,
7
- SearchProfilePattern,
8
- StupifyCheck,
9
- } from "./types.ts";
10
-
11
- export async function loadSearchProfile(profilePath: string | null): Promise<SearchProfile | null> {
12
- if (!profilePath) return null;
13
- const fullPath = path.resolve(profilePath);
14
- const value = JSON.parse(await readFile(fullPath, "utf8")) as SearchProfile;
15
- if (!value || typeof value !== "object" || typeof value.id !== "string" || value.id.length === 0) {
16
- throw new Error(`Invalid search profile: ${profilePath}`);
17
- }
18
- return value;
19
- }
20
-
21
- export function effectiveSearchChecks(
22
- explicitCheckIds: readonly string[] | null,
23
- profile: SearchProfile | null,
24
- ): readonly StupifyCheck[] {
25
- const checks = explicitCheckIds
26
- ? searchChecks(explicitCheckIds)
27
- : profilePatternIds(profile).length > 0
28
- ? checksById(profilePatternIds(profile))
29
- : searchChecks(null);
30
-
31
- if (!profile?.patterns) return checks;
32
- return checks.map((check) => applyPatternOverride(check, profile.patterns?.[check.id]));
33
- }
34
-
35
- export function effectiveMaxCandidates(defaultValue: number, profile: SearchProfile | null): number {
36
- return positiveInteger(profile?.maxCandidates) ?? defaultValue;
37
- }
38
-
39
- export function effectiveMaxSearchInputTokens(defaultValue: number, profile: SearchProfile | null): number {
40
- return positiveInteger(profile?.maxSearchInputTokens) ?? defaultValue;
41
- }
42
-
43
- export function effectiveRepomixConfig(
44
- defaultValue: RepomixSearchConfig,
45
- profile: SearchProfile | null,
46
- ): RepomixSearchConfig {
47
- const override = profile?.repomix;
48
- if (!override) return defaultValue;
49
- return {
50
- compress: override.compress ?? defaultValue.compress,
51
- showLineNumbers: override.showLineNumbers ?? defaultValue.showLineNumbers,
52
- removeEmptyLines: override.removeEmptyLines ?? defaultValue.removeEmptyLines,
53
- maxFileSizeBytes: positiveInteger(override.maxFileBytes) ?? defaultValue.maxFileSizeBytes,
54
- maxTotalSizeBytes: positiveInteger(override.maxTotalBytes) ?? defaultValue.maxTotalSizeBytes,
55
- ignorePatterns: override.ignorePatterns ?? defaultValue.ignorePatterns,
56
- };
57
- }
58
-
59
- function profilePatternIds(profile: SearchProfile | null): readonly string[] {
60
- if (!profile?.patterns) return [];
61
- return Object.entries(profile.patterns)
62
- .filter(([, pattern]) => pattern.enabled === true)
63
- .map(([id]) => id);
64
- }
65
-
66
- function checksById(ids: readonly string[]): readonly StupifyCheck[] {
67
- const byId = new Map<string, StupifyCheck>(defaultChecks.map((check) => [check.id, check]));
68
- return ids.map((id) => {
69
- const check = byId.get(id);
70
- if (!check) throw new Error(`Unknown check in search profile: ${id}`);
71
- return check;
72
- });
73
- }
74
-
75
- function applyPatternOverride(check: StupifyCheck, override: SearchProfilePattern | undefined): StupifyCheck {
76
- if (!override) return check;
77
- return {
78
- ...check,
79
- searchPrompt: override.searchPrompt ?? check.searchPrompt,
80
- searchExamples: {
81
- match: override.matchExamples ?? check.searchExamples?.match ?? check.examples?.match ?? [],
82
- nonMatch: override.nonMatchExamples ?? check.searchExamples?.nonMatch ?? check.examples?.noMatch ?? [],
83
- },
84
- };
85
- }
86
-
87
- function positiveInteger(value: unknown): number | null {
88
- return Number.isInteger(value) && Number(value) > 0 ? Number(value) : null;
89
- }
@@ -1,297 +0,0 @@
1
- import { execFile, spawn } from "node:child_process";
2
- import { mkdtemp, realpath, rm } from "node:fs/promises";
3
- import { createRequire } from "node:module";
4
- import { tmpdir } from "node:os";
5
- import path from "node:path";
6
- import { promisify } from "node:util";
7
- import { cachedJson, fingerprint } from "./cache.ts";
8
- import { readDiffFromStdin } from "./diff.ts";
9
- import {
10
- gitUserLabel,
11
- sourceRangeForCommit,
12
- sourceRangeForRecentCommits,
13
- sourceRangeSince,
14
- stagedDiff,
15
- } from "./git.ts";
16
- import { diagnostic } from "./ui.ts";
17
- import type {
18
- SearchCommand,
19
- SemChange,
20
- SemChangeSet,
21
- SemChangeSummary,
22
- SourceRange,
23
- } from "./types.ts";
24
- import { sourceId } from "./types.ts";
25
-
26
- const execFileAsync = promisify(execFile);
27
-
28
- export async function semChangeSetForCommand(
29
- command: SearchCommand,
30
- ): Promise<SemChangeSet> {
31
- if (command.kind === "stdin") return semChangeSetFromPatch(await readDiffFromStdin(), command.debugSem, "stdin", ["stdin"]);
32
- if (command.kind === "staged") {
33
- const [diff, committer] = await Promise.all([stagedDiff(), gitUserLabel()]);
34
- const committers = [committer];
35
- if (!diff.text.trim()) return emptyChangeSet("staged", diff.stats, committers);
36
- return semChangeSetFromPatch(diff.text, command.debugSem, "staged", committers);
37
- }
38
- if (command.kind === "commit") {
39
- const range = await sourceRangeForCommit(command.commit);
40
- const raw = await cachedSemDiff(
41
- ["diff", "--commit", command.commit, "--format", "json"],
42
- range,
43
- command.debugSem,
44
- );
45
- return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
46
- }
47
-
48
- const range = await semRangeForCommand(command);
49
- const raw = await cachedSemDiff(
50
- ["diff", "--from", range.base, "--to", range.target, "--format", "json"],
51
- range,
52
- command.debugSem,
53
- );
54
- return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
55
- }
56
-
57
- function emptyChangeSet(
58
- label: string,
59
- stats: SourceRange["stats"],
60
- committers?: readonly string[],
61
- ): SemChangeSet {
62
- return {
63
- id: sourceId(label),
64
- label,
65
- base: label,
66
- target: label,
67
- committers,
68
- contextCwd: process.cwd(),
69
- cleanup: async () => undefined,
70
- changes: [],
71
- summary: {
72
- added: stats.additions,
73
- deleted: stats.deletions,
74
- modified: 0,
75
- moved: 0,
76
- renamed: 0,
77
- fileCount: stats.filesChanged,
78
- total: 0,
79
- },
80
- };
81
- }
82
-
83
- async function semRangeForCommand(command: SearchCommand): Promise<SourceRange> {
84
- if (command.kind === "since") return sourceRangeSince(command.since);
85
- if (command.kind === "commit") return sourceRangeForCommit(command.commit);
86
- if (command.kind === "commits") return sourceRangeForRecentCommits(command.count);
87
- throw new Error("sem cannot resolve stdin as a git range.");
88
- }
89
-
90
- async function semChangeSetFromPatch(
91
- patch: string,
92
- debugSem: boolean,
93
- label = "stdin",
94
- committers?: readonly string[],
95
- ): Promise<SemChangeSet> {
96
- if (!patch.trim()) throw new Error("No diff received on stdin.");
97
- const raw = await cachedJson(
98
- "sem-diff",
99
- fingerprint({
100
- version: 1,
101
- cwd: process.cwd(),
102
- command: ["diff", "--patch", "--format", "json"],
103
- patchHash: fingerprint(patch),
104
- }),
105
- () => runSemWithInput(["diff", "--patch", "--format", "json"], patch, debugSem),
106
- );
107
- return {
108
- ...normalizeSemDiff(raw, {
109
- id: sourceId(label),
110
- label,
111
- base: label,
112
- target: label,
113
- committers,
114
- stats: { filesChanged: 0, additions: 0, deletions: 0 },
115
- }),
116
- contextCwd: process.cwd(),
117
- cleanup: async () => undefined,
118
- };
119
- }
120
-
121
- async function cachedSemDiff(
122
- args: readonly string[],
123
- range: SourceRange,
124
- debugSem: boolean,
125
- ): Promise<unknown> {
126
- return cachedJson(
127
- "sem-diff",
128
- fingerprint({
129
- version: 1,
130
- cwd: process.cwd(),
131
- args,
132
- base: range.base,
133
- target: range.target,
134
- }),
135
- () => runSem(args, debugSem),
136
- );
137
- }
138
-
139
- async function withContextWorkspace(changeSet: SemChangeSet, debugSem: boolean): Promise<SemChangeSet> {
140
- const tempDir = await realpath(await mkdtemp(path.join(tmpdir(), "stupify-sem-context-")));
141
- let worktreeAdded = false;
142
- try {
143
- await git(["worktree", "add", "--detach", tempDir, changeSet.target], debugSem);
144
- worktreeAdded = true;
145
- return {
146
- ...changeSet,
147
- contextCwd: tempDir,
148
- cleanup: async () => cleanupWorktree(tempDir, worktreeAdded, debugSem),
149
- };
150
- } catch (error) {
151
- await cleanupWorktree(tempDir, worktreeAdded, debugSem);
152
- throw error;
153
- }
154
- }
155
-
156
- async function runSem(args: readonly string[], debugSem: boolean, cwd = process.cwd()): Promise<unknown> {
157
- if (debugSem) diagnostic(`sem ${args.join(" ")}`);
158
- const { command, commandArgs } = resolveSemCommand(args);
159
- try {
160
- const { stdout, stderr } = await execFileAsync(command, commandArgs, {
161
- cwd,
162
- maxBuffer: 128 * 1024 * 1024,
163
- });
164
- if (debugSem && stderr.trim()) diagnostic(stderr.trim());
165
- return JSON.parse(stdout);
166
- } catch (error) {
167
- const message = error instanceof Error ? error.message : String(error);
168
- throw new Error(`sem failed. Install @ataraxy-labs/sem and ensure its binary is downloaded.\n${message}`);
169
- }
170
- }
171
-
172
- async function runSemWithInput(args: readonly string[], stdin: string, debugSem: boolean): Promise<unknown> {
173
- if (debugSem) diagnostic(`sem ${args.join(" ")}`);
174
- const { command, commandArgs } = resolveSemCommand(args);
175
- return new Promise((resolve, reject) => {
176
- const child = spawn(command, commandArgs, { stdio: ["pipe", "pipe", "pipe"] });
177
- const stdout: Buffer[] = [];
178
- const stderr: Buffer[] = [];
179
- child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk));
180
- child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk));
181
- child.on("error", (error) => reject(error));
182
- child.on("close", (code) => {
183
- const stderrText = Buffer.concat(stderr).toString("utf8");
184
- if (debugSem && stderrText.trim()) diagnostic(stderrText.trim());
185
- if (code !== 0) {
186
- reject(new Error(`sem failed with exit code ${code}${stderrText ? `: ${stderrText.trim()}` : ""}`));
187
- return;
188
- }
189
- try {
190
- resolve(JSON.parse(Buffer.concat(stdout).toString("utf8")));
191
- } catch (error) {
192
- reject(error);
193
- }
194
- });
195
- child.stdin.end(stdin);
196
- });
197
- }
198
-
199
- async function git(args: readonly string[], debugSem: boolean): Promise<void> {
200
- if (debugSem) diagnostic(`git ${args.join(" ")}`);
201
- await execFileAsync("git", [...args], { maxBuffer: 128 * 1024 * 1024 });
202
- }
203
-
204
- async function cleanupWorktree(tempDir: string, worktreeAdded: boolean, debugSem: boolean): Promise<void> {
205
- if (!worktreeAdded) {
206
- await rm(tempDir, { recursive: true, force: true });
207
- return;
208
- }
209
- try {
210
- await git(["worktree", "remove", "--force", tempDir], debugSem);
211
- } catch {
212
- await rm(tempDir, { recursive: true, force: true });
213
- await git(["worktree", "prune"], debugSem).catch(() => undefined);
214
- }
215
- }
216
-
217
- function resolveSemCommand(args: readonly string[]): Readonly<{ command: string; commandArgs: readonly string[] }> {
218
- const packageBin = semPackageBin();
219
- if (packageBin) return { command: process.execPath, commandArgs: [packageBin, ...args] };
220
- return { command: "sem", commandArgs: args };
221
- }
222
-
223
- function semPackageBin(): string | null {
224
- try {
225
- const require = createRequire(import.meta.url);
226
- return require.resolve("@ataraxy-labs/sem/bin/sem.js");
227
- } catch {
228
- return null;
229
- }
230
- }
231
-
232
- function normalizeSemDiff(value: unknown, range: SourceRange): SemChangeSet {
233
- if (!value || typeof value !== "object") throw new Error("sem returned invalid diff JSON.");
234
- const record = value as Record<string, unknown>;
235
- const changes = Array.isArray(record.changes) ? record.changes.flatMap(normalizeSemChange) : [];
236
- const summary = normalizeSemSummary(record.summary, changes);
237
- return {
238
- id: range.id,
239
- label: range.label,
240
- base: range.base,
241
- target: range.target,
242
- committers: range.committers,
243
- contextCwd: process.cwd(),
244
- cleanup: async () => undefined,
245
- changes,
246
- summary,
247
- };
248
- }
249
-
250
- function normalizeSemChange(value: unknown): readonly SemChange[] {
251
- if (!value || typeof value !== "object") return [];
252
- const record = value as Record<string, unknown>;
253
- const entityId = stringValue(record.entityId);
254
- const entityName = stringValue(record.entityName);
255
- const entityType = stringValue(record.entityType);
256
- const filePath = stringValue(record.filePath);
257
- const changeType = stringValue(record.changeType);
258
- if (!entityId || !entityName || !entityType || !filePath || !changeType) return [];
259
- return [{
260
- entityId,
261
- entityName,
262
- entityType,
263
- filePath,
264
- changeType,
265
- beforeContent: nullableString(record.beforeContent),
266
- afterContent: nullableString(record.afterContent),
267
- }];
268
- }
269
-
270
- function normalizeSemSummary(value: unknown, changes: readonly SemChange[]): SemChangeSummary {
271
- const record = value && typeof value === "object" ? value as Record<string, unknown> : {};
272
- return {
273
- added: numberValue(record.added) ?? countByChange(changes, "added"),
274
- deleted: numberValue(record.deleted) ?? countByChange(changes, "deleted"),
275
- modified: numberValue(record.modified) ?? countByChange(changes, "modified"),
276
- moved: numberValue(record.moved) ?? countByChange(changes, "moved"),
277
- renamed: numberValue(record.renamed) ?? countByChange(changes, "renamed"),
278
- fileCount: numberValue(record.fileCount) ?? new Set(changes.map((change) => change.filePath)).size,
279
- total: numberValue(record.total) ?? changes.length,
280
- };
281
- }
282
-
283
- function countByChange(changes: readonly SemChange[], changeType: string): number {
284
- return changes.filter((change) => change.changeType === changeType).length;
285
- }
286
-
287
- function stringValue(value: unknown): string {
288
- return typeof value === "string" ? value : "";
289
- }
290
-
291
- function nullableString(value: unknown): string | null {
292
- return typeof value === "string" ? value : null;
293
- }
294
-
295
- function numberValue(value: unknown): number | null {
296
- return typeof value === "number" && Number.isFinite(value) ? value : null;
297
- }