@vaultcompass/conductor 0.7.0 → 0.8.1

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.
@@ -7,15 +7,17 @@
7
7
  // branch after this branch forked to this branch, so somebody else's merge
8
8
  // breaches this pull request's change budget.
9
9
  //
10
- // The umbrella computes the path set ITSELF rather than handing intent-guard
11
- // its own `--base`, and that is a deliberate cost. The contract for a run may
12
- // live in a temporary directory (see intent-prepare.ts), and `--base` inside
13
- // the gate resolves git relative to `--project`, where there is no
14
- // repository. Computing the set here and passing `--paths` is the one
15
- // arrangement that works for both the native and the imported contract, and
16
- // the flags below are copied from intent-guard 1.2.1 so the two agree on what
17
- // "changed" means rather than agreeing by coincidence.
18
- import { spawnSync } from 'node:child_process';
10
+ // The umbrella computes the path set ITSELF, and how it reaches the gate
11
+ // depends on the contract. An imported contract lives in a temporary
12
+ // directory (see intent-prepare.ts), where the gate's own `--base` would
13
+ // resolve git relative to a `--project` with no repository in it, so that
14
+ // gate is always handed `--paths`. A native contract on an intent-guard new
15
+ // enough to read the change set from git itself is handed the gate's own
16
+ // `--base` instead (`intentChangeChannel` in gate-runner.ts), and `--paths`
17
+ // otherwise. The flags below are copied from intent-guard so the two agree
18
+ // on what "changed" means rather than agreeing by coincidence.
19
+ import { runGit } from './git.js';
20
+ import { PRIVATE_TRUST_BASE_REF, exactRefLookup, exactRefObject, refuseAmbiguousRef, resolveRev, } from './trust-base.js';
19
21
  /**
20
22
  * The ref to diff against, or null for a run that is not a pull request.
21
23
  *
@@ -35,6 +37,109 @@ export function resolveBaseRef(options) {
35
37
  }
36
38
  return null;
37
39
  }
40
+ /**
41
+ * The base to diff against, resolved against the repository and always
42
+ * spelled so a tag cannot shadow it (the same class as the trust base: git
43
+ * resolves a short origin/<base> through refs/tags/ first, so a tag at
44
+ * HEAD~1 would narrow the change set to the last commit).
45
+ *
46
+ * - An explicit --base keeps its meaning, but a short name that matches
47
+ * more than one kind of ref is refused (refuseAmbiguousRef).
48
+ * - Otherwise, on a pull request: refs/conductor/trust-base (the composite
49
+ * action's private fetch) when it exists, else refs/remotes/origin/<base>,
50
+ * both in full. The private ref wins and is not compared against the
51
+ * remote-tracking ref. If neither exists the spelled remote ref is returned and the
52
+ * diff fails closed, naming fetch-depth: 0.
53
+ * - Otherwise, on a run with a trust base: the commit the trust base
54
+ * resolves to.
55
+ * - Null outside a pull request.
56
+ *
57
+ * Nothing is deepened here: a depth-1 private ref has no merge base and the
58
+ * diff fails closed, the same outcome as a depth-1 origin/<base> always had.
59
+ */
60
+ export function resolveBaseRefInRepo(repoRoot, options) {
61
+ if (options.base !== undefined && options.base !== '') {
62
+ // A base that starts with a dash is an OPTION to git, not a ref:
63
+ // "--output=/x" made "git diff --output=/x...HEAD" write that file and
64
+ // print nothing, an empty change set the intent gate then judged as clean.
65
+ // Refused here, and changedPathsSince refuses it again itself.
66
+ if (options.base.startsWith('-')) {
67
+ return {
68
+ ok: false,
69
+ detail: `refusing "${options.base}" as the intent gate base: it starts with a dash, so git would ` +
70
+ 'read it as an option rather than a ref. Nothing was checked.',
71
+ };
72
+ }
73
+ const ambiguous = refuseAmbiguousRef(repoRoot, options.base, 'the intent gate base');
74
+ if (ambiguous !== null) {
75
+ return { ok: false, detail: ambiguous };
76
+ }
77
+ // A refs/ name is checked with show-ref: git's name resolution would
78
+ // otherwise fall back to refs/tags/<that name> when the ref is absent.
79
+ if (options.base.startsWith('refs/') && exactRefObject(repoRoot, options.base) === null) {
80
+ return {
81
+ ok: false,
82
+ detail: `refusing "${options.base}" as the intent gate base: no such ref exists in this ` +
83
+ 'repository (a tag of that name does not count). Nothing was checked.',
84
+ };
85
+ }
86
+ return { ok: true, base: { ref: options.base, source: 'flag' } };
87
+ }
88
+ const fromGithub = options.env.GITHUB_BASE_REF;
89
+ if (fromGithub === undefined || fromGithub === '') {
90
+ if (options.trustBase === undefined) {
91
+ return { ok: true, base: null };
92
+ }
93
+ // No other base was named, so the change set is measured from the trust
94
+ // base: the commit the rules were read from is the one the pull request
95
+ // is judged against. Handed on as the commit id it resolves to, read the
96
+ // same way the trust base itself was (resolveRev never sends a refs/
97
+ // name through git's name resolution).
98
+ const commit = resolveRev(repoRoot, options.trustBase, 'commit');
99
+ if (commit === null) {
100
+ return {
101
+ ok: false,
102
+ detail: `the trust base does not resolve to a commit: "${options.trustBase}" names no commit in ` +
103
+ 'this repository, so there is no base to measure the intent gate against and nothing ' +
104
+ 'was checked. Fix: pass a trust base that is a commit in this checkout (in CI, check out ' +
105
+ 'with fetch-depth: 0).',
106
+ };
107
+ }
108
+ return { ok: true, base: { ref: commit, source: 'trust-base' } };
109
+ }
110
+ const remoteRef = `refs/remotes/origin/${fromGithub}`;
111
+ // The private ref is the authority when it exists; refs/remotes/origin/<base>
112
+ // is fixed at checkout time, is the side pull-request code can move, and is
113
+ // not compared against it (a merge to the base in between would be a false
114
+ // refusal). It is the base only when the private ref is absent. Both are
115
+ // read with show-ref (the EXACT ref, never git's name resolution, which
116
+ // would take a tag of that name) and the base handed on is the commit id.
117
+ // A private ref git could not read is a refusal, never taken for an absent
118
+ // one; the remote-tracking ref is used only when the private ref is absent.
119
+ const privLookup = exactRefLookup(repoRoot, PRIVATE_TRUST_BASE_REF);
120
+ if (privLookup.kind === 'error') {
121
+ return {
122
+ ok: false,
123
+ detail: `the base branch could not be read: ${privLookup.detail}, so there is no base to measure ` +
124
+ 'the intent gate against and nothing was checked. Fix: check that git can read the ' +
125
+ `repository and that the action's fetch of the base branch into ${PRIVATE_TRUST_BASE_REF} ` +
126
+ 'succeeded, then run again.',
127
+ };
128
+ }
129
+ const priv = privLookup.kind === 'ref' ? privLookup.oid : null;
130
+ const remote = priv === null ? exactRefObject(repoRoot, remoteRef) : null;
131
+ const chosen = priv ?? remote;
132
+ if (chosen === null) {
133
+ return {
134
+ ok: false,
135
+ detail: `the base branch is not in this repository: neither ${PRIVATE_TRUST_BASE_REF} (the ` +
136
+ `composite action's fetch of the base branch) nor ${remoteRef} exists, so there is no base ` +
137
+ 'to measure the intent gate against and nothing was checked. Fix: in CI, check out with ' +
138
+ 'fetch-depth: 0, or fetch the base ref before the run.',
139
+ };
140
+ }
141
+ return { ok: true, base: { ref: chosen, source: 'github' } };
142
+ }
38
143
  /**
39
144
  * The branch this run is about.
40
145
  *
@@ -48,11 +153,8 @@ export function currentBranch(repoRoot, env) {
48
153
  if (fromGithub !== undefined && fromGithub !== '') {
49
154
  return fromGithub;
50
155
  }
51
- const child = spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
52
- cwd: repoRoot,
53
- encoding: 'utf8',
54
- });
55
- const value = (child.stdout ?? '').trim();
156
+ const child = runGit(repoRoot, ['rev-parse', '--abbrev-ref', 'HEAD']);
157
+ const value = child.stdout.trim();
56
158
  if (child.status !== 0 || value === '' || value === 'HEAD') {
57
159
  return null;
58
160
  }
@@ -78,14 +180,33 @@ export function currentBranch(repoRoot, env) {
78
180
  * git error, because an empty path set is indistinguishable from a clean run.
79
181
  */
80
182
  export function changedPathsSince(repoRoot, base) {
81
- const child = spawnSync('git', [
183
+ // Refused here, inside the function that calls git, so no caller can skip
184
+ // it: "--output=/x...HEAD" is an option to git diff, not a range.
185
+ if (base.startsWith('-')) {
186
+ return {
187
+ ok: false,
188
+ detail: `refusing "${base}" as the intent gate base: it starts with a dash, so git would read it ` +
189
+ 'as an option rather than a ref. Nothing was checked.',
190
+ };
191
+ }
192
+ const child = runGit(repoRoot, [
82
193
  '-c',
83
194
  'core.quotePath=false',
84
195
  'diff',
85
196
  '--name-only',
197
+ // NUL-terminated, so a name with a quote, a backslash, a tab or a
198
+ // newline arrives byte for byte instead of C-quoted ("a\"b.txt"), which
199
+ // names no file and which intent-guard refuses outright (backslash).
200
+ '-z',
86
201
  '--no-renames',
202
+ // A moved submodule pointer is a change, and is listed whatever
203
+ // .gitmodules or git config says about ignoring submodules: the command
204
+ // line value overrides both.
205
+ '--ignore-submodules=none',
87
206
  `${base}...HEAD`,
88
- ], { cwd: repoRoot, encoding: 'utf8' });
207
+ // Ends the revisions, so the range is always read as a range.
208
+ '--',
209
+ ], { maxBuffer: 64 * 1024 * 1024 });
89
210
  if (child.error !== undefined) {
90
211
  return { ok: false, detail: `git could not be run: ${child.error.message}` };
91
212
  }
@@ -93,43 +214,76 @@ export function changedPathsSince(repoRoot, base) {
93
214
  // git's own first line usually ends in a full stop, and this message
94
215
  // continues after it. Two in a row reads as a typo in a message somebody
95
216
  // is already reading because something went wrong.
96
- const stderr = ((child.stderr ?? '').trim().split('\n')[0] ?? '').replace(/\.+$/, '');
217
+ const stderr = (child.stderr.trim().split('\n')[0] ?? '').replace(/\.+$/, '');
97
218
  return {
98
219
  ok: false,
99
220
  detail: `git could not resolve "${base}...HEAD" (exit ${child.status ?? -1})` +
100
221
  `${stderr === '' ? '' : `: ${stderr}`}. ` +
101
- 'In Actions this is usually a shallow checkout with no merge base; ' +
102
- 'check out with fetch-depth: 0, or fetch the base ref before the run.',
222
+ 'In Actions this is usually a shallow checkout with no merge base. ' +
223
+ 'Fix: check out with fetch-depth: 0, or fetch the base ref before the run.',
103
224
  };
104
225
  }
105
- // Split on newlines and NOTHING else. Trimming each line was corrupting a
226
+ // Split on NUL and NOTHING else. Trimming each entry was corrupting a
106
227
  // filename with leading or trailing whitespace into a different filename,
107
228
  // which is worse than refusing it: the gate would then check a path that
108
229
  // does not exist and never check the one that changed.
109
- const paths = (child.stdout ?? '').split('\n').filter((line) => line.length > 0);
230
+ const paths = child.stdout.split('\0').filter((line) => line.length > 0);
231
+ return { ok: true, paths };
232
+ }
233
+ /**
234
+ * Why this change set cannot be handed to intent-guard as one `--paths`
235
+ * value, or null when it can.
236
+ *
237
+ * Asked only where `--paths` is the channel: a contract imported from a spec
238
+ * (its project is a temporary directory with no repository, so the gate
239
+ * cannot read the change set from git itself) or an intent-guard older than
240
+ * the release whose own `--base` reads the change set from git. Where the
241
+ * gate reads git itself, none of this applies and the change is judged.
242
+ *
243
+ * - A comma: `--paths` is comma-joined, so a comma in a filename arrives as
244
+ * two paths, a phantom that can be reported as outside allowed_paths and a
245
+ * real path that quietly stops being measured against a protected one.
246
+ * - A backslash: intent-guard refuses an explicit path list entry with one.
247
+ * - Leading or trailing whitespace: it cannot survive the list intact.
248
+ * - EVERY path starting with "-": intent-guard reads a `--paths` value that
249
+ * starts with "-" as a missing value, and there is no `--paths=value` form
250
+ * (verified against intent-guard 1.8.0: it is an unknown option). One path
251
+ * starting with "-" is fine once another leads the list
252
+ * (`orderForPathsFlag`), so this is refused only when every path does.
253
+ *
254
+ * A space or any other byte inside a path passes through unchanged.
255
+ */
256
+ export function pathsFlagRefusal(paths) {
110
257
  for (const entry of paths) {
111
- // Fail closed rather than hand over a path the encoding cannot carry.
112
- // `--paths` is comma-joined, which is the only shape intent-guard 1.2.1
113
- // accepts, so a comma in a filename arrives as two paths: a phantom that
114
- // can be reported as outside allowed_paths, and a real path that quietly
115
- // stops being measured against a protected one. It invents a breach and
116
- // hides one, and neither half is visible in the report.
117
258
  if (entry.includes(',')) {
118
- return {
119
- ok: false,
120
- detail: `the changed path "${entry}" contains a comma, and the intent gate takes its ` +
121
- 'path list comma-joined, so it cannot be passed without splitting into two paths. ' +
122
- 'Nothing was checked, rather than checking a path that does not exist.',
123
- };
259
+ return (`the changed path "${entry}" contains a comma, and intent-guard takes an explicit path ` +
260
+ 'list comma-joined, so it cannot be passed without splitting into two paths.');
261
+ }
262
+ if (entry.includes('\\')) {
263
+ return (`the changed path "${entry}" contains a backslash, and intent-guard refuses an explicit ` +
264
+ 'path list entry with one.');
124
265
  }
125
266
  if (entry !== entry.trim()) {
126
- return {
127
- ok: false,
128
- detail: `the changed path "${entry}" has leading or trailing whitespace, which cannot ` +
129
- 'survive the gate path list intact. Nothing was checked, rather than checking a ' +
130
- 'path that does not exist. A space inside a path is fine.',
131
- };
267
+ return (`the changed path "${entry}" has leading or trailing whitespace, which cannot survive ` +
268
+ 'the path list intact. A space inside a path is fine.');
132
269
  }
133
270
  }
134
- return { ok: true, paths };
271
+ if (paths.length > 0 && paths.every((entry) => entry.startsWith('-'))) {
272
+ return (`every changed path starts with "-" (the first is "${paths[0]}"), and intent-guard reads ` +
273
+ 'a path list that starts with "-" as a missing value.');
274
+ }
275
+ return null;
276
+ }
277
+ /**
278
+ * The change set in the order it is joined for `--paths`: unchanged, except
279
+ * that when the first path starts with "-" the first path that does not is
280
+ * moved to the front, so the joined value does not start with "-". Order
281
+ * carries no meaning to the gate.
282
+ */
283
+ export function orderForPathsFlag(paths) {
284
+ const lead = paths.findIndex((entry) => !entry.startsWith('-'));
285
+ if (paths.length === 0 || !paths[0].startsWith('-') || lead === -1) {
286
+ return [...paths];
287
+ }
288
+ return [paths[lead], ...paths.slice(0, lead), ...paths.slice(lead + 1)];
135
289
  }
@@ -41,11 +41,16 @@ export type ContractSource =
41
41
  kind: 'native';
42
42
  path: string;
43
43
  }
44
- /** A spec imported and frozen for this run only. Repository-relative paths. */
44
+ /**
45
+ * A spec imported and frozen for this run only. Repository-relative paths.
46
+ * `ref` is set on a run with a trust base: the spec and plan were read from
47
+ * that ref, never from the tree being judged.
48
+ */
45
49
  | {
46
50
  kind: 'imported';
47
51
  spec: string;
48
52
  plan: string | null;
53
+ ref?: string;
49
54
  } | {
50
55
  kind: 'none';
51
56
  };
@@ -54,13 +59,32 @@ export interface IntentPreparation {
54
59
  /** The ref the change set was measured against, or null for a staged run. */
55
60
  baseRef: string | null;
56
61
  /** Whether that ref was asked for or inferred from the CI environment. */
57
- baseSource: 'flag' | 'github' | null;
62
+ baseSource: 'flag' | 'github' | 'trust-base' | null;
58
63
  /** What to hand the gate as --project: "." or an absolute temporary path. */
59
64
  projectDir: string;
60
65
  /** The changed paths, or null to leave v0.1's staged behaviour alone. */
61
66
  paths: string[] | null;
62
67
  /** Removes anything written outside the repository. Safe to call twice. */
63
68
  cleanup: () => void;
69
+ /**
70
+ * On a run with a trust base: intent control files this pull request adds
71
+ * or changes that did not decide this run, one line each. Reported as
72
+ * proposals, never acted on.
73
+ */
74
+ proposals?: string[];
75
+ /**
76
+ * One plain sentence the report prints under the contract line, for an
77
+ * input the person gave that this run did not use: --spec, when the base's
78
+ * frozen contract outranks it.
79
+ */
80
+ note?: string;
81
+ /**
82
+ * Why `paths` cannot be handed over as one --paths value (pathsFlagRefusal
83
+ * in intent-base.ts), on a native contract. The runner then uses the
84
+ * gate's own --base where the gate has it, and refuses with this reason
85
+ * where it does not.
86
+ */
87
+ pathsRefusal?: string;
64
88
  }
65
89
  /** Which link of the chain broke. Carried into the report verbatim. */
66
90
  export type PrepareStep = 'spec' | 'base'
@@ -90,6 +114,8 @@ export type IntentPrepareResult = {
90
114
  kind: 'skip';
91
115
  reason: 'no-contract' | 'contract-waived';
92
116
  detail: string;
117
+ /** As on IntentPreparation: what this pull request proposes. */
118
+ proposals?: string[];
93
119
  } | {
94
120
  kind: 'failed';
95
121
  step: PrepareStep;
@@ -102,6 +128,11 @@ export interface IntentPrepareOptions {
102
128
  env: NodeJS.ProcessEnv;
103
129
  base?: string;
104
130
  spec?: string;
131
+ /**
132
+ * The trust base, on a pull-request run. The change set is measured from
133
+ * it when no --base and no GITHUB_BASE_REF names another base.
134
+ */
135
+ trustBase?: string;
105
136
  timeoutMs?: number;
106
137
  /**
107
138
  * The directory the per-run temporary project is created under.