semantic-atlas 0.1.4 → 0.3.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 (84) hide show
  1. package/.agents/skills/semantic-atlas/SKILL.md +62 -43
  2. package/.agents/skills/semantic-atlas/references/graph-patch.md +22 -5
  3. package/.agents/skills/semantic-atlas/references/product-feedback.md +34 -0
  4. package/.agents/skills/semantic-atlas/references/result-routing.md +32 -19
  5. package/.agents/skills/semantic-atlas/references/snapshot-bootstrap.md +17 -32
  6. package/.agents/skills/semantic-atlas-insights/SKILL.md +55 -0
  7. package/README.md +209 -80
  8. package/README.zh-CN.md +187 -81
  9. package/dist/cli/argument-parser.js +49 -23
  10. package/dist/cli/bin.js +16 -3
  11. package/dist/cli/cli-application.d.ts +3 -2
  12. package/dist/cli/cli-application.d.ts.map +1 -1
  13. package/dist/cli/cli-application.js +104 -48
  14. package/dist/cli/insights-cli.d.ts +6 -0
  15. package/dist/cli/insights-cli.d.ts.map +1 -0
  16. package/dist/cli/insights-cli.js +236 -0
  17. package/dist/cli/main.d.ts.map +1 -1
  18. package/dist/cli/main.js +94 -2
  19. package/dist/cli/standalone-cli.d.ts +3 -0
  20. package/dist/cli/standalone-cli.d.ts.map +1 -0
  21. package/dist/cli/standalone-cli.js +96 -0
  22. package/dist/cli/types.d.ts +9 -6
  23. package/dist/cli/types.d.ts.map +1 -1
  24. package/dist/contracts/cli.d.ts +473 -1400
  25. package/dist/contracts/cli.d.ts.map +1 -1
  26. package/dist/contracts/cli.js +65 -65
  27. package/dist/contracts/graph.d.ts +4 -0
  28. package/dist/contracts/graph.d.ts.map +1 -1
  29. package/dist/contracts/graph.js +2 -2
  30. package/dist/contracts/insights.d.ts +2 -0
  31. package/dist/contracts/insights.d.ts.map +1 -0
  32. package/dist/contracts/insights.js +1 -0
  33. package/dist/contracts/public-contracts.d.ts +1 -0
  34. package/dist/contracts/public-contracts.d.ts.map +1 -1
  35. package/dist/contracts/public-contracts.js +1 -0
  36. package/dist/evaluation/contracts.d.ts +11 -2
  37. package/dist/evaluation/contracts.d.ts.map +1 -1
  38. package/dist/evaluation/contracts.js +7 -2
  39. package/dist/graph/graph-store.d.ts +3 -1
  40. package/dist/graph/graph-store.d.ts.map +1 -1
  41. package/dist/graph/graph-store.js +48 -2
  42. package/dist/graph/types.d.ts +30 -9
  43. package/dist/graph/types.d.ts.map +1 -1
  44. package/dist/index.d.ts +0 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/insights/contracts.d.ts +266 -0
  47. package/dist/insights/contracts.d.ts.map +1 -0
  48. package/dist/insights/contracts.js +124 -0
  49. package/dist/insights/insights-store.d.ts +73 -0
  50. package/dist/insights/insights-store.d.ts.map +1 -0
  51. package/dist/insights/insights-store.js +246 -0
  52. package/dist/package-metadata.d.ts +2 -0
  53. package/dist/package-metadata.d.ts.map +1 -0
  54. package/dist/package-metadata.js +9 -0
  55. package/dist/setup/package-upgrader.d.ts +54 -0
  56. package/dist/setup/package-upgrader.d.ts.map +1 -0
  57. package/dist/setup/package-upgrader.js +169 -0
  58. package/dist/setup/skill-installer.d.ts +20 -0
  59. package/dist/setup/skill-installer.d.ts.map +1 -0
  60. package/dist/setup/skill-installer.js +187 -0
  61. package/dist/storage/atlas-database.d.ts +1 -0
  62. package/dist/storage/atlas-database.d.ts.map +1 -1
  63. package/dist/storage/atlas-database.js +13 -6
  64. package/dist/world/business-map-projector.d.ts +14 -0
  65. package/dist/world/business-map-projector.d.ts.map +1 -0
  66. package/dist/world/business-map-projector.js +178 -0
  67. package/dist/world/world-graph-query.d.ts +8 -13
  68. package/dist/world/world-graph-query.d.ts.map +1 -1
  69. package/dist/world/world-graph-query.js +26 -229
  70. package/docs/README.md +30 -0
  71. package/docs/architecture/codegraph-backend.md +2 -2
  72. package/docs/architecture/continuous-business-learning.md +240 -0
  73. package/docs/contracts/cli-v1.md +62 -18
  74. package/docs/contracts/graph-model.md +11 -7
  75. package/docs/contracts/graph-patch-v1.md +8 -1
  76. package/docs/contracts/insights-v1.md +84 -0
  77. package/docs/evaluation.md +20 -15
  78. package/docs/index.md +43 -0
  79. package/docs/log.md +80 -0
  80. package/docs/product-contract.md +24 -12
  81. package/package.json +3 -1
  82. package/schemas/cli-envelope-v1.schema.json +450 -565
  83. package/schemas/evaluation-run-v1.schema.json +4 -1
  84. package/schemas/insights-envelope-v1.schema.json +428 -0
@@ -52,6 +52,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
52
52
  });
53
53
  import { cliEnvelopeSchema } from "../contracts/cli.js";
54
54
  import { graphPatchV1Schema } from "../contracts/graph.js";
55
+ import { feedbackReportInputSchema } from "../contracts/insights.js";
55
56
  import { GraphStore } from "../graph/graph-store.js";
56
57
  import { BusinessKnowledgeService } from "../knowledge/business-knowledge-service.js";
57
58
  import { hasSupportedRepositoryLanguage, inspectRepositoryLanguages, } from "../repository/repository-language-support.js";
@@ -59,6 +60,7 @@ import { inspectGitRepository, } from "../repository/repository-inspector.js";
59
60
  import { createRepositorySnapshot } from "../snapshots/repository-snapshot.js";
60
61
  import { SnapshotStore, } from "../storage/snapshot-store.js";
61
62
  import { resolveAtlasDatabasePath } from "../storage/atlas-database.js";
63
+ import { InsightsStore } from "../insights/insights-store.js";
62
64
  import { CodeGraphStructuralBackend } from "../structural-backend/codegraph-backend.js";
63
65
  import { WorldGraphQuery } from "../world/world-graph-query.js";
64
66
  import { WorldModelService } from "../world/world-model-service.js";
@@ -117,16 +119,18 @@ export class CliApplication {
117
119
  return this.status(context);
118
120
  case "index":
119
121
  return this.index(context);
120
- case "map.roots":
121
- return this.mapRoots(context);
122
- case "map.children":
123
- return this.mapChildren(command.nodeId, context);
122
+ case "map.view":
123
+ return this.mapView(command.focusKey, context);
124
124
  case "map.search":
125
125
  return this.mapSearch(command.query, command.limit, context);
126
126
  case "map.show":
127
- return this.mapShow(command.nodeId, command.depth, context);
127
+ return this.mapShow(command.businessKey, context);
128
+ case "code.search":
129
+ return this.codeSearch(command.query, command.limit, context);
128
130
  case "learn":
129
131
  return this.learn(context);
132
+ case "feedback.report":
133
+ return this.feedbackReport(context);
130
134
  case "changes":
131
135
  return this.changes(command, context);
132
136
  }
@@ -216,13 +220,22 @@ export class CliApplication {
216
220
  warnings,
217
221
  };
218
222
  }
219
- async mapRoots(context) {
223
+ async mapView(focusKey, context) {
220
224
  const env_2 = { stack: [], error: void 0, hasError: false };
221
225
  try {
222
- this.requireCurrentWorld(context, "map.roots");
226
+ this.requireCurrentWorld(context, "map.view");
223
227
  const query = __addDisposableResource(env_2, new WorldGraphQuery(context.repository), false);
224
- const nodes = await query.roots();
225
- return mapResult({ command: "map.roots", nodes }, context.languages);
228
+ const view = await query.view(focusKey);
229
+ if (view === undefined) {
230
+ throw invalidInput(`Business node ${focusKey} was not found.`, "map.view");
231
+ }
232
+ const warnings = view.focus === null && view.regions.length === 0
233
+ ? [{
234
+ code: "BUSINESS_KNOWLEDGE_EMPTY",
235
+ message: "No business knowledge has been learned from engineering work yet.",
236
+ }]
237
+ : [];
238
+ return mapResult({ command: "map.view", ...view }, context.languages, warnings);
226
239
  }
227
240
  catch (e_2) {
228
241
  env_2.error = e_2;
@@ -232,17 +245,13 @@ export class CliApplication {
232
245
  __disposeResources(env_2);
233
246
  }
234
247
  }
235
- async mapChildren(nodeId, context) {
248
+ async mapSearch(lexicalQuery, limit, context) {
236
249
  const env_3 = { stack: [], error: void 0, hasError: false };
237
250
  try {
238
- this.requireCurrentWorld(context, "map.children");
251
+ this.requireCurrentWorld(context, "map.search");
239
252
  const query = __addDisposableResource(env_3, new WorldGraphQuery(context.repository), false);
240
- const reference = nodeReference(nodeId);
241
- const children = await query.children(reference);
242
- if (children === undefined) {
243
- throw invalidInput(`Map node ${nodeId} was not found.`, "map.children");
244
- }
245
- return mapResult({ command: "map.children", nodeId, children }, context.languages);
253
+ const results = await query.searchBusiness(lexicalQuery, { limit });
254
+ return mapResult({ command: "map.search", query: lexicalQuery, limit, results }, context.languages);
246
255
  }
247
256
  catch (e_3) {
248
257
  env_3.error = e_3;
@@ -252,13 +261,18 @@ export class CliApplication {
252
261
  __disposeResources(env_3);
253
262
  }
254
263
  }
255
- async mapSearch(lexicalQuery, limit, context) {
264
+ async codeSearch(structuralQuery, limit, context) {
256
265
  const env_4 = { stack: [], error: void 0, hasError: false };
257
266
  try {
258
- this.requireCurrentWorld(context, "map.search");
267
+ this.requireCurrentWorld(context, "code.search");
259
268
  const query = __addDisposableResource(env_4, new WorldGraphQuery(context.repository), false);
260
- const results = await query.search(lexicalQuery, { limit });
261
- return mapResult({ command: "map.search", query: lexicalQuery, limit, results }, context.languages);
269
+ const results = await query.searchCode(structuralQuery, { limit });
270
+ return mapResult({
271
+ command: "code.search",
272
+ query: structuralQuery,
273
+ limit,
274
+ results,
275
+ }, context.languages);
262
276
  }
263
277
  catch (e_4) {
264
278
  env_4.error = e_4;
@@ -268,23 +282,19 @@ export class CliApplication {
268
282
  __disposeResources(env_4);
269
283
  }
270
284
  }
271
- async mapShow(nodeId, depth, context) {
285
+ async mapShow(businessKey, context) {
272
286
  const env_5 = { stack: [], error: void 0, hasError: false };
273
287
  try {
274
288
  this.requireCurrentWorld(context, "map.show");
275
289
  const query = __addDisposableResource(env_5, new WorldGraphQuery(context.repository), false);
276
- const view = await query.show(nodeReference(nodeId), { maxDepth: depth });
290
+ const view = await query.showBusiness(businessKey);
277
291
  if (view === undefined) {
278
- throw invalidInput(`Map node ${nodeId} was not found.`, "map.show");
292
+ throw invalidInput(`Business node ${businessKey} was not found.`, "map.show");
279
293
  }
280
294
  return mapResult({
281
295
  command: "map.show",
282
296
  node: view.node,
283
- depth: view.depth,
284
- neighbors: view.neighbors.map(presentNeighbor),
285
- invariants: view.invariants,
286
- tests: view.tests,
287
- unknowns: view.unknowns,
297
+ relations: view.relations.map(presentNeighbor),
288
298
  }, context.languages);
289
299
  }
290
300
  catch (e_5) {
@@ -329,11 +339,59 @@ export class CliApplication {
329
339
  __disposeResources(env_6);
330
340
  }
331
341
  }
332
- changes(command, context) {
342
+ async feedbackReport(context) {
333
343
  const env_7 = { stack: [], error: void 0, hasError: false };
344
+ try {
345
+ const input = await readStandardInput(this.io.stdin);
346
+ let inputValue;
347
+ try {
348
+ inputValue = JSON.parse(input);
349
+ }
350
+ catch {
351
+ throw invalidInput("Standard input must contain one complete JSON value.", "feedback.report");
352
+ }
353
+ const parsed = feedbackReportInputSchema.safeParse(inputValue);
354
+ if (!parsed.success) {
355
+ throw invalidInput("The feedback report input is invalid.", "feedback.report", {
356
+ issues: parsed.error.issues.map(({ code, message, path }) => ({ code, message, path })),
357
+ });
358
+ }
359
+ const insights = __addDisposableResource(env_7, new InsightsStore(), false);
360
+ const report = insights.recordFeedback({
361
+ ...parsed.data,
362
+ repositoryId: context.repository.repositoryId,
363
+ snapshotId: snapshotDescriptor(context)?.id ?? null,
364
+ });
365
+ return {
366
+ data: {
367
+ command: "feedback.report",
368
+ report: {
369
+ id: report.id,
370
+ kind: report.kind,
371
+ category: report.category,
372
+ impact: report.impact,
373
+ sourceConfirmed: report.sourceConfirmed,
374
+ status: report.status,
375
+ contextEventCount: report.contextEventIds.length,
376
+ createdAt: report.createdAt,
377
+ },
378
+ },
379
+ warnings: [],
380
+ };
381
+ }
382
+ catch (e_7) {
383
+ env_7.error = e_7;
384
+ env_7.hasError = true;
385
+ }
386
+ finally {
387
+ __disposeResources(env_7);
388
+ }
389
+ }
390
+ changes(command, context) {
391
+ const env_8 = { stack: [], error: void 0, hasError: false };
334
392
  try {
335
393
  this.requireCurrentWorld(context, "changes");
336
- const query = __addDisposableResource(env_7, new WorldGraphQuery(context.repository), false);
394
+ const query = __addDisposableResource(env_8, new WorldGraphQuery(context.repository), false);
337
395
  let changes;
338
396
  try {
339
397
  changes = query.changes({
@@ -357,12 +415,12 @@ export class CliApplication {
357
415
  ];
358
416
  return { data: { command: "changes", ...changes }, warnings };
359
417
  }
360
- catch (e_7) {
361
- env_7.error = e_7;
362
- env_7.hasError = true;
418
+ catch (e_8) {
419
+ env_8.error = e_8;
420
+ env_8.hasError = true;
363
421
  }
364
422
  finally {
365
- __disposeResources(env_7);
423
+ __disposeResources(env_8);
366
424
  }
367
425
  }
368
426
  requireAvailableWorld(context, command) {
@@ -418,17 +476,17 @@ function worldFreshness(context) {
418
476
  : "stale";
419
477
  }
420
478
  function findPublishedSnapshot(repository, snapshotId) {
421
- const env_8 = { stack: [], error: void 0, hasError: false };
479
+ const env_9 = { stack: [], error: void 0, hasError: false };
422
480
  try {
423
- const snapshots = __addDisposableResource(env_8, new SnapshotStore(repository), false);
481
+ const snapshots = __addDisposableResource(env_9, new SnapshotStore(repository), false);
424
482
  return snapshots.findStored(snapshotId) ?? null;
425
483
  }
426
- catch (e_8) {
427
- env_8.error = e_8;
428
- env_8.hasError = true;
484
+ catch (e_9) {
485
+ env_9.error = e_9;
486
+ env_9.hasError = true;
429
487
  }
430
488
  finally {
431
- __disposeResources(env_8);
489
+ __disposeResources(env_9);
432
490
  }
433
491
  }
434
492
  function changeCounts(snapshot) {
@@ -457,8 +515,11 @@ function unsupportedLanguageWarnings(languages) {
457
515
  ? [{ code: "UNSUPPORTED_LANGUAGE", message: language.reason, details: { language: language.language } }]
458
516
  : []));
459
517
  }
460
- function mapResult(data, languages) {
461
- const warnings = unsupportedLanguageWarnings(languages);
518
+ function mapResult(data, languages, additionalWarnings = []) {
519
+ const warnings = [
520
+ ...unsupportedLanguageWarnings(languages),
521
+ ...additionalWarnings,
522
+ ];
462
523
  if (containsUnknown(data)) {
463
524
  warnings.push({ code: "UNKNOWN_BOUNDARY", message: "The result contains unresolved structural behavior." });
464
525
  }
@@ -479,11 +540,6 @@ function presentNeighbor(neighbor) {
479
540
  depth: neighbor.depth,
480
541
  };
481
542
  }
482
- function nodeReference(nodeId) {
483
- return /^(?:repository|module|file|symbol|test|unknown):/u.test(nodeId)
484
- ? { domain: "structural", id: nodeId }
485
- : { domain: "business", key: nodeId };
486
- }
487
543
  function containsUnknown(data) {
488
544
  return JSON.stringify(data).includes('"validity":"unknown"');
489
545
  }
@@ -0,0 +1,6 @@
1
+ import { type InsightsPeriod } from "../contracts/insights.js";
2
+ import { type InsightsRange } from "../insights/insights-store.js";
3
+ import type { CliIo } from "./types.js";
4
+ export declare function runInsightsCli(arguments_: readonly string[], io: CliIo): Promise<number | undefined>;
5
+ export declare function resolveInsightsRange(period: InsightsPeriod, now?: Date): InsightsRange;
6
+ //# sourceMappingURL=insights-cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"insights-cli.d.ts","sourceRoot":"","sources":["../../src/cli/insights-cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,cAAc,EAEpB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAClF,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAcxC,wBAAsB,cAAc,CAClC,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,EAAE,EAAE,KAAK,GACR,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CA6B7B;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,OAAa,GAAG,aAAa,CAY5F"}
@@ -0,0 +1,236 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ import { feedbackReportUpdateInputSchema, feedbackStatusSchema, insightsEnvelopeSchema, insightsPeriodSchema, } from "../contracts/insights.js";
54
+ import { InsightsStore } from "../insights/insights-store.js";
55
+ class InsightsInputError extends Error {
56
+ }
57
+ export async function runInsightsCli(arguments_, io) {
58
+ if (arguments_[0] !== "insights")
59
+ return undefined;
60
+ let command = identifyInsightsCommand(arguments_);
61
+ try {
62
+ const env_1 = { stack: [], error: void 0, hasError: false };
63
+ try {
64
+ const parsed = parseInsightsArguments(arguments_);
65
+ command = parsed.name;
66
+ const store = __addDisposableResource(env_1, new InsightsStore(), false);
67
+ const data = await executeInsightsCommand(parsed, store, io.stdin);
68
+ writeInsightsEnvelope(io, { schemaVersion: 1, status: "ok", data }, parsed.pretty);
69
+ return 0;
70
+ }
71
+ catch (e_1) {
72
+ env_1.error = e_1;
73
+ env_1.hasError = true;
74
+ }
75
+ finally {
76
+ __disposeResources(env_1);
77
+ }
78
+ }
79
+ catch (error) {
80
+ const inputError = error instanceof InsightsInputError;
81
+ writeInsightsEnvelope(io, {
82
+ schemaVersion: 1,
83
+ status: "error",
84
+ data: {
85
+ command,
86
+ error: {
87
+ code: inputError ? "INVALID_INPUT" : "INTERNAL_ERROR",
88
+ message: inputError ? error.message : "Semantic Atlas insights failed unexpectedly.",
89
+ },
90
+ },
91
+ }, false);
92
+ if (!inputError && error instanceof Error && error.stack !== undefined) {
93
+ io.stderr.write(`${error.stack}\n`);
94
+ }
95
+ return inputError ? 2 : 1;
96
+ }
97
+ }
98
+ export function resolveInsightsRange(period, now = new Date()) {
99
+ if (period === "all") {
100
+ return { from: "1970-01-01T00:00:00.000Z", to: "9999-12-31T23:59:59.999Z" };
101
+ }
102
+ const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
103
+ const start = new Date(today);
104
+ if (period === "yesterday")
105
+ start.setDate(start.getDate() - 1);
106
+ if (period === "7d")
107
+ start.setDate(start.getDate() - 6);
108
+ if (period === "30d")
109
+ start.setDate(start.getDate() - 29);
110
+ const end = period === "yesterday" ? today : new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
111
+ return { from: start.toISOString(), to: end.toISOString() };
112
+ }
113
+ async function executeInsightsCommand(command, store, stdin) {
114
+ if (command.name === "insights.summary") {
115
+ const range = resolveInsightsRange(command.period);
116
+ return { command: command.name, range, summary: store.summary(range) };
117
+ }
118
+ if (command.name === "insights.feedback") {
119
+ const range = resolveInsightsRange(command.period);
120
+ return { command: command.name, range, reports: store.listFeedback(range, command.status) };
121
+ }
122
+ const input = await readJsonStandardInput(stdin, command.name);
123
+ const parsed = feedbackReportUpdateInputSchema.safeParse(input);
124
+ if (!parsed.success) {
125
+ throw new InsightsInputError("The feedback update input is invalid.");
126
+ }
127
+ return {
128
+ command: command.name,
129
+ report: store.updateFeedback({
130
+ id: parsed.data.id,
131
+ status: parsed.data.status,
132
+ note: parsed.data.note,
133
+ }),
134
+ };
135
+ }
136
+ function parseInsightsArguments(arguments_) {
137
+ const argumentsWithoutPretty = removePrettyOption(arguments_.slice(1));
138
+ const [subcommand, action, ...rest] = argumentsWithoutPretty.remaining;
139
+ if (subcommand === "summary") {
140
+ return {
141
+ name: "insights.summary",
142
+ period: parsePeriodOption([action, ...rest], "insights summary"),
143
+ pretty: argumentsWithoutPretty.pretty,
144
+ };
145
+ }
146
+ if (subcommand === "feedback" && action === "update") {
147
+ if (rest.length !== 1 || rest[0] !== "--stdin") {
148
+ throw new InsightsInputError("insights feedback update requires exactly --stdin.");
149
+ }
150
+ return { name: "insights.feedback.update", pretty: argumentsWithoutPretty.pretty };
151
+ }
152
+ if (subcommand === "feedback") {
153
+ const options = parseOptions([action, ...rest].filter(isDefined), ["--period", "--status"], "insights feedback");
154
+ const statusValue = options.get("--status");
155
+ const parsedStatus = statusValue === undefined ? undefined : feedbackStatusSchema.safeParse(statusValue);
156
+ if (parsedStatus !== undefined && !parsedStatus.success) {
157
+ throw new InsightsInputError("--status must be a feedback status.");
158
+ }
159
+ return {
160
+ name: "insights.feedback",
161
+ period: parsePeriod(options.get("--period")),
162
+ ...(parsedStatus === undefined ? {} : { status: parsedStatus.data }),
163
+ pretty: argumentsWithoutPretty.pretty,
164
+ };
165
+ }
166
+ throw new InsightsInputError("The insights command is invalid.");
167
+ }
168
+ function removePrettyOption(arguments_) {
169
+ const remaining = [];
170
+ let pretty = false;
171
+ for (const argument of arguments_) {
172
+ if (argument === "--pretty") {
173
+ if (pretty)
174
+ throw new InsightsInputError("--pretty may only be provided once.");
175
+ pretty = true;
176
+ }
177
+ else {
178
+ remaining.push(argument);
179
+ }
180
+ }
181
+ return { remaining, pretty };
182
+ }
183
+ function parsePeriodOption(arguments_, command) {
184
+ return parsePeriod(parseOptions(arguments_.filter(isDefined), ["--period"], command).get("--period"));
185
+ }
186
+ function parseOptions(arguments_, allowed, command) {
187
+ if (arguments_.length % 2 !== 0) {
188
+ throw new InsightsInputError(`${command} options require values.`);
189
+ }
190
+ const options = new Map();
191
+ for (let index = 0; index < arguments_.length; index += 2) {
192
+ const option = arguments_[index];
193
+ const value = arguments_[index + 1];
194
+ if (!allowed.includes(option) || value.startsWith("-") || options.has(option)) {
195
+ throw new InsightsInputError(`${command} accepts only ${allowed.join(" and ")}.`);
196
+ }
197
+ options.set(option, value);
198
+ }
199
+ return options;
200
+ }
201
+ function parsePeriod(value) {
202
+ if (value === undefined)
203
+ return "today";
204
+ const parsed = insightsPeriodSchema.safeParse(value);
205
+ if (!parsed.success) {
206
+ throw new InsightsInputError("--period must be one of today, yesterday, 7d, 30d, or all.");
207
+ }
208
+ return parsed.data;
209
+ }
210
+ async function readJsonStandardInput(stream, command) {
211
+ let contents = "";
212
+ for await (const chunk of stream)
213
+ contents += String(chunk);
214
+ try {
215
+ return JSON.parse(contents);
216
+ }
217
+ catch {
218
+ throw new InsightsInputError(`${command} requires one complete JSON value on standard input.`);
219
+ }
220
+ }
221
+ function identifyInsightsCommand(arguments_) {
222
+ if (arguments_[0] !== "insights")
223
+ return null;
224
+ if (arguments_[1] === "summary")
225
+ return "insights.summary";
226
+ if (arguments_[1] !== "feedback")
227
+ return null;
228
+ return arguments_[2] === "update" ? "insights.feedback.update" : "insights.feedback";
229
+ }
230
+ function writeInsightsEnvelope(io, envelope, pretty) {
231
+ const validated = insightsEnvelopeSchema.parse(envelope);
232
+ io.stdout.write(`${JSON.stringify(validated, null, pretty ? 2 : undefined)}\n`);
233
+ }
234
+ function isDefined(value) {
235
+ return value !== undefined;
236
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/cli/main.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAkB,KAAK,EAAE,MAAM,YAAY,CAAC;AAExD,wBAAsB,MAAM,CAC1B,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,EAAE,EAAE,KAAK,EACT,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CAuCjB"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/cli/main.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAkB,KAAK,EAAE,MAAM,YAAY,CAAC;AAExD,wBAAsB,MAAM,CAC1B,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,EAAE,EAAE,KAAK,EACT,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CAkEjB"}
package/dist/cli/main.js CHANGED
@@ -1,3 +1,56 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ import { InsightsStore } from "../insights/insights-store.js";
1
54
  import { parseCliArguments } from "./argument-parser.js";
2
55
  import { CliApplication } from "./cli-application.js";
3
56
  import { classifyCliError, CliError } from "./cli-error.js";
@@ -9,12 +62,18 @@ export async function runCli(arguments_, io, currentDirectory) {
9
62
  let repository = null;
10
63
  let snapshot = null;
11
64
  let application;
65
+ let observedRepository = null;
66
+ let outcome = "error";
67
+ let warningCodes = [];
68
+ let exitCode = 1;
69
+ const startedAt = performance.now();
12
70
  try {
13
71
  const invocation = parseCliArguments(arguments_, currentDirectory);
14
72
  pretty = invocation.pretty;
15
73
  command = invocation.command.name;
16
74
  application = new CliApplication(io);
17
75
  const context = await application.openRepository(invocation.repo, command);
76
+ observedRepository = context.repository;
18
77
  ({ repository, snapshot } = application.responseContext(context));
19
78
  const result = await application.execute(invocation.command, context);
20
79
  const responseContext = invocation.command.name === "index"
@@ -22,8 +81,11 @@ export async function runCli(arguments_, io, currentDirectory) {
22
81
  : context;
23
82
  const envelope = application.envelope(result, responseContext);
24
83
  snapshot = envelope.snapshot;
84
+ outcome = envelope.status;
85
+ warningCodes = envelope.warnings.map((warning) => warning.code);
25
86
  presenter.write(envelope, pretty);
26
- return 0;
87
+ exitCode = 0;
88
+ return exitCode;
27
89
  }
28
90
  catch (error) {
29
91
  const classified = classifyCliError(error, command);
@@ -40,6 +102,36 @@ export async function runCli(arguments_, io, currentDirectory) {
40
102
  if (classified.exitCode === 1 && !(error instanceof CliError)) {
41
103
  presenter.writeUnexpectedDiagnostic(error);
42
104
  }
43
- return classified.exitCode;
105
+ exitCode = classified.exitCode;
106
+ return exitCode;
107
+ }
108
+ finally {
109
+ if (command !== null && observedRepository !== null) {
110
+ try {
111
+ const env_1 = { stack: [], error: void 0, hasError: false };
112
+ try {
113
+ const insights = __addDisposableResource(env_1, new InsightsStore(), false);
114
+ insights.recordCommand({
115
+ repositoryId: observedRepository.repositoryId,
116
+ command: command,
117
+ outcome,
118
+ exitCode,
119
+ warningCodes,
120
+ durationMs: performance.now() - startedAt,
121
+ snapshotId: snapshot?.id ?? null,
122
+ });
123
+ }
124
+ catch (e_1) {
125
+ env_1.error = e_1;
126
+ env_1.hasError = true;
127
+ }
128
+ finally {
129
+ __disposeResources(env_1);
130
+ }
131
+ }
132
+ catch {
133
+ // Observability must not change the outcome or output of a development command.
134
+ }
135
+ }
44
136
  }
45
137
  }
@@ -0,0 +1,3 @@
1
+ import type { CliIo } from "./types.js";
2
+ export declare function runStandaloneCli(arguments_: readonly string[], io: CliIo): Promise<number | undefined>;
3
+ //# sourceMappingURL=standalone-cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standalone-cli.d.ts","sourceRoot":"","sources":["../../src/cli/standalone-cli.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AA6BxC,wBAAsB,gBAAgB,CACpC,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,EAAE,EAAE,KAAK,GACR,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CA6D7B"}