@intentset/cli 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/agents.ts ADDED
@@ -0,0 +1,185 @@
1
+ /**
2
+ * The agent guide `intentset init` writes to `.intentset/agents.md`: what a
3
+ * coding agent does, before and after it changes code, to keep the model
4
+ * current (Core §10: load the owning slice first, update affected semantics
5
+ * and checks in the same review, never self-approve). A repository points its
6
+ * agents at it from AGENTS.md or CLAUDE.md.
7
+ *
8
+ * Generated rather than copied, so the commands, the required sections and
9
+ * the trailer can never disagree with the toolchain that wrote it.
10
+ */
11
+ import { ARTIFACT_TYPES, REQUIRED_SECTIONS } from "@intentset/core";
12
+ import { UNCHANGED_TRAILER } from "./git.ts";
13
+ import { TOOL } from "./output.ts";
14
+
15
+ export const AGENT_GUIDE_PATH = ".intentset/agents.md";
16
+
17
+ /** The lines a repository adds so its agents read the guide, by the file that takes each. */
18
+ export const AGENT_POINTERS: Array<[string, string]> = [
19
+ ["CLAUDE.md", `@${AGENT_GUIDE_PATH}`],
20
+ ["AGENTS.md", `Before changing code, read ${AGENT_GUIDE_PATH} and follow it.`],
21
+ ];
22
+
23
+ /** How the guide runs the CLI: through the package manager the repository uses. */
24
+ export type PackageManager = "npm" | "pnpm";
25
+
26
+ /** pnpm when the repository says so (its lockfile, workspace file or packageManager field), npm otherwise. */
27
+ export function detectPackageManager(files: {
28
+ has(path: string): boolean;
29
+ packageJson: string | null;
30
+ }): PackageManager {
31
+ if (files.has("pnpm-lock.yaml") || files.has("pnpm-workspace.yaml")) return "pnpm";
32
+ try {
33
+ const manager = (JSON.parse(files.packageJson ?? "{}") as { packageManager?: unknown }).packageManager;
34
+ if (typeof manager === "string" && manager.startsWith("pnpm@")) return "pnpm";
35
+ } catch {
36
+ // An unreadable package.json says nothing about the package manager.
37
+ }
38
+ return "npm";
39
+ }
40
+
41
+ export function agentGuide(scope: readonly string[], manager: PackageManager = "npm"): string {
42
+ const run = manager === "pnpm" ? "pnpm exec intentset" : "npx intentset";
43
+ const isolated =
44
+ manager === "pnpm"
45
+ ? "pnpm dlx --package=@intentset/cli --package=typescript@7 intentset ..."
46
+ : "npx -p @intentset/cli -p typescript@7 intentset ...";
47
+ const where = scope.map((pattern) => `\`${pattern}\``).join(", ");
48
+ const sections = ARTIFACT_TYPES.map(
49
+ (type) => `| ${type} | ${REQUIRED_SECTIONS[type].map((section) => `## ${section}`).join(", ")} |`,
50
+ );
51
+ return `# Keeping the product model current
52
+
53
+ This repository keeps an Intentset product model: one Markdown file per product behavior, rule, scenario, slice,
54
+ contract, decision, verification and knowledge article, under ${where}. The records say what the product promises,
55
+ which code delivers it and how it is checked. You keep them true as part of every change, in the same commit as the
56
+ code, so that people can review what the product does without reading every line of what changed.
57
+
58
+ Written by \`intentset init\` (${TOOL.name} ${TOOL.version}). To refresh it, delete it and run
59
+ \`${run} init --agents\`.
60
+
61
+ ## Before you change code
62
+
63
+ Load what the code you are about to change promises:
64
+
65
+ \`\`\`sh
66
+ ${run} context <a file you will edit>
67
+ ${run} context <an ID, such as BEH-...>
68
+ \`\`\`
69
+
70
+ The result is the owning slice, its behaviors, rules, scenarios, contracts, decisions and checks, with their paths.
71
+ Keep the change inside that slice, and reach other slices only through the contracts it consumes.
72
+
73
+ ## When behavior changes
74
+
75
+ Update the records in the same change as the code.
76
+
77
+ - A changed promise: edit the behavior's Behavior, Preconditions and Outcomes, and the rules and scenarios it
78
+ touches.
79
+ - A new promise: add a behavior record with \`status: draft\`, and add its ID to \`implements\` on the slice that
80
+ delivers it. New rules, scenarios and verifications start as drafts too.
81
+ - New code that no slice claims: add a slice record with \`status: draft\`, its claims and its entrypoints. While a
82
+ slice is a draft, the paths it plans may not exist yet.
83
+ - A changed check: a verification record names the test that checks a claim. Change the test and its record
84
+ together, and add one for a behavior or rule that has none.
85
+
86
+ Write each link once, on the record that points: a behavior is \`governedBy\` its rules, a scenario \`illustrates\`
87
+ a behavior, a slice \`implements\` behaviors, a verification \`verifies\` what it checks. Never write the reverse.
88
+
89
+ ## When behavior does not change
90
+
91
+ A refactor changes code and no behavior. Say so in the commit message with a trailer naming each slice:
92
+
93
+ \`\`\`text
94
+ ${UNCHANGED_TRAILER}: SLICE-...
95
+ \`\`\`
96
+
97
+ \`intentset review\` lists every slice whose code changed while its records did not, and this trailer is how a
98
+ commit answers it. Use it only when no behavior changed: a reviewer reads it as your claim.
99
+
100
+ ## Never
101
+
102
+ - Set a status to approved, implemented or released, or publish knowledge. People approve, and validation passing
103
+ is not approval.
104
+ - Change an ID or reuse a retired one. Moving or renaming a file keeps its ID; splitting a behavior retires the old
105
+ one with \`replacedBy\`.
106
+ - Name an owner, audience, release, role, edition or flag that is not in \`.intentset/registries.yaml\`. Ask
107
+ instead.
108
+ - Commit run evidence (\`.intentset/evidence/\`) or an export. They describe one commit and are stale once it lands.
109
+ - Edit generated output: an export, the Atlas, published pages.
110
+
111
+ ## Before you finish
112
+
113
+ \`\`\`sh
114
+ ${run} validate
115
+ ${run} architecture check # when .intentset/architecture.yaml exists
116
+ ${run} review --base main
117
+ \`\`\`
118
+
119
+ Fix every error. Resolve each slice \`review\` lists under "Code changed, records unchanged", by updating its records
120
+ or with the trailer. The architecture check needs TypeScript 7; on an earlier TypeScript, run the toolchain as
121
+ \`${isolated}\`.
122
+
123
+ ## Record format
124
+
125
+ A record is Markdown with YAML frontmatter under \`intentset:\`, and its level-one heading repeats its title. An
126
+ existing record of the same type in this repository is the best template; a new behavior looks like this, one
127
+ recognizable promise including how it fails:
128
+
129
+ \`\`\`markdown
130
+ ---
131
+ markset: 0
132
+ intentset:
133
+ spec: '0.1'
134
+ profile: intentset/behavior/0.1
135
+ id: BEH-AREA-NAME
136
+ type: behavior
137
+ title: The promise in a few words, such as Schedule an assessment
138
+ status: draft
139
+ owner: <an owner from the registry>
140
+ visibility: internal
141
+ audiences: [engineering, product]
142
+ parent: CAP-...
143
+ links:
144
+ governedBy: [RULE-...]
145
+ availability:
146
+ products: [PRD-...]
147
+ releases: [<a release from the registry>]
148
+ roles: [<a role>]
149
+ editions: [<an edition>]
150
+ flags: []
151
+ ---
152
+
153
+ # The promise in a few words, such as Schedule an assessment
154
+
155
+ ## Behavior
156
+ Who acts, and what the system does.
157
+
158
+ ## Preconditions
159
+ What must be true first.
160
+
161
+ ## Outcomes
162
+ Success: what happens. Failure: what is rejected, and what is left unchanged.
163
+ \`\`\`
164
+
165
+ A knowledge record explains behaviors, rules or capabilities to one audience, in that audience's words rather than
166
+ the behavior's. It may carry \`tips\`: one plain sentence per explained ID, at most 160 characters, which the
167
+ product shows beside the control that delivers that behavior once the knowledge is reviewed and published.
168
+
169
+ \`\`\`yaml
170
+ links:
171
+ explains: [BEH-AREA-NAME, RULE-AREA-LIMIT]
172
+ tips:
173
+ BEH-AREA-NAME: What this control does, in one sentence.
174
+ RULE-AREA-LIMIT: The limit, and what happens at it.
175
+ \`\`\`
176
+
177
+ Each type requires these level-two sections:
178
+
179
+ | Type | Required sections |
180
+ |---|---|
181
+ ${sections.join("\n")}
182
+
183
+ The specifications are at https://intentset.org/specifications/.
184
+ `;
185
+ }
@@ -9,7 +9,12 @@
9
9
  *
10
10
  * `restricted` artifacts are withheld unless asked for, and the report says
11
11
  * how many, so a short context never reads as a complete one. Never writes.
12
+ *
13
+ * Given a file rather than an ID, it reads the context of the slice whose
14
+ * claims include the file, since an agent about to edit code knows the file
15
+ * and not the ID of what it promises.
12
16
  */
17
+ import { isAbsolute, relative, resolve, sep } from "node:path";
13
18
  import {
14
19
  type Artifact,
15
20
  type ContextArtifact,
@@ -21,8 +26,29 @@ import {
21
26
  } from "@intentset/core";
22
27
  import { count, formatDiagnostic, type Io, json, snapshotLine, TOOL } from "../output.ts";
23
28
  import type { Session } from "../session.ts";
29
+ import { claimingSlices } from "../slices.ts";
24
30
  import { unresolved, validationLine, validationSummary } from "./shared.ts";
25
31
 
32
+ /**
33
+ * The artifact a `context` argument names: itself when it is an ID, else the
34
+ * one slice whose claims include it as a file path, relative to the working
35
+ * directory. Several claiming slices are an overlap VSA reports; the caller
36
+ * is asked to choose.
37
+ */
38
+ export function resolveTarget(
39
+ session: Session,
40
+ argument: string,
41
+ cwd: string,
42
+ ): { id: string; path: string | null } | { path: string; slices: string[] } {
43
+ const graph = session.result.graph;
44
+ if (graph.artifacts.has(argument)) return { id: argument, path: null };
45
+ const absolute = isAbsolute(argument) ? argument : resolve(cwd, argument);
46
+ const path = relative(session.repo.root, absolute).split(sep).join("/");
47
+ const slices = claimingSlices(graph, path).map((slice) => slice.meta.id);
48
+ if (slices.length === 1) return { id: slices[0], path };
49
+ return { path, slices };
50
+ }
51
+
26
52
  const HEADINGS: Record<ContextRole, string> = {
27
53
  start: "Start",
28
54
  owner: "Owning slice",
@@ -39,12 +65,22 @@ const HEADINGS: Record<ContextRole, string> = {
39
65
 
40
66
  export function contextCommand(
41
67
  session: Session,
42
- id: string,
68
+ argument: string,
43
69
  includeRestricted: boolean,
44
70
  asJson: boolean,
45
71
  io: Io,
46
72
  ): number {
47
73
  const { graph } = session.result;
74
+ const target = resolveTarget(session, argument, io.cwd);
75
+ if ("slices" in target && target.slices.length > 1) {
76
+ io.stderr(
77
+ `intentset context: ${target.path} is claimed by ${target.slices.join(" and ")}, an overlap VSA reports; ` +
78
+ "name one of them.\n",
79
+ );
80
+ return 2;
81
+ }
82
+ const via = "id" in target ? target.path : null;
83
+ const id = "id" in target ? target.id : argument;
48
84
  const bundle = contextFor(graph, id, { includeRestricted });
49
85
  if (bundle === null) {
50
86
  const diagnostic = unresolved(id, session);
@@ -73,6 +109,7 @@ export function contextCommand(
73
109
  snapshot: session.snapshot,
74
110
  validation: validationSummary(session, session.level),
75
111
  start: id,
112
+ ...(via === null ? {} : { resolvedFrom: via }),
76
113
  included: bundle.artifacts.length,
77
114
  withheld: bundle.withheld,
78
115
  restrictedIncluded: includeRestricted,
@@ -93,6 +130,7 @@ export function contextCommand(
93
130
  `# Context for ${id}`,
94
131
  "",
95
132
  `Bounded engineering context (Core §10) for ${start.meta.type} ${id}, "${start.meta.title}".`,
133
+ ...(via === null ? [] : ["", `${via} is claimed by ${id}, so this is that slice's context.`]),
96
134
  "",
97
135
  `- ${snapshotLine(session.snapshot)}`,
98
136
  `- ${validationLine(session, session.level)}`,
@@ -1,16 +1,20 @@
1
1
  /**
2
2
  * `intentset init`: the one command that writes, and it only creates. It
3
- * writes `.intentset/config.yaml` and an empty-but-valid
4
- * `.intentset/registries.yaml`, and with `--example` the scheduling example's
5
- * records under `product/scheduling/` and its registries in place of the
6
- * empty ones. Every target is checked before anything is written, and each
7
- * file is opened with exclusive create, so an existing file is never
3
+ * writes `.intentset/config.yaml`, an empty-but-valid
4
+ * `.intentset/registries.yaml` and `.intentset/agents.md`, the guide coding
5
+ * agents follow to keep the model current, and with `--example` the
6
+ * scheduling example's records under `product/scheduling/` and its
7
+ * registries in place of the empty ones. `--agents` writes the guide alone,
8
+ * for a repository initialized before there was one, with the scope its
9
+ * config already names. Every target is checked before anything is written,
10
+ * and each file is opened with exclusive create, so an existing file is never
8
11
  * overwritten, not even by a race.
9
12
  */
10
13
  import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
11
14
  import { basename, dirname, join, resolve } from "node:path";
12
15
  import { fileURLToPath } from "node:url";
13
16
  import { compareStrings, DEFAULT_IGNORE, readConfig, readRegistries } from "@intentset/core";
17
+ import { AGENT_GUIDE_PATH, AGENT_POINTERS, agentGuide, detectPackageManager } from "../agents.ts";
14
18
  import type { Io } from "../output.ts";
15
19
  import { CONFIG_PATH } from "../repository.ts";
16
20
 
@@ -22,6 +26,8 @@ export interface InitOptions {
22
26
  root?: string;
23
27
  repository?: string;
24
28
  example: boolean;
29
+ /** Write only the agent guide. */
30
+ agentsOnly?: boolean;
25
31
  }
26
32
 
27
33
  /** YAML double-quoted scalar; the strict reader takes JSON's escapes. */
@@ -82,6 +88,8 @@ export async function initCommand(options: InitOptions, io: Io): Promise<number>
82
88
  return 2;
83
89
  }
84
90
 
91
+ if (options.agentsOnly === true) return writeGuideOnly(root, io);
92
+
85
93
  // Everything to write, decided before anything is written.
86
94
  const files = new Map<string, string>([[CONFIG_PATH, configText(repository)]]);
87
95
  let registries = EMPTY_REGISTRIES_TEXT;
@@ -103,6 +111,7 @@ export async function initCommand(options: InitOptions, io: Io): Promise<number>
103
111
  }
104
112
  }
105
113
  files.set(REGISTRIES_PATH, registries);
114
+ files.set(AGENT_GUIDE_PATH, agentGuide([DEFAULT_INIT_SCOPE], managerAt(root)));
106
115
 
107
116
  // What init writes must read back clean, or it has handed the user a broken repository.
108
117
  const config = readConfig(files.get(CONFIG_PATH) as string, CONFIG_PATH);
@@ -112,6 +121,37 @@ export async function initCommand(options: InitOptions, io: Io): Promise<number>
112
121
  return 2;
113
122
  }
114
123
 
124
+ if (!create(root, files, io)) return 2;
125
+ const scope = options.example ? `${files.size - 3} example records in scope` : "an empty scope";
126
+ io.stdout(`Initialized ${quote(repository)} with ${scope}. Next: intentset validate\n`);
127
+ pointers(io);
128
+ return 0;
129
+ }
130
+
131
+ /** `init --agents`: the guide alone, for the scope the repository's config names, or the default with no config. */
132
+ function writeGuideOnly(root: string, io: Io): number {
133
+ let scope = [DEFAULT_INIT_SCOPE];
134
+ const configFile = join(root, CONFIG_PATH);
135
+ if (existsSync(configFile)) {
136
+ const { config, diagnostics } = readConfig(readFileSync(configFile, "utf8"), CONFIG_PATH);
137
+ if (diagnostics.length === 0 && config.scope.length > 0) scope = config.scope;
138
+ }
139
+ if (!create(root, new Map([[AGENT_GUIDE_PATH, agentGuide(scope, managerAt(root))]]), io)) return 2;
140
+ pointers(io);
141
+ return 0;
142
+ }
143
+
144
+ /** The package manager the repository at root uses, so the guide's commands are ones it can run. */
145
+ function managerAt(root: string) {
146
+ const packageJson = join(root, "package.json");
147
+ return detectPackageManager({
148
+ has: (path) => existsSync(join(root, path)),
149
+ packageJson: existsSync(packageJson) ? readFileSync(packageJson, "utf8") : null,
150
+ });
151
+ }
152
+
153
+ /** Create every file or none: refuse if any exists, then write each with exclusive create. */
154
+ function create(root: string, files: Map<string, string>, io: Io): boolean {
115
155
  const ordered = [...files.keys()].sort(compareStrings);
116
156
  const existing = ordered.filter((path) => existsSync(join(root, path)));
117
157
  if (existing.length > 0) {
@@ -119,7 +159,7 @@ export async function initCommand(options: InitOptions, io: Io): Promise<number>
119
159
  `intentset init: refusing to overwrite ${existing.join(", ")}; init only creates files.\n` +
120
160
  "Remove them first if a fresh start is what you want.\n",
121
161
  );
122
- return 2;
162
+ return false;
123
163
  }
124
164
  for (const path of ordered) {
125
165
  const target = join(root, path);
@@ -128,11 +168,15 @@ export async function initCommand(options: InitOptions, io: Io): Promise<number>
128
168
  writeFileSync(target, files.get(path) as string, { flag: "wx" });
129
169
  } catch (error) {
130
170
  io.stderr(`intentset init: could not create ${path}: ${(error as Error).message}\n`);
131
- return 2;
171
+ return false;
132
172
  }
133
173
  io.stdout(`created ${path}\n`);
134
174
  }
135
- const scope = options.example ? `${files.size - 2} example records in scope` : "an empty scope";
136
- io.stdout(`Initialized ${quote(repository)} with ${scope}. Next: intentset validate\n`);
137
- return 0;
175
+ return true;
176
+ }
177
+
178
+ /** How to point a repository's agents at the guide. init never edits a file it did not create, so it says rather than does. */
179
+ function pointers(io: Io): void {
180
+ io.stdout(`\nPoint your coding agents at ${AGENT_GUIDE_PATH} with one line in the file they read:\n`);
181
+ for (const [file, line] of AGENT_POINTERS) io.stdout(` ${file}: ${line}\n`);
138
182
  }
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * `intentset publish` (Core §9): the deny-by-default projection of reviewed
3
3
  * knowledge for one audience, release and entitlement, as generated Markset
4
- * with provenance, optional HTML, the index a customer tool may read, and
5
- * retrieval chunks that carry the same filters. Refuses outright when the
4
+ * with provenance, optional HTML, the index a customer tool may read, the
5
+ * help file a product's runtime reads, and retrieval chunks that carry the
6
+ * same filters. Refuses outright when the
6
7
  * graph does not validate. Review pins are read as written: a real pin is a
7
8
  * literal hash, and the fixtures' `@current` is never bound here, so an
8
9
  * unbound one reads as changed. Writes only into the --out directory, which
@@ -11,7 +12,7 @@
11
12
  import { existsSync, mkdirSync, readdirSync, writeFileSync } from "node:fs";
12
13
  import { join, resolve } from "node:path";
13
14
  import { type Visibility, sortDiagnostics } from "@intentset/core";
14
- import { type PublicationRequest, publish, toChunks } from "@intentset/publisher";
15
+ import { HELP_FILE, type PublicationRequest, publish, toChunks } from "@intentset/publisher";
15
16
  import { count, formatDiagnostic, type Io } from "../output.ts";
16
17
  import { outputDirProblem, outputProblem } from "../outputs.ts";
17
18
  import type { Session } from "../session.ts";
@@ -86,6 +87,7 @@ export function publishCommand(session: Session, options: PublishCliOptions, io:
86
87
  if (document.html !== undefined) outputs.set(`${document.id}.html`, document.html);
87
88
  }
88
89
  outputs.set("index.json", `${JSON.stringify(published.index, null, 2)}\n`);
90
+ outputs.set(HELP_FILE, `${JSON.stringify(published.help, null, 2)}\n`);
89
91
  const chunks = toChunks(published.documents);
90
92
  outputs.set("chunks.jsonl", chunks.map((chunk) => `${JSON.stringify(chunk)}\n`).join(""));
91
93
  }
@@ -7,13 +7,19 @@
7
7
  * touches, a changed record to its own artifact and a changed source file
8
8
  * through the claiming slice to that slice's behaviors, each start's direct
9
9
  * dependents shown apart from its candidates. Markdown by default. Read-only.
10
+ *
11
+ * It also lists every slice whose code changed while none of the records that
12
+ * describe it did (Core §10: an agent updates affected semantics in the same
13
+ * review). A refactor changes code and no behavior, so the list is a prompt,
14
+ * not an error: a commit says so with an Intentset-Unchanged trailer, and
15
+ * `--fail-on-drift` fails only on slices no commit has spoken for.
10
16
  */
11
17
  import { type Graph, impact, compareStrings } from "@intentset/core";
12
- import { type Changes, changedFiles, chooseBase } from "../git.ts";
13
- import { matchGlob } from "../glob.ts";
18
+ import { acknowledgedSlices, type Changes, changedFiles, chooseBase, UNCHANGED_TRAILER } from "../git.ts";
14
19
  import { count, formatDiagnostic, type Io, json, snapshotLine, TOOL } from "../output.ts";
15
20
  import { levelJson, levelSections } from "../reports.ts";
16
21
  import type { Session } from "../session.ts";
22
+ import { activeSlices, claimingSlices, describingRecords, isSliceCode } from "../slices.ts";
17
23
  import { pathText, REACHABILITY_NOTE } from "./impact.ts";
18
24
 
19
25
  export interface Attribution {
@@ -27,9 +33,6 @@ export interface Attribution {
27
33
  export function attribute(graph: Graph, changed: readonly string[]): { starts: Attribution[]; unattributed: string[] } {
28
34
  const byPath = new Map<string, string>();
29
35
  for (const [id, artifact] of graph.artifacts) byPath.set(artifact.path, id);
30
- const slices = [...graph.artifacts.values()]
31
- .filter((a) => a.meta.type === "slice" && a.meta.status !== "retired" && a.meta.slice !== undefined)
32
- .sort((a, b) => compareStrings(a.meta.id, b.meta.id));
33
36
  const starts = new Map<string, Set<string>>();
34
37
  const add = (id: string, reason: string) => {
35
38
  const set = starts.get(id) ?? new Set<string>();
@@ -43,7 +46,7 @@ export function attribute(graph: Graph, changed: readonly string[]): { starts: A
43
46
  add(id, `${path} is its record`);
44
47
  continue;
45
48
  }
46
- const owners = slices.filter((slice) => slice.meta.slice?.claims.some((claim) => matchGlob(claim.path, path)));
49
+ const owners = claimingSlices(graph, path);
47
50
  let attributed = false;
48
51
  for (const slice of owners) {
49
52
  for (const behavior of slice.meta.links.implements ?? []) {
@@ -62,9 +65,46 @@ export function attribute(graph: Graph, changed: readonly string[]): { starts: A
62
65
  };
63
66
  }
64
67
 
68
+ export interface Drift {
69
+ slice: string;
70
+ title: string;
71
+ /** The slice's changed code: files its source, backend or contract claims match. */
72
+ code: string[];
73
+ /** The records that describe the slice, none of which changed. */
74
+ records: string[];
75
+ /** True when a commit after the base names the slice in an Intentset-Unchanged trailer. */
76
+ acknowledged: boolean;
77
+ }
78
+
79
+ export const DRIFT_NOTE =
80
+ "Slices whose code changed while none of the records that describe them did: the slice, its behaviors, their " +
81
+ "rules, scenarios and verifications, its contracts and their decisions. If behavior changed, update those records " +
82
+ `in this change. If it did not, say so in a commit with the trailer \`${UNCHANGED_TRAILER}: <slice ID>\`.`;
83
+
84
+ /** Each slice whose code is among the changed files while none of its describing records is, sorted by slice ID. */
85
+ export function drift(graph: Graph, changed: readonly string[], acknowledged: ReadonlySet<string>): Drift[] {
86
+ const records = new Set([...graph.artifacts.values()].map((artifact) => artifact.path));
87
+ const touched = new Set(changed);
88
+ const out: Drift[] = [];
89
+ for (const slice of activeSlices(graph)) {
90
+ const code = changed.filter((path) => !records.has(path) && isSliceCode(slice, path));
91
+ if (code.length === 0) continue;
92
+ const describing = describingRecords(graph, slice);
93
+ if (describing.some((id) => touched.has(graph.artifacts.get(id)?.path ?? ""))) continue;
94
+ out.push({
95
+ slice: slice.meta.id,
96
+ title: slice.meta.title,
97
+ code,
98
+ records: describing,
99
+ acknowledged: acknowledged.has(slice.meta.id),
100
+ });
101
+ }
102
+ return out;
103
+ }
104
+
65
105
  export function reviewCommand(
66
106
  session: Session,
67
- options: { base?: string; levelReason: string },
107
+ options: { base?: string; levelReason: string; failOnDrift?: boolean },
68
108
  asJson: boolean,
69
109
  io: Io,
70
110
  ): number {
@@ -80,6 +120,9 @@ export function reviewCommand(
80
120
  if ("unavailable" in chosen) changes.baseUnavailable = chosen.unavailable;
81
121
  const changed = [...new Set([...changes.committed, ...changes.uncommitted])].sort(compareStrings);
82
122
  const { starts, unattributed } = attribute(graph, changed);
123
+ const drifted = drift(graph, changed, new Set(acknowledgedSlices(repo.root, base)));
124
+ const failing = options.failOnDrift === true && drifted.some((item) => !item.acknowledged);
125
+ const exit = session.errors > 0 || failing ? 1 : 0;
83
126
  const impacts = starts.map(({ start, because }) => {
84
127
  const artifact = graph.artifacts.get(start);
85
128
  const report = impact(graph, start);
@@ -108,10 +151,11 @@ export function reviewCommand(
108
151
  ...levelJson(session),
109
152
  note: REACHABILITY_NOTE,
110
153
  impact: impacts,
154
+ drift: drifted,
111
155
  unattributed,
112
156
  }),
113
157
  );
114
- return session.errors > 0 ? 1 : 0;
158
+ return exit;
115
159
  }
116
160
 
117
161
  const baseLine =
@@ -147,9 +191,22 @@ export function reviewCommand(
147
191
  if (item.candidates.length === 0) out.push("- none");
148
192
  for (const hit of item.candidates) out.push(`- ${hit.id} (${hit.type}): \`${pathText(item.start, hit)}\``);
149
193
  }
194
+ const open = drifted.filter((item) => !item.acknowledged).length;
195
+ out.push("", `## Code changed, records unchanged (${drifted.length}, ${open} not acknowledged)`, "", DRIFT_NOTE);
196
+ if (drifted.length === 0) out.push("", "None.");
197
+ for (const item of drifted) {
198
+ out.push("", `### ${item.slice}: ${item.title}`, "");
199
+ out.push(
200
+ item.acknowledged
201
+ ? `- Acknowledged: a commit since the base says this changes no behavior (${UNCHANGED_TRAILER}).`
202
+ : "- Not acknowledged: update the records below, or say in a commit that no behavior changed.",
203
+ );
204
+ out.push(...item.code.map((path) => `- Code changed: ${path}`));
205
+ out.push(`- Records describing it, none changed: ${item.records.join(", ")}`);
206
+ }
150
207
  out.push("", `## Changed files no artifact accounts for (${unattributed.length})`, "");
151
208
  if (unattributed.length === 0) out.push("None.");
152
209
  else out.push(...unattributed.map((path) => `- ${path}`));
153
210
  io.stdout(`${out.join("\n")}\n`);
154
- return session.errors > 0 ? 1 : 0;
211
+ return exit;
155
212
  }
package/src/git.ts CHANGED
@@ -79,3 +79,18 @@ export function changedFiles(root: string, base: Base | null): Omit<Changes, "ba
79
79
  uncommitted: [...new Set(uncommitted)].sort(compareStrings),
80
80
  };
81
81
  }
82
+
83
+ /**
84
+ * The trailer a commit carries to say it changes a slice's code without
85
+ * changing its behavior: `Intentset-Unchanged: SLICE-A, SLICE-B`. A reviewer
86
+ * reads it in the commit, so the claim is made where it can be questioned.
87
+ */
88
+ export const UNCHANGED_TRAILER = "Intentset-Unchanged";
89
+
90
+ /** The slice IDs named in an Intentset-Unchanged trailer of any commit after the base, sorted. */
91
+ export function acknowledgedSlices(root: string, base: Base | null): string[] {
92
+ if (base === null) return [];
93
+ const out = git(root, ["log", `--format=%(trailers:key=${UNCHANGED_TRAILER},valueonly)`, `${base.commit}..HEAD`]);
94
+ if (out === null) return [];
95
+ return [...new Set(out.split(/[\s,]+/).filter((id) => id !== ""))].sort(compareStrings);
96
+ }