@goodbones/campaigns 0.1.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.
Files changed (102) hide show
  1. package/LICENSE +21 -0
  2. package/build/dts/core/campaign-state.d.ts +38 -0
  3. package/build/dts/core/campaign-state.d.ts.map +1 -0
  4. package/build/dts/core/campaigns.d.ts +196 -0
  5. package/build/dts/core/campaigns.d.ts.map +1 -0
  6. package/build/dts/core/ledger.d.ts +173 -0
  7. package/build/dts/core/ledger.d.ts.map +1 -0
  8. package/build/dts/core/phases.d.ts +26 -0
  9. package/build/dts/core/phases.d.ts.map +1 -0
  10. package/build/dts/core/sectors.d.ts +52 -0
  11. package/build/dts/core/sectors.d.ts.map +1 -0
  12. package/build/dts/domain/config.d.ts +985 -0
  13. package/build/dts/domain/config.d.ts.map +1 -0
  14. package/build/dts/domain/report.d.ts +16 -0
  15. package/build/dts/domain/report.d.ts.map +1 -0
  16. package/build/dts/host/campaigns.d.ts +195 -0
  17. package/build/dts/host/campaigns.d.ts.map +1 -0
  18. package/build/dts/host/diff.d.ts +31 -0
  19. package/build/dts/host/diff.d.ts.map +1 -0
  20. package/build/dts/index.d.ts +17 -0
  21. package/build/dts/index.d.ts.map +1 -0
  22. package/build/dts/infrastructure/campaign-functions.d.ts +7 -0
  23. package/build/dts/infrastructure/campaign-functions.d.ts.map +1 -0
  24. package/build/dts/infrastructure/report-source-fake.d.ts +4 -0
  25. package/build/dts/infrastructure/report-source-fake.d.ts.map +1 -0
  26. package/build/dts/infrastructure/report-source-live.d.ts +3 -0
  27. package/build/dts/infrastructure/report-source-live.d.ts.map +1 -0
  28. package/build/dts/load/decode.d.ts +7 -0
  29. package/build/dts/load/decode.d.ts.map +1 -0
  30. package/build/dts/load/extension.d.ts +25 -0
  31. package/build/dts/load/extension.d.ts.map +1 -0
  32. package/build/dts/manifest/lower.d.ts +14 -0
  33. package/build/dts/manifest/lower.d.ts.map +1 -0
  34. package/build/dts/manifest/spec.d.ts +515 -0
  35. package/build/dts/manifest/spec.d.ts.map +1 -0
  36. package/build/dts/ports/campaign-predicate.d.ts +23 -0
  37. package/build/dts/ports/campaign-predicate.d.ts.map +1 -0
  38. package/build/dts/ports/report-source.d.ts +14 -0
  39. package/build/dts/ports/report-source.d.ts.map +1 -0
  40. package/build/dts/testing.d.ts +2 -0
  41. package/build/dts/testing.d.ts.map +1 -0
  42. package/build/esm/core/campaign-state.js +140 -0
  43. package/build/esm/core/campaign-state.js.map +1 -0
  44. package/build/esm/core/campaigns.js +954 -0
  45. package/build/esm/core/campaigns.js.map +1 -0
  46. package/build/esm/core/ledger.js +519 -0
  47. package/build/esm/core/ledger.js.map +1 -0
  48. package/build/esm/core/phases.js +87 -0
  49. package/build/esm/core/phases.js.map +1 -0
  50. package/build/esm/core/sectors.js +313 -0
  51. package/build/esm/core/sectors.js.map +1 -0
  52. package/build/esm/domain/config.js +245 -0
  53. package/build/esm/domain/config.js.map +1 -0
  54. package/build/esm/domain/report.js +168 -0
  55. package/build/esm/domain/report.js.map +1 -0
  56. package/build/esm/host/campaigns.js +1159 -0
  57. package/build/esm/host/campaigns.js.map +1 -0
  58. package/build/esm/host/diff.js +156 -0
  59. package/build/esm/host/diff.js.map +1 -0
  60. package/build/esm/index.js +27 -0
  61. package/build/esm/index.js.map +1 -0
  62. package/build/esm/infrastructure/campaign-functions.js +67 -0
  63. package/build/esm/infrastructure/campaign-functions.js.map +1 -0
  64. package/build/esm/infrastructure/report-source-fake.js +6 -0
  65. package/build/esm/infrastructure/report-source-fake.js.map +1 -0
  66. package/build/esm/infrastructure/report-source-live.js +165 -0
  67. package/build/esm/infrastructure/report-source-live.js.map +1 -0
  68. package/build/esm/load/decode.js +60 -0
  69. package/build/esm/load/decode.js.map +1 -0
  70. package/build/esm/load/extension.js +326 -0
  71. package/build/esm/load/extension.js.map +1 -0
  72. package/build/esm/manifest/lower.js +488 -0
  73. package/build/esm/manifest/lower.js.map +1 -0
  74. package/build/esm/manifest/spec.js +296 -0
  75. package/build/esm/manifest/spec.js.map +1 -0
  76. package/build/esm/ports/campaign-predicate.js +2 -0
  77. package/build/esm/ports/campaign-predicate.js.map +1 -0
  78. package/build/esm/ports/report-source.js +7 -0
  79. package/build/esm/ports/report-source.js.map +1 -0
  80. package/build/esm/testing.js +5 -0
  81. package/build/esm/testing.js.map +1 -0
  82. package/package.json +65 -0
  83. package/src/core/campaign-state.ts +227 -0
  84. package/src/core/campaigns.ts +1319 -0
  85. package/src/core/ledger.ts +675 -0
  86. package/src/core/phases.ts +138 -0
  87. package/src/core/sectors.ts +399 -0
  88. package/src/domain/config.ts +314 -0
  89. package/src/domain/report.ts +203 -0
  90. package/src/host/campaigns.ts +1664 -0
  91. package/src/host/diff.ts +203 -0
  92. package/src/index.ts +256 -0
  93. package/src/infrastructure/campaign-functions.ts +99 -0
  94. package/src/infrastructure/report-source-fake.ts +10 -0
  95. package/src/infrastructure/report-source-live.ts +193 -0
  96. package/src/load/decode.ts +81 -0
  97. package/src/load/extension.ts +466 -0
  98. package/src/manifest/lower.ts +615 -0
  99. package/src/manifest/spec.ts +364 -0
  100. package/src/ports/campaign-predicate.ts +29 -0
  101. package/src/ports/report-source.ts +35 -0
  102. package/src/testing.ts +4 -0
@@ -0,0 +1,193 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { readFileSync } from "node:fs";
3
+ import { availableParallelism } from "node:os";
4
+ import * as path from "node:path";
5
+
6
+ import { ReportUnavailable } from "@goodbones/core";
7
+
8
+ import { type Diagnostic, indexByFile, parseReport, uniqueDiagnostics } from "../domain/report.js";
9
+ import { type ReportSource, reportSourcesOf, type ReportSpec } from "../ports/report-source.js";
10
+
11
+ // The report source on this host: runs a `report` term's commands from the
12
+ // repository root, or reads the files it names, and parses the output once
13
+ // — a report is about the whole repository, a campaign asks per file, and
14
+ // the plugin asks for one file at a time across a whole lint run. Cached
15
+ // per spec for the life of the process: the CLI is one process per `check`,
16
+ // and oxlint's language server is one process per editor session, which
17
+ // sees the report as of when it loaded the plugin. A report the build
18
+ // writes to a file is the predictable form for the editor, and for CI.
19
+ //
20
+ // A term may name several commands — a tool that checks one project at a
21
+ // time, run once per project — and their outputs are one report: parsed
22
+ // each, joined, and a diagnostic two of them print kept once. `read` runs
23
+ // them concurrently, as many at a time as the machine has cores, which is
24
+ // why both hosts call it before any file asks; `diagnosticsOf` with no
25
+ // answer kept runs them one after another, synchronously, and keeps that.
26
+ //
27
+ // A non-zero exit is not a failure — `tsc` exits 2 when there are errors,
28
+ // which is the case the term exists for. A command that cannot be spawned,
29
+ // or a file that is not there, is — and that failure is cached as the report
30
+ // would have been, so the second file to ask gets the same answer without a
31
+ // second spawn. The command is forked from the asking process; under the
32
+ // oxlint plugin that is the linter, which Linux refuses to fork once its AST
33
+ // buffers have merged into one mapping larger than RAM and swap — so the
34
+ // plugin asks at load, and retrying per file would only repeat the refusal.
35
+
36
+ const MAX_BUFFER = 256 * 1024 * 1024;
37
+
38
+ const fileTextOf = (repoRoot: string, file: string): string => {
39
+ const at = path.resolve(repoRoot, file);
40
+ try {
41
+ return readFileSync(at, "utf8");
42
+ } catch (cause) {
43
+ throw new ReportUnavailable({ kind: "file", source: file, detail: String(cause) });
44
+ }
45
+ };
46
+
47
+ const commandTextSync = (repoRoot: string, command: string): string => {
48
+ const run = spawnSync(command, {
49
+ cwd: repoRoot,
50
+ shell: true,
51
+ encoding: "utf8",
52
+ maxBuffer: MAX_BUFFER,
53
+ stdio: ["ignore", "pipe", "pipe"],
54
+ });
55
+ if (run.error !== undefined) {
56
+ throw new ReportUnavailable({ kind: "command", source: command, detail: run.error.message });
57
+ }
58
+ return run.stdout;
59
+ };
60
+
61
+ const commandText = (repoRoot: string, command: string): Promise<string> =>
62
+ new Promise((resolve, reject) => {
63
+ const child = spawn(command, {
64
+ cwd: repoRoot,
65
+ shell: true,
66
+ stdio: ["ignore", "pipe", "pipe"],
67
+ });
68
+ const chunks: Array<Buffer> = [];
69
+ let size = 0;
70
+ child.stdout.on("data", (chunk: Buffer) => {
71
+ size += chunk.length;
72
+ if (size > MAX_BUFFER) {
73
+ child.kill();
74
+ reject(
75
+ new ReportUnavailable({
76
+ kind: "command",
77
+ source: command,
78
+ detail: `stdout exceeded ${String(MAX_BUFFER)} bytes`,
79
+ }),
80
+ );
81
+ return;
82
+ }
83
+ chunks.push(chunk);
84
+ });
85
+ child.stderr.resume();
86
+ child.on("error", (cause) => {
87
+ reject(new ReportUnavailable({ kind: "command", source: command, detail: cause.message }));
88
+ });
89
+ child.on("close", () => {
90
+ resolve(Buffer.concat(chunks).toString("utf8"));
91
+ });
92
+ });
93
+
94
+ // `work` over `items`, at most `lanes` at a time, answers in order.
95
+ const inLanes = async <A, B>(
96
+ items: ReadonlyArray<A>,
97
+ lanes: number,
98
+ work: (item: A) => Promise<B>,
99
+ ): Promise<ReadonlyArray<B>> => {
100
+ const answers = new Array<B>(items.length);
101
+ let next = 0;
102
+ const lane = async (): Promise<void> => {
103
+ while (next < items.length) {
104
+ const index = next;
105
+ next += 1;
106
+ answers[index] = await work(items[index] as A);
107
+ }
108
+ };
109
+ await Promise.all(Array.from({ length: Math.min(lanes, items.length) }, lane));
110
+ return answers;
111
+ };
112
+
113
+ // One command named as a string and the same one as a list of one are the
114
+ // same report.
115
+ const keyOf = (spec: ReportSpec): string =>
116
+ JSON.stringify([
117
+ spec.file === undefined ? "command" : "file",
118
+ reportSourcesOf(spec),
119
+ spec.format,
120
+ spec.pattern ?? null,
121
+ ]);
122
+
123
+ type Indexed = ReadonlyMap<string, ReadonlyArray<Diagnostic>>;
124
+
125
+ export const makeReportSourceLive = (repoRoot: string): ReportSource => {
126
+ const cache = new Map<string, Indexed | ReportUnavailable>();
127
+ const reading = new Map<string, Promise<void>>();
128
+
129
+ // The outputs, in the order named, as one report.
130
+ const indexOf = (spec: ReportSpec, texts: ReadonlyArray<string>): Indexed =>
131
+ indexByFile(
132
+ uniqueDiagnostics(
133
+ texts.flatMap((text) =>
134
+ parseReport(spec.format, text, { repoRoot, pattern: spec.pattern }),
135
+ ),
136
+ ),
137
+ );
138
+
139
+ const answerSync = (spec: ReportSpec): Indexed | ReportUnavailable => {
140
+ try {
141
+ const texts = reportSourcesOf(spec).map((source) =>
142
+ spec.file === undefined ? commandTextSync(repoRoot, source) : fileTextOf(repoRoot, source),
143
+ );
144
+ return indexOf(spec, texts);
145
+ } catch (cause) {
146
+ if (cause instanceof ReportUnavailable) return cause;
147
+ throw cause;
148
+ }
149
+ };
150
+
151
+ const answer = async (spec: ReportSpec): Promise<Indexed | ReportUnavailable> => {
152
+ try {
153
+ const sources = reportSourcesOf(spec);
154
+ const texts =
155
+ spec.file === undefined
156
+ ? await inLanes(sources, availableParallelism(), (command) =>
157
+ commandText(repoRoot, command),
158
+ )
159
+ : sources.map((file) => fileTextOf(repoRoot, file));
160
+ return indexOf(spec, texts);
161
+ } catch (cause) {
162
+ if (cause instanceof ReportUnavailable) return cause;
163
+ throw cause;
164
+ }
165
+ };
166
+
167
+ const read = (spec: ReportSpec): Promise<void> => {
168
+ const key = keyOf(spec);
169
+ if (cache.has(key)) return Promise.resolve();
170
+ const pending = reading.get(key);
171
+ if (pending !== undefined) return pending;
172
+ const started = answer(spec).then((found) => {
173
+ cache.set(key, found);
174
+ reading.delete(key);
175
+ });
176
+ reading.set(key, started);
177
+ return started;
178
+ };
179
+
180
+ return {
181
+ read,
182
+ diagnosticsOf: (spec, file) => {
183
+ const key = keyOf(spec);
184
+ let found = cache.get(key);
185
+ if (found === undefined) {
186
+ found = answerSync(spec);
187
+ cache.set(key, found);
188
+ }
189
+ if (found instanceof ReportUnavailable) throw found;
190
+ return found.get(file) ?? [];
191
+ },
192
+ };
193
+ };
@@ -0,0 +1,81 @@
1
+ import type { ManifestPath } from "@goodbones/core";
2
+ import * as Result from "effect/Result";
3
+ import * as Schema from "effect/Schema";
4
+ import * as SchemaIssue from "effect/SchemaIssue";
5
+
6
+ import { CampaignsManifest } from "../manifest/spec.js";
7
+
8
+ // Decoding this family's slice of the manifest. The core has already expanded
9
+ // `defs`/`use` and `include` and split these keys off; what arrives here is
10
+ // `{ campaigns?, ledger? }` as written.
11
+ //
12
+ // Every issue, not the first, and each rendered through the `describe` the
13
+ // core handed over — so a campaign's decode error carries the same line,
14
+ // path and `use` trail a tree node's does.
15
+
16
+ export { CampaignsManifest } from "../manifest/spec.js";
17
+ export { DEFAULT_LEDGER_DIR } from "../manifest/spec.js";
18
+
19
+ const decode = Schema.decodeUnknownResult(CampaignsManifest, {
20
+ errors: "all",
21
+ onExcessProperty: "error",
22
+ });
23
+ const flatten = SchemaIssue.makeFormatterStandardSchemaV1();
24
+
25
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
26
+ typeof value === "object" && value !== null && !Array.isArray(value);
27
+
28
+ const isList = (value: unknown): value is ReadonlyArray<unknown> => Array.isArray(value);
29
+
30
+ // The standard-schema formatter flattens the issue tree to `{ path, message }`
31
+ // pairs; a path segment may arrive wrapped as `{ key }`.
32
+ const pathOf = (issue: {
33
+ readonly path?: ReadonlyArray<PropertyKey | { readonly key: PropertyKey }> | undefined;
34
+ }): ManifestPath =>
35
+ (issue.path ?? []).map((segment) => (typeof segment === "object" ? segment.key : segment));
36
+
37
+ // The family's first shape — a list of campaigns each carrying one `detect` —
38
+ // is refused by name, since the decoder's own message for it would be a wall
39
+ // of union issues. A campaign now owns its `objectives`, each of which is
40
+ // what a campaign used to be.
41
+ const legacyCampaignsIssue = (campaigns: unknown): string | null => {
42
+ if (campaigns === undefined) return null;
43
+ if (isList(campaigns)) {
44
+ return (
45
+ "`campaigns` is a list. A campaign is now a map keyed by its id, and what a campaign used " +
46
+ "to be — a detector with a ledger — is one of its `objectives`: write " +
47
+ "`campaigns: { <id>: { objectives: { <objective>: { holdout, match, probes } } } }`, " +
48
+ "with `unit` renamed `holdout` and `detect` renamed `match`."
49
+ );
50
+ }
51
+ if (!isRecord(campaigns)) return null;
52
+ for (const [id, campaign] of Object.entries(campaigns)) {
53
+ if (!isRecord(campaign)) continue;
54
+ if ("detect" in campaign && !("objectives" in campaign)) {
55
+ return (
56
+ `campaign "${id}" carries a \`detect\` and no \`objectives\`. A campaign owns its ` +
57
+ `objectives, each a detector with a ledger: move \`detect\` (now \`match\`), \`unit\` ` +
58
+ `(now \`holdout\`) and \`probes\` under \`objectives: { <objective>: … }\`.`
59
+ );
60
+ }
61
+ }
62
+ return null;
63
+ };
64
+
65
+ export const decodeCampaigns = (
66
+ slice: Readonly<Record<string, unknown>>,
67
+ describe: (path: ManifestPath, detail: string) => string,
68
+ ): Result.Result<CampaignsManifest, ReadonlyArray<string>> => {
69
+ const legacy = legacyCampaignsIssue(slice.campaigns);
70
+ if (legacy !== null) return Result.fail([describe(["campaigns"], legacy)]);
71
+
72
+ const decoded = decode(slice);
73
+ if (Result.isFailure(decoded)) {
74
+ return Result.fail(
75
+ flatten(decoded.failure.issue).issues.map((issue) =>
76
+ describe(pathOf(issue), issue.message),
77
+ ),
78
+ );
79
+ }
80
+ return Result.succeed(decoded.success);
81
+ };