@stupify/cli 0.0.2 → 0.0.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/README.md +60 -0
- package/dist/analysis.d.ts +14 -0
- package/dist/analysis.js +276 -0
- package/dist/batcher.d.ts +3 -0
- package/dist/batcher.js +142 -0
- package/dist/cache.d.ts +2 -0
- package/dist/cache.js +59 -0
- package/dist/candidate-context.d.ts +2 -0
- package/dist/candidate-context.js +40 -0
- package/dist/checks.d.ts +3 -0
- package/dist/checks.js +131 -0
- package/dist/command.d.ts +2 -0
- package/dist/command.js +183 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +53 -0
- package/dist/counter-scout.d.ts +14 -0
- package/dist/counter-scout.js +97 -0
- package/dist/diff.d.ts +1 -0
- package/dist/diff.js +10 -0
- package/dist/experiment.d.ts +1 -0
- package/dist/experiment.js +225 -0
- package/dist/git.d.ts +8 -0
- package/dist/git.js +219 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/model.d.ts +24 -0
- package/dist/model.js +281 -0
- package/dist/prompts.d.ts +5 -0
- package/dist/prompts.js +197 -0
- package/dist/render.d.ts +3 -0
- package/dist/render.js +101 -0
- package/dist/repomix-provider.d.ts +4 -0
- package/dist/repomix-provider.js +145 -0
- package/dist/sem-provider.d.ts +2 -0
- package/dist/sem-provider.js +221 -0
- package/dist/stupify.d.ts +2 -0
- package/dist/stupify.js +387 -0
- package/dist/trace.d.ts +29 -0
- package/dist/trace.js +64 -0
- package/dist/types.d.ts +236 -0
- package/dist/types.js +6 -0
- package/package.json +42 -5
- package/src/analysis.ts +408 -0
- package/src/batcher.ts +198 -0
- package/src/cache.ts +65 -0
- package/src/candidate-context.ts +43 -0
- package/src/checks.ts +132 -0
- package/src/command.ts +218 -0
- package/src/constants.ts +56 -0
- package/src/counter-scout.ts +119 -0
- package/src/diff.ts +9 -0
- package/src/experiment.ts +317 -0
- package/src/git.ts +228 -0
- package/src/index.ts +1 -0
- package/src/model.ts +360 -0
- package/src/prompts.ts +234 -0
- package/src/render.ts +107 -0
- package/src/repomix-provider.ts +163 -0
- package/src/sem-provider.ts +255 -0
- package/src/stupify.ts +598 -0
- package/src/trace.ts +103 -0
- package/src/types.ts +264 -0
- package/bin/stupify.mjs +0 -3
|
@@ -0,0 +1,255 @@
|
|
|
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
|
+
sourceRangeForCommit,
|
|
11
|
+
sourceRangeForRecentCommits,
|
|
12
|
+
sourceRangeSince,
|
|
13
|
+
} from "./git.ts";
|
|
14
|
+
import type {
|
|
15
|
+
AnalyzeCommand,
|
|
16
|
+
SemChange,
|
|
17
|
+
SemChangeSet,
|
|
18
|
+
SemChangeSummary,
|
|
19
|
+
SourceRange,
|
|
20
|
+
} from "./types.ts";
|
|
21
|
+
import { sourceId } from "./types.ts";
|
|
22
|
+
|
|
23
|
+
const execFileAsync = promisify(execFile);
|
|
24
|
+
|
|
25
|
+
export async function semChangeSetForCommand(
|
|
26
|
+
command: AnalyzeCommand,
|
|
27
|
+
): Promise<SemChangeSet> {
|
|
28
|
+
if (command.kind === "stdin") return semChangeSetFromPatch(await readDiffFromStdin(), command.debugSem);
|
|
29
|
+
if (command.kind === "commit") {
|
|
30
|
+
const range = await sourceRangeForCommit(command.commit);
|
|
31
|
+
const raw = await cachedSemDiff(
|
|
32
|
+
["diff", "--commit", command.commit, "--format", "json"],
|
|
33
|
+
range,
|
|
34
|
+
command.debugSem,
|
|
35
|
+
);
|
|
36
|
+
return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const range = await semRangeForCommand(command);
|
|
40
|
+
const raw = await cachedSemDiff(
|
|
41
|
+
["diff", "--from", range.base, "--to", range.target, "--format", "json"],
|
|
42
|
+
range,
|
|
43
|
+
command.debugSem,
|
|
44
|
+
);
|
|
45
|
+
return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function semRangeForCommand(command: AnalyzeCommand): Promise<SourceRange> {
|
|
49
|
+
if (command.kind === "since") return sourceRangeSince(command.since);
|
|
50
|
+
if (command.kind === "commit") return sourceRangeForCommit(command.commit);
|
|
51
|
+
if (command.kind === "commits") return sourceRangeForRecentCommits(command.count);
|
|
52
|
+
throw new Error("sem engine cannot resolve stdin as a git range.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function semChangeSetFromPatch(patch: string, debugSem: boolean): Promise<SemChangeSet> {
|
|
56
|
+
if (!patch.trim()) throw new Error("No diff received on stdin.");
|
|
57
|
+
const raw = await cachedJson(
|
|
58
|
+
"sem-diff",
|
|
59
|
+
fingerprint({
|
|
60
|
+
version: 1,
|
|
61
|
+
cwd: process.cwd(),
|
|
62
|
+
command: ["diff", "--patch", "--format", "json"],
|
|
63
|
+
patchHash: fingerprint(patch),
|
|
64
|
+
}),
|
|
65
|
+
() => runSemWithInput(["diff", "--patch", "--format", "json"], patch, debugSem),
|
|
66
|
+
);
|
|
67
|
+
return {
|
|
68
|
+
...normalizeSemDiff(raw, {
|
|
69
|
+
id: sourceId("stdin"),
|
|
70
|
+
label: "stdin",
|
|
71
|
+
base: "stdin",
|
|
72
|
+
target: "stdin",
|
|
73
|
+
stats: { filesChanged: 0, additions: 0, deletions: 0 },
|
|
74
|
+
}),
|
|
75
|
+
contextCwd: process.cwd(),
|
|
76
|
+
cleanup: async () => undefined,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function cachedSemDiff(
|
|
81
|
+
args: readonly string[],
|
|
82
|
+
range: SourceRange,
|
|
83
|
+
debugSem: boolean,
|
|
84
|
+
): Promise<unknown> {
|
|
85
|
+
return cachedJson(
|
|
86
|
+
"sem-diff",
|
|
87
|
+
fingerprint({
|
|
88
|
+
version: 1,
|
|
89
|
+
cwd: process.cwd(),
|
|
90
|
+
args,
|
|
91
|
+
base: range.base,
|
|
92
|
+
target: range.target,
|
|
93
|
+
}),
|
|
94
|
+
() => runSem(args, debugSem),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function withContextWorkspace(changeSet: SemChangeSet, debugSem: boolean): Promise<SemChangeSet> {
|
|
99
|
+
const tempDir = await realpath(await mkdtemp(path.join(tmpdir(), "stupify-sem-context-")));
|
|
100
|
+
let worktreeAdded = false;
|
|
101
|
+
try {
|
|
102
|
+
await git(["worktree", "add", "--detach", tempDir, changeSet.target], debugSem);
|
|
103
|
+
worktreeAdded = true;
|
|
104
|
+
return {
|
|
105
|
+
...changeSet,
|
|
106
|
+
contextCwd: tempDir,
|
|
107
|
+
cleanup: async () => cleanupWorktree(tempDir, worktreeAdded, debugSem),
|
|
108
|
+
};
|
|
109
|
+
} catch (error) {
|
|
110
|
+
await cleanupWorktree(tempDir, worktreeAdded, debugSem);
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function runSem(args: readonly string[], debugSem: boolean, cwd = process.cwd()): Promise<unknown> {
|
|
116
|
+
if (debugSem) console.error(`sem ${args.join(" ")}`);
|
|
117
|
+
const { command, commandArgs } = resolveSemCommand(args);
|
|
118
|
+
try {
|
|
119
|
+
const { stdout, stderr } = await execFileAsync(command, commandArgs, {
|
|
120
|
+
cwd,
|
|
121
|
+
maxBuffer: 128 * 1024 * 1024,
|
|
122
|
+
});
|
|
123
|
+
if (debugSem && stderr.trim()) console.error(stderr.trim());
|
|
124
|
+
return JSON.parse(stdout);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
127
|
+
throw new Error(`sem failed. Install @ataraxy-labs/sem and ensure its binary is downloaded.\n${message}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function runSemWithInput(args: readonly string[], stdin: string, debugSem: boolean): Promise<unknown> {
|
|
132
|
+
if (debugSem) console.error(`sem ${args.join(" ")}`);
|
|
133
|
+
const { command, commandArgs } = resolveSemCommand(args);
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
const child = spawn(command, commandArgs, { stdio: ["pipe", "pipe", "pipe"] });
|
|
136
|
+
const stdout: Buffer[] = [];
|
|
137
|
+
const stderr: Buffer[] = [];
|
|
138
|
+
child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk));
|
|
139
|
+
child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk));
|
|
140
|
+
child.on("error", (error) => reject(error));
|
|
141
|
+
child.on("close", (code) => {
|
|
142
|
+
const stderrText = Buffer.concat(stderr).toString("utf8");
|
|
143
|
+
if (debugSem && stderrText.trim()) console.error(stderrText.trim());
|
|
144
|
+
if (code !== 0) {
|
|
145
|
+
reject(new Error(`sem failed with exit code ${code}${stderrText ? `: ${stderrText.trim()}` : ""}`));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
resolve(JSON.parse(Buffer.concat(stdout).toString("utf8")));
|
|
150
|
+
} catch (error) {
|
|
151
|
+
reject(error);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
child.stdin.end(stdin);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function git(args: readonly string[], debugSem: boolean): Promise<void> {
|
|
159
|
+
if (debugSem) console.error(`git ${args.join(" ")}`);
|
|
160
|
+
await execFileAsync("git", [...args], { maxBuffer: 128 * 1024 * 1024 });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function cleanupWorktree(tempDir: string, worktreeAdded: boolean, debugSem: boolean): Promise<void> {
|
|
164
|
+
if (!worktreeAdded) {
|
|
165
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
await git(["worktree", "remove", "--force", tempDir], debugSem);
|
|
170
|
+
} catch {
|
|
171
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
172
|
+
await git(["worktree", "prune"], debugSem).catch(() => undefined);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function resolveSemCommand(args: readonly string[]): Readonly<{ command: string; commandArgs: readonly string[] }> {
|
|
177
|
+
const packageBin = semPackageBin();
|
|
178
|
+
if (packageBin) return { command: process.execPath, commandArgs: [packageBin, ...args] };
|
|
179
|
+
return { command: "sem", commandArgs: args };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function semPackageBin(): string | null {
|
|
183
|
+
try {
|
|
184
|
+
const require = createRequire(import.meta.url);
|
|
185
|
+
return require.resolve("@ataraxy-labs/sem/bin/sem.js");
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function normalizeSemDiff(value: unknown, range: SourceRange): SemChangeSet {
|
|
192
|
+
if (!value || typeof value !== "object") throw new Error("sem returned invalid diff JSON.");
|
|
193
|
+
const record = value as Record<string, unknown>;
|
|
194
|
+
const changes = Array.isArray(record.changes) ? record.changes.flatMap(normalizeSemChange) : [];
|
|
195
|
+
const summary = normalizeSemSummary(record.summary, changes);
|
|
196
|
+
return {
|
|
197
|
+
id: range.id,
|
|
198
|
+
label: range.label,
|
|
199
|
+
base: range.base,
|
|
200
|
+
target: range.target,
|
|
201
|
+
contextCwd: process.cwd(),
|
|
202
|
+
cleanup: async () => undefined,
|
|
203
|
+
changes,
|
|
204
|
+
summary,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function normalizeSemChange(value: unknown): readonly SemChange[] {
|
|
209
|
+
if (!value || typeof value !== "object") return [];
|
|
210
|
+
const record = value as Record<string, unknown>;
|
|
211
|
+
const entityId = stringValue(record.entityId);
|
|
212
|
+
const entityName = stringValue(record.entityName);
|
|
213
|
+
const entityType = stringValue(record.entityType);
|
|
214
|
+
const filePath = stringValue(record.filePath);
|
|
215
|
+
const changeType = stringValue(record.changeType);
|
|
216
|
+
if (!entityId || !entityName || !entityType || !filePath || !changeType) return [];
|
|
217
|
+
return [{
|
|
218
|
+
entityId,
|
|
219
|
+
entityName,
|
|
220
|
+
entityType,
|
|
221
|
+
filePath,
|
|
222
|
+
changeType,
|
|
223
|
+
beforeContent: nullableString(record.beforeContent),
|
|
224
|
+
afterContent: nullableString(record.afterContent),
|
|
225
|
+
}];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function normalizeSemSummary(value: unknown, changes: readonly SemChange[]): SemChangeSummary {
|
|
229
|
+
const record = value && typeof value === "object" ? value as Record<string, unknown> : {};
|
|
230
|
+
return {
|
|
231
|
+
added: numberValue(record.added) ?? countByChange(changes, "added"),
|
|
232
|
+
deleted: numberValue(record.deleted) ?? countByChange(changes, "deleted"),
|
|
233
|
+
modified: numberValue(record.modified) ?? countByChange(changes, "modified"),
|
|
234
|
+
moved: numberValue(record.moved) ?? countByChange(changes, "moved"),
|
|
235
|
+
renamed: numberValue(record.renamed) ?? countByChange(changes, "renamed"),
|
|
236
|
+
fileCount: numberValue(record.fileCount) ?? new Set(changes.map((change) => change.filePath)).size,
|
|
237
|
+
total: numberValue(record.total) ?? changes.length,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function countByChange(changes: readonly SemChange[], changeType: string): number {
|
|
242
|
+
return changes.filter((change) => change.changeType === changeType).length;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function stringValue(value: unknown): string {
|
|
246
|
+
return typeof value === "string" ? value : "";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function nullableString(value: unknown): string | null {
|
|
250
|
+
return typeof value === "string" ? value : null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function numberValue(value: unknown): number | null {
|
|
254
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
255
|
+
}
|