@diffci.com/diffci 0.2.4 → 0.2.6

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/action.yml CHANGED
@@ -1,155 +1,155 @@
1
- # DiffCI observer - the GitHub Action a third-party repository installs (Phase 02, 2026-08-26).
2
- #
3
- # WHAT THIS DOES: analyses one commit range against the checkout the job already has, writes a JSON
4
- # report to the runner's temp directory, prints a summary to the job log and the job summary, optionally
5
- # uploads the report as an artifact, and - only when `api-url` and `api-token` are both set - sends that
6
- # same report to DiffCI. With no token configured nothing leaves the runner at all.
7
- #
8
- # WHAT THIS DOES NOT DO, and cannot: run tests, skip tests, cancel a job, re-order steps, write to the
9
- # repository, comment on a pull request, or set a check status. There is no input below that turns any
10
- # of that on, because the claim being tested for seven days is that installing DiffCI leaves CI
11
- # byte-identical, and a flag that could change what CI runs would eventually be set by accident.
12
- #
13
- # INSTALL IT AS ITS OWN JOB. A job of its own is what makes the claim structural rather than careful:
14
- #
15
- # jobs:
16
- # diffci:
17
- # runs-on: ubuntu-latest
18
- # continue-on-error: true # a DiffCI failure must not become the workflow's conclusion
19
- # permissions:
20
- # contents: read
21
- # steps:
22
- # - uses: actions/checkout@v4
23
- # with:
24
- # fetch-depth: 0 # the base commit must exist locally, or DiffCI refuses
1
+ # DiffCI observer - the GitHub Action a third-party repository installs (Phase 02, 2026-08-26).
2
+ #
3
+ # WHAT THIS DOES: analyses one commit range against the checkout the job already has, writes a JSON
4
+ # report to the runner's temp directory, prints a summary to the job log and the job summary, optionally
5
+ # uploads the report as an artifact, and - only when `api-url` and `api-token` are both set - sends that
6
+ # same report to DiffCI. With no token configured nothing leaves the runner at all.
7
+ #
8
+ # WHAT THIS DOES NOT DO, and cannot: run tests, skip tests, cancel a job, re-order steps, write to the
9
+ # repository, comment on a pull request, or set a check status. There is no input below that turns any
10
+ # of that on, because the claim being tested for seven days is that installing DiffCI leaves CI
11
+ # byte-identical, and a flag that could change what CI runs would eventually be set by accident.
12
+ #
13
+ # INSTALL IT AS ITS OWN JOB. A job of its own is what makes the claim structural rather than careful:
14
+ #
15
+ # jobs:
16
+ # diffci:
17
+ # runs-on: ubuntu-latest
18
+ # continue-on-error: true # a DiffCI failure must not become the workflow's conclusion
19
+ # permissions:
20
+ # contents: read
21
+ # steps:
22
+ # - uses: actions/checkout@v4
23
+ # with:
24
+ # fetch-depth: 0 # the base commit must exist locally, or DiffCI refuses
25
25
  # - uses: DiffCI/DiffCI.com@<40-character commit sha>
26
- #
27
- # `diffci verify-workflow` checks those properties against your own workflow files and exits non-zero
28
- # if any of them does not hold. Run it before the observation window starts.
29
- name: DiffCI observer
30
- description: Observation-only change-aware CI analysis. Runs nothing, changes nothing, skips nothing.
31
- branding:
32
- icon: eye
33
- color: gray-dark
34
-
35
- inputs:
36
- repository-path:
37
- description: The checkout to observe. Defaults to the workspace.
38
- required: false
39
- default: ${{ github.workspace }}
40
- base-sha:
41
- description: Base commit. Leave empty to derive it from the event (pull request base, push before).
42
- required: false
43
- default: ""
44
- head-sha:
45
- description: Head commit. Must be given together with base-sha.
46
- required: false
47
- default: ""
48
- node-version:
49
- description: Node version used to run the observer. Only affects this job.
50
- required: false
51
- default: "22"
52
- report-path:
53
- description: Where to write the report. Must be outside the checkout. Defaults to RUNNER_TEMP.
54
- required: false
55
- default: ""
56
- redact-paths:
57
- description: Replace every file path in the report with a stable 12-character digest.
58
- required: false
59
- default: "false"
60
- upload-artifact:
61
- description: Upload the report as a workflow artifact.
62
- required: false
63
- default: "true"
64
- artifact-name:
65
- description: Name of the uploaded artifact.
66
- required: false
67
- default: diffci-observation
68
- artifact-retention-days:
69
- description: Retention for the uploaded artifact.
70
- required: false
71
- default: "7"
72
- api-url:
73
- description: >-
74
- DiffCI ingest endpoint. Leave empty and nothing is sent anywhere - the report stays on the runner
75
- as an artifact. Must be https.
76
- required: false
77
- default: ""
78
- api-token:
79
- description: >-
80
- Ingest token for THIS repository, from a repository secret. Pass it as the workflow expression
81
- for secrets.DIFFCI_TOKEN, never as a literal. Nothing is sent without it. (This text deliberately
82
- does not spell out the expression: GitHub evaluates expressions inside action metadata, and
83
- "secrets" is not a context an action file may use - a literal expression here made the whole
84
- action fail to load on every run until 2026-09-06.)
85
- required: false
86
- default: ""
87
- fail-on-error:
88
- description: >-
89
- Fail this step when the observation refuses or errors. Default false, so a DiffCI problem never
90
- shows up as a red step in your CI. Turn it on only while debugging an installation.
91
- required: false
92
- default: "false"
93
-
94
- outputs:
95
- report-path:
96
- description: Absolute path of the written report.
97
- value: ${{ steps.observe.outputs.report-path }}
98
- status:
99
- description: OBSERVED, REFUSED, or ERROR.
100
- value: ${{ steps.observe.outputs.status }}
101
-
102
- runs:
103
- using: composite
104
- steps:
105
- - name: Set up Node for the observer
106
- uses: actions/setup-node@v4
107
- with:
108
- node-version: ${{ inputs.node-version }}
109
-
110
- # Installed into the action's own directory, never into the observed repository: nothing here
111
- # touches the workspace, its node_modules, or its lockfile. --omit=dev keeps this to TypeScript and
112
- # a YAML parser; --ignore-scripts means no dependency of DiffCI's runs code on your runner.
113
- - name: Install observer dependencies
114
- shell: bash
115
- working-directory: ${{ github.action_path }}
116
- run: npm ci --omit=dev --no-audit --fund=false --ignore-scripts
117
-
118
- - name: Build observer
119
- shell: bash
120
- working-directory: ${{ github.action_path }}
121
- run: node_modules/.bin/tsc -p tsconfig.client.json
122
-
123
- - name: Observe
124
- id: observe
125
- shell: bash
126
- env:
127
- DIFFCI_REPO: ${{ inputs.repository-path }}
128
- DIFFCI_BASE: ${{ inputs.base-sha }}
129
- DIFFCI_HEAD: ${{ inputs.head-sha }}
130
- DIFFCI_OUT: ${{ inputs.report-path }}
131
- DIFFCI_REDACT: ${{ inputs.redact-paths }}
132
- DIFFCI_FAIL_ON_ERROR: ${{ inputs.fail-on-error }}
133
- # Passed through the environment, never on the command line: an argv is visible to every other
134
- # process on the runner and shows up in traces. The observer reads these directly and never
135
- # prints them.
136
- DIFFCI_API_URL: ${{ inputs.api-url }}
137
- DIFFCI_TOKEN: ${{ inputs.api-token }}
138
- run: |
139
- set -euo pipefail
140
- args=(observe --repo "$DIFFCI_REPO")
141
- if [ -n "$DIFFCI_BASE" ]; then args+=(--base "$DIFFCI_BASE"); fi
142
- if [ -n "$DIFFCI_HEAD" ]; then args+=(--head "$DIFFCI_HEAD"); fi
143
- if [ -n "$DIFFCI_OUT" ]; then args+=(--out "$DIFFCI_OUT"); fi
144
- if [ "$DIFFCI_REDACT" = "true" ]; then args+=(--redact-paths); fi
145
- if [ "$DIFFCI_FAIL_ON_ERROR" = "true" ]; then args+=(--fail-on-error); fi
146
- node "$GITHUB_ACTION_PATH/dist-client/src/client/cli.js" "${args[@]}"
147
-
148
- - name: Upload observation report
149
- if: ${{ inputs.upload-artifact == 'true' && steps.observe.outputs.report-path != '' }}
150
- uses: actions/upload-artifact@v4
151
- with:
152
- name: ${{ inputs.artifact-name }}
153
- path: ${{ steps.observe.outputs.report-path }}
154
- retention-days: ${{ inputs.artifact-retention-days }}
155
- if-no-files-found: warn
26
+ #
27
+ # `diffci verify-workflow` checks those properties against your own workflow files and exits non-zero
28
+ # if any of them does not hold. Run it before the observation window starts.
29
+ name: DiffCI observer
30
+ description: Observation-only change-aware CI analysis. Runs nothing, changes nothing, skips nothing.
31
+ branding:
32
+ icon: eye
33
+ color: gray-dark
34
+
35
+ inputs:
36
+ repository-path:
37
+ description: The checkout to observe. Defaults to the workspace.
38
+ required: false
39
+ default: ${{ github.workspace }}
40
+ base-sha:
41
+ description: Base commit. Leave empty to derive it from the event (pull request base, push before).
42
+ required: false
43
+ default: ""
44
+ head-sha:
45
+ description: Head commit. Must be given together with base-sha.
46
+ required: false
47
+ default: ""
48
+ node-version:
49
+ description: Node version used to run the observer. Only affects this job.
50
+ required: false
51
+ default: "22"
52
+ report-path:
53
+ description: Where to write the report. Must be outside the checkout. Defaults to RUNNER_TEMP.
54
+ required: false
55
+ default: ""
56
+ redact-paths:
57
+ description: Replace every file path in the report with a stable 12-character digest.
58
+ required: false
59
+ default: "false"
60
+ upload-artifact:
61
+ description: Upload the report as a workflow artifact.
62
+ required: false
63
+ default: "true"
64
+ artifact-name:
65
+ description: Name of the uploaded artifact.
66
+ required: false
67
+ default: diffci-observation
68
+ artifact-retention-days:
69
+ description: Retention for the uploaded artifact.
70
+ required: false
71
+ default: "7"
72
+ api-url:
73
+ description: >-
74
+ DiffCI ingest endpoint. Leave empty and nothing is sent anywhere - the report stays on the runner
75
+ as an artifact. Must be https.
76
+ required: false
77
+ default: ""
78
+ api-token:
79
+ description: >-
80
+ Ingest token for THIS repository, from a repository secret. Pass it as the workflow expression
81
+ for secrets.DIFFCI_TOKEN, never as a literal. Nothing is sent without it. (This text deliberately
82
+ does not spell out the expression: GitHub evaluates expressions inside action metadata, and
83
+ "secrets" is not a context an action file may use - a literal expression here made the whole
84
+ action fail to load on every run until 2026-09-06.)
85
+ required: false
86
+ default: ""
87
+ fail-on-error:
88
+ description: >-
89
+ Fail this step when the observation refuses or errors. Default false, so a DiffCI problem never
90
+ shows up as a red step in your CI. Turn it on only while debugging an installation.
91
+ required: false
92
+ default: "false"
93
+
94
+ outputs:
95
+ report-path:
96
+ description: Absolute path of the written report.
97
+ value: ${{ steps.observe.outputs.report-path }}
98
+ status:
99
+ description: OBSERVED, REFUSED, or ERROR.
100
+ value: ${{ steps.observe.outputs.status }}
101
+
102
+ runs:
103
+ using: composite
104
+ steps:
105
+ - name: Set up Node for the observer
106
+ uses: actions/setup-node@v4
107
+ with:
108
+ node-version: ${{ inputs.node-version }}
109
+
110
+ # Installed into the action's own directory, never into the observed repository: nothing here
111
+ # touches the workspace, its node_modules, or its lockfile. --omit=dev keeps this to TypeScript and
112
+ # a YAML parser; --ignore-scripts means no dependency of DiffCI's runs code on your runner.
113
+ - name: Install observer dependencies
114
+ shell: bash
115
+ working-directory: ${{ github.action_path }}
116
+ run: npm ci --omit=dev --no-audit --fund=false --ignore-scripts
117
+
118
+ - name: Build observer
119
+ shell: bash
120
+ working-directory: ${{ github.action_path }}
121
+ run: node_modules/.bin/tsc -p tsconfig.client.json
122
+
123
+ - name: Observe
124
+ id: observe
125
+ shell: bash
126
+ env:
127
+ DIFFCI_REPO: ${{ inputs.repository-path }}
128
+ DIFFCI_BASE: ${{ inputs.base-sha }}
129
+ DIFFCI_HEAD: ${{ inputs.head-sha }}
130
+ DIFFCI_OUT: ${{ inputs.report-path }}
131
+ DIFFCI_REDACT: ${{ inputs.redact-paths }}
132
+ DIFFCI_FAIL_ON_ERROR: ${{ inputs.fail-on-error }}
133
+ # Passed through the environment, never on the command line: an argv is visible to every other
134
+ # process on the runner and shows up in traces. The observer reads these directly and never
135
+ # prints them.
136
+ DIFFCI_API_URL: ${{ inputs.api-url }}
137
+ DIFFCI_TOKEN: ${{ inputs.api-token }}
138
+ run: |
139
+ set -euo pipefail
140
+ args=(observe --repo "$DIFFCI_REPO")
141
+ if [ -n "$DIFFCI_BASE" ]; then args+=(--base "$DIFFCI_BASE"); fi
142
+ if [ -n "$DIFFCI_HEAD" ]; then args+=(--head "$DIFFCI_HEAD"); fi
143
+ if [ -n "$DIFFCI_OUT" ]; then args+=(--out "$DIFFCI_OUT"); fi
144
+ if [ "$DIFFCI_REDACT" = "true" ]; then args+=(--redact-paths); fi
145
+ if [ "$DIFFCI_FAIL_ON_ERROR" = "true" ]; then args+=(--fail-on-error); fi
146
+ node "$GITHUB_ACTION_PATH/dist-client/src/client/cli.js" "${args[@]}"
147
+
148
+ - name: Upload observation report
149
+ if: ${{ inputs.upload-artifact == 'true' && steps.observe.outputs.report-path != '' }}
150
+ uses: actions/upload-artifact@v4
151
+ with:
152
+ name: ${{ inputs.artifact-name }}
153
+ path: ${{ steps.observe.outputs.report-path }}
154
+ retention-days: ${{ inputs.artifact-retention-days }}
155
+ if-no-files-found: warn
@@ -30,6 +30,7 @@ import { tmpdir } from "node:os";
30
30
  import { basename, dirname, join, resolve } from "node:path";
31
31
  import { observe, isInsideRepository } from "./observe.js";
32
32
  import { inferFullCommand, inferSelectedCommand } from "./full-command.js";
33
+ import { addDiffciPackageScripts, installDiffci } from "./install.js";
33
34
  import { submitObservation } from "./submit.js";
34
35
  import { formatVerifySavingsSummary, measureCommand, runVerifySavings, writeVerifySavingsReport } from "./verify-savings.js";
35
36
  import { auditWorkflows, isNonInterfering } from "./workflow-guard.js";
@@ -99,16 +100,16 @@ function defaultReportPath(env) {
99
100
  const stamp = env.GITHUB_RUN_ID ? `${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT ?? "1"}` : String(Date.now());
100
101
  return join(base, `diffci-observation-${stamp}.json`);
101
102
  }
102
- const AGENT_INSTRUCTIONS = `# DiffCI Agent Instructions
103
-
104
- Use DiffCI as the default CI/CD validation lens before proposing, committing, or opening pull requests.
105
-
106
- Default command:
107
-
108
- \`\`\`bash
109
- npx @diffci.com/diffci@latest check
110
- \`\`\`
111
-
103
+ const AGENT_INSTRUCTIONS = `# DiffCI Agent Instructions
104
+
105
+ Use DiffCI as the default CI/CD validation lens before proposing, committing, or opening pull requests.
106
+
107
+ Default command:
108
+
109
+ \`\`\`bash
110
+ npx @diffci.com/diffci@latest check
111
+ \`\`\`
112
+
112
113
  What this does:
113
114
 
114
115
  - analyzes the current checkout and commit range;
@@ -119,55 +120,55 @@ What this does:
119
120
 
120
121
  Test commands may write generated files to the checkout. Use \`observe --no-send\` when analysis
121
122
  without test execution is needed.
122
-
123
- If DiffCI reports \`REFUSED\` or \`ERROR\`, treat that as an installation or analysis issue and continue with the repository's normal test commands. If DiffCI reports a selected command, prefer using it as evidence for what changed, not as permission to skip required CI.
123
+
124
+ If DiffCI reports \`REFUSED\` or \`ERROR\`, treat that as an installation or analysis issue and continue with the repository's normal test commands. If DiffCI reports a selected command, prefer using it as evidence for what changed, not as permission to skip required CI.
124
125
  `;
125
- const CURSOR_RULE = `---
126
- description: Use DiffCI as the default CI/CD validation lens
127
- alwaysApply: true
128
- ---
129
-
130
- Before proposing, committing, or opening pull requests, run:
131
-
132
- \`\`\`bash
133
- npx @diffci.com/diffci@latest check
134
- \`\`\`
135
-
126
+ const CURSOR_RULE = `---
127
+ description: Use DiffCI as the default CI/CD validation lens
128
+ alwaysApply: true
129
+ ---
130
+
131
+ Before proposing, committing, or opening pull requests, run:
132
+
133
+ \`\`\`bash
134
+ npx @diffci.com/diffci@latest check
135
+ \`\`\`
136
+
136
137
  DiffCI check analyzes the change and runs full and selected test commands when it can infer them. It sends nothing. Test commands may write generated files. Use observe --no-send for analysis without execution. Required project CI remains authoritative.
137
138
  `;
138
- const GITHUB_COPILOT_INSTRUCTIONS = `# Repository CI/CD Validation
139
-
140
- Use DiffCI as the default change-aware CI/CD validation lens:
141
-
142
- \`\`\`bash
143
- npx @diffci.com/diffci@latest check
144
- \`\`\`
145
-
139
+ const GITHUB_COPILOT_INSTRUCTIONS = `# Repository CI/CD Validation
140
+
141
+ Use DiffCI as the default change-aware CI/CD validation lens:
142
+
143
+ \`\`\`bash
144
+ npx @diffci.com/diffci@latest check
145
+ \`\`\`
146
+
146
147
  DiffCI check analyzes the change and runs inferred full and selected test commands to measure a paired runtime. It sends nothing; required repository checks remain authoritative.
147
148
  `;
148
- const DIFFCI_CONFIG = `{
149
- "$schema": "https://diffci.com/schemas/diffci.config.schema.json",
150
- "agentDefaultCommand": "npx @diffci.com/diffci@latest check",
149
+ const DIFFCI_CONFIG = `{
150
+ "$schema": "https://diffci.com/schemas/diffci.config.schema.json",
151
+ "agentDefaultCommand": "npx @diffci.com/diffci@latest check",
151
152
  "mode": "check",
152
- "sendReports": false
153
- }
153
+ "sendReports": false
154
+ }
154
155
  `;
155
156
  function diffciWorkflow(version) {
156
- return `name: DiffCI observation
157
- on: [push, pull_request]
158
- permissions:
159
- contents: read
160
- jobs:
161
- diffci:
162
- runs-on: ubuntu-latest
163
- continue-on-error: true
164
- steps:
165
- - uses: actions/checkout@v4
166
- with:
167
- fetch-depth: 0
168
- - uses: actions/setup-node@v4
169
- with:
170
- node-version: 22
157
+ return `name: DiffCI observation
158
+ on: [push, pull_request]
159
+ permissions:
160
+ contents: read
161
+ jobs:
162
+ diffci:
163
+ runs-on: ubuntu-latest
164
+ continue-on-error: true
165
+ steps:
166
+ - uses: actions/checkout@v4
167
+ with:
168
+ fetch-depth: 0
169
+ - uses: actions/setup-node@v4
170
+ with:
171
+ node-version: 22
171
172
  - run: npx @diffci.com/diffci@${version} observe --no-send
172
173
  `;
173
174
  }
@@ -194,9 +195,29 @@ function runInit(flags, env) {
194
195
  ];
195
196
  if (includeWorkflow)
196
197
  writes.push(writeInitFile(repoPath, ".github/workflows/diffci.yml", diffciWorkflow(identity.version), force));
198
+ let installed;
199
+ if (flags.install === true) {
200
+ const { plan, result } = installDiffci(repoPath, identity.version);
201
+ if (result.error)
202
+ throw result.error;
203
+ if (result.status !== 0) {
204
+ const detail = result.stderr.trim() || result.stdout.trim() || `${plan.manager} exited with status ${result.status ?? "unknown"}`;
205
+ throw new Error(`could not install ${plan.packageSpec}: ${detail}`);
206
+ }
207
+ const scripts = addDiffciPackageScripts(repoPath);
208
+ installed = `installed ${plan.packageSpec} as an exact dev dependency with ${plan.manager}`;
209
+ if (scripts.added.length > 0)
210
+ installed += `; added package scripts ${scripts.added.join(", ")}`;
211
+ if (scripts.kept.length > 0)
212
+ installed += `; kept existing package scripts ${scripts.kept.join(", ")}`;
213
+ }
197
214
  console.log(`DiffCI initialized for AI coding agents in ${repoPath}`);
198
215
  for (const write of writes)
199
216
  console.log(` ${write}`);
217
+ if (installed)
218
+ console.log(` ${installed}`);
219
+ else
220
+ console.log(" skipped package installation (pass --install to add an exact dev dependency)");
200
221
  if (!includeWorkflow)
201
222
  console.log(" skipped .github/workflows/diffci.yml (pass --workflow to add it)");
202
223
  console.log("\nDefault agent command: npx @diffci.com/diffci@latest check");
@@ -357,7 +378,7 @@ async function runCheck(flags, env) {
357
378
  print(` markdown: ${markdownPath}`);
358
379
  if (flags.json === true)
359
380
  console.log(JSON.stringify({ observation, savings }, null, 2));
360
- return savings.comparison.fullCommandSucceeded && savings.comparison.selectedCommandSucceeded ? 0 : 1;
381
+ return savings.comparison.evidenceValid ? 0 : 1;
361
382
  }
362
383
  async function runObserve(flags, env) {
363
384
  const repoPath = resolve(typeof flags.repo === "string" ? flags.repo : env.GITHUB_WORKSPACE ?? process.cwd());
@@ -520,7 +541,7 @@ function runVerifySavingsCommand(flags, env) {
520
541
  console.log(` report: ${options.out}`);
521
542
  if (options.markdown)
522
543
  console.log(` markdown: ${options.markdown}`);
523
- return report.comparison.fullCommandSucceeded && report.comparison.selectedCommandSucceeded ? 0 : 1;
544
+ return report.comparison.evidenceValid ? 0 : 1;
524
545
  }
525
546
  function defaultPilotOutputDir(repoPath) {
526
547
  return join(dirname(repoPath), "diffci-output");
@@ -583,36 +604,39 @@ async function runPilot(flags, env) {
583
604
  console.log(formatVerifySavingsSummary(savings));
584
605
  console.log(` savings report: ${savingsPath}`);
585
606
  console.log(` markdown: ${markdownPath}`);
586
- return savings.comparison.fullCommandSucceeded && savings.comparison.selectedCommandSucceeded ? 0 : 1;
607
+ return savings.comparison.evidenceValid ? 0 : 1;
587
608
  }
588
609
  const USAGE = `diffci - change-aware CI analysis and paired timing
589
-
610
+
590
611
  Usage:
591
- diffci init [--repo <path>] [--workflow] [--force]
612
+ diffci init [--repo <path>] [--workflow] [--install] [--force]
592
613
  diffci mcp
593
614
  diffci check [--repo <path>] [--out <file>] [--base <sha> --head <sha>]
594
615
  [--redact-paths] [--json] [--quiet] [--fail-on-error] [--timeout-ms <ms>]
595
- diffci pilot --full <command> [--repo <path>] [--out-dir <dir>] [--label <name>]
596
- diffci observe [--repo <path>] [--out <file>] [--base <sha> --head <sha>]
597
- [--redact-paths] [--json] [--quiet] [--fail-on-error]
598
- [--api-url <url> --api-token <token>] [--no-send]
599
- diffci verify-savings --repo <path> --full <command>
600
- (--selected <command> | --selected-from-report <file>)
601
- --out <file> [--markdown <file>] [--label <name>]
602
- diffci verify-workflow [--repo <path>]
603
- diffci version
604
-
616
+ diffci pilot --full <command> [--repo <path>] [--out-dir <dir>] [--label <name>]
617
+ diffci observe [--repo <path>] [--out <file>] [--base <sha> --head <sha>]
618
+ [--redact-paths] [--json] [--quiet] [--fail-on-error]
619
+ [--api-url <url> --api-token <token>] [--no-send]
620
+ diffci verify-savings --repo <path> --full <command>
621
+ (--selected <command> | --selected-from-report <file>)
622
+ --out <file> [--markdown <file>] [--label <name>]
623
+ diffci verify-workflow [--repo <path>]
624
+ diffci version
625
+
605
626
  init writes AGENTS.md, CLAUDE.md, Cursor rules, Copilot instructions, and diffci.config.json.
627
+ --workflow adds a separate non-blocking observation workflow. --install detects npm, pnpm, Yarn,
628
+ or Bun, installs this DiffCI version as an exact dev dependency, updates the manager's lockfile,
629
+ and adds diffci:check and diffci:observe package scripts without replacing existing scripts.
606
630
  mcp runs the stdio MCP server for native agent integrations.
607
631
  check analyzes the change, runs inferred full and selected commands, and shows measured savings.
608
- pilot runs observe and verify-savings together, writing reports to ../diffci-output by default.
632
+ pilot runs observe and verify-savings together, writing reports to ../diffci-output by default.
609
633
  observe analyses the checkout and writes one JSON report. It runs nothing and changes nothing.
610
- verify-savings runs both commands and reports measured paired runtime; it is an opt-in pilot command.
611
- verify-workflow checks that the job running DiffCI cannot affect any other job, and exits 1 if it can.
612
-
613
- The report is sent only when both --api-url and --api-token are given (or DIFFCI_API_URL and
614
- DIFFCI_TOKEN are set). A failed send is reported and never fails the step - the report is on disk
615
- either way. Plain http is refused; the token is never printed.
634
+ verify-savings runs both commands and reports measured paired runtime; it is an opt-in pilot command.
635
+ verify-workflow checks that the job running DiffCI cannot affect any other job, and exits 1 if it can.
636
+
637
+ The report is sent only when both --api-url and --api-token are given (or DIFFCI_API_URL and
638
+ DIFFCI_TOKEN are set). A failed send is reported and never fails the step - the report is on disk
639
+ either way. Plain http is refused; the token is never printed.
616
640
  `;
617
641
  async function main() {
618
642
  const { command, flags } = parseArgs(process.argv);
@@ -0,0 +1,101 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { existsSync, readFileSync, writeFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ const DIFFCI_SCRIPTS = {
5
+ "diffci:check": "diffci check",
6
+ "diffci:observe": "diffci observe --no-send",
7
+ };
8
+ const LOCKFILES = [
9
+ ["pnpm", "pnpm-lock.yaml"],
10
+ ["yarn", "yarn.lock"],
11
+ ["bun", "bun.lock"],
12
+ ["bun", "bun.lockb"],
13
+ ["npm", "package-lock.json"],
14
+ ["npm", "npm-shrinkwrap.json"],
15
+ ];
16
+ function declaredPackageManager(repoPath) {
17
+ const packagePath = join(repoPath, "package.json");
18
+ if (!existsSync(packagePath))
19
+ throw new Error("--install requires a package.json in the repository root");
20
+ const pkg = JSON.parse(readFileSync(packagePath, "utf8"));
21
+ if (typeof pkg.packageManager !== "string")
22
+ return undefined;
23
+ const name = pkg.packageManager.split("@", 1)[0];
24
+ if (["npm", "pnpm", "yarn", "bun"].includes(name))
25
+ return name;
26
+ throw new Error(`unsupported packageManager ${JSON.stringify(pkg.packageManager)}; supported managers are npm, pnpm, yarn, and bun`);
27
+ }
28
+ export function detectPackageManager(repoPath) {
29
+ const declared = declaredPackageManager(repoPath);
30
+ if (declared)
31
+ return declared;
32
+ const detected = new Set();
33
+ for (const [manager, lockfile] of LOCKFILES) {
34
+ if (existsSync(join(repoPath, lockfile)))
35
+ detected.add(manager);
36
+ }
37
+ if (detected.size > 1) {
38
+ throw new Error(`multiple package-manager lockfiles found (${[...detected].join(", ")}); set packageManager in package.json to choose one`);
39
+ }
40
+ return detected.values().next().value ?? "npm";
41
+ }
42
+ export function installPlan(repoPath, version, platform = process.platform) {
43
+ const manager = detectPackageManager(repoPath);
44
+ const packageSpec = `@diffci.com/diffci@${version}`;
45
+ const managerCommand = platform === "win32" ? `${manager}.cmd` : manager;
46
+ const argsByManager = {
47
+ npm: ["install", "--save-dev", "--save-exact", "--ignore-scripts", packageSpec],
48
+ pnpm: ["add", "--save-dev", "--save-exact", "--ignore-scripts", packageSpec],
49
+ yarn: ["add", "--dev", "--exact", "--ignore-scripts", packageSpec],
50
+ bun: ["add", "--dev", "--exact", "--ignore-scripts", packageSpec],
51
+ };
52
+ const managerArgs = argsByManager[manager];
53
+ if (platform === "win32") {
54
+ // npm/pnpm/yarn/bun are .cmd shims on Windows. Node does not execute those reliably without
55
+ // cmd.exe, so use its explicit /d /s /c form. Every token is generated here (not user input).
56
+ return { manager, command: process.env.ComSpec ?? "cmd.exe", args: ["/d", "/s", "/c", [managerCommand, ...managerArgs].join(" ")], packageSpec };
57
+ }
58
+ return { manager, command: managerCommand, args: managerArgs, packageSpec };
59
+ }
60
+ export function installDiffci(repoPath, version, run = spawnSync, packageSpecOverride) {
61
+ const basePlan = installPlan(repoPath, version);
62
+ const plan = packageSpecOverride === undefined
63
+ ? basePlan
64
+ : {
65
+ ...basePlan,
66
+ packageSpec: packageSpecOverride,
67
+ args: basePlan.args.map((arg) => arg.replace(basePlan.packageSpec, packageSpecOverride)),
68
+ };
69
+ const result = run(plan.command, plan.args, {
70
+ cwd: repoPath,
71
+ encoding: "utf8",
72
+ stdio: ["inherit", "pipe", "pipe"],
73
+ windowsHide: true,
74
+ });
75
+ return { plan, result };
76
+ }
77
+ export function addDiffciPackageScripts(repoPath) {
78
+ const packagePath = join(repoPath, "package.json");
79
+ const source = readFileSync(packagePath, "utf8");
80
+ const manifest = JSON.parse(source);
81
+ if (manifest.scripts !== undefined && (typeof manifest.scripts !== "object" || manifest.scripts === null || Array.isArray(manifest.scripts))) {
82
+ throw new Error("package.json scripts must be an object before DiffCI scripts can be added");
83
+ }
84
+ const scripts = (manifest.scripts ?? {});
85
+ const added = [];
86
+ const kept = [];
87
+ for (const [name, command] of Object.entries(DIFFCI_SCRIPTS)) {
88
+ if (name in scripts) {
89
+ kept.push(name);
90
+ continue;
91
+ }
92
+ scripts[name] = command;
93
+ added.push(name);
94
+ }
95
+ manifest.scripts = scripts;
96
+ if (added.length > 0) {
97
+ const newline = source.includes("\r\n") ? "\r\n" : "\n";
98
+ writeFileSync(packagePath, `${JSON.stringify(manifest, null, 2)}${newline}`, "utf8");
99
+ }
100
+ return { added, kept };
101
+ }