@beignet/cli 0.0.49 → 0.0.50

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 (47) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +41 -1
  3. package/dist/analysis/workspace.d.ts +1 -0
  4. package/dist/analysis/workspace.d.ts.map +1 -1
  5. package/dist/analysis/workspace.js +20 -10
  6. package/dist/analysis/workspace.js.map +1 -1
  7. package/dist/app-map-changes.d.ts +103 -0
  8. package/dist/app-map-changes.d.ts.map +1 -0
  9. package/dist/app-map-changes.js +949 -0
  10. package/dist/app-map-changes.js.map +1 -0
  11. package/dist/git-changes.d.ts +30 -0
  12. package/dist/git-changes.d.ts.map +1 -0
  13. package/dist/git-changes.js +367 -0
  14. package/dist/git-changes.js.map +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +26 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/inspect.js +291 -18
  19. package/dist/inspect.js.map +1 -1
  20. package/dist/lib.d.ts +3 -0
  21. package/dist/lib.d.ts.map +1 -1
  22. package/dist/lib.js +1 -0
  23. package/dist/lib.js.map +1 -1
  24. package/dist/make/shared.d.ts.map +1 -1
  25. package/dist/make/shared.js +31 -27
  26. package/dist/make/shared.js.map +1 -1
  27. package/dist/mcp.d.ts.map +1 -1
  28. package/dist/mcp.js +24 -2
  29. package/dist/mcp.js.map +1 -1
  30. package/dist/templates/agents.d.ts.map +1 -1
  31. package/dist/templates/agents.js +21 -1
  32. package/dist/templates/agents.js.map +1 -1
  33. package/dist/templates/base.d.ts.map +1 -1
  34. package/dist/templates/base.js +4 -1
  35. package/dist/templates/base.js.map +1 -1
  36. package/package.json +2 -2
  37. package/skills/app-structure/SKILL.md +25 -2
  38. package/src/analysis/workspace.ts +25 -9
  39. package/src/app-map-changes.ts +1462 -0
  40. package/src/git-changes.ts +511 -0
  41. package/src/index.ts +39 -1
  42. package/src/inspect.ts +422 -23
  43. package/src/lib.ts +21 -0
  44. package/src/make/shared.ts +31 -27
  45. package/src/mcp.ts +32 -2
  46. package/src/templates/agents.ts +21 -1
  47. package/src/templates/base.ts +4 -1
@@ -0,0 +1,511 @@
1
+ import { createHash } from "node:crypto";
2
+ import type { Stats } from "node:fs";
3
+ import { createReadStream } from "node:fs";
4
+ import { lstat, readlink } from "node:fs/promises";
5
+ import path from "node:path";
6
+ import { spawnCommand } from "./db.js";
7
+
8
+ const emptyTreeObjectIds = {
9
+ sha1: "4b825dc642cb6eb9a060e54bf8d69288fbee4904",
10
+ sha256: "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321",
11
+ } as const;
12
+ const maxGitOutputBytes = 64 * 1024 * 1024;
13
+
14
+ type GitCommandResult = Omit<
15
+ Awaited<ReturnType<typeof spawnCommand>>,
16
+ "stderr" | "stdout"
17
+ > & {
18
+ stderr: string;
19
+ stdout: string;
20
+ };
21
+
22
+ /** Git status values represented by changed-app mapping. */
23
+ export type GitChangedFileStatus =
24
+ | "added"
25
+ | "copied"
26
+ | "deleted"
27
+ | "modified"
28
+ | "renamed"
29
+ | "type-changed"
30
+ | "unmerged"
31
+ | "unknown"
32
+ | "untracked";
33
+
34
+ export type GitChangedFile = {
35
+ status: GitChangedFileStatus;
36
+ path: string;
37
+ oldPath?: string;
38
+ };
39
+
40
+ /** Resolved local Git comparison used by changed-app mapping. */
41
+ export type GitChangeComparison =
42
+ | {
43
+ mode: "worktree";
44
+ comparisonCommit: string;
45
+ head?: string;
46
+ }
47
+ | {
48
+ mode: "base";
49
+ base: string;
50
+ baseCommit: string;
51
+ mergeBase: string;
52
+ head: string;
53
+ };
54
+
55
+ export type GitChangeSet = {
56
+ repositoryRoot: string;
57
+ comparison: GitChangeComparison;
58
+ files: GitChangedFile[];
59
+ };
60
+
61
+ export async function readGitChangeSet(options: {
62
+ cwd: string;
63
+ base?: string;
64
+ }): Promise<GitChangeSet> {
65
+ if (options.base !== undefined && options.base.length === 0) {
66
+ throw new Error("Git base must not be empty.");
67
+ }
68
+ const repositoryRoot = path.resolve(
69
+ await gitText(options.cwd, ["rev-parse", "--show-toplevel"]),
70
+ );
71
+ const head = await resolveHeadCommit(repositoryRoot);
72
+
73
+ let comparison: GitChangeComparison;
74
+ let comparisonCommit: string;
75
+ if (options.base !== undefined) {
76
+ if (!head) {
77
+ throw new Error(
78
+ `Cannot compare --base ${JSON.stringify(options.base)} because this repository does not have a HEAD commit.`,
79
+ );
80
+ }
81
+ const baseCommit = await gitText(repositoryRoot, [
82
+ "rev-parse",
83
+ "--verify",
84
+ "--end-of-options",
85
+ `${options.base}^{commit}`,
86
+ ]).catch(() => {
87
+ throw new Error(
88
+ `Could not resolve local Git base ${JSON.stringify(options.base)}. Fetch it before running Beignet; changed mapping never accesses the network.`,
89
+ );
90
+ });
91
+ const mergeBase = await gitText(repositoryRoot, [
92
+ "merge-base",
93
+ baseCommit,
94
+ head,
95
+ ]).catch(() => {
96
+ throw new Error(
97
+ `Could not find a merge base between ${JSON.stringify(options.base)} and HEAD.`,
98
+ );
99
+ });
100
+ comparisonCommit = mergeBase;
101
+ comparison = {
102
+ mode: "base",
103
+ base: options.base,
104
+ baseCommit,
105
+ mergeBase,
106
+ head,
107
+ };
108
+ } else {
109
+ comparisonCommit = head ?? (await emptyTreeObjectId(repositoryRoot));
110
+ comparison = {
111
+ mode: "worktree",
112
+ comparisonCommit,
113
+ ...(head ? { head } : {}),
114
+ };
115
+ }
116
+
117
+ const committed =
118
+ comparison.mode === "base"
119
+ ? await gitDiffOutput(repositoryRoot, [
120
+ comparison.mergeBase,
121
+ comparison.head,
122
+ ])
123
+ : "";
124
+ const staged = await gitDiffOutput(repositoryRoot, [
125
+ "--cached",
126
+ head ?? comparisonCommit,
127
+ ]);
128
+ const unstaged = await gitDiffOutput(repositoryRoot, []);
129
+ const untracked = await gitOutput(repositoryRoot, [
130
+ "ls-files",
131
+ "--others",
132
+ "--exclude-standard",
133
+ "-z",
134
+ ]);
135
+ const files = mergeChangedFiles(
136
+ parseNameStatus(committed),
137
+ parseNameStatus(staged),
138
+ parseNameStatus(unstaged),
139
+ parseUntracked(untracked),
140
+ );
141
+
142
+ return { repositoryRoot, comparison, files };
143
+ }
144
+
145
+ async function emptyTreeObjectId(repositoryRoot: string): Promise<string> {
146
+ const objectFormat =
147
+ (await tryGitText(repositoryRoot, [
148
+ "rev-parse",
149
+ "--show-object-format=storage",
150
+ ])) ?? "sha1";
151
+ const objectId =
152
+ emptyTreeObjectIds[objectFormat as keyof typeof emptyTreeObjectIds];
153
+ if (!objectId) {
154
+ throw new Error(
155
+ `Unsupported Git object format ${JSON.stringify(objectFormat)}.`,
156
+ );
157
+ }
158
+ return objectId;
159
+ }
160
+
161
+ export async function hashGitChanges(
162
+ changeSet: Pick<GitChangeSet, "comparison" | "files" | "repositoryRoot">,
163
+ files = changeSet.files,
164
+ ): Promise<string> {
165
+ const hash = createHash("sha256");
166
+ const indexEntries = await readGitIndexEntries(changeSet.repositoryRoot);
167
+ hash.update(JSON.stringify(changeSet.comparison));
168
+ hash.update("\0");
169
+
170
+ for (const file of [...files].sort(compareChangedFiles)) {
171
+ hash.update(
172
+ JSON.stringify({
173
+ status: file.status,
174
+ path: file.path,
175
+ oldPath: file.oldPath,
176
+ }),
177
+ );
178
+ hash.update("\0");
179
+ for (const relativePath of uniquePaths([file.path, file.oldPath])) {
180
+ hash.update(relativePath);
181
+ hash.update("\0index\0");
182
+ hash.update(indexEntries.get(relativePath) ?? "missing");
183
+ hash.update("\0worktree\0");
184
+ await hashCurrentPath(hash, changeSet.repositoryRoot, relativePath);
185
+ hash.update("\0");
186
+ }
187
+ }
188
+
189
+ return `sha256:${hash.digest("hex")}`;
190
+ }
191
+
192
+ async function readGitIndexEntries(
193
+ repositoryRoot: string,
194
+ ): Promise<Map<string, string>> {
195
+ const output = await gitOutput(repositoryRoot, ["ls-files", "--stage", "-z"]);
196
+ const entries = new Map<string, string[]>();
197
+ for (const field of output.split("\0").filter(Boolean)) {
198
+ const separator = field.indexOf("\t");
199
+ if (separator === -1) {
200
+ throw new Error("Git returned an incomplete index entry.");
201
+ }
202
+ const metadata = field.slice(0, separator);
203
+ const file = normalizeGitPath(field.slice(separator + 1));
204
+ const values = entries.get(file) ?? [];
205
+ values.push(metadata);
206
+ entries.set(file, values);
207
+ }
208
+ return new Map(
209
+ [...entries].map(([file, values]) => [file, [...values].sort().join("\0")]),
210
+ );
211
+ }
212
+
213
+ async function hashCurrentPath(
214
+ hash: ReturnType<typeof createHash>,
215
+ repositoryRoot: string,
216
+ relativePath: string,
217
+ ): Promise<void> {
218
+ const absolutePath = path.join(repositoryRoot, relativePath);
219
+ let stats: Stats;
220
+ try {
221
+ stats = await lstat(absolutePath);
222
+ } catch (error) {
223
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
224
+ hash.update("missing");
225
+ return;
226
+ }
227
+ throw error;
228
+ }
229
+
230
+ if (stats.isSymbolicLink()) {
231
+ hash.update("symlink\0");
232
+ hash.update(String(stats.mode));
233
+ hash.update("\0");
234
+ hash.update(await readlink(absolutePath));
235
+ return;
236
+ }
237
+ if (stats.isDirectory()) {
238
+ hash.update("directory\0");
239
+ const head = await tryGitText(absolutePath, [
240
+ "rev-parse",
241
+ "--verify",
242
+ "HEAD^{commit}",
243
+ ]);
244
+ const status = await tryGitOutput(absolutePath, [
245
+ "status",
246
+ "--porcelain=v1",
247
+ "-z",
248
+ "--untracked-files=all",
249
+ ]);
250
+ hash.update(head ?? "unknown-head");
251
+ hash.update("\0");
252
+ hash.update(status ?? "unknown-status");
253
+ return;
254
+ }
255
+ if (!stats.isFile()) {
256
+ hash.update("special");
257
+ return;
258
+ }
259
+
260
+ hash.update("file\0");
261
+ hash.update(String(stats.mode));
262
+ hash.update("\0");
263
+ for await (const chunk of createReadStream(absolutePath)) {
264
+ hash.update(chunk as Buffer);
265
+ }
266
+ }
267
+
268
+ function parseNameStatus(output: string): GitChangedFile[] {
269
+ if (!output) return [];
270
+ const fields = output.split("\0");
271
+ const files: GitChangedFile[] = [];
272
+ for (let index = 0; index < fields.length; ) {
273
+ const rawStatus = fields[index++];
274
+ if (!rawStatus) continue;
275
+ const status = changedFileStatus(rawStatus[0]);
276
+ if (status === "renamed" || status === "copied") {
277
+ const oldPath = fields[index++];
278
+ const nextPath = fields[index++];
279
+ if (!oldPath || !nextPath) {
280
+ throw new Error("Git returned an incomplete rename or copy record.");
281
+ }
282
+ files.push({
283
+ status,
284
+ oldPath: normalizeGitPath(oldPath),
285
+ path: normalizeGitPath(nextPath),
286
+ });
287
+ continue;
288
+ }
289
+ const file = fields[index++];
290
+ if (!file)
291
+ throw new Error("Git returned an incomplete changed-file record.");
292
+ files.push({ status, path: normalizeGitPath(file) });
293
+ }
294
+ return files;
295
+ }
296
+
297
+ function parseUntracked(output: string): GitChangedFile[] {
298
+ return output
299
+ .split("\0")
300
+ .filter(Boolean)
301
+ .map((file) => ({ status: "untracked", path: normalizeGitPath(file) }));
302
+ }
303
+
304
+ function mergeChangedFiles(...groups: GitChangedFile[][]): GitChangedFile[] {
305
+ const files = new Map<string, GitChangedFile>();
306
+ for (const group of groups) {
307
+ for (const file of group) {
308
+ if (file.status === "renamed" && file.oldPath) {
309
+ const source = files.get(file.oldPath);
310
+ if (source) {
311
+ files.delete(file.oldPath);
312
+ const composed = composeRename(source, file);
313
+ files.set(
314
+ composed.path,
315
+ mergeSameDestination(files.get(composed.path), composed),
316
+ );
317
+ continue;
318
+ }
319
+ }
320
+ files.set(file.path, mergeSameDestination(files.get(file.path), file));
321
+ }
322
+ }
323
+ return [...files.values()].sort(compareChangedFiles);
324
+ }
325
+
326
+ function composeRename(
327
+ source: GitChangedFile,
328
+ rename: GitChangedFile,
329
+ ): GitChangedFile {
330
+ if (source.status === "renamed" && source.oldPath) {
331
+ return rename.path === source.oldPath
332
+ ? source
333
+ : { status: "renamed", oldPath: source.oldPath, path: rename.path };
334
+ }
335
+ if (source.status === "copied" && source.oldPath) {
336
+ return { status: "copied", oldPath: source.oldPath, path: rename.path };
337
+ }
338
+ if (source.status === "added" || source.status === "untracked") {
339
+ return { status: source.status, path: rename.path };
340
+ }
341
+ return rename;
342
+ }
343
+
344
+ function mergeSameDestination(
345
+ previous: GitChangedFile | undefined,
346
+ next: GitChangedFile,
347
+ ): GitChangedFile {
348
+ if (
349
+ previous &&
350
+ (previous.status === "added" ||
351
+ previous.status === "copied" ||
352
+ previous.status === "renamed" ||
353
+ previous.status === "untracked") &&
354
+ next.status !== "unmerged" &&
355
+ next.status !== "unknown"
356
+ ) {
357
+ return previous;
358
+ }
359
+ return next;
360
+ }
361
+
362
+ function uniquePaths(paths: Array<string | undefined>): string[] {
363
+ return [...new Set(paths.filter((file): file is string => Boolean(file)))];
364
+ }
365
+
366
+ function changedFileStatus(status: string | undefined): GitChangedFileStatus {
367
+ switch (status) {
368
+ case "A":
369
+ return "added";
370
+ case "C":
371
+ return "copied";
372
+ case "D":
373
+ return "deleted";
374
+ case "M":
375
+ return "modified";
376
+ case "R":
377
+ return "renamed";
378
+ case "T":
379
+ return "type-changed";
380
+ case "U":
381
+ return "unmerged";
382
+ default:
383
+ return "unknown";
384
+ }
385
+ }
386
+
387
+ function compareChangedFiles(
388
+ left: GitChangedFile,
389
+ right: GitChangedFile,
390
+ ): number {
391
+ return `${left.path}\0${left.oldPath ?? ""}\0${left.status}`.localeCompare(
392
+ `${right.path}\0${right.oldPath ?? ""}\0${right.status}`,
393
+ );
394
+ }
395
+
396
+ function normalizeGitPath(file: string): string {
397
+ return process.platform === "win32" ? file.replaceAll("\\", "/") : file;
398
+ }
399
+
400
+ async function gitText(cwd: string, args: readonly string[]): Promise<string> {
401
+ return (await gitOutput(cwd, args)).trim();
402
+ }
403
+
404
+ async function resolveHeadCommit(
405
+ repositoryRoot: string,
406
+ ): Promise<string | undefined> {
407
+ const head = await runGit(repositoryRoot, [
408
+ "rev-parse",
409
+ "--verify",
410
+ "HEAD^{commit}",
411
+ ]);
412
+ if (head.exitCode === 0) return head.stdout.trim();
413
+
414
+ const symbolicHead = await runGit(repositoryRoot, [
415
+ "symbolic-ref",
416
+ "--quiet",
417
+ "HEAD",
418
+ ]);
419
+ if (symbolicHead.exitCode !== 0) {
420
+ throw gitCommandError(["rev-parse", "--verify", "HEAD^{commit}"], head);
421
+ }
422
+
423
+ const ref = symbolicHead.stdout.trim();
424
+ const refExists = await runGit(repositoryRoot, [
425
+ "show-ref",
426
+ "--verify",
427
+ "--quiet",
428
+ ref,
429
+ ]);
430
+ if (refExists.exitCode === 1) return undefined;
431
+ throw gitCommandError(["rev-parse", "--verify", "HEAD^{commit}"], head);
432
+ }
433
+
434
+ async function tryGitText(
435
+ cwd: string,
436
+ args: readonly string[],
437
+ ): Promise<string | undefined> {
438
+ try {
439
+ return await gitText(cwd, args);
440
+ } catch {
441
+ return undefined;
442
+ }
443
+ }
444
+
445
+ async function tryGitOutput(
446
+ cwd: string,
447
+ args: readonly string[],
448
+ ): Promise<string | undefined> {
449
+ try {
450
+ return await gitOutput(cwd, args);
451
+ } catch {
452
+ return undefined;
453
+ }
454
+ }
455
+
456
+ async function gitOutput(
457
+ cwd: string,
458
+ args: readonly string[],
459
+ ): Promise<string> {
460
+ const result = await runGit(cwd, args);
461
+ if (result.exitCode !== 0) throw gitCommandError(args, result);
462
+ return result.stdout;
463
+ }
464
+
465
+ async function runGit(
466
+ cwd: string,
467
+ args: readonly string[],
468
+ ): Promise<GitCommandResult> {
469
+ const result = await spawnCommand("git", args, cwd, {
470
+ captureOutput: true,
471
+ maxOutputBytes: maxGitOutputBytes,
472
+ });
473
+ if (result.outputTruncated) {
474
+ throw new Error(
475
+ `Git output exceeded ${maxGitOutputBytes} bytes; changed mapping cannot produce a complete report.`,
476
+ );
477
+ }
478
+ return {
479
+ ...result,
480
+ stdout: result.stdout ?? "",
481
+ stderr: result.stderr ?? "",
482
+ };
483
+ }
484
+
485
+ function gitCommandError(
486
+ args: readonly string[],
487
+ result: GitCommandResult,
488
+ ): Error {
489
+ const detail = result.stderr.trim() || result.stdout.trim();
490
+ return new Error(
491
+ `Git command failed: git ${args.join(" ")}${detail ? `\n${detail}` : ""}`,
492
+ );
493
+ }
494
+
495
+ async function gitDiffOutput(
496
+ repositoryRoot: string,
497
+ comparisonArgs: readonly string[],
498
+ ): Promise<string> {
499
+ return gitOutput(repositoryRoot, [
500
+ "diff",
501
+ "--no-ext-diff",
502
+ "--no-textconv",
503
+ "--name-status",
504
+ "-z",
505
+ "--find-renames=50%",
506
+ "--find-copies=50%",
507
+ "--find-copies-harder",
508
+ ...comparisonArgs,
509
+ "--",
510
+ ]);
511
+ }
package/src/index.ts CHANGED
@@ -120,6 +120,8 @@ type RoutesFlags = {
120
120
  type MapFlags = RoutesFlags & {
121
121
  feature?: string;
122
122
  kind?: readonly AppMapNodeKind[];
123
+ changed?: boolean;
124
+ base?: string;
123
125
  };
124
126
 
125
127
  type ExplainFlags = RoutesFlags;
@@ -391,6 +393,15 @@ const routesFlagParameters = {
391
393
 
392
394
  const mapFlagParameters = {
393
395
  json: jsonFlag,
396
+ changed: {
397
+ kind: "boolean",
398
+ optional: true,
399
+ withNegated: false,
400
+ brief: "Map the current Git change set to potentially affected concepts.",
401
+ },
402
+ base: parsedStringFlag(
403
+ "Include committed changes since the merge base of this local Git ref. Requires --changed.",
404
+ ),
394
405
  feature: parsedStringFlag(
395
406
  "Project one feature and its direct relationships.",
396
407
  ),
@@ -889,7 +900,7 @@ const mapCommand = buildCommand<MapFlags, [], CliContext>({
889
900
  docs: {
890
901
  brief: "Map the app's architecture and registered workflows.",
891
902
  fullDescription:
892
- "Build a deterministic graph of features, HTTP contracts, routes, use cases, policies, workflows, ports, providers, OpenAPI, tests, dependencies, and doctor findings.",
903
+ "Build a deterministic graph of features, HTTP contracts, routes, use cases, policies, workflows, ports, providers, OpenAPI, tests, dependencies, and doctor findings. Use --changed for a bounded, report-only semantic map of the current Git change set.",
893
904
  },
894
905
  parameters: {
895
906
  flags: mapFlagParameters,
@@ -898,8 +909,35 @@ const mapCommand = buildCommand<MapFlags, [], CliContext>({
898
909
  const { formatAppMap, mapApp, projectAppMap } = await import(
899
910
  "./app-map.js"
900
911
  );
912
+ const { formatAppChangeImpact, mapAppChanges } = await import(
913
+ "./app-map-changes.js"
914
+ );
901
915
 
902
916
  return async function runMap(this: CliContext, flags: MapFlags) {
917
+ if (flags.base !== undefined && !flags.changed) {
918
+ throw new Error("map --base requires --changed.");
919
+ }
920
+ if (
921
+ flags.changed &&
922
+ (flags.feature !== undefined || flags.kind !== undefined)
923
+ ) {
924
+ throw new Error(
925
+ "map --changed cannot be combined with --feature or --kind.",
926
+ );
927
+ }
928
+ if (flags.changed) {
929
+ const changed = await mapAppChanges({
930
+ cwd: flags.cwd,
931
+ base: flags.base,
932
+ });
933
+ writeOutput(
934
+ this,
935
+ flags.json
936
+ ? JSON.stringify(changed, null, 2)
937
+ : formatAppChangeImpact(changed),
938
+ );
939
+ return;
940
+ }
903
941
  const result = projectAppMap(
904
942
  await mapApp({ cwd: flags.cwd, strict: true }),
905
943
  {