@diffci.com/diffci 0.3.20 → 0.3.22
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/dist-client/src/client/agent-identity.d.ts +26 -0
- package/dist-client/src/client/agent-identity.js +62 -0
- package/dist-client/src/client/agent-selection.d.ts +55 -0
- package/dist-client/src/client/cli.d.ts +2 -0
- package/dist-client/src/client/cli.js +164 -3
- package/dist-client/src/client/command-batches.d.ts +26 -0
- package/dist-client/src/client/command-batches.js +7 -1
- package/dist-client/src/client/context.d.ts +47 -0
- package/dist-client/src/client/delivery-warning.d.ts +1 -0
- package/dist-client/src/client/economics.d.ts +17 -0
- package/dist-client/src/client/enforce-misses-collect.d.ts +49 -0
- package/dist-client/src/client/enforce-misses.d.ts +124 -0
- package/dist-client/src/client/enforce-policy.d.ts +33 -0
- package/dist-client/src/client/failure-baseline.d.ts +67 -0
- package/dist-client/src/client/failure-baseline.js +43 -19
- package/dist-client/src/client/feedback-prompt.d.ts +6 -0
- package/dist-client/src/client/full-command.d.ts +16 -0
- package/dist-client/src/client/gate-checks.d.ts +54 -0
- package/dist-client/src/client/gate-checks.js +21 -7
- package/dist-client/src/client/gate.d.ts +154 -0
- package/dist-client/src/client/gate.js +221 -0
- package/dist-client/src/client/hook.d.ts +27 -0
- package/dist-client/src/client/install.d.ts +19 -0
- package/dist-client/src/client/mcp.d.ts +2 -0
- package/dist-client/src/client/observe.d.ts +52 -0
- package/dist-client/src/client/observe.js +25 -3
- package/dist-client/src/client/package-identity.d.ts +14 -0
- package/dist-client/src/client/pilot-packet.d.ts +6 -0
- package/dist-client/src/client/report.d.ts +265 -0
- package/dist-client/src/client/selected-coverage.d.ts +19 -0
- package/dist-client/src/client/selected-coverage.js +86 -0
- package/dist-client/src/client/shared-graph-cache.d.ts +113 -0
- package/dist-client/src/client/shared-graph-cache.js +347 -0
- package/dist-client/src/client/spec-conflicts.d.ts +19 -0
- package/dist-client/src/client/submit.d.ts +61 -0
- package/dist-client/src/client/telemetry.d.ts +13 -0
- package/dist-client/src/client/usage-signal.d.ts +19 -0
- package/dist-client/src/client/verify-savings.d.ts +186 -0
- package/dist-client/src/client/verify-savings.js +3 -0
- package/dist-client/src/client/verify.d.ts +113 -0
- package/dist-client/src/client/verify.js +37 -13
- package/dist-client/src/client/workflow-guard.d.ts +59 -0
- package/dist-client/src/client/workflow-guard.js +253 -0
- package/dist-client/src/client/workflow-templates.d.ts +8 -0
- package/dist-client/src/client/working-tree.d.ts +26 -0
- package/dist-client/src/preflight/fingerprint.d.ts +29 -0
- package/dist-client/src/preflight/taxonomy.d.ts +10 -0
- package/node_modules/@diffci.com/core/README.md +4 -0
- package/node_modules/@diffci.com/core/dist/git/git-diff.js +17 -2
- package/node_modules/@diffci.com/core/dist/git/git-diff.js.map +1 -1
- package/node_modules/@diffci.com/core/dist/planner/test-command.d.ts +2 -0
- package/node_modules/@diffci.com/core/dist/planner/test-command.js +46 -1
- package/node_modules/@diffci.com/core/dist/planner/test-command.js.map +1 -1
- package/node_modules/@diffci.com/core/src/git/git-diff.ts +17 -2
- package/node_modules/@diffci.com/core/src/planner/test-command.ts +45 -1
- package/package.json +9 -2
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { inferFullCommand } from "./full-command.js";
|
|
5
|
+
import { plannedChecks } from "./gate-checks.js";
|
|
6
|
+
import { observe } from "./observe.js";
|
|
7
|
+
import { executableSelection } from "./verify.js";
|
|
8
|
+
import { measureCommand } from "./verify-savings.js";
|
|
9
|
+
/**
|
|
10
|
+
* `diffci gate` in observe mode (2026-10-30, docs/design/pipeline-impact.md, phase 2).
|
|
11
|
+
*
|
|
12
|
+
* WHY. Builds that run a script instead of workflow jobs (DentalPresence's Cloudflare Sandbox build runs
|
|
13
|
+
* `npm run check` and its whole test suite on every staging build, about 19 of 44 build-hours) cannot use
|
|
14
|
+
* per-job `if:` conditions. `diffci gate` replaces that script. Before anything is skipped in a build, the
|
|
15
|
+
* miss rate has to be measured on real builds, so this mode changes nothing about what runs.
|
|
16
|
+
*
|
|
17
|
+
* WHAT IT DOES. Runs every leaf of the gate and the full test command, exactly as the build would, and
|
|
18
|
+
* exits with their result. Alongside, it records what DiffCI would have done for the same commit range:
|
|
19
|
+
* each check's RUN/NARROWED/SKIP decision and the test selection. A miss is a step DiffCI would have
|
|
20
|
+
* skipped or narrowed that failed in full while DiffCI's version passed; those are only rerun when the full
|
|
21
|
+
* step failed, so a passing build pays for nothing but the analysis. `--measure-selected` also runs the
|
|
22
|
+
* narrowed checks and selected tests on passing builds, to measure rather than estimate the saving.
|
|
23
|
+
*
|
|
24
|
+
* DiffCI never decides the build's outcome here: an analysis that fails still runs and reports the full gate.
|
|
25
|
+
*/
|
|
26
|
+
export const GATE_SCHEMA = "diffci.gate.v1";
|
|
27
|
+
function stepRun(measurement) {
|
|
28
|
+
const status = measurement.timedOut ? "timed_out" : measurement.exitCode === 0 && measurement.signal === null ? "passed" : "failed";
|
|
29
|
+
return { status, wall_ms: measurement.wallMs, exit_code: measurement.exitCode };
|
|
30
|
+
}
|
|
31
|
+
export const GATE_PLAN_SCHEMA = "diffci.gate-plan.v1";
|
|
32
|
+
export async function planGate(options) {
|
|
33
|
+
const repoPath = resolve(options.repoPath);
|
|
34
|
+
const temporary = mkdtempSync(join(tmpdir(), "diffci-gate-"));
|
|
35
|
+
let observation;
|
|
36
|
+
let analysisError;
|
|
37
|
+
try {
|
|
38
|
+
observation = await (options.analyse ?? observe)({
|
|
39
|
+
repoPath, env: options.env, version: options.version, engineSha: options.engineSha,
|
|
40
|
+
baseOverride: options.baseOverride, headOverride: options.headOverride,
|
|
41
|
+
reportPath: join(temporary, "observation.json"), selectTestPhases: true,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
analysisError = error instanceof Error ? error.message : String(error);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
rmSync(temporary, { recursive: true, force: true });
|
|
49
|
+
}
|
|
50
|
+
const range = observation?.commitRange;
|
|
51
|
+
const result = observation?.status === "OBSERVED" ? observation.result : undefined;
|
|
52
|
+
const unanalysed = analysisError ?? observation?.reason ?? "the change could not be analysed";
|
|
53
|
+
const checks = range ? plannedChecks(repoPath, { baseSha: range.baseSha, headSha: range.headSha }, { extraRoots: options.scripts ?? [] }) : [];
|
|
54
|
+
const selected = result ? executableSelection(repoPath, result) : undefined;
|
|
55
|
+
const selection = !selected || !selected.command ? "unavailable" : selected.selection;
|
|
56
|
+
return {
|
|
57
|
+
schema: GATE_PLAN_SCHEMA,
|
|
58
|
+
diffci_version: options.version,
|
|
59
|
+
...(range ? { range: { base_sha: range.baseSha, head_sha: range.headSha, source: range.source } } : {}),
|
|
60
|
+
analysis: {
|
|
61
|
+
status: analysisError ? "ERROR" : observation?.status ?? "ERROR",
|
|
62
|
+
...(analysisError ?? observation?.reason ? { reason: unanalysed } : {}),
|
|
63
|
+
...(result?.mode ? { mode: result.mode } : {}),
|
|
64
|
+
fallback_reasons: result?.fallbackReasons ?? [],
|
|
65
|
+
...(observation?.timings?.totalMs !== undefined ? { duration_ms: observation.timings.totalMs } : {}),
|
|
66
|
+
},
|
|
67
|
+
checks,
|
|
68
|
+
tests: {
|
|
69
|
+
selection,
|
|
70
|
+
reason: selected?.reason ?? unanalysed,
|
|
71
|
+
tests_selected: selection === "selected" ? result?.selectedTests.length ?? 0 : result?.totalTestCount ?? 0,
|
|
72
|
+
tests_available: result?.totalTestCount ?? 0,
|
|
73
|
+
...(selection === "selected" ? { selected_commands: selected.commands } : {}),
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** A plan that is not this version's, or not for the commit being gated, is not used. */
|
|
78
|
+
function usablePlan(plan, expectHead) {
|
|
79
|
+
const candidate = plan;
|
|
80
|
+
if (!candidate || candidate.schema !== GATE_PLAN_SCHEMA || !Array.isArray(candidate.checks) || !candidate.tests || !candidate.analysis) {
|
|
81
|
+
return { reason: "the gate plan is missing or not a diffci.gate-plan.v1 document" };
|
|
82
|
+
}
|
|
83
|
+
if (expectHead && candidate.range?.head_sha !== expectHead) {
|
|
84
|
+
return { reason: `the gate plan is for ${candidate.range?.head_sha ?? "no commit"}, not ${expectHead}` };
|
|
85
|
+
}
|
|
86
|
+
return { plan: candidate };
|
|
87
|
+
}
|
|
88
|
+
export async function runGate(options) {
|
|
89
|
+
const repoPath = resolve(options.repoPath);
|
|
90
|
+
const run = options.run ?? measureCommand;
|
|
91
|
+
const started = Date.now();
|
|
92
|
+
const remaining = () => Math.max(options.timeoutMs - (Date.now() - started), 0);
|
|
93
|
+
const execute = (command) => run(command, { cwd: repoPath, timeoutMs: remaining(), tailBytes: options.tailBytes });
|
|
94
|
+
// 1. What DiffCI would do: a plan made elsewhere, or an analysis here. Never fatal: the full gate runs
|
|
95
|
+
// whatever happens here.
|
|
96
|
+
const supplied = options.plan !== undefined ? usablePlan(options.plan, options.expectHead) : undefined;
|
|
97
|
+
const plan = supplied ? supplied.plan : await planGate(options);
|
|
98
|
+
const analysis = plan?.analysis ?? { status: "PLAN_UNUSABLE", reason: supplied?.reason, fallback_reasons: [] };
|
|
99
|
+
const unplanned = analysis.reason ?? "the change could not be analysed";
|
|
100
|
+
const analysisMs = supplied?.plan ? 0 : plan?.analysis.duration_ms ?? 0;
|
|
101
|
+
const runChecks = options.only !== "tests";
|
|
102
|
+
const full = runChecks ? plannedChecks(repoPath, undefined, { extraRoots: options.scripts ?? [] }) : [];
|
|
103
|
+
const decisionOf = new Map((plan?.checks ?? []).map((check) => [check.id, check]));
|
|
104
|
+
// 2. The full gate, every leaf, continuing past failures so each one is attributed.
|
|
105
|
+
const checks = full.map((check) => {
|
|
106
|
+
const decided = decisionOf.get(check.id);
|
|
107
|
+
const measured = stepRun(execute(check.command));
|
|
108
|
+
return {
|
|
109
|
+
id: check.id, kind: check.kind,
|
|
110
|
+
decision: decided?.decision ?? "UNPLANNED",
|
|
111
|
+
reason: decided?.reason ?? unplanned,
|
|
112
|
+
command: check.command,
|
|
113
|
+
...(decided?.decision === "NARROWED" ? { narrowed_command: decided.command } : {}),
|
|
114
|
+
full: measured,
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
const testCommand = options.only === "checks" ? undefined : options.tests ?? inferFullCommand(repoPath).command;
|
|
118
|
+
let tests;
|
|
119
|
+
if (testCommand) {
|
|
120
|
+
const measurement = execute(testCommand);
|
|
121
|
+
const fullRun = { ...stepRun(measurement), ...(measurement.failingTests?.length ? { failing_tests: measurement.failingTests } : {}) };
|
|
122
|
+
tests = {
|
|
123
|
+
command: testCommand, full: fullRun,
|
|
124
|
+
selection: plan?.tests.selection ?? "unavailable",
|
|
125
|
+
reason: plan?.tests.reason ?? unplanned,
|
|
126
|
+
tests_selected: plan?.tests.tests_selected ?? 0,
|
|
127
|
+
tests_available: plan?.tests.tests_available ?? 0,
|
|
128
|
+
...(plan?.tests.selected_commands ? { selected_commands: plan.tests.selected_commands } : {}),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
// 3. Misses, and optional timing of DiffCI's own versions of the steps. Reruns happen only on failure
|
|
132
|
+
// unless --measure-selected asks for timings on a passing build.
|
|
133
|
+
for (const check of checks) {
|
|
134
|
+
if (check.full.status === "passed" && !options.measureSelected)
|
|
135
|
+
continue;
|
|
136
|
+
if (check.decision === "SKIP") {
|
|
137
|
+
check.missed = check.full.status !== "passed";
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (check.decision !== "NARROWED" || !check.narrowed_command)
|
|
141
|
+
continue;
|
|
142
|
+
check.narrowed = stepRun(execute(check.narrowed_command));
|
|
143
|
+
if (check.full.status !== "passed")
|
|
144
|
+
check.missed = check.narrowed.status === "passed";
|
|
145
|
+
}
|
|
146
|
+
if (tests?.selected_commands && (tests.full.status !== "passed" || options.measureSelected)) {
|
|
147
|
+
const runs = tests.selected_commands.map((command) => execute(command));
|
|
148
|
+
const failed = runs.find((measurement) => stepRun(measurement).status !== "passed");
|
|
149
|
+
tests.selected = { ...stepRun(failed ?? runs[runs.length - 1]), wall_ms: runs.reduce((sum, measurement) => sum + measurement.wallMs, 0) };
|
|
150
|
+
if (tests.full.status !== "passed")
|
|
151
|
+
tests.missed = tests.selected.status === "passed";
|
|
152
|
+
}
|
|
153
|
+
// 4. Summary. The build's result is the full gate's, never DiffCI's.
|
|
154
|
+
const failedSteps = [...checks.filter((check) => check.full.status !== "passed").map((check) => check.id), ...(tests && tests.full.status !== "passed" ? ["tests"] : [])];
|
|
155
|
+
const fullMs = checks.reduce((sum, check) => sum + (check.full.wall_ms ?? 0), 0) + (tests?.full.wall_ms ?? 0);
|
|
156
|
+
let estimated = false;
|
|
157
|
+
let diffciMs = analysisMs;
|
|
158
|
+
for (const check of checks) {
|
|
159
|
+
if (check.decision === "SKIP")
|
|
160
|
+
continue;
|
|
161
|
+
if (check.decision === "NARROWED" && check.narrowed?.wall_ms !== undefined)
|
|
162
|
+
diffciMs += check.narrowed.wall_ms;
|
|
163
|
+
else {
|
|
164
|
+
if (check.decision === "NARROWED")
|
|
165
|
+
estimated = true;
|
|
166
|
+
diffciMs += check.full.wall_ms ?? 0;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (tests) {
|
|
170
|
+
if (tests.selection !== "selected")
|
|
171
|
+
diffciMs += tests.full.wall_ms ?? 0;
|
|
172
|
+
else if (tests.selected?.wall_ms !== undefined)
|
|
173
|
+
diffciMs += tests.selected.wall_ms;
|
|
174
|
+
else {
|
|
175
|
+
estimated = true;
|
|
176
|
+
diffciMs += Math.round((tests.full.wall_ms ?? 0) * (tests.tests_available ? tests.tests_selected / tests.tests_available : 1));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const misses = [...checks.filter((check) => check.missed).map((check) => check.id), ...(tests?.missed ? ["tests"] : [])];
|
|
180
|
+
return {
|
|
181
|
+
schema: GATE_SCHEMA, schema_version: "1", produced_at: new Date().toISOString(), diffci_version: options.version, mode: "observe",
|
|
182
|
+
...(options.only ? { only: options.only } : {}),
|
|
183
|
+
...(plan?.range ? { range: plan.range } : {}),
|
|
184
|
+
...(supplied ? { plan: supplied.plan ? "supplied" : "unusable" } : {}),
|
|
185
|
+
analysis,
|
|
186
|
+
gate: {
|
|
187
|
+
status: failedSteps.length ? "failed" : "passed",
|
|
188
|
+
wall_ms: Date.now() - started,
|
|
189
|
+
reason: failedSteps.length ? `failed: ${failedSteps.join(", ")}`
|
|
190
|
+
: options.only === "checks" ? "every gate check passed"
|
|
191
|
+
: options.only === "tests" ? (testCommand ? "the tests passed" : "no test command was found")
|
|
192
|
+
: testCommand ? "every gate step passed" : "every gate check passed; no test command was found",
|
|
193
|
+
},
|
|
194
|
+
checks,
|
|
195
|
+
...(tests ? { tests } : {}),
|
|
196
|
+
summary: { full_ms: fullMs, diffci_ms: diffciMs, saved_ms: fullMs - diffciMs, basis: estimated ? "estimated" : "measured", misses },
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
export function gateExitCode(report) {
|
|
200
|
+
return report.gate.status === "passed" ? 0 : 1;
|
|
201
|
+
}
|
|
202
|
+
const seconds = (ms) => `${(ms / 1000).toFixed(1)}s`;
|
|
203
|
+
export function formatGateSummary(report) {
|
|
204
|
+
const lines = [
|
|
205
|
+
`DiffCI gate (observe): ${report.gate.status.toUpperCase()} - ${report.gate.reason}`,
|
|
206
|
+
...(report.range ? [` range: ${report.range.base_sha.slice(0, 12)}..${report.range.head_sha.slice(0, 12)}`] : []),
|
|
207
|
+
` analysis: ${report.analysis.status}${report.analysis.mode ? ` (${report.analysis.mode})` : ""}${report.analysis.reason ? ` - ${report.analysis.reason}` : ""}`,
|
|
208
|
+
];
|
|
209
|
+
for (const check of report.checks) {
|
|
210
|
+
lines.push(` ${check.full.status === "passed" ? "ok " : "FAIL"} ${check.id} ${seconds(check.full.wall_ms ?? 0)} - DiffCI: ${check.decision}${check.missed ? " (MISSED)" : ""}`);
|
|
211
|
+
}
|
|
212
|
+
if (report.tests) {
|
|
213
|
+
const t = report.tests;
|
|
214
|
+
const scope = t.selection === "selected" ? `${t.tests_selected}/${t.tests_available} test files` : t.selection;
|
|
215
|
+
lines.push(` ${t.full.status === "passed" ? "ok " : "FAIL"} tests ${seconds(t.full.wall_ms ?? 0)} - DiffCI: ${scope}${t.missed ? " (MISSED)" : ""}`);
|
|
216
|
+
}
|
|
217
|
+
const s = report.summary;
|
|
218
|
+
lines.push(` full gate ${seconds(s.full_ms)}, DiffCI ${seconds(s.diffci_ms)} (${s.basis}), would save ${seconds(s.saved_ms)}`);
|
|
219
|
+
lines.push(s.misses.length ? ` MISSES: ${s.misses.join(", ")}` : " misses: none");
|
|
220
|
+
return lines.join("\n");
|
|
221
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pre-push hook `diffci init --hook` installs (2026-10-28).
|
|
3
|
+
*
|
|
4
|
+
* WHY. In 449 agent sessions on DentalPresence, agents ran DiffCI in 24 of over 1,500 test commands:
|
|
5
|
+
* they picked test files themselves (928) or ran the whole suite (282), and 104 pushes then failed the
|
|
6
|
+
* repository gates in CI. Instructions alone did not make DiffCI the default. A hook does: every push,
|
|
7
|
+
* by an agent or a person, verifies the commits being pushed before they leave the machine.
|
|
8
|
+
*
|
|
9
|
+
* BEHAVIOUR. For each pushed ref that is the checked-out HEAD, the hook runs
|
|
10
|
+
* `diffci verify --base <remote sha> --head <local sha>` (a new branch uses its merge base with the
|
|
11
|
+
* remote's default branch). Failing verification (exit 1) blocks the push. When DiffCI cannot verify
|
|
12
|
+
* (exit 2: dirty checkout, unknown base, analysis refused) it says why and lets the push through,
|
|
13
|
+
* because required CI still runs; `DIFFCI_HOOK_STRICT=1` blocks instead. `git push --no-verify` skips it.
|
|
14
|
+
*
|
|
15
|
+
* REPORTING (2026-10-29). When DIFFCI_API_URL and DIFFCI_TOKEN are set, the hook also sends an
|
|
16
|
+
* observation report for the pushed range, the same report the GitHub Action sends. A dogfood repository
|
|
17
|
+
* whose GitHub Actions minutes ran out sent nothing for two weeks while its builds moved to another CI;
|
|
18
|
+
* reporting from the push keeps that data. It runs after verification and can never block the push.
|
|
19
|
+
*/
|
|
20
|
+
export declare const HOOK_MARKER = "# diffci:managed pre-push hook";
|
|
21
|
+
export declare function prePushHook(command: string): string;
|
|
22
|
+
/** The command the hook runs: the installed binary when the repository has DiffCI as a dependency,
|
|
23
|
+
* otherwise a pinned npx invocation, so the hook never floats to a newer release. */
|
|
24
|
+
export declare function hookCommand(manager: "npm" | "pnpm" | "yarn" | "bun" | undefined, version: string): string;
|
|
25
|
+
/** Writes the hook into the directory Git actually runs hooks from (`core.hooksPath` included, such as
|
|
26
|
+
* `.husky`). An existing hook DiffCI did not write is kept unless `force` is set. */
|
|
27
|
+
export declare function installPrePushHook(repoPath: string, command: string, force: boolean): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { spawnSync, type SpawnSyncReturns } from "node:child_process";
|
|
2
|
+
export type SupportedPackageManager = "npm" | "pnpm" | "yarn" | "bun";
|
|
3
|
+
export interface InstallPlan {
|
|
4
|
+
manager: SupportedPackageManager;
|
|
5
|
+
command: string;
|
|
6
|
+
args: string[];
|
|
7
|
+
packageSpec: string;
|
|
8
|
+
}
|
|
9
|
+
export interface PackageScriptResult {
|
|
10
|
+
added: string[];
|
|
11
|
+
kept: string[];
|
|
12
|
+
}
|
|
13
|
+
export declare function detectPackageManager(repoPath: string): SupportedPackageManager;
|
|
14
|
+
export declare function installPlan(repoPath: string, version: string, platform?: NodeJS.Platform): InstallPlan;
|
|
15
|
+
export declare function installDiffci(repoPath: string, version: string, run?: typeof spawnSync, packageSpecOverride?: string): {
|
|
16
|
+
plan: InstallPlan;
|
|
17
|
+
result: SpawnSyncReturns<string>;
|
|
18
|
+
};
|
|
19
|
+
export declare function addDiffciPackageScripts(repoPath: string): PackageScriptResult;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ObservationAgent } from "./agent-identity.js";
|
|
2
|
+
import { type GitRunner, type ObservationEnvironment } from "./context.js";
|
|
3
|
+
import { type ObservationReport } from "./report.js";
|
|
4
|
+
import type { DependencyGraphResult } from "@diffci.com/core/repo/types";
|
|
5
|
+
export interface ObserveOptions {
|
|
6
|
+
/** The checked-out repository to observe. Never written to. */
|
|
7
|
+
repoPath: string;
|
|
8
|
+
env: ObservationEnvironment;
|
|
9
|
+
/** DiffCI's own version string, recorded in the report. */
|
|
10
|
+
version: string;
|
|
11
|
+
/** The DiffCI commit that produced this report, when it can be determined. */
|
|
12
|
+
engineSha?: string;
|
|
13
|
+
baseOverride?: string;
|
|
14
|
+
headOverride?: string;
|
|
15
|
+
/** Replace every path in the report with a stable 12-character digest. */
|
|
16
|
+
redactPaths?: boolean;
|
|
17
|
+
/** Where the report will be written, so the report can state whether that is inside the checkout. */
|
|
18
|
+
reportPath?: string;
|
|
19
|
+
/** Injected for tests. Defaults to a real git in `repoPath`. */
|
|
20
|
+
git?: GitRunner;
|
|
21
|
+
/** Optional external CI timing history. It can only bypass to full validation. */
|
|
22
|
+
economicsHistory?: unknown;
|
|
23
|
+
economicsJobKey?: string;
|
|
24
|
+
forceAnalysis?: boolean;
|
|
25
|
+
/** Optional cache outside the checkout; graph resolution and selection remain fresh. */
|
|
26
|
+
vueAnalysisCacheDir?: string;
|
|
27
|
+
/** Let the engine cover a file the test script runs directly by naming that phase
|
|
28
|
+
* (`result.testPhases`) instead of falling back. Only for callers that execute the phases with the
|
|
29
|
+
* selection - verification does; check, observe and enforce do not, so they keep the fallback. */
|
|
30
|
+
selectTestPhases?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Opt-in dependency-graph cache (local and/or DiffCI Cloud, src/client/shared-graph-cache.ts). Used only
|
|
33
|
+
* for a clean checkout; any failure is a miss, and a hit yields the same graph a build would.
|
|
34
|
+
*/
|
|
35
|
+
graphCache?: {
|
|
36
|
+
load(key: string): Promise<{
|
|
37
|
+
result: DependencyGraphResult;
|
|
38
|
+
} | undefined>;
|
|
39
|
+
save(key: string, result: DependencyGraphResult): void;
|
|
40
|
+
};
|
|
41
|
+
/** Opt-in agent identity (src/client/agent-identity.ts); absent means the report carries none. */
|
|
42
|
+
agent?: ObservationAgent;
|
|
43
|
+
}
|
|
44
|
+
export declare function makeGitRunner(repoPath: string): GitRunner;
|
|
45
|
+
/**
|
|
46
|
+
* True when the report would land inside the observed checkout. That is not a style preference: an
|
|
47
|
+
* untracked file in the working tree changes `git status`, and CI jobs that assert a clean tree (a
|
|
48
|
+
* generated-code check, a lockfile check, `git diff --exit-code`) would start failing because DiffCI
|
|
49
|
+
* was installed. The report is written outside the repository by default for exactly that reason.
|
|
50
|
+
*/
|
|
51
|
+
export declare function isInsideRepository(repoPath: string, candidate: string): boolean;
|
|
52
|
+
export declare function observe(options: ObserveOptions): Promise<ObservationReport>;
|
|
@@ -26,6 +26,7 @@ import { economicsContext, evaluateEconomics } from "./economics.js";
|
|
|
26
26
|
import { readCiEnvironment, resolveCommitRange, } from "./context.js";
|
|
27
27
|
import { OBSERVATION_SCHEMA, readTypeScriptProgramMetrics, redactPath, } from "./report.js";
|
|
28
28
|
import { auditWorkflows } from "./workflow-guard.js";
|
|
29
|
+
import { deriveSharedGraphCacheKey } from "./shared-graph-cache.js";
|
|
29
30
|
/** Build output and dependencies are never sources of truth about a repository's own structure. */
|
|
30
31
|
const EXCLUDE_DIRS = ["node_modules", ".next", "dist", "build"];
|
|
31
32
|
export function makeGitRunner(repoPath) {
|
|
@@ -128,6 +129,7 @@ export async function observe(options) {
|
|
|
128
129
|
reason: extra.reason,
|
|
129
130
|
result: extra.result,
|
|
130
131
|
economics,
|
|
132
|
+
...(options.agent ? { agent: options.agent } : {}),
|
|
131
133
|
payload: {
|
|
132
134
|
includesFilePaths: options.redactPaths !== true,
|
|
133
135
|
includesFileContents: false,
|
|
@@ -163,7 +165,7 @@ export async function observe(options) {
|
|
|
163
165
|
if (economics.decision === "BYPASS_FULL")
|
|
164
166
|
return finish("REFUSED", "eligibility", { reason: `ECONOMICS_FULL_BYPASS: ${economics.reason}. No selective command is authorized.` });
|
|
165
167
|
}
|
|
166
|
-
const [{ analyzeGitDelta }, { classifyRepositoryProject, buildDependencyGraph }, { ImpactAnalyzer }, { runPathBaseline }, { commandSpecToString, planSelectiveTestCommands }, { repositoryFileReader }] = await Promise.all([
|
|
168
|
+
const [{ analyzeGitDelta }, { classifyRepositoryProject, buildDependencyGraph, hydrateDependencyGraph }, { ImpactAnalyzer }, { runPathBaseline }, { commandSpecToString, planSelectiveTestCommands }, { repositoryFileReader }] = await Promise.all([
|
|
167
169
|
import("@diffci.com/core/git/git-diff"), import("@diffci.com/core/repo/graph"), import("@diffci.com/core/repo/impact"), import("@diffci.com/core/planner/path-baseline"), import("@diffci.com/core/planner/test-command"), import("@diffci.com/core/repo/file-references"),
|
|
168
170
|
]);
|
|
169
171
|
markPhase("engineLoad");
|
|
@@ -182,8 +184,28 @@ export async function observe(options) {
|
|
|
182
184
|
}
|
|
183
185
|
const delta = deltaResult.delta;
|
|
184
186
|
markPhase("delta");
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
+
const graphCacheKey = options.graphCache
|
|
188
|
+
? deriveSharedGraphCacheKey({ repoPath, git, version: options.version, engineSha: options.engineSha, excludeDirs: EXCLUDE_DIRS })
|
|
189
|
+
: undefined;
|
|
190
|
+
let cachedGraph;
|
|
191
|
+
if (options.graphCache && graphCacheKey) {
|
|
192
|
+
try {
|
|
193
|
+
const hit = await options.graphCache.load(graphCacheKey);
|
|
194
|
+
if (hit)
|
|
195
|
+
cachedGraph = { ...hit.result, graph: hydrateDependencyGraph(hit.result.graph, repoPath, hit.result.profile?.testPatterns) };
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
cachedGraph = undefined;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const graphResult = cachedGraph ?? await buildDependencyGraph({ repoPath, excludeDirs: EXCLUDE_DIRS, vueAnalysisCache: options.vueAnalysisCacheDir ? { directory: options.vueAnalysisCacheDir, version: `${options.version}:${options.engineSha ?? ""}` } : undefined });
|
|
202
|
+
if (!cachedGraph && options.graphCache && graphCacheKey) {
|
|
203
|
+
try {
|
|
204
|
+
options.graphCache.save(graphCacheKey, graphResult);
|
|
205
|
+
}
|
|
206
|
+
catch { /* caching never fails an observation */ }
|
|
207
|
+
}
|
|
208
|
+
markPhase(cachedGraph ? "graphCached" : "graph");
|
|
187
209
|
if (graphResult.graph.nodes.length === 0) {
|
|
188
210
|
return finish("REFUSED", "graph", {
|
|
189
211
|
reason: "the dependency graph came back empty - DiffCI will not propose a selection from a graph that sees none of this repository" +
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface DiffciPackageIdentity {
|
|
2
|
+
version: string;
|
|
3
|
+
root?: string;
|
|
4
|
+
sha?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Find package metadata without assuming whether this code is running from
|
|
8
|
+
* source, a build directory, or an installed npm tarball.
|
|
9
|
+
*/
|
|
10
|
+
export declare function findDiffciPackageIdentity(): DiffciPackageIdentity | undefined;
|
|
11
|
+
/** Preserve the CLI's historical unknown-version fallback for diagnostics. */
|
|
12
|
+
export declare function observerIdentity(): DiffciPackageIdentity;
|
|
13
|
+
/** MCP negotiation must never advertise an invented release version. */
|
|
14
|
+
export declare function requireDiffciPackageIdentity(): DiffciPackageIdentity;
|