@diffci.com/diffci 0.3.14 → 0.3.16

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 (100) hide show
  1. package/LICENSE +20 -661
  2. package/README.md +1 -1
  3. package/dist-client/src/client/agent-selection.js +20 -0
  4. package/dist-client/src/client/cli.js +155 -12
  5. package/dist-client/src/client/context.js +1 -1
  6. package/dist-client/src/client/enforce-misses-collect.js +157 -0
  7. package/dist-client/src/client/enforce-misses.js +229 -0
  8. package/dist-client/src/client/enforce-policy.js +169 -0
  9. package/dist-client/src/client/feedback-prompt.js +42 -0
  10. package/dist-client/src/client/hook.js +94 -0
  11. package/dist-client/src/client/mcp.js +12 -4
  12. package/dist-client/src/client/observe.js +8 -3
  13. package/dist-client/src/client/telemetry.js +46 -0
  14. package/dist-client/src/client/usage-signal.js +17 -10
  15. package/dist-client/src/client/verify.js +48 -6
  16. package/node_modules/@diffci.com/core/LICENSE +21 -661
  17. package/node_modules/@diffci.com/core/NOTICE +2 -2
  18. package/node_modules/@diffci.com/core/README.md +21 -3
  19. package/node_modules/@diffci.com/core/dist/analyze.js +1 -1
  20. package/node_modules/@diffci.com/core/dist/analyze.js.map +1 -1
  21. package/node_modules/@diffci.com/core/dist/cache/graph-cache.js +1 -1
  22. package/node_modules/@diffci.com/core/dist/cache/graph-cache.js.map +1 -1
  23. package/node_modules/@diffci.com/core/dist/ci-inference/evidence.js +1 -1
  24. package/node_modules/@diffci.com/core/dist/ci-inference/evidence.js.map +1 -1
  25. package/node_modules/@diffci.com/core/dist/ci-inference/infer.js +1 -1
  26. package/node_modules/@diffci.com/core/dist/ci-inference/infer.js.map +1 -1
  27. package/node_modules/@diffci.com/core/dist/ci-inference/jobs.js +1 -1
  28. package/node_modules/@diffci.com/core/dist/ci-inference/jobs.js.map +1 -1
  29. package/node_modules/@diffci.com/core/dist/ci-inference/purpose.js +1 -1
  30. package/node_modules/@diffci.com/core/dist/ci-inference/purpose.js.map +1 -1
  31. package/node_modules/@diffci.com/core/dist/ci-inference/resolve.js +1 -1
  32. package/node_modules/@diffci.com/core/dist/ci-inference/resolve.js.map +1 -1
  33. package/node_modules/@diffci.com/core/dist/ci-inference/schema.js +1 -1
  34. package/node_modules/@diffci.com/core/dist/ci-inference/schema.js.map +1 -1
  35. package/node_modules/@diffci.com/core/dist/cli.js +1 -1
  36. package/node_modules/@diffci.com/core/dist/cli.js.map +1 -1
  37. package/node_modules/@diffci.com/core/dist/git/git-diff.d.ts +29 -4
  38. package/node_modules/@diffci.com/core/dist/git/git-diff.js +136 -22
  39. package/node_modules/@diffci.com/core/dist/git/git-diff.js.map +1 -1
  40. package/node_modules/@diffci.com/core/dist/git/types.d.ts +7 -0
  41. package/node_modules/@diffci.com/core/dist/index.js +1 -1
  42. package/node_modules/@diffci.com/core/dist/index.js.map +1 -1
  43. package/node_modules/@diffci.com/core/dist/measurement.js +1 -1
  44. package/node_modules/@diffci.com/core/dist/measurement.js.map +1 -1
  45. package/node_modules/@diffci.com/core/dist/repo/compiled-projects.js +1 -1
  46. package/node_modules/@diffci.com/core/dist/repo/compiled-projects.js.map +1 -1
  47. package/node_modules/@diffci.com/core/dist/repo/file-references.d.ts +32 -0
  48. package/node_modules/@diffci.com/core/dist/repo/file-references.js +180 -9
  49. package/node_modules/@diffci.com/core/dist/repo/file-references.js.map +1 -1
  50. package/node_modules/@diffci.com/core/dist/repo/impact-types.d.ts +4 -0
  51. package/node_modules/@diffci.com/core/dist/repo/impact.d.ts +19 -0
  52. package/node_modules/@diffci.com/core/dist/repo/impact.js +105 -3
  53. package/node_modules/@diffci.com/core/dist/repo/impact.js.map +1 -1
  54. package/node_modules/@diffci.com/core/dist/repo/npm-lockfile.d.ts +27 -0
  55. package/node_modules/@diffci.com/core/dist/repo/npm-lockfile.js +116 -0
  56. package/node_modules/@diffci.com/core/dist/repo/npm-lockfile.js.map +1 -0
  57. package/node_modules/@diffci.com/core/dist/repo/script-universe.js +1 -1
  58. package/node_modules/@diffci.com/core/dist/repo/script-universe.js.map +1 -1
  59. package/node_modules/@diffci.com/core/dist/repo/workflow-test-steps.js +1 -1
  60. package/node_modules/@diffci.com/core/dist/repo/workflow-test-steps.js.map +1 -1
  61. package/node_modules/@diffci.com/core/dist/repo/workspaces.js +1 -1
  62. package/node_modules/@diffci.com/core/dist/repo/workspaces.js.map +1 -1
  63. package/node_modules/@diffci.com/core/dist/research/baseline/matcher.js +1 -1
  64. package/node_modules/@diffci.com/core/dist/research/baseline/matcher.js.map +1 -1
  65. package/node_modules/@diffci.com/core/dist/research/baseline/test-activity.js +1 -1
  66. package/node_modules/@diffci.com/core/dist/research/baseline/test-activity.js.map +1 -1
  67. package/node_modules/@diffci.com/core/dist/research/baseline/workflow-parser.js +1 -1
  68. package/node_modules/@diffci.com/core/dist/research/baseline/workflow-parser.js.map +1 -1
  69. package/node_modules/@diffci.com/core/package.json +1 -1
  70. package/node_modules/@diffci.com/core/src/analyze.ts +1 -1
  71. package/node_modules/@diffci.com/core/src/cache/graph-cache.ts +1 -1
  72. package/node_modules/@diffci.com/core/src/ci-inference/causal.ts +1 -1
  73. package/node_modules/@diffci.com/core/src/ci-inference/evidence.ts +1 -1
  74. package/node_modules/@diffci.com/core/src/ci-inference/expression.ts +1 -1
  75. package/node_modules/@diffci.com/core/src/ci-inference/infer.ts +1 -1
  76. package/node_modules/@diffci.com/core/src/ci-inference/jobs.ts +1 -1
  77. package/node_modules/@diffci.com/core/src/ci-inference/matrix.ts +1 -1
  78. package/node_modules/@diffci.com/core/src/ci-inference/purpose.ts +1 -1
  79. package/node_modules/@diffci.com/core/src/ci-inference/reference-graph.ts +1 -1
  80. package/node_modules/@diffci.com/core/src/ci-inference/resolve.ts +1 -1
  81. package/node_modules/@diffci.com/core/src/ci-inference/schema.ts +1 -1
  82. package/node_modules/@diffci.com/core/src/cli.ts +1 -1
  83. package/node_modules/@diffci.com/core/src/git/git-diff.ts +127 -21
  84. package/node_modules/@diffci.com/core/src/git/types.ts +7 -0
  85. package/node_modules/@diffci.com/core/src/index.ts +1 -1
  86. package/node_modules/@diffci.com/core/src/measurement.ts +1 -1
  87. package/node_modules/@diffci.com/core/src/repo/compiled-projects.ts +1 -1
  88. package/node_modules/@diffci.com/core/src/repo/file-references.ts +170 -9
  89. package/node_modules/@diffci.com/core/src/repo/impact-types.ts +4 -0
  90. package/node_modules/@diffci.com/core/src/repo/impact.ts +115 -2
  91. package/node_modules/@diffci.com/core/src/repo/npm-lockfile.ts +109 -0
  92. package/node_modules/@diffci.com/core/src/repo/script-universe.ts +1 -1
  93. package/node_modules/@diffci.com/core/src/repo/workflow-test-steps.ts +1 -1
  94. package/node_modules/@diffci.com/core/src/repo/workspaces.ts +1 -1
  95. package/node_modules/@diffci.com/core/src/research/baseline/matcher.ts +1 -1
  96. package/node_modules/@diffci.com/core/src/research/baseline/path-baseline.ts +1 -1
  97. package/node_modules/@diffci.com/core/src/research/baseline/registry.ts +1 -1
  98. package/node_modules/@diffci.com/core/src/research/baseline/test-activity.ts +1 -1
  99. package/node_modules/@diffci.com/core/src/research/baseline/workflow-parser.ts +1 -1
  100. package/package.json +6 -4
@@ -0,0 +1,46 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
+ import { homedir } from "node:os";
4
+ import { dirname, join } from "node:path";
5
+ import { deliverUsageSignal } from "./usage-signal.js";
6
+ export function preferencePath(env) {
7
+ return join(env.DIFFCI_TELEMETRY_HOME ?? join(homedir(), ".diffci"), "telemetry.json");
8
+ }
9
+ export function readTelemetryPreference(env) {
10
+ try {
11
+ const value = JSON.parse(readFileSync(preferencePath(env), "utf8"));
12
+ if (!["off", "anonymous", "installations"].includes(value.mode))
13
+ throw new Error("invalid preference");
14
+ if (value.mode === "installations" && !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value.installationId ?? ""))
15
+ throw new Error("invalid identifier");
16
+ return { mode: value.mode, internal: value.internal === true, ...(value.mode === "installations" ? { installationId: value.installationId } : {}) };
17
+ }
18
+ catch {
19
+ return { mode: "off", internal: false };
20
+ }
21
+ }
22
+ export function configureTelemetry(mode, internal, env) {
23
+ if (!["off", "anonymous", "installations"].includes(mode))
24
+ throw new Error("Choose off, anonymous, or installations.");
25
+ const previous = readTelemetryPreference(env);
26
+ const value = { mode: mode, internal,
27
+ ...(mode === "installations" ? { installationId: previous.installationId ?? randomUUID() } : {}) };
28
+ const path = preferencePath(env);
29
+ mkdirSync(dirname(path), { recursive: true });
30
+ writeFileSync(path, `${JSON.stringify(value)}\n`, { encoding: "utf8", mode: 0o600 });
31
+ return value;
32
+ }
33
+ export async function recordUsage(command, outcome, version, flags, env) {
34
+ // Explicit denial overrides saved consent and every opt-in route.
35
+ if (flags["no-send"] === true || env.DIFFCI_SHARE_USAGE === "0" || env.DIFFCI_NO_SEND === "1")
36
+ return;
37
+ const preference = readTelemetryPreference(env);
38
+ if (flags["share-usage"] !== true && env.DIFFCI_SHARE_USAGE !== "1" && preference.mode === "off")
39
+ return;
40
+ const result = await deliverUsageSignal({ command, outcome, version,
41
+ surface: env.DIFFCI_MCP === "1" ? "local-mcp" : "cli",
42
+ activity: flags["internal-usage"] === true || env.DIFFCI_USAGE_INTERNAL === "1" || preference.internal ? "internal" : "external",
43
+ ...(preference.mode === "installations" ? { installationId: preference.installationId } : {}),
44
+ });
45
+ console.error(`DiffCI telemetry: ${result.status}${result.httpStatus ? ` (HTTP ${result.httpStatus})` : ""}. No repository content sent.`);
46
+ }
@@ -1,18 +1,25 @@
1
- /** Explicit, best-effort aggregate usage signal. No repository or machine identifier is sent. */
2
- export async function sendUsageSignal(input) {
3
- const url = new URL(input.endpoint ?? "https://app.diffci.com/v1/usage-events");
4
- if (url.protocol !== "https:")
5
- return false;
1
+ export async function deliverUsageSignal(input) {
6
2
  try {
3
+ const url = new URL(input.endpoint ?? "https://app.diffci.com/v1/usage-events");
4
+ if (url.protocol !== "https:")
5
+ return { status: "failed" };
7
6
  const response = await (input.fetchImpl ?? fetch)(url, {
8
7
  method: "POST",
9
8
  headers: { "Content-Type": "application/json" },
10
- body: JSON.stringify({ schema: "diffci.usage.v1", command: input.command, outcome: input.outcome, version: input.version }),
11
- signal: AbortSignal.timeout(2500),
9
+ body: JSON.stringify({ schema: "diffci.usage.v1", command: input.command, outcome: input.outcome, version: input.version,
10
+ ...(input.surface ? { surface: input.surface } : {}),
11
+ ...(input.activity ? { activity: input.activity } : {}),
12
+ ...(input.installationId ? { installation_id: input.installationId } : {}),
13
+ }),
14
+ signal: AbortSignal.timeout(5000),
12
15
  });
13
- return response.ok;
16
+ return { status: !response.ok ? "failed" : response.status === 202 ? "accepted" : "delivered", httpStatus: response.status };
14
17
  }
15
- catch {
16
- return false;
18
+ catch (error) {
19
+ return { status: error instanceof Error && error.name === "TimeoutError" ? "timed-out" : "failed" };
17
20
  }
18
21
  }
22
+ export async function sendUsageSignal(input) {
23
+ const result = await deliverUsageSignal(input);
24
+ return result.status === "delivered" || result.status === "accepted";
25
+ }
@@ -3,8 +3,9 @@ import { tmpdir } from "node:os";
3
3
  import { join, resolve } from "node:path";
4
4
  import { batchCommand, measureCommands } from "./command-batches.js";
5
5
  import { compareWithBaseline } from "./failure-baseline.js";
6
+ import { collectEnforcementContext, evaluateEnforcement } from "./enforce-policy.js";
6
7
  import { inferFullCommand, inferSelectedCommand } from "./full-command.js";
7
- import { observe } from "./observe.js";
8
+ import { makeGitRunner, observe } from "./observe.js";
8
9
  import { captureWorkingTreeSnapshot } from "./working-tree.js";
9
10
  export const VERIFICATION_SCHEMA = "diffci.verification.v1";
10
11
  function receiptRange(range) {
@@ -49,6 +50,18 @@ function unknownSnapshot() {
49
50
  return { baseSha: "unknown", treeSha: "unknown", commitSha: "unknown", changed: true };
50
51
  }
51
52
  async function executeObservation(options, scope, before, observation) {
53
+ const enforcement = options.policy === "enforce"
54
+ ? evaluateEnforcement(collectEnforcementContext(makeGitRunner(options.repoPath), options.env, observation), observation)
55
+ : undefined;
56
+ const report = await executeWithPolicy(options, scope, before, observation, enforcement);
57
+ if (!enforcement)
58
+ return report;
59
+ return { ...report, enforcement: { ...enforcement, applied: enforcement.fired_fallback === null && report.selection === "selected" } };
60
+ }
61
+ async function executeWithPolicy(options, scope, before, observation, enforcement) {
62
+ // Under enforce, a fired fallback runs the full suite even where plain verify would block: an
63
+ // unusable analysis is a reason to run everything, never a reason to pass with nothing run.
64
+ const enforcedFull = enforcement !== undefined && enforcement.fired_fallback !== null;
52
65
  const result = observation.result;
53
66
  const range = observation.commitRange;
54
67
  const boundRange = scope === "commit-range" ? range : undefined;
@@ -66,13 +79,13 @@ async function executeObservation(options, scope, before, observation) {
66
79
  ...(observation.timings?.totalMs !== undefined ? { duration_ms: observation.timings.totalMs } : {}),
67
80
  ...(result?.graph ? { graph_confidence: result.graph.confidence, effective_graph_confidence: result.graph.effectiveConfidence } : {}),
68
81
  };
69
- if (observation.status !== "OBSERVED" || !result) {
82
+ if ((observation.status !== "OBSERVED" || !result) && !enforcedFull) {
70
83
  return blocked(before, scope, observation.reason ?? "DiffCI analysis did not produce a verification plan", analysis, counts, boundRange);
71
84
  }
72
85
  if (scope === "commit-range") {
73
- if (!range)
86
+ if (!range && !enforcedFull)
74
87
  return blocked(before, scope, "DiffCI did not resolve a commit range", analysis, counts);
75
- if (range.headSha !== before.baseSha) {
88
+ if (range && range.headSha !== before.baseSha) {
76
89
  return blocked(before, scope, `the analyzed head ${range.headSha} is not the checked-out HEAD ${before.baseSha}`, analysis, counts, range);
77
90
  }
78
91
  }
@@ -90,7 +103,9 @@ async function executeObservation(options, scope, before, observation) {
90
103
  report.snapshot.after_tree_sha = afterAnalysis.treeSha;
91
104
  return report;
92
105
  }
93
- const { selection, command, commands, reason, outsideSuite } = executableSelection(options.repoPath, result);
106
+ const { selection, command, commands, reason, outsideSuite } = enforcedFull || !result
107
+ ? fullSelection(options.repoPath, `enforce fallback (${enforcement?.fired_fallback}): ${enforcement?.detail}`)
108
+ : executableSelection(options.repoPath, result);
94
109
  if (!command) {
95
110
  const inferred = inferFullCommand(options.repoPath);
96
111
  return blocked(before, scope, `no executable verification command is available: ${inferred.reason}`, analysis, counts, boundRange);
@@ -137,6 +152,7 @@ async function executeObservation(options, scope, before, observation) {
137
152
  tests_selected: selection === "full" ? counts.testsAvailable : counts.testsSelected,
138
153
  ...(outsideSuite ? { tests_outside_full_suite: outsideSuite } : {}),
139
154
  selection,
155
+ ...(selection === "selected" && result ? { selected_tests: result.selectedTests } : {}),
140
156
  command,
141
157
  ...(commands.length > 1 ? { commands } : {}),
142
158
  execution,
@@ -200,6 +216,7 @@ async function verifyChangedSnapshot(options) {
200
216
  headOverride: before.commitSha,
201
217
  redactPaths: options.redactPaths,
202
218
  reportPath: join(temporaryDirectory, "observation.json"),
219
+ selectTestPhases: true,
203
220
  });
204
221
  return await executeObservation({ ...options, repoPath }, "working-tree", before, observation);
205
222
  }
@@ -235,6 +252,7 @@ async function verifyCommitRange(options) {
235
252
  headOverride: options.headOverride,
236
253
  redactPaths: options.redactPaths,
237
254
  reportPath: join(temporaryDirectory, "observation.json"),
255
+ selectTestPhases: true,
238
256
  });
239
257
  return await executeObservation({ ...options, repoPath }, "commit-range", before, observation);
240
258
  }
@@ -262,16 +280,34 @@ export function formatVerificationSummary(report) {
262
280
  lines.splice(2, 0, ` range: ${report.commit_range.base_sha.slice(0, 12)}..${report.commit_range.head_sha.slice(0, 12)}`);
263
281
  if (report.command)
264
282
  lines.push(` command: ${report.command}`);
283
+ if (report.enforcement) {
284
+ const fired = report.enforcement.fired_fallback;
285
+ lines.push(` enforce: ${report.enforcement.applied ? "applied" : fired ? `fell back to full (${fired}: ${report.enforcement.detail})` : "not applied"}`);
286
+ }
265
287
  return lines.join("\n");
266
288
  }
289
+ function fullSelection(repoPath, reason) {
290
+ const command = inferFullCommand(repoPath).command;
291
+ return { selection: "full", command, commands: command ? [command] : [], reason, outsideSuite: 0 };
292
+ }
267
293
  /** Shared executable plan for read-only agent selection and verification. */
268
294
  export function executableSelection(repoPath, result) {
269
295
  let selection;
270
296
  let command;
271
297
  let reason;
298
+ let primaryPhases = [];
272
299
  if (result.mode === "SELECTIVE" && !result.commandRefusalReason && result.proposedCommands.length === 1) {
273
300
  const selected = inferSelectedCommand(repoPath, result.proposedCommands[0], result.selectedTests);
274
- if (selected.command) {
301
+ const phases = result.testPhases ?? [];
302
+ if (selected.command && phases.length) {
303
+ // The selection covers some changed files only through these test-script phases, so they run
304
+ // after the selected files, as the test script would run them (2026-10-28).
305
+ selection = "selected";
306
+ primaryPhases = phases.map(phaseCommand);
307
+ command = selected.command;
308
+ reason = `dependency impact; also ran ${phases.length} test-script phase(s) that read changed files: ${phases.join("; ")}`;
309
+ }
310
+ else if (selected.command) {
275
311
  selection = "selected";
276
312
  command = selected.command;
277
313
  reason = "dependency impact";
@@ -312,7 +348,13 @@ export function executableSelection(repoPath, result) {
312
348
  // Each part stays within the shell's command-line limit (cmd.exe stops at 8,191 characters).
313
349
  const commands = command ? [
314
350
  ...batchCommand(primary ?? command, selection === "selected" ? result.selectedTests : []),
351
+ ...primaryPhases,
315
352
  ...(auxiliaryCommand ? batchCommand(auxiliaryCommand, outside) : []),
316
353
  ] : [];
317
354
  return { selection, command: commands.length ? commands.join(" && ") : command, commands, reason, outsideSuite };
318
355
  }
356
+ /** A test-script phase as a standalone command. `npm test` puts `node_modules/.bin` on PATH; outside
357
+ * it a repository-local runner such as tsx is reached the way selected TypeScript tests are. */
358
+ export function phaseCommand(phase) {
359
+ return /^node\s/.test(phase) ? phase : `npx --no-install ${phase}`;
360
+ }