@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,145 @@
|
|
|
1
|
+
import { mkdtemp, readFile, rm, stat } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { pack, setLogLevel } from "repomix";
|
|
5
|
+
const MAX_PACK_FILE_SIZE_BYTES = 48 * 1024;
|
|
6
|
+
const MAX_PACK_TOTAL_SIZE_BYTES = 128 * 1024;
|
|
7
|
+
export function emptyContextPack() {
|
|
8
|
+
return {
|
|
9
|
+
provider: "repomix",
|
|
10
|
+
filePaths: [],
|
|
11
|
+
totalCharacters: 0,
|
|
12
|
+
totalTokens: 0,
|
|
13
|
+
text: "",
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export async function repomixContextPack(cwd, contexts, changes) {
|
|
17
|
+
const filePaths = await candidateFilePaths(cwd, contexts, changes);
|
|
18
|
+
if (filePaths.length === 0) {
|
|
19
|
+
return emptyContextPack();
|
|
20
|
+
}
|
|
21
|
+
setLogLevel(-1);
|
|
22
|
+
const tempDir = await mkdtemp(path.join(tmpdir(), "stupify-repomix-"));
|
|
23
|
+
const outputPath = path.join(tempDir, "context.xml");
|
|
24
|
+
try {
|
|
25
|
+
const result = await pack([cwd], {
|
|
26
|
+
cwd,
|
|
27
|
+
input: { maxFileSize: MAX_PACK_FILE_SIZE_BYTES },
|
|
28
|
+
output: {
|
|
29
|
+
filePath: outputPath,
|
|
30
|
+
style: "xml",
|
|
31
|
+
parsableStyle: false,
|
|
32
|
+
fileSummary: false,
|
|
33
|
+
directoryStructure: false,
|
|
34
|
+
files: true,
|
|
35
|
+
removeComments: false,
|
|
36
|
+
removeEmptyLines: true,
|
|
37
|
+
compress: true,
|
|
38
|
+
topFilesLength: 0,
|
|
39
|
+
showLineNumbers: true,
|
|
40
|
+
truncateBase64: true,
|
|
41
|
+
copyToClipboard: false,
|
|
42
|
+
includeFullDirectoryStructure: false,
|
|
43
|
+
tokenCountTree: false,
|
|
44
|
+
git: {
|
|
45
|
+
sortByChanges: false,
|
|
46
|
+
sortByChangesMaxCommits: 1,
|
|
47
|
+
includeDiffs: false,
|
|
48
|
+
includeLogs: false,
|
|
49
|
+
includeLogsCount: 1,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
include: [],
|
|
53
|
+
ignore: {
|
|
54
|
+
useGitignore: true,
|
|
55
|
+
useDotIgnore: true,
|
|
56
|
+
useDefaultPatterns: true,
|
|
57
|
+
customPatterns: [],
|
|
58
|
+
},
|
|
59
|
+
security: { enableSecurityCheck: false },
|
|
60
|
+
tokenCount: { encoding: "o200k_base" },
|
|
61
|
+
}, () => undefined, {}, [...filePaths]);
|
|
62
|
+
return {
|
|
63
|
+
provider: "repomix",
|
|
64
|
+
filePaths,
|
|
65
|
+
totalCharacters: result.totalCharacters,
|
|
66
|
+
totalTokens: result.totalTokens,
|
|
67
|
+
text: await readFile(outputPath, "utf8"),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export function entityContextsFromChanges(candidates, changes) {
|
|
75
|
+
const byEntityId = new Map(changes.map((change) => [change.entityId, change]));
|
|
76
|
+
return candidates.flatMap((candidate) => {
|
|
77
|
+
const change = byEntityId.get(candidate.entityId);
|
|
78
|
+
if (!change)
|
|
79
|
+
return [];
|
|
80
|
+
return [{
|
|
81
|
+
targetId: candidate.targetId,
|
|
82
|
+
entityId: change.entityId,
|
|
83
|
+
entityName: change.entityName,
|
|
84
|
+
entityKind: change.entityType,
|
|
85
|
+
changeKind: change.changeType,
|
|
86
|
+
checkId: candidate.checkId,
|
|
87
|
+
reason: candidate.reason,
|
|
88
|
+
filePath: change.filePath,
|
|
89
|
+
text: JSON.stringify({
|
|
90
|
+
source: "sem diff",
|
|
91
|
+
file: change.filePath,
|
|
92
|
+
type: change.entityType,
|
|
93
|
+
name: change.entityName,
|
|
94
|
+
change: change.changeType,
|
|
95
|
+
before: shortenCode(change.beforeContent),
|
|
96
|
+
after: shortenCode(change.afterContent),
|
|
97
|
+
}, null, 2),
|
|
98
|
+
}];
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
async function candidateFilePaths(cwd, contexts, changes) {
|
|
102
|
+
const byEntityId = new Map(changes.map((change) => [change.entityId, change.filePath]));
|
|
103
|
+
const paths = contexts.flatMap((context) => context.filePath ?? byEntityId.get(context.entityId) ?? []);
|
|
104
|
+
const safePaths = [...new Set(paths)].filter(isSafeRelativeFilePath);
|
|
105
|
+
const selected = [];
|
|
106
|
+
let totalBytes = 0;
|
|
107
|
+
for (const filePath of safePaths) {
|
|
108
|
+
const bytes = await fileSize(cwd, filePath);
|
|
109
|
+
if (bytes === null || bytes > MAX_PACK_FILE_SIZE_BYTES)
|
|
110
|
+
continue;
|
|
111
|
+
if (totalBytes + bytes > MAX_PACK_TOTAL_SIZE_BYTES)
|
|
112
|
+
continue;
|
|
113
|
+
totalBytes += bytes;
|
|
114
|
+
selected.push(filePath);
|
|
115
|
+
}
|
|
116
|
+
return selected;
|
|
117
|
+
}
|
|
118
|
+
function isSafeRelativeFilePath(value) {
|
|
119
|
+
if (!value || path.isAbsolute(value))
|
|
120
|
+
return false;
|
|
121
|
+
const normalized = path.normalize(value);
|
|
122
|
+
return normalized !== "." && !normalized.startsWith("..") && !path.isAbsolute(normalized);
|
|
123
|
+
}
|
|
124
|
+
async function fileSize(cwd, filePath) {
|
|
125
|
+
try {
|
|
126
|
+
const fullPath = path.join(cwd, filePath);
|
|
127
|
+
if (!fullPath.startsWith(`${cwd}${path.sep}`))
|
|
128
|
+
return null;
|
|
129
|
+
const result = await stat(fullPath);
|
|
130
|
+
return result.isFile() ? result.size : null;
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function shortenCode(value) {
|
|
137
|
+
if (!value)
|
|
138
|
+
return "(none)";
|
|
139
|
+
const lines = value.split(/\r?\n/);
|
|
140
|
+
const limit = 120;
|
|
141
|
+
if (lines.length <= limit)
|
|
142
|
+
return value;
|
|
143
|
+
return `${lines.slice(0, limit).join("\n")}
|
|
144
|
+
[stupify: sem entity content shortened after ${limit} lines]`;
|
|
145
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
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.js";
|
|
8
|
+
import { readDiffFromStdin } from "./diff.js";
|
|
9
|
+
import { sourceRangeForCommit, sourceRangeForRecentCommits, sourceRangeSince, } from "./git.js";
|
|
10
|
+
import { sourceId } from "./types.js";
|
|
11
|
+
const execFileAsync = promisify(execFile);
|
|
12
|
+
export async function semChangeSetForCommand(command) {
|
|
13
|
+
if (command.kind === "stdin")
|
|
14
|
+
return semChangeSetFromPatch(await readDiffFromStdin(), command.debugSem);
|
|
15
|
+
if (command.kind === "commit") {
|
|
16
|
+
const range = await sourceRangeForCommit(command.commit);
|
|
17
|
+
const raw = await cachedSemDiff(["diff", "--commit", command.commit, "--format", "json"], range, command.debugSem);
|
|
18
|
+
return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
|
|
19
|
+
}
|
|
20
|
+
const range = await semRangeForCommand(command);
|
|
21
|
+
const raw = await cachedSemDiff(["diff", "--from", range.base, "--to", range.target, "--format", "json"], range, command.debugSem);
|
|
22
|
+
return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
|
|
23
|
+
}
|
|
24
|
+
async function semRangeForCommand(command) {
|
|
25
|
+
if (command.kind === "since")
|
|
26
|
+
return sourceRangeSince(command.since);
|
|
27
|
+
if (command.kind === "commit")
|
|
28
|
+
return sourceRangeForCommit(command.commit);
|
|
29
|
+
if (command.kind === "commits")
|
|
30
|
+
return sourceRangeForRecentCommits(command.count);
|
|
31
|
+
throw new Error("sem engine cannot resolve stdin as a git range.");
|
|
32
|
+
}
|
|
33
|
+
async function semChangeSetFromPatch(patch, debugSem) {
|
|
34
|
+
if (!patch.trim())
|
|
35
|
+
throw new Error("No diff received on stdin.");
|
|
36
|
+
const raw = await cachedJson("sem-diff", fingerprint({
|
|
37
|
+
version: 1,
|
|
38
|
+
cwd: process.cwd(),
|
|
39
|
+
command: ["diff", "--patch", "--format", "json"],
|
|
40
|
+
patchHash: fingerprint(patch),
|
|
41
|
+
}), () => runSemWithInput(["diff", "--patch", "--format", "json"], patch, debugSem));
|
|
42
|
+
return {
|
|
43
|
+
...normalizeSemDiff(raw, {
|
|
44
|
+
id: sourceId("stdin"),
|
|
45
|
+
label: "stdin",
|
|
46
|
+
base: "stdin",
|
|
47
|
+
target: "stdin",
|
|
48
|
+
stats: { filesChanged: 0, additions: 0, deletions: 0 },
|
|
49
|
+
}),
|
|
50
|
+
contextCwd: process.cwd(),
|
|
51
|
+
cleanup: async () => undefined,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async function cachedSemDiff(args, range, debugSem) {
|
|
55
|
+
return cachedJson("sem-diff", fingerprint({
|
|
56
|
+
version: 1,
|
|
57
|
+
cwd: process.cwd(),
|
|
58
|
+
args,
|
|
59
|
+
base: range.base,
|
|
60
|
+
target: range.target,
|
|
61
|
+
}), () => runSem(args, debugSem));
|
|
62
|
+
}
|
|
63
|
+
async function withContextWorkspace(changeSet, debugSem) {
|
|
64
|
+
const tempDir = await realpath(await mkdtemp(path.join(tmpdir(), "stupify-sem-context-")));
|
|
65
|
+
let worktreeAdded = false;
|
|
66
|
+
try {
|
|
67
|
+
await git(["worktree", "add", "--detach", tempDir, changeSet.target], debugSem);
|
|
68
|
+
worktreeAdded = true;
|
|
69
|
+
return {
|
|
70
|
+
...changeSet,
|
|
71
|
+
contextCwd: tempDir,
|
|
72
|
+
cleanup: async () => cleanupWorktree(tempDir, worktreeAdded, debugSem),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
await cleanupWorktree(tempDir, worktreeAdded, debugSem);
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function runSem(args, debugSem, cwd = process.cwd()) {
|
|
81
|
+
if (debugSem)
|
|
82
|
+
console.error(`sem ${args.join(" ")}`);
|
|
83
|
+
const { command, commandArgs } = resolveSemCommand(args);
|
|
84
|
+
try {
|
|
85
|
+
const { stdout, stderr } = await execFileAsync(command, commandArgs, {
|
|
86
|
+
cwd,
|
|
87
|
+
maxBuffer: 128 * 1024 * 1024,
|
|
88
|
+
});
|
|
89
|
+
if (debugSem && stderr.trim())
|
|
90
|
+
console.error(stderr.trim());
|
|
91
|
+
return JSON.parse(stdout);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
95
|
+
throw new Error(`sem failed. Install @ataraxy-labs/sem and ensure its binary is downloaded.\n${message}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function runSemWithInput(args, stdin, debugSem) {
|
|
99
|
+
if (debugSem)
|
|
100
|
+
console.error(`sem ${args.join(" ")}`);
|
|
101
|
+
const { command, commandArgs } = resolveSemCommand(args);
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
const child = spawn(command, commandArgs, { stdio: ["pipe", "pipe", "pipe"] });
|
|
104
|
+
const stdout = [];
|
|
105
|
+
const stderr = [];
|
|
106
|
+
child.stdout.on("data", (chunk) => stdout.push(chunk));
|
|
107
|
+
child.stderr.on("data", (chunk) => stderr.push(chunk));
|
|
108
|
+
child.on("error", (error) => reject(error));
|
|
109
|
+
child.on("close", (code) => {
|
|
110
|
+
const stderrText = Buffer.concat(stderr).toString("utf8");
|
|
111
|
+
if (debugSem && stderrText.trim())
|
|
112
|
+
console.error(stderrText.trim());
|
|
113
|
+
if (code !== 0) {
|
|
114
|
+
reject(new Error(`sem failed with exit code ${code}${stderrText ? `: ${stderrText.trim()}` : ""}`));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
resolve(JSON.parse(Buffer.concat(stdout).toString("utf8")));
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
reject(error);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
child.stdin.end(stdin);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
async function git(args, debugSem) {
|
|
128
|
+
if (debugSem)
|
|
129
|
+
console.error(`git ${args.join(" ")}`);
|
|
130
|
+
await execFileAsync("git", [...args], { maxBuffer: 128 * 1024 * 1024 });
|
|
131
|
+
}
|
|
132
|
+
async function cleanupWorktree(tempDir, worktreeAdded, debugSem) {
|
|
133
|
+
if (!worktreeAdded) {
|
|
134
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
await git(["worktree", "remove", "--force", tempDir], debugSem);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
142
|
+
await git(["worktree", "prune"], debugSem).catch(() => undefined);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function resolveSemCommand(args) {
|
|
146
|
+
const packageBin = semPackageBin();
|
|
147
|
+
if (packageBin)
|
|
148
|
+
return { command: process.execPath, commandArgs: [packageBin, ...args] };
|
|
149
|
+
return { command: "sem", commandArgs: args };
|
|
150
|
+
}
|
|
151
|
+
function semPackageBin() {
|
|
152
|
+
try {
|
|
153
|
+
const require = createRequire(import.meta.url);
|
|
154
|
+
return require.resolve("@ataraxy-labs/sem/bin/sem.js");
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function normalizeSemDiff(value, range) {
|
|
161
|
+
if (!value || typeof value !== "object")
|
|
162
|
+
throw new Error("sem returned invalid diff JSON.");
|
|
163
|
+
const record = value;
|
|
164
|
+
const changes = Array.isArray(record.changes) ? record.changes.flatMap(normalizeSemChange) : [];
|
|
165
|
+
const summary = normalizeSemSummary(record.summary, changes);
|
|
166
|
+
return {
|
|
167
|
+
id: range.id,
|
|
168
|
+
label: range.label,
|
|
169
|
+
base: range.base,
|
|
170
|
+
target: range.target,
|
|
171
|
+
contextCwd: process.cwd(),
|
|
172
|
+
cleanup: async () => undefined,
|
|
173
|
+
changes,
|
|
174
|
+
summary,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function normalizeSemChange(value) {
|
|
178
|
+
if (!value || typeof value !== "object")
|
|
179
|
+
return [];
|
|
180
|
+
const record = value;
|
|
181
|
+
const entityId = stringValue(record.entityId);
|
|
182
|
+
const entityName = stringValue(record.entityName);
|
|
183
|
+
const entityType = stringValue(record.entityType);
|
|
184
|
+
const filePath = stringValue(record.filePath);
|
|
185
|
+
const changeType = stringValue(record.changeType);
|
|
186
|
+
if (!entityId || !entityName || !entityType || !filePath || !changeType)
|
|
187
|
+
return [];
|
|
188
|
+
return [{
|
|
189
|
+
entityId,
|
|
190
|
+
entityName,
|
|
191
|
+
entityType,
|
|
192
|
+
filePath,
|
|
193
|
+
changeType,
|
|
194
|
+
beforeContent: nullableString(record.beforeContent),
|
|
195
|
+
afterContent: nullableString(record.afterContent),
|
|
196
|
+
}];
|
|
197
|
+
}
|
|
198
|
+
function normalizeSemSummary(value, changes) {
|
|
199
|
+
const record = value && typeof value === "object" ? value : {};
|
|
200
|
+
return {
|
|
201
|
+
added: numberValue(record.added) ?? countByChange(changes, "added"),
|
|
202
|
+
deleted: numberValue(record.deleted) ?? countByChange(changes, "deleted"),
|
|
203
|
+
modified: numberValue(record.modified) ?? countByChange(changes, "modified"),
|
|
204
|
+
moved: numberValue(record.moved) ?? countByChange(changes, "moved"),
|
|
205
|
+
renamed: numberValue(record.renamed) ?? countByChange(changes, "renamed"),
|
|
206
|
+
fileCount: numberValue(record.fileCount) ?? new Set(changes.map((change) => change.filePath)).size,
|
|
207
|
+
total: numberValue(record.total) ?? changes.length,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function countByChange(changes, changeType) {
|
|
211
|
+
return changes.filter((change) => change.changeType === changeType).length;
|
|
212
|
+
}
|
|
213
|
+
function stringValue(value) {
|
|
214
|
+
return typeof value === "string" ? value : "";
|
|
215
|
+
}
|
|
216
|
+
function nullableString(value) {
|
|
217
|
+
return typeof value === "string" ? value : null;
|
|
218
|
+
}
|
|
219
|
+
function numberValue(value) {
|
|
220
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
221
|
+
}
|