@stupify/cli 0.0.2 → 0.0.4
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 +55 -0
- package/dist/analysis.d.ts +16 -0
- package/dist/analysis.js +133 -0
- package/dist/cache.d.ts +2 -0
- package/dist/cache.js +59 -0
- package/dist/checks.d.ts +4 -0
- package/dist/checks.js +218 -0
- package/dist/command.d.ts +2 -0
- package/dist/command.js +147 -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 +159 -0
- package/dist/diff.d.ts +1 -0
- package/dist/diff.js +10 -0
- package/dist/doctor.d.ts +4 -0
- package/dist/doctor.js +131 -0
- package/dist/git.d.ts +11 -0
- package/dist/git.js +253 -0
- package/dist/hooks.d.ts +3 -0
- package/dist/hooks.js +117 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/model.d.ts +10 -0
- package/dist/model.js +297 -0
- package/dist/prompts.d.ts +8 -0
- package/dist/prompts.js +87 -0
- package/dist/render.d.ts +3 -0
- package/dist/render.js +93 -0
- package/dist/repomix-provider.d.ts +12 -0
- package/dist/repomix-provider.js +196 -0
- package/dist/search-bench.d.ts +1 -0
- package/dist/search-bench.js +675 -0
- package/dist/search-profile.d.ts +6 -0
- package/dist/search-profile.js +73 -0
- package/dist/sem-provider.d.ts +2 -0
- package/dist/sem-provider.js +247 -0
- package/dist/stupify.d.ts +4 -0
- package/dist/stupify.js +237 -0
- package/dist/trace.d.ts +29 -0
- package/dist/trace.js +64 -0
- package/dist/types.d.ts +320 -0
- package/dist/types.js +6 -0
- package/package.json +42 -5
- package/src/analysis.ts +188 -0
- package/src/cache.ts +65 -0
- package/src/checks.ts +221 -0
- package/src/command.ts +173 -0
- package/src/constants.ts +56 -0
- package/src/counter-scout.ts +175 -0
- package/src/diff.ts +9 -0
- package/src/doctor.ts +140 -0
- package/src/git.ts +262 -0
- package/src/hooks.ts +134 -0
- package/src/index.ts +1 -0
- package/src/model.ts +373 -0
- package/src/prompts.ts +98 -0
- package/src/render.ts +96 -0
- package/src/repomix-provider.ts +219 -0
- package/src/search-bench.ts +783 -0
- package/src/search-profile.ts +89 -0
- package/src/sem-provider.ts +282 -0
- package/src/stupify.ts +285 -0
- package/src/trace.ts +103 -0
- package/src/types.ts +340 -0
- package/bin/stupify.mjs +0 -3
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { defaultChecks, searchChecks } from "./checks.js";
|
|
4
|
+
export async function loadSearchProfile(profilePath) {
|
|
5
|
+
if (!profilePath)
|
|
6
|
+
return null;
|
|
7
|
+
const fullPath = path.resolve(profilePath);
|
|
8
|
+
const value = JSON.parse(await readFile(fullPath, "utf8"));
|
|
9
|
+
if (!value || typeof value !== "object" || typeof value.id !== "string" || value.id.length === 0) {
|
|
10
|
+
throw new Error(`Invalid search profile: ${profilePath}`);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
export function effectiveSearchChecks(explicitCheckIds, profile) {
|
|
15
|
+
const checks = explicitCheckIds
|
|
16
|
+
? searchChecks(explicitCheckIds)
|
|
17
|
+
: profilePatternIds(profile).length > 0
|
|
18
|
+
? checksById(profilePatternIds(profile))
|
|
19
|
+
: searchChecks(null);
|
|
20
|
+
if (!profile?.patterns)
|
|
21
|
+
return checks;
|
|
22
|
+
return checks.map((check) => applyPatternOverride(check, profile.patterns?.[check.id]));
|
|
23
|
+
}
|
|
24
|
+
export function effectiveMaxCandidates(defaultValue, profile) {
|
|
25
|
+
return positiveInteger(profile?.maxCandidates) ?? defaultValue;
|
|
26
|
+
}
|
|
27
|
+
export function effectiveMaxSearchInputTokens(defaultValue, profile) {
|
|
28
|
+
return positiveInteger(profile?.maxSearchInputTokens) ?? defaultValue;
|
|
29
|
+
}
|
|
30
|
+
export function effectiveRepomixConfig(defaultValue, profile) {
|
|
31
|
+
const override = profile?.repomix;
|
|
32
|
+
if (!override)
|
|
33
|
+
return defaultValue;
|
|
34
|
+
return {
|
|
35
|
+
compress: override.compress ?? defaultValue.compress,
|
|
36
|
+
showLineNumbers: override.showLineNumbers ?? defaultValue.showLineNumbers,
|
|
37
|
+
removeEmptyLines: override.removeEmptyLines ?? defaultValue.removeEmptyLines,
|
|
38
|
+
maxFileSizeBytes: positiveInteger(override.maxFileBytes) ?? defaultValue.maxFileSizeBytes,
|
|
39
|
+
maxTotalSizeBytes: positiveInteger(override.maxTotalBytes) ?? defaultValue.maxTotalSizeBytes,
|
|
40
|
+
ignorePatterns: override.ignorePatterns ?? defaultValue.ignorePatterns,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function profilePatternIds(profile) {
|
|
44
|
+
if (!profile?.patterns)
|
|
45
|
+
return [];
|
|
46
|
+
return Object.entries(profile.patterns)
|
|
47
|
+
.filter(([, pattern]) => pattern.enabled === true)
|
|
48
|
+
.map(([id]) => id);
|
|
49
|
+
}
|
|
50
|
+
function checksById(ids) {
|
|
51
|
+
const byId = new Map(defaultChecks.map((check) => [check.id, check]));
|
|
52
|
+
return ids.map((id) => {
|
|
53
|
+
const check = byId.get(id);
|
|
54
|
+
if (!check)
|
|
55
|
+
throw new Error(`Unknown check in search profile: ${id}`);
|
|
56
|
+
return check;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function applyPatternOverride(check, override) {
|
|
60
|
+
if (!override)
|
|
61
|
+
return check;
|
|
62
|
+
return {
|
|
63
|
+
...check,
|
|
64
|
+
searchPrompt: override.searchPrompt ?? check.searchPrompt,
|
|
65
|
+
searchExamples: {
|
|
66
|
+
match: override.matchExamples ?? check.searchExamples?.match ?? check.examples?.match ?? [],
|
|
67
|
+
nonMatch: override.nonMatchExamples ?? check.searchExamples?.nonMatch ?? check.examples?.noMatch ?? [],
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function positiveInteger(value) {
|
|
72
|
+
return Number.isInteger(value) && Number(value) > 0 ? Number(value) : null;
|
|
73
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
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, stagedDiff, } 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 === "staged") {
|
|
16
|
+
const diff = await stagedDiff();
|
|
17
|
+
if (!diff.text.trim())
|
|
18
|
+
return emptyChangeSet("staged", diff.stats);
|
|
19
|
+
return semChangeSetFromPatch(diff.text, command.debugSem, "staged");
|
|
20
|
+
}
|
|
21
|
+
if (command.kind === "commit") {
|
|
22
|
+
const range = await sourceRangeForCommit(command.commit);
|
|
23
|
+
const raw = await cachedSemDiff(["diff", "--commit", command.commit, "--format", "json"], range, command.debugSem);
|
|
24
|
+
return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
|
|
25
|
+
}
|
|
26
|
+
const range = await semRangeForCommand(command);
|
|
27
|
+
const raw = await cachedSemDiff(["diff", "--from", range.base, "--to", range.target, "--format", "json"], range, command.debugSem);
|
|
28
|
+
return withContextWorkspace(normalizeSemDiff(raw, range), command.debugSem);
|
|
29
|
+
}
|
|
30
|
+
function emptyChangeSet(label, stats) {
|
|
31
|
+
return {
|
|
32
|
+
id: sourceId(label),
|
|
33
|
+
label,
|
|
34
|
+
base: label,
|
|
35
|
+
target: label,
|
|
36
|
+
contextCwd: process.cwd(),
|
|
37
|
+
cleanup: async () => undefined,
|
|
38
|
+
changes: [],
|
|
39
|
+
summary: {
|
|
40
|
+
added: stats.additions,
|
|
41
|
+
deleted: stats.deletions,
|
|
42
|
+
modified: 0,
|
|
43
|
+
moved: 0,
|
|
44
|
+
renamed: 0,
|
|
45
|
+
fileCount: stats.filesChanged,
|
|
46
|
+
total: 0,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async function semRangeForCommand(command) {
|
|
51
|
+
if (command.kind === "since")
|
|
52
|
+
return sourceRangeSince(command.since);
|
|
53
|
+
if (command.kind === "commit")
|
|
54
|
+
return sourceRangeForCommit(command.commit);
|
|
55
|
+
if (command.kind === "commits")
|
|
56
|
+
return sourceRangeForRecentCommits(command.count);
|
|
57
|
+
throw new Error("sem cannot resolve stdin as a git range.");
|
|
58
|
+
}
|
|
59
|
+
async function semChangeSetFromPatch(patch, debugSem, label = "stdin") {
|
|
60
|
+
if (!patch.trim())
|
|
61
|
+
throw new Error("No diff received on stdin.");
|
|
62
|
+
const raw = await cachedJson("sem-diff", fingerprint({
|
|
63
|
+
version: 1,
|
|
64
|
+
cwd: process.cwd(),
|
|
65
|
+
command: ["diff", "--patch", "--format", "json"],
|
|
66
|
+
patchHash: fingerprint(patch),
|
|
67
|
+
}), () => runSemWithInput(["diff", "--patch", "--format", "json"], patch, debugSem));
|
|
68
|
+
return {
|
|
69
|
+
...normalizeSemDiff(raw, {
|
|
70
|
+
id: sourceId(label),
|
|
71
|
+
label,
|
|
72
|
+
base: label,
|
|
73
|
+
target: label,
|
|
74
|
+
stats: { filesChanged: 0, additions: 0, deletions: 0 },
|
|
75
|
+
}),
|
|
76
|
+
contextCwd: process.cwd(),
|
|
77
|
+
cleanup: async () => undefined,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async function cachedSemDiff(args, range, debugSem) {
|
|
81
|
+
return cachedJson("sem-diff", fingerprint({
|
|
82
|
+
version: 1,
|
|
83
|
+
cwd: process.cwd(),
|
|
84
|
+
args,
|
|
85
|
+
base: range.base,
|
|
86
|
+
target: range.target,
|
|
87
|
+
}), () => runSem(args, debugSem));
|
|
88
|
+
}
|
|
89
|
+
async function withContextWorkspace(changeSet, debugSem) {
|
|
90
|
+
const tempDir = await realpath(await mkdtemp(path.join(tmpdir(), "stupify-sem-context-")));
|
|
91
|
+
let worktreeAdded = false;
|
|
92
|
+
try {
|
|
93
|
+
await git(["worktree", "add", "--detach", tempDir, changeSet.target], debugSem);
|
|
94
|
+
worktreeAdded = true;
|
|
95
|
+
return {
|
|
96
|
+
...changeSet,
|
|
97
|
+
contextCwd: tempDir,
|
|
98
|
+
cleanup: async () => cleanupWorktree(tempDir, worktreeAdded, debugSem),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
await cleanupWorktree(tempDir, worktreeAdded, debugSem);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function runSem(args, debugSem, cwd = process.cwd()) {
|
|
107
|
+
if (debugSem)
|
|
108
|
+
console.error(`sem ${args.join(" ")}`);
|
|
109
|
+
const { command, commandArgs } = resolveSemCommand(args);
|
|
110
|
+
try {
|
|
111
|
+
const { stdout, stderr } = await execFileAsync(command, commandArgs, {
|
|
112
|
+
cwd,
|
|
113
|
+
maxBuffer: 128 * 1024 * 1024,
|
|
114
|
+
});
|
|
115
|
+
if (debugSem && stderr.trim())
|
|
116
|
+
console.error(stderr.trim());
|
|
117
|
+
return JSON.parse(stdout);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
121
|
+
throw new Error(`sem failed. Install @ataraxy-labs/sem and ensure its binary is downloaded.\n${message}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function runSemWithInput(args, stdin, debugSem) {
|
|
125
|
+
if (debugSem)
|
|
126
|
+
console.error(`sem ${args.join(" ")}`);
|
|
127
|
+
const { command, commandArgs } = resolveSemCommand(args);
|
|
128
|
+
return new Promise((resolve, reject) => {
|
|
129
|
+
const child = spawn(command, commandArgs, { stdio: ["pipe", "pipe", "pipe"] });
|
|
130
|
+
const stdout = [];
|
|
131
|
+
const stderr = [];
|
|
132
|
+
child.stdout.on("data", (chunk) => stdout.push(chunk));
|
|
133
|
+
child.stderr.on("data", (chunk) => stderr.push(chunk));
|
|
134
|
+
child.on("error", (error) => reject(error));
|
|
135
|
+
child.on("close", (code) => {
|
|
136
|
+
const stderrText = Buffer.concat(stderr).toString("utf8");
|
|
137
|
+
if (debugSem && stderrText.trim())
|
|
138
|
+
console.error(stderrText.trim());
|
|
139
|
+
if (code !== 0) {
|
|
140
|
+
reject(new Error(`sem failed with exit code ${code}${stderrText ? `: ${stderrText.trim()}` : ""}`));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
resolve(JSON.parse(Buffer.concat(stdout).toString("utf8")));
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
reject(error);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
child.stdin.end(stdin);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
async function git(args, debugSem) {
|
|
154
|
+
if (debugSem)
|
|
155
|
+
console.error(`git ${args.join(" ")}`);
|
|
156
|
+
await execFileAsync("git", [...args], { maxBuffer: 128 * 1024 * 1024 });
|
|
157
|
+
}
|
|
158
|
+
async function cleanupWorktree(tempDir, worktreeAdded, debugSem) {
|
|
159
|
+
if (!worktreeAdded) {
|
|
160
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
await git(["worktree", "remove", "--force", tempDir], debugSem);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
168
|
+
await git(["worktree", "prune"], debugSem).catch(() => undefined);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function resolveSemCommand(args) {
|
|
172
|
+
const packageBin = semPackageBin();
|
|
173
|
+
if (packageBin)
|
|
174
|
+
return { command: process.execPath, commandArgs: [packageBin, ...args] };
|
|
175
|
+
return { command: "sem", commandArgs: args };
|
|
176
|
+
}
|
|
177
|
+
function semPackageBin() {
|
|
178
|
+
try {
|
|
179
|
+
const require = createRequire(import.meta.url);
|
|
180
|
+
return require.resolve("@ataraxy-labs/sem/bin/sem.js");
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function normalizeSemDiff(value, range) {
|
|
187
|
+
if (!value || typeof value !== "object")
|
|
188
|
+
throw new Error("sem returned invalid diff JSON.");
|
|
189
|
+
const record = value;
|
|
190
|
+
const changes = Array.isArray(record.changes) ? record.changes.flatMap(normalizeSemChange) : [];
|
|
191
|
+
const summary = normalizeSemSummary(record.summary, changes);
|
|
192
|
+
return {
|
|
193
|
+
id: range.id,
|
|
194
|
+
label: range.label,
|
|
195
|
+
base: range.base,
|
|
196
|
+
target: range.target,
|
|
197
|
+
contextCwd: process.cwd(),
|
|
198
|
+
cleanup: async () => undefined,
|
|
199
|
+
changes,
|
|
200
|
+
summary,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function normalizeSemChange(value) {
|
|
204
|
+
if (!value || typeof value !== "object")
|
|
205
|
+
return [];
|
|
206
|
+
const record = value;
|
|
207
|
+
const entityId = stringValue(record.entityId);
|
|
208
|
+
const entityName = stringValue(record.entityName);
|
|
209
|
+
const entityType = stringValue(record.entityType);
|
|
210
|
+
const filePath = stringValue(record.filePath);
|
|
211
|
+
const changeType = stringValue(record.changeType);
|
|
212
|
+
if (!entityId || !entityName || !entityType || !filePath || !changeType)
|
|
213
|
+
return [];
|
|
214
|
+
return [{
|
|
215
|
+
entityId,
|
|
216
|
+
entityName,
|
|
217
|
+
entityType,
|
|
218
|
+
filePath,
|
|
219
|
+
changeType,
|
|
220
|
+
beforeContent: nullableString(record.beforeContent),
|
|
221
|
+
afterContent: nullableString(record.afterContent),
|
|
222
|
+
}];
|
|
223
|
+
}
|
|
224
|
+
function normalizeSemSummary(value, changes) {
|
|
225
|
+
const record = value && typeof value === "object" ? value : {};
|
|
226
|
+
return {
|
|
227
|
+
added: numberValue(record.added) ?? countByChange(changes, "added"),
|
|
228
|
+
deleted: numberValue(record.deleted) ?? countByChange(changes, "deleted"),
|
|
229
|
+
modified: numberValue(record.modified) ?? countByChange(changes, "modified"),
|
|
230
|
+
moved: numberValue(record.moved) ?? countByChange(changes, "moved"),
|
|
231
|
+
renamed: numberValue(record.renamed) ?? countByChange(changes, "renamed"),
|
|
232
|
+
fileCount: numberValue(record.fileCount) ?? new Set(changes.map((change) => change.filePath)).size,
|
|
233
|
+
total: numberValue(record.total) ?? changes.length,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function countByChange(changes, changeType) {
|
|
237
|
+
return changes.filter((change) => change.changeType === changeType).length;
|
|
238
|
+
}
|
|
239
|
+
function stringValue(value) {
|
|
240
|
+
return typeof value === "string" ? value : "";
|
|
241
|
+
}
|
|
242
|
+
function nullableString(value) {
|
|
243
|
+
return typeof value === "string" ? value : null;
|
|
244
|
+
}
|
|
245
|
+
function numberValue(value) {
|
|
246
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
247
|
+
}
|
package/dist/stupify.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { countPromptTokens, runSearch, searchRequest } from "./analysis.js";
|
|
4
|
+
import { searchChecks } from "./checks.js";
|
|
5
|
+
import { parseCommand } from "./command.js";
|
|
6
|
+
import { counterScoutTargets } from "./counter-scout.js";
|
|
7
|
+
import { runDoctor } from "./doctor.js";
|
|
8
|
+
import { runHookCommand } from "./hooks.js";
|
|
9
|
+
import { firstRunModelBootstrap, loadLocalModel } from "./model.js";
|
|
10
|
+
import { entityContextsFromChanges, emptyContextPack, repomixContextPack, repomixSearchConfig } from "./repomix-provider.js";
|
|
11
|
+
import { helpText, renderSearchRun } from "./render.js";
|
|
12
|
+
import { effectiveMaxCandidates, effectiveMaxSearchInputTokens, effectiveRepomixConfig, effectiveSearchChecks, loadSearchProfile, } from "./search-profile.js";
|
|
13
|
+
import { semChangeSetForCommand } from "./sem-provider.js";
|
|
14
|
+
import { createTracer } from "./trace.js";
|
|
15
|
+
export async function main(argv = process.argv.slice(2)) {
|
|
16
|
+
const startedAt = Date.now();
|
|
17
|
+
try {
|
|
18
|
+
const command = parseCommand(argv);
|
|
19
|
+
if (command.kind === "help") {
|
|
20
|
+
console.log(helpText());
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
if (command.kind === "hook") {
|
|
24
|
+
console.log(await runHookCommand(command.action));
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
if (command.kind === "doctor") {
|
|
28
|
+
const result = await runDoctor();
|
|
29
|
+
console.log(result.text);
|
|
30
|
+
return result.exitCode;
|
|
31
|
+
}
|
|
32
|
+
if (command.kind === "bench-search") {
|
|
33
|
+
const { runSearchBench } = await import("./search-bench.js");
|
|
34
|
+
console.log(await runSearchBench(command.configPath));
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
const run = await runSearchCommand(command, startedAt);
|
|
38
|
+
console.log(renderSearchRun(run, command));
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export async function runSearchCommand(command, startedAt) {
|
|
47
|
+
const t = createTracer({
|
|
48
|
+
writeLine: () => undefined,
|
|
49
|
+
onEvent: (event) => {
|
|
50
|
+
const parts = [`trace ${event.name}`, `${event.ms}ms`];
|
|
51
|
+
if (event.count !== undefined)
|
|
52
|
+
parts.push(`count=${event.count}`);
|
|
53
|
+
if (event.detail)
|
|
54
|
+
parts.push(event.detail);
|
|
55
|
+
console.error(parts.join(" "));
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const profile = await loadSearchProfile(command.searchProfilePath);
|
|
59
|
+
const checks = profile ? effectiveSearchChecks(command.checkIds, profile) : searchChecks(command.checkIds);
|
|
60
|
+
const patternIds = checks.map((check) => check.id);
|
|
61
|
+
const maxCandidates = effectiveMaxCandidates(command.maxCandidates, profile);
|
|
62
|
+
const maxSearchInputTokens = effectiveMaxSearchInputTokens(command.maxSearchInputTokens, profile);
|
|
63
|
+
const { value: changeSet } = await t.trace("entity.diff", () => semChangeSetForCommand(command), {
|
|
64
|
+
count: (v) => v.summary.total,
|
|
65
|
+
detail: (v) => `${v.summary.fileCount} files`,
|
|
66
|
+
});
|
|
67
|
+
try {
|
|
68
|
+
printRunPlan(command, changeSet.summary.fileCount, changeSet.summary.total, patternIds);
|
|
69
|
+
const candidates = counterScoutTargets(changeSet, checks, maxCandidates);
|
|
70
|
+
const contexts = entityContextsFromChanges(candidates, changeSet.changes);
|
|
71
|
+
const targetsByPattern = countTargetsByPattern(contexts);
|
|
72
|
+
const targetsPreview = previewTargets(contexts);
|
|
73
|
+
if (contexts.length === 0) {
|
|
74
|
+
return {
|
|
75
|
+
schemaVersion: "search.v1",
|
|
76
|
+
mode: "search",
|
|
77
|
+
source: command.source,
|
|
78
|
+
model: { id: command.model },
|
|
79
|
+
patterns: patternIds,
|
|
80
|
+
stats: {
|
|
81
|
+
elapsedMs: Date.now() - startedAt,
|
|
82
|
+
modelCalls: 0,
|
|
83
|
+
skipped: true,
|
|
84
|
+
skipReason: "no_candidates",
|
|
85
|
+
filesChanged: changeSet.summary.fileCount,
|
|
86
|
+
entitiesScanned: changeSet.summary.total,
|
|
87
|
+
candidates: 0,
|
|
88
|
+
searchTargets: 0,
|
|
89
|
+
repomixFiles: 0,
|
|
90
|
+
repomixTokens: 0,
|
|
91
|
+
profileId: profile?.id,
|
|
92
|
+
targetsByPattern,
|
|
93
|
+
targetsPreview,
|
|
94
|
+
},
|
|
95
|
+
matches: [],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const baseRepomixConfig = effectiveRepomixConfig(repomixSearchConfig(), profile);
|
|
99
|
+
const initialPack = profile?.context === "sem"
|
|
100
|
+
? emptyContextPack()
|
|
101
|
+
: await t.trace("context.pack", () => repomixContextPack(changeSet.contextCwd, contexts, changeSet.changes, baseRepomixConfig), {
|
|
102
|
+
count: (v) => v.filePaths.length,
|
|
103
|
+
detail: (v) => `${v.totalTokens} tokens`,
|
|
104
|
+
}).then((result) => result.value);
|
|
105
|
+
const packedFiles = new Set(initialPack.filePaths);
|
|
106
|
+
const searchContexts = profile?.context === "sem"
|
|
107
|
+
? contexts
|
|
108
|
+
: contexts.filter((context) => context.filePath && packedFiles.has(context.filePath));
|
|
109
|
+
if (searchContexts.length === 0) {
|
|
110
|
+
return {
|
|
111
|
+
schemaVersion: "search.v1",
|
|
112
|
+
mode: "search",
|
|
113
|
+
source: command.source,
|
|
114
|
+
model: { id: command.model },
|
|
115
|
+
patterns: patternIds,
|
|
116
|
+
stats: {
|
|
117
|
+
elapsedMs: Date.now() - startedAt,
|
|
118
|
+
modelCalls: 0,
|
|
119
|
+
skipped: true,
|
|
120
|
+
skipReason: "no_candidates",
|
|
121
|
+
filesChanged: changeSet.summary.fileCount,
|
|
122
|
+
entitiesScanned: changeSet.summary.total,
|
|
123
|
+
candidates: contexts.length,
|
|
124
|
+
searchTargets: 0,
|
|
125
|
+
repomixFiles: initialPack.filePaths.length,
|
|
126
|
+
repomixTokens: initialPack.totalTokens,
|
|
127
|
+
repomixConfig: initialPack.config,
|
|
128
|
+
profileId: profile?.id,
|
|
129
|
+
targetsByPattern,
|
|
130
|
+
targetsPreview,
|
|
131
|
+
},
|
|
132
|
+
matches: [],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const pack = profile?.context === "sem" || searchContexts.length === contexts.length
|
|
136
|
+
? initialPack
|
|
137
|
+
: await repomixContextPack(changeSet.contextCwd, searchContexts, changeSet.changes, baseRepomixConfig);
|
|
138
|
+
const modelPath = await firstRunModelBootstrap(command.model);
|
|
139
|
+
const model = await loadLocalModel(modelPath, command.model, "scout");
|
|
140
|
+
const request = buildSearchRequest(changeSet, searchContexts, pack, checks, profile, command.includeCounterReasonInPrompt);
|
|
141
|
+
const inputTokens = await countPromptTokens(model, request.prompt);
|
|
142
|
+
if (inputTokens > maxSearchInputTokens) {
|
|
143
|
+
return {
|
|
144
|
+
schemaVersion: "search.v1",
|
|
145
|
+
mode: "search",
|
|
146
|
+
source: command.source,
|
|
147
|
+
model: { id: command.model },
|
|
148
|
+
patterns: patternIds,
|
|
149
|
+
stats: {
|
|
150
|
+
elapsedMs: Date.now() - startedAt,
|
|
151
|
+
modelCalls: 0,
|
|
152
|
+
inputTokens,
|
|
153
|
+
inputTokenCap: maxSearchInputTokens,
|
|
154
|
+
skipped: true,
|
|
155
|
+
skipReason: "input_too_large",
|
|
156
|
+
filesChanged: changeSet.summary.fileCount,
|
|
157
|
+
entitiesScanned: changeSet.summary.total,
|
|
158
|
+
candidates: contexts.length,
|
|
159
|
+
searchTargets: searchContexts.length,
|
|
160
|
+
repomixFiles: pack.filePaths.length,
|
|
161
|
+
repomixTokens: pack.totalTokens,
|
|
162
|
+
repomixConfig: pack.config,
|
|
163
|
+
profileId: profile?.id,
|
|
164
|
+
targetsByPattern: countTargetsByPattern(searchContexts),
|
|
165
|
+
targetsPreview: previewTargets(searchContexts),
|
|
166
|
+
},
|
|
167
|
+
matches: [],
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const { value: matches } = await t.trace("search.model", () => runSearch(model, request), { count: (v) => v.length });
|
|
171
|
+
return {
|
|
172
|
+
schemaVersion: "search.v1",
|
|
173
|
+
mode: "search",
|
|
174
|
+
source: command.source,
|
|
175
|
+
model: { id: command.model },
|
|
176
|
+
patterns: patternIds,
|
|
177
|
+
stats: {
|
|
178
|
+
elapsedMs: Date.now() - startedAt,
|
|
179
|
+
modelCalls: 1,
|
|
180
|
+
inputTokens,
|
|
181
|
+
inputTokenCap: maxSearchInputTokens,
|
|
182
|
+
filesChanged: changeSet.summary.fileCount,
|
|
183
|
+
entitiesScanned: changeSet.summary.total,
|
|
184
|
+
candidates: contexts.length,
|
|
185
|
+
searchTargets: searchContexts.length,
|
|
186
|
+
repomixFiles: pack.filePaths.length,
|
|
187
|
+
repomixTokens: pack.totalTokens,
|
|
188
|
+
repomixConfig: pack.config,
|
|
189
|
+
profileId: profile?.id,
|
|
190
|
+
targetsByPattern: countTargetsByPattern(searchContexts),
|
|
191
|
+
targetsPreview: previewTargets(searchContexts),
|
|
192
|
+
},
|
|
193
|
+
matches,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
await changeSet.cleanup();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function buildSearchRequest(changeSet, contexts, pack, patterns, profile, includeCounterReasonInPrompt) {
|
|
201
|
+
return searchRequest({
|
|
202
|
+
changeSet,
|
|
203
|
+
contexts,
|
|
204
|
+
pack,
|
|
205
|
+
patterns,
|
|
206
|
+
includeCounterReasonInPrompt: profile?.includeCounterReasonInPrompt ?? includeCounterReasonInPrompt,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function printRunPlan(command, filesChanged, entitiesScanned, patternIds) {
|
|
210
|
+
if (command.json)
|
|
211
|
+
return;
|
|
212
|
+
console.error("🧙 stupify 🪄");
|
|
213
|
+
console.error(`Mode: search (${command.source})`);
|
|
214
|
+
console.error(`Sem: ${filesChanged} files, ${entitiesScanned} changed entities`);
|
|
215
|
+
console.error(`Patterns: ${patternIds.join(", ")}`);
|
|
216
|
+
}
|
|
217
|
+
function countTargetsByPattern(contexts) {
|
|
218
|
+
const counts = {};
|
|
219
|
+
for (const context of contexts)
|
|
220
|
+
counts[context.checkId] = (counts[context.checkId] ?? 0) + 1;
|
|
221
|
+
return counts;
|
|
222
|
+
}
|
|
223
|
+
function previewTargets(contexts) {
|
|
224
|
+
return contexts.map((context) => ({
|
|
225
|
+
targetId: context.targetId,
|
|
226
|
+
patternId: context.checkId,
|
|
227
|
+
entityKind: context.entityKind || undefined,
|
|
228
|
+
sourceKind: context.filePath ? pathKind(context.filePath) : undefined,
|
|
229
|
+
}));
|
|
230
|
+
}
|
|
231
|
+
function pathKind(filePath) {
|
|
232
|
+
const ext = filePath.split(".").pop();
|
|
233
|
+
return ext && ext !== filePath ? ext : "unknown";
|
|
234
|
+
}
|
|
235
|
+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
236
|
+
process.exitCode = await main();
|
|
237
|
+
}
|
package/dist/trace.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type TraceFields = Record<string, string | number | boolean | null | undefined>;
|
|
2
|
+
export type Tracer = {
|
|
3
|
+
trace<T>(span: string, fn: () => Promise<T>, options?: SpanTraceOptions<T>): Promise<{
|
|
4
|
+
value: T;
|
|
5
|
+
ms: number;
|
|
6
|
+
}>;
|
|
7
|
+
trace<T>(span: string, fn: () => T, options?: SpanTraceOptions<T>): {
|
|
8
|
+
value: T;
|
|
9
|
+
ms: number;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export type SpanTraceEvent = Readonly<{
|
|
13
|
+
name: string;
|
|
14
|
+
ms: number;
|
|
15
|
+
count?: number;
|
|
16
|
+
detail?: string;
|
|
17
|
+
}>;
|
|
18
|
+
export type SpanTraceOptions<T> = Readonly<{
|
|
19
|
+
fields?: TraceFields;
|
|
20
|
+
count?: (value: T) => number;
|
|
21
|
+
detail?: (value: T) => string;
|
|
22
|
+
}>;
|
|
23
|
+
export type CreateTracerOptions = {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
writeLine?: (line: string) => void;
|
|
26
|
+
onEvent?: (event: SpanTraceEvent) => void;
|
|
27
|
+
};
|
|
28
|
+
export declare function createTracer(options?: CreateTracerOptions): Tracer;
|
|
29
|
+
export declare const trace: Tracer;
|
package/dist/trace.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
export function createTracer(options) {
|
|
3
|
+
const enabled = options?.enabled ?? true;
|
|
4
|
+
const writeLine = options?.writeLine ?? ((line) => process.stderr.write(line + "\n"));
|
|
5
|
+
const onEvent = options?.onEvent;
|
|
6
|
+
const nowMs = () => performance.now();
|
|
7
|
+
function emit(span, durationMs, fields) {
|
|
8
|
+
if (!enabled)
|
|
9
|
+
return;
|
|
10
|
+
const payload = { span, ms: Math.round(durationMs) };
|
|
11
|
+
for (const [k, v] of Object.entries(fields ?? {})) {
|
|
12
|
+
if (v !== undefined)
|
|
13
|
+
payload[k] = v;
|
|
14
|
+
}
|
|
15
|
+
writeLine(`trace ${JSON.stringify(payload)}`);
|
|
16
|
+
}
|
|
17
|
+
function trace(span, fn, options) {
|
|
18
|
+
const startedAtMs = nowMs();
|
|
19
|
+
try {
|
|
20
|
+
const out = fn();
|
|
21
|
+
if (isPromiseLike(out)) {
|
|
22
|
+
return (async () => {
|
|
23
|
+
let durationMs;
|
|
24
|
+
try {
|
|
25
|
+
const value = await out;
|
|
26
|
+
durationMs = nowMs() - startedAtMs;
|
|
27
|
+
const event = {
|
|
28
|
+
name: span,
|
|
29
|
+
ms: Math.round(durationMs),
|
|
30
|
+
count: options?.count?.(value),
|
|
31
|
+
detail: options?.detail?.(value),
|
|
32
|
+
};
|
|
33
|
+
onEvent?.(event);
|
|
34
|
+
return { value, ms: event.ms };
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
durationMs ??= nowMs() - startedAtMs;
|
|
38
|
+
emit(span, durationMs, options?.fields);
|
|
39
|
+
}
|
|
40
|
+
})();
|
|
41
|
+
}
|
|
42
|
+
const durationMs = nowMs() - startedAtMs;
|
|
43
|
+
emit(span, durationMs, options?.fields);
|
|
44
|
+
const event = {
|
|
45
|
+
name: span,
|
|
46
|
+
ms: Math.round(durationMs),
|
|
47
|
+
count: options?.count?.(out),
|
|
48
|
+
detail: options?.detail?.(out),
|
|
49
|
+
};
|
|
50
|
+
onEvent?.(event);
|
|
51
|
+
return { value: out, ms: event.ms };
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
const durationMs = nowMs() - startedAtMs;
|
|
55
|
+
emit(span, durationMs, options?.fields);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { trace };
|
|
60
|
+
}
|
|
61
|
+
export const trace = createTracer();
|
|
62
|
+
function isPromiseLike(value) {
|
|
63
|
+
return typeof value === "object" && value !== null && "then" in value;
|
|
64
|
+
}
|