@davidbyoung/software-factory 0.1.2 → 0.1.4

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 (120) hide show
  1. package/README.md +20 -20
  2. package/actions/factory-agent/action.yml +1 -1
  3. package/agents/factory-e2e-reviewer.md +102 -0
  4. package/agents/factory-test-reviewer.md +26 -64
  5. package/agents/factory-ticket-readiness.md +14 -6
  6. package/dist/agent/claude-code.d.mts +3 -0
  7. package/dist/agent/claude-code.mjs +89 -0
  8. package/dist/agent/index.d.mts +2 -0
  9. package/dist/agent/index.mjs +5 -0
  10. package/dist/board.mjs +1 -1
  11. package/dist/cli.mjs +46 -4
  12. package/dist/code-host/github-setup.d.mts +5 -0
  13. package/dist/code-host/github-setup.mjs +133 -0
  14. package/dist/code-host/github.mjs +4 -0
  15. package/dist/code-host/index.d.mts +1 -1
  16. package/dist/code-host/index.mjs +2 -1
  17. package/dist/code-host/types.d.mts +2 -0
  18. package/dist/commands.d.mts +16 -0
  19. package/dist/commands.mjs +129 -0
  20. package/dist/config.d.mts +87 -20
  21. package/dist/config.mjs +344 -89
  22. package/dist/deps.d.mts +7 -4
  23. package/dist/deps.mjs +20 -9
  24. package/dist/doctor.d.mts +9 -16
  25. package/dist/doctor.mjs +71 -68
  26. package/dist/explain.d.mts +4 -0
  27. package/dist/explain.mjs +174 -0
  28. package/dist/guard-commit.d.mts +7 -2
  29. package/dist/guard-commit.mjs +58 -21
  30. package/dist/init.d.mts +12 -14
  31. package/dist/init.mjs +194 -136
  32. package/dist/lib/default-branch.d.mts +35 -0
  33. package/dist/lib/default-branch.mjs +117 -0
  34. package/dist/lib/has-command.d.mts +5 -0
  35. package/dist/lib/has-command.mjs +8 -0
  36. package/dist/lib/listed.d.mts +2 -0
  37. package/dist/lib/listed.mjs +4 -0
  38. package/dist/lib/refined-check.d.mts +7 -0
  39. package/dist/lib/refined-check.mjs +76 -0
  40. package/dist/lib/work-status.d.mts +12 -2
  41. package/dist/lib/work-status.mjs +14 -3
  42. package/dist/parts.d.mts +18 -15
  43. package/dist/parts.mjs +15 -43
  44. package/dist/patch-coverage.d.mts +13 -0
  45. package/dist/patch-coverage.mjs +73 -0
  46. package/dist/poller.d.mts +20 -3
  47. package/dist/poller.mjs +62 -15
  48. package/dist/pr-watch.d.mts +3 -2
  49. package/dist/pr-watch.mjs +22 -5
  50. package/dist/pr.mjs +34 -2
  51. package/dist/remove.d.mts +11 -0
  52. package/dist/remove.mjs +156 -0
  53. package/dist/runner/github-actions.d.mts +1 -1
  54. package/dist/runner/github-actions.mjs +12 -0
  55. package/dist/runner/index.d.mts +1 -1
  56. package/dist/runner/index.mjs +2 -1
  57. package/dist/runner/setup.d.mts +2 -0
  58. package/dist/runner/setup.mjs +22 -0
  59. package/dist/runner/types.d.mts +3 -0
  60. package/dist/runner/types.mjs +0 -2
  61. package/dist/session-start.d.mts +23 -13
  62. package/dist/session-start.mjs +56 -44
  63. package/dist/setup.d.mts +98 -0
  64. package/dist/setup.mjs +3 -0
  65. package/dist/site/servers.d.mts +6 -2
  66. package/dist/site/servers.mjs +14 -9
  67. package/dist/site.mjs +2 -2
  68. package/dist/status.d.mts +4 -4
  69. package/dist/status.mjs +8 -7
  70. package/dist/sync.d.mts +26 -2
  71. package/dist/sync.mjs +177 -13
  72. package/dist/ticket.mjs +19 -1
  73. package/dist/tickets/github-setup.d.mts +2 -0
  74. package/dist/tickets/github-setup.mjs +50 -0
  75. package/dist/tickets/github.mjs +0 -2
  76. package/dist/tickets/index.mjs +2 -1
  77. package/dist/tickets/transport.mjs +1 -3
  78. package/dist/tickets/types.mjs +0 -2
  79. package/dist/tool-names.d.mts +11 -0
  80. package/dist/tool-names.mjs +20 -0
  81. package/dist/tools.d.mts +56 -0
  82. package/dist/tools.mjs +76 -0
  83. package/docs/builds.md +111 -59
  84. package/docs/cli.md +106 -69
  85. package/docs/configuration.md +489 -171
  86. package/docs/design.md +18 -5
  87. package/docs/developing.md +179 -73
  88. package/docs/existing-repo.md +25 -12
  89. package/docs/github/by-hand.md +334 -0
  90. package/docs/github/reference.md +76 -67
  91. package/docs/github/setup.md +105 -286
  92. package/docs/glossary.md +18 -7
  93. package/docs/limitations.md +39 -12
  94. package/docs/plugins.md +305 -56
  95. package/docs/quick-start.md +85 -100
  96. package/docs/removing.md +37 -17
  97. package/docs/skills.md +121 -62
  98. package/docs/updating.md +150 -9
  99. package/docs/workflow.md +23 -19
  100. package/package.json +1 -1
  101. package/poller.Dockerfile +10 -18
  102. package/review.md +20 -11
  103. package/rules.md +36 -30
  104. package/settings.schema.json +313 -69
  105. package/skills/factory-address-review/SKILL.md +50 -30
  106. package/skills/factory-build/SKILL.md +76 -23
  107. package/skills/factory-deps/SKILL.md +46 -21
  108. package/skills/factory-design/SKILL.md +17 -8
  109. package/skills/factory-diagnose/SKILL.md +22 -20
  110. package/skills/factory-mutate/SKILL.md +38 -24
  111. package/skills/factory-refine/SKILL.md +55 -31
  112. package/skills/factory-resolve-conflicts/SKILL.md +76 -34
  113. package/skills/factory-trace/SKILL.md +80 -0
  114. package/workflows/factory-build.yml +6 -5
  115. package/workflows/factory-conflicts.yml +8 -8
  116. package/workflows/factory-deps-auto-merge.yml +3 -2
  117. package/workflows/factory-deps.yml +1 -1
  118. package/workflows/factory-poller.yml +46 -0
  119. package/workflows/factory-review.yml +1 -1
  120. package/skills/factory-address-review/patch-coverage.py +0 -68
@@ -0,0 +1,117 @@
1
+ // The repository's default branch, by name, for everything that used to
2
+ // assume `main`. Each source is asked in turn, and the first to answer wins:
3
+ //
4
+ // 1. In a workflow run, the event's payload, which names it.
5
+ // 2. `origin/HEAD`, which a clone sets.
6
+ // 3. The code host. Its answer is written to `origin/HEAD`, so the next
7
+ // run asks nobody.
8
+ //
9
+ // A checkout with no `origin` remote has nobody to ask, and takes git's own
10
+ // default: `init.defaultBranch`, or `main`. With a remote and no answer,
11
+ // nothing is assumed: the caller stops with CANNOT_TELL.
12
+ //
13
+ // factory default-branch # prints the name, or exits 1 with the fix
14
+ import { spawnSync } from "node:child_process";
15
+ import { readFileSync } from "node:fs";
16
+ /** Git, run in `cwd`, with its errors dropped. */
17
+ export const gitIn = (cwd) => (args) => {
18
+ const r = spawnSync("git", args, {
19
+ cwd,
20
+ encoding: "utf8",
21
+ stdio: ["ignore", "pipe", "ignore"],
22
+ });
23
+ return { ok: r.status === 0, out: r.stdout ?? "" };
24
+ };
25
+ export const CANNOT_TELL = "Could not tell the default branch. Run: git remote set-head origin --auto";
26
+ /** Thrown where a command cannot go on without the default branch. */
27
+ export class NoDefaultBranch extends Error {
28
+ constructor() {
29
+ super(CANNOT_TELL);
30
+ }
31
+ }
32
+ /** The default branch a workflow run's event names, or null outside a run
33
+ * and for an event that names none. */
34
+ function fromEvent(d) {
35
+ const path = d.env.GITHUB_EVENT_PATH;
36
+ if (!path)
37
+ return null;
38
+ try {
39
+ const event = JSON.parse(d.readFile(path));
40
+ const name = event.repository?.default_branch;
41
+ return typeof name === "string" && name ? name : null;
42
+ }
43
+ catch {
44
+ return null;
45
+ }
46
+ }
47
+ /** The default branch's name, or null when no source the caller allows
48
+ * answers. */
49
+ export async function defaultBranch(d, sources = "all") {
50
+ if (sources === "all") {
51
+ const named = fromEvent(d);
52
+ if (named)
53
+ return named;
54
+ }
55
+ const head = d.git(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]);
56
+ const pointed = head.out.trim().replace(/^origin\//, "");
57
+ if (head.ok && pointed)
58
+ return pointed;
59
+ if (sources === "origin-head")
60
+ return null;
61
+ if (!d.git(["remote", "get-url", "origin"]).ok) {
62
+ const own = d.git(["config", "init.defaultBranch"]);
63
+ return (own.ok && own.out.trim()) || "main";
64
+ }
65
+ let name;
66
+ try {
67
+ name = (await d.host()).trim();
68
+ }
69
+ catch {
70
+ return null;
71
+ }
72
+ if (!name)
73
+ return null;
74
+ // Remembered, so the next run asks nobody. It can fail in a shallow or
75
+ // single-branch checkout, where the host is asked again next time.
76
+ d.git(["remote", "set-head", "origin", name]);
77
+ return name;
78
+ }
79
+ /** The default branch's name, for a command that cannot go on without it. */
80
+ export async function needDefaultBranch(d, sources = "all") {
81
+ const name = await defaultBranch(d, sources);
82
+ if (name === null)
83
+ throw new NoDefaultBranch();
84
+ return name;
85
+ }
86
+ /** The sources of the checkout at `root`. The code host is the one the
87
+ * checkout's settings name, and is loaded only when it is asked, so a
88
+ * caller that runs before every command stays quick. */
89
+ export const branchDeps = (root, env = process.env) => ({
90
+ env,
91
+ readFile: (path) => readFileSync(path, "utf8"),
92
+ git: gitIn(root),
93
+ host: async () => {
94
+ const { defaultDeps, loadSettings, reachOf } = await import("../config.mjs");
95
+ const { openCodeHost } = await import("../code-host/index.mjs");
96
+ const { repository, codeHost } = await reachOf(env, () => loadSettings(defaultDeps(root)));
97
+ return openCodeHost(repository, codeHost).defaultBranch();
98
+ },
99
+ });
100
+ export async function main(argv, d, out, err) {
101
+ if (argv.length > 0) {
102
+ err("Usage: factory default-branch");
103
+ return 2;
104
+ }
105
+ const name = await defaultBranch(d);
106
+ if (name === null) {
107
+ err(CANNOT_TELL);
108
+ return 1;
109
+ }
110
+ out(name);
111
+ return 0;
112
+ }
113
+ /** `factory default-branch`. */
114
+ export async function run(argv) {
115
+ const { repoRoot } = await import("../config.mjs");
116
+ return main(argv, branchDeps(repoRoot()), (line) => console.log(line), (line) => console.error(line));
117
+ }
@@ -0,0 +1,5 @@
1
+ /** The line a command that asks questions prints with no terminal to ask
2
+ * in, or null when there is one. */
3
+ export declare const noTerminal: (command: string) => string | null;
4
+ /** Whether a command is installed, by running it with `--version`. */
5
+ export declare const hasCommand: (name: string) => boolean;
@@ -0,0 +1,8 @@
1
+ import { spawnSync } from "node:child_process";
2
+ /** The line a command that asks questions prints with no terminal to ask
3
+ * in, or null when there is one. */
4
+ export const noTerminal = (command) => process.stdin.isTTY
5
+ ? null
6
+ : `factory ${command} asks its questions in a terminal: run it in one.`;
7
+ /** Whether a command is installed, by running it with `--version`. */
8
+ export const hasCommand = (name) => !spawnSync(name, ["--version"], { stdio: "ignore" }).error;
@@ -0,0 +1,2 @@
1
+ /** Items as a sentence lists them: "a", "a and b", "a, b, and c". */
2
+ export declare const listed: (items: readonly string[]) => string;
@@ -0,0 +1,4 @@
1
+ /** Items as a sentence lists them: "a", "a and b", "a, b, and c". */
2
+ export const listed = (items) => items.length < 3
3
+ ? items.join(" and ")
4
+ : `${items.slice(0, -1).join(", ")}, and ${items.at(-1)}`;
@@ -0,0 +1,7 @@
1
+ /** Each part a build needs that the ticket's body lacks, as the line
2
+ * `factory ticket refined` prints for it; none when the ticket is sound.
3
+ * The follow-ups issue is sound whatever its body holds. A ticket without
4
+ * the `spec` label needs its decisions and at least one acceptance
5
+ * criterion. One with it needs its decisions, a spec with a capability
6
+ * and a numbered line, and the builder's design and tasks. */
7
+ export declare function missingParts(body: string, labels: string[]): string[];
@@ -0,0 +1,76 @@
1
+ // Whether a refined ticket's body is one a build can read: the check
2
+ // `factory ticket refined` makes before it posts the marker, for the
3
+ // factory's own refiner as for a plug-in's. It reads headings only, never
4
+ // what a section says.
5
+ const SPEC_LABEL = "spec";
6
+ const FOLLOW_UP_LABEL = "follow-up";
7
+ /** The body's lines outside its fenced code blocks, so a sample body in a
8
+ * code block is not read as the ticket's own. */
9
+ function proseLines(body) {
10
+ const out = [];
11
+ let fence = null;
12
+ for (const line of body.split(/\r?\n/)) {
13
+ const mark = /^\s*(```+|~~~+)/.exec(line)?.[1];
14
+ if (fence === null && mark)
15
+ fence = mark[0];
16
+ else if (fence !== null && mark?.[0] === fence)
17
+ fence = null;
18
+ else if (fence === null)
19
+ out.push(line.trimEnd());
20
+ }
21
+ return out;
22
+ }
23
+ /** The lines under the heading `heading`, or null when the body has no such
24
+ * heading. They end at the next heading of its level or above, or at the
25
+ * body's end when `toEnd` is set. */
26
+ function under(lines, heading, toEnd = false) {
27
+ const at = lines.indexOf(heading);
28
+ if (at === -1)
29
+ return null;
30
+ const rest = lines.slice(at + 1);
31
+ if (toEnd)
32
+ return rest;
33
+ const end = rest.findIndex((line) => /^##? /.test(line));
34
+ return end === -1 ? rest : rest.slice(0, end);
35
+ }
36
+ /** Each part a build needs that the ticket's body lacks, as the line
37
+ * `factory ticket refined` prints for it; none when the ticket is sound.
38
+ * The follow-ups issue is sound whatever its body holds. A ticket without
39
+ * the `spec` label needs its decisions and at least one acceptance
40
+ * criterion. One with it needs its decisions, a spec with a capability
41
+ * and a numbered line, and the builder's design and tasks. */
42
+ export function missingParts(body, labels) {
43
+ if (labels.includes(FOLLOW_UP_LABEL))
44
+ return [];
45
+ const lines = proseLines(body);
46
+ const missing = [];
47
+ const need = (found, part) => {
48
+ if (!found)
49
+ missing.push(`missing: ${part}`);
50
+ };
51
+ need(lines.includes("## Decisions"), "## Decisions");
52
+ if (!labels.includes(SPEC_LABEL)) {
53
+ const criteria = under(lines, "## Acceptance criteria");
54
+ if (criteria === null)
55
+ need(false, "## Acceptance criteria");
56
+ else
57
+ need(criteria.some((line) => /^\s*([-*+]|\d+[.)])\s+\S/.test(line)), "a list item under ## Acceptance criteria");
58
+ return missing;
59
+ }
60
+ const spec = under(lines, "## Spec");
61
+ if (spec === null)
62
+ need(false, "## Spec");
63
+ else {
64
+ need(spec.some((line) => /^### \S.* \((new|modified)\)$/.test(line)), "a ### NAME (new) or ### NAME (modified) heading under ## Spec");
65
+ need(spec.some((line) => /^\d+\.\s+\S/.test(line)), "a numbered line under ## Spec");
66
+ }
67
+ // The tasks are grouped under level-two headings, as OpenSpec's tasks.md
68
+ // has them, so this section runs to the body's end.
69
+ const builder = under(lines, "## For the builder", true);
70
+ if (builder === null)
71
+ need(false, "## For the builder");
72
+ else
73
+ for (const heading of ["### Design", "### Tasks"])
74
+ need(builder.includes(heading), `${heading} under ## For the builder`);
75
+ return missing;
76
+ }
@@ -163,8 +163,18 @@ export declare function actionsRunLink(derived: Pick<Derived, "session" | "runsI
163
163
  export declare function derive(issue: Issue, card: BoardCard | null, pulls: PullRequest[], columns?: Columns): Derived;
164
164
  /** Over 24 hours In progress or In refinement with a session. */
165
165
  export declare function isStale(derived: Derived, now: Date): boolean;
166
- /** Ready for over 5 minutes with no start: the poller should have fired. */
167
- export declare function isNotStarted(derived: Derived, openBlockers: number[], now: Date): boolean;
166
+ /** How long a card may sit in Ready before the poller should have started
167
+ * it, and what the flag asks then; null when nothing starts builds from
168
+ * the board. A schedule runs every few minutes and often late, so it gets
169
+ * its interval and 15 minutes more. */
170
+ export type Watch = {
171
+ minutes: number;
172
+ flag: string;
173
+ };
174
+ export declare function watchOf(trigger: "own-server" | "github-action" | "manual", pollMinutes: number): Watch | null;
175
+ /** Ready for longer than the poller takes, with no start: it should have
176
+ * fired. */
177
+ export declare function isNotStarted(derived: Derived, openBlockers: number[], now: Date, minutes?: number): boolean;
168
178
  /** A claim or start over 24 hours old with no pull request, which the
169
179
  * nightly sync clears. Null when there is nothing to clear. */
170
180
  export declare function toClear(issue: Issue, pulls: PullRequest[], now: Date): {
@@ -389,12 +389,23 @@ export function isStale(derived, now) {
389
389
  return (derived.since !== null &&
390
390
  now.getTime() - Date.parse(derived.since) > 24 * HOUR);
391
391
  }
392
- /** Ready for over 5 minutes with no start: the poller should have fired. */
393
- export function isNotStarted(derived, openBlockers, now) {
392
+ export function watchOf(trigger, pollMinutes) {
393
+ if (trigger === "manual")
394
+ return null;
395
+ if (trigger === "github-action")
396
+ return {
397
+ minutes: pollMinutes + 15,
398
+ flag: "not started, is the factory-poller workflow running?",
399
+ };
400
+ return { minutes: 5, flag: "not started, is the poller running?" };
401
+ }
402
+ /** Ready for longer than the poller takes, with no start: it should have
403
+ * fired. */
404
+ export function isNotStarted(derived, openBlockers, now, minutes = 5) {
394
405
  return (derived.status === "Ready" &&
395
406
  openBlockers.length === 0 &&
396
407
  derived.since !== null &&
397
- now.getTime() - Date.parse(derived.since) > 5 * 60 * 1000);
408
+ now.getTime() - Date.parse(derived.since) > minutes * 60 * 1000);
398
409
  }
399
410
  /** A claim or start over 24 hours old with no pull request, which the
400
411
  * nightly sync clears. Null when there is nothing to clear. */
package/dist/parts.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Settings } from "./config.mts";
2
- import type { TokenNeed } from "./code-host/types.mts";
2
+ import type { Need } from "./setup.mts";
3
3
  export declare const PARTS: readonly ["review", "board", "cloud", "from-board", "auto-merge", "signing"];
4
4
  export type Part = (typeof PARTS)[number];
5
5
  /** Each part's name, and what it gives you, in plain words. */
@@ -16,9 +16,10 @@ export declare const BOARD_TOKEN = "FACTORY_BOARD_TOKEN";
16
16
  export declare const BUILD_TOKEN = "FACTORY_BUILD_TOKEN";
17
17
  export declare const AUTOMERGE_TOKEN = "FACTORY_AUTOMERGE_TOKEN";
18
18
  export declare const SIGNING_KEY = "FACTORY_SIGNING_KEY";
19
- /** The parts on: those with a setting, from the settings; and cloud
20
- * builds and signing, from the secrets set. */
21
- export declare function partsOn(s: Settings | null, secrets: string[]): Set<Part>;
19
+ /** The parts on: those with a setting, from the settings; cloud builds,
20
+ * when every secret the tools read for them is set; and signing, from its
21
+ * key. */
22
+ export declare function partsOn(s: Settings | null, secrets: string[], cloudNeeds: Need[]): Set<Part>;
22
23
  /** The parts chosen, with what one needs turned on alongside it, and a part
23
24
  * turned off when one it needs was just turned off. `before` is what was on,
24
25
  * so turning a needed part off wins over keeping the part that needs it. */
@@ -26,14 +27,16 @@ export declare function resolveParts(before: Set<Part>, chosen: Set<Part>): {
26
27
  parts: Set<Part>;
27
28
  notes: string[];
28
29
  };
29
- /** The secrets the parts read: each entry is met by any one of its names. */
30
- export declare function neededSecrets(parts: Set<Part>): {
31
- part: Part;
32
- anyOf: string[];
33
- }[];
34
- /** The names the one access token is stored under: the board's with a
35
- * board, which builds also read, the build token's without one, and the
36
- * auto-merge token's. */
37
- export declare function tokenNames(parts: Set<Part>): string[];
38
- /** What the access token must be able to do for the parts. */
39
- export declare function tokenNeeds(parts: Set<Part>): TokenNeed[];
30
+ /** A secret read, by the tool whose step reads it, or the factory's own. */
31
+ export type Needed = Need & {
32
+ tool: string;
33
+ };
34
+ /** The secrets read for the parts on, from the tools' secret steps, with
35
+ * the signing key, which is the factory's own. Each is met by any one of
36
+ * its names. */
37
+ export declare function neededSecrets(parts: ReadonlySet<Part>, steps: {
38
+ tool: string;
39
+ step: {
40
+ needs: Need[];
41
+ };
42
+ }[]): Needed[];
package/dist/parts.mjs CHANGED
@@ -22,7 +22,7 @@ export const PART_TEXT = {
22
22
  },
23
23
  "from-board": {
24
24
  name: "Builds that start when you move a card to Ready",
25
- gives: "needs a small server you host",
25
+ gives: "a schedule on GitHub Actions, or a small server you host",
26
26
  },
27
27
  "auto-merge": {
28
28
  name: "Pull requests that merge themselves",
@@ -42,18 +42,17 @@ export const BOARD_TOKEN = "FACTORY_BOARD_TOKEN";
42
42
  export const BUILD_TOKEN = "FACTORY_BUILD_TOKEN";
43
43
  export const AUTOMERGE_TOKEN = "FACTORY_AUTOMERGE_TOKEN";
44
44
  export const SIGNING_KEY = "FACTORY_SIGNING_KEY";
45
- const AGENT = [AGENT_TOKEN, AGENT_KEY];
46
- const ACCESS = [BUILD_TOKEN, BOARD_TOKEN];
47
- /** The parts on: those with a setting, from the settings; and cloud
48
- * builds and signing, from the secrets set. */
49
- export function partsOn(s, secrets) {
45
+ /** The parts on: those with a setting, from the settings; cloud builds,
46
+ * when every secret the tools read for them is set; and signing, from its
47
+ * key. */
48
+ export function partsOn(s, secrets, cloudNeeds) {
50
49
  const has = (names) => names.some((n) => secrets.includes(n));
51
50
  const on = new Set();
52
51
  if (s?.ciReview)
53
52
  on.add("review");
54
53
  if (s?.tickets.board != null)
55
54
  on.add("board");
56
- if (has(AGENT) && has(ACCESS))
55
+ if (cloudNeeds.length && cloudNeeds.every((n) => has(n.anyOf)))
57
56
  on.add("cloud");
58
57
  if (s && s.builds.trigger !== "manual")
59
58
  on.add("from-board");
@@ -98,41 +97,14 @@ export function resolveParts(before, chosen) {
98
97
  }
99
98
  return { parts, notes };
100
99
  }
101
- /** The secrets the parts read: each entry is met by any one of its names. */
102
- export function neededSecrets(parts) {
103
- const out = [];
104
- for (const part of PARTS) {
105
- if (!parts.has(part))
106
- continue;
107
- if (part === "review")
108
- out.push({ part, anyOf: AGENT });
109
- if (part === "cloud")
110
- out.push({ part, anyOf: AGENT }, { part, anyOf: ACCESS });
111
- if (part === "board" || part === "from-board")
112
- out.push({ part, anyOf: [BOARD_TOKEN] });
113
- if (part === "auto-merge")
114
- out.push({ part, anyOf: [AUTOMERGE_TOKEN] });
115
- if (part === "signing")
116
- out.push({ part, anyOf: [SIGNING_KEY] });
117
- }
100
+ /** The secrets read for the parts on, from the tools' secret steps, with
101
+ * the signing key, which is the factory's own. Each is met by any one of
102
+ * its names. */
103
+ export function neededSecrets(parts, steps) {
104
+ const out = steps.flatMap(({ tool, step }) => step.needs
105
+ .filter((n) => n.part === null || parts.has(n.part))
106
+ .map((n) => ({ ...n, tool })));
107
+ if (parts.has("signing"))
108
+ out.push({ part: "signing", anyOf: [SIGNING_KEY], tool: "factory" });
118
109
  return out;
119
110
  }
120
- /** The names the one access token is stored under: the board's with a
121
- * board, which builds also read, the build token's without one, and the
122
- * auto-merge token's. */
123
- export function tokenNames(parts) {
124
- const names = [];
125
- if (parts.has("board"))
126
- names.push(BOARD_TOKEN);
127
- else if (parts.has("cloud"))
128
- names.push(BUILD_TOKEN);
129
- if (parts.has("auto-merge"))
130
- names.push(AUTOMERGE_TOKEN);
131
- return names;
132
- }
133
- /** What the access token must be able to do for the parts. */
134
- export function tokenNeeds(parts) {
135
- return parts.has("board")
136
- ? ["push", "workflows", "board"]
137
- : ["push", "workflows"];
138
- }
@@ -0,0 +1,13 @@
1
+ /** One file's uncovered lines: never run, and run with a branch not taken. */
2
+ export type FileCoverage = {
3
+ miss: Set<number>;
4
+ part: Set<number>;
5
+ };
6
+ /** An lcov report's uncovered lines, by each file's path from `cwd`. */
7
+ export declare function parseLcov(text: string, cwd: string): Map<string, FileCoverage>;
8
+ /** The line numbers a `git diff -U0` adds or changes, by file. */
9
+ export declare function changedLines(diff: string): Map<string, Set<number>>;
10
+ /** The lines to print: a header for each file with a changed line the
11
+ * report shows uncovered, then each such line. `source` gives a file's
12
+ * text, or null when it cannot be read. */
13
+ export declare function uncovered(coverage: Map<string, FileCoverage>, changed: Map<string, Set<number>>, source: (path: string) => string | null): string[];
@@ -0,0 +1,73 @@
1
+ // What `factory pr uncovered` prints: the lines a branch added or changed
2
+ // that its coverage report shows no test ran, or a branch never taken.
3
+ import { relative, resolve } from "node:path";
4
+ /** An lcov report's uncovered lines, by each file's path from `cwd`. */
5
+ export function parseLcov(text, cwd) {
6
+ const files = new Map();
7
+ let current = null;
8
+ for (const raw of text.split("\n")) {
9
+ const line = raw.trim();
10
+ if (line.startsWith("SF:")) {
11
+ current = { miss: new Set(), part: new Set() };
12
+ files.set(relative(cwd, resolve(cwd, line.slice(3))), current);
13
+ }
14
+ else if (current && line.startsWith("DA:")) {
15
+ const [number, hits] = line.slice(3).split(",");
16
+ if (hits === "0")
17
+ current.miss.add(Number(number));
18
+ }
19
+ else if (current && line.startsWith("BRDA:")) {
20
+ const [number, , , taken] = line.slice(5).split(",");
21
+ if (taken === "0" || taken === "-")
22
+ current.part.add(Number(number));
23
+ }
24
+ }
25
+ return files;
26
+ }
27
+ /** The line numbers a `git diff -U0` adds or changes, by file. */
28
+ export function changedLines(diff) {
29
+ const files = new Map();
30
+ let current = null;
31
+ for (const line of diff.split("\n")) {
32
+ if (line.startsWith("+++ ")) {
33
+ // A deleted file's new side is /dev/null, and adds nothing.
34
+ current = line.startsWith("+++ b/") ? new Set() : null;
35
+ if (current)
36
+ files.set(line.slice(6), current);
37
+ continue;
38
+ }
39
+ const hunk = /^@@ .* \+(\d+)(?:,(\d+))? @@/.exec(line);
40
+ if (!hunk || !current)
41
+ continue;
42
+ const start = Number(hunk[1]);
43
+ const count = hunk[2] === undefined ? 1 : Number(hunk[2]);
44
+ for (let n = start; n < start + count; n++)
45
+ current.add(n);
46
+ }
47
+ return files;
48
+ }
49
+ /** The lines to print: a header for each file with a changed line the
50
+ * report shows uncovered, then each such line. `source` gives a file's
51
+ * text, or null when it cannot be read. */
52
+ export function uncovered(coverage, changed, source) {
53
+ const out = [];
54
+ for (const path of [...changed.keys()].sort()) {
55
+ const report = coverage.get(path);
56
+ if (!report)
57
+ continue;
58
+ const lines = changed.get(path);
59
+ const found = [...new Set([...report.miss, ...report.part])]
60
+ .filter((n) => lines.has(n))
61
+ .sort((a, b) => a - b);
62
+ if (!found.length)
63
+ continue;
64
+ const text = source(path)?.split(/\r?\n/);
65
+ out.push(`=== ${path}`);
66
+ for (const n of found) {
67
+ const kind = report.miss.has(n) ? "MISS" : "PART";
68
+ const shown = text?.[n - 1]?.trim().slice(0, 110);
69
+ out.push(shown === undefined ? `${kind} ${n}:` : `${kind} ${n}: ${shown}`);
70
+ }
71
+ }
72
+ return out;
73
+ }
package/dist/poller.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { type Settings } from "./config.mts";
2
3
  import { type Columns, type Board, type RepoSnapshot } from "./lib/work-status.mts";
3
4
  import type { CodeHost } from "./code-host/types.mts";
4
5
  import type { Runner } from "./runner/types.mts";
@@ -17,6 +18,10 @@ export declare function dispatchFailure(error: unknown): {
17
18
  status: string;
18
19
  reason: string;
19
20
  };
21
+ /** When each build started in the past hour went out, read from the start
22
+ * comments on the tickets, so the count holds across a scheduled run and a
23
+ * restart, neither of which keeps it in memory. */
24
+ export declare function recentStarts(issues: RepoSnapshot["issues"], now: number): number[];
20
25
  /** Whether another fire fits in the rolling hour, given when the recent ones
21
26
  * went out. */
22
27
  export declare function capAllows(fired: number[], now: number): boolean;
@@ -31,7 +36,6 @@ export declare function readConfig(env: Record<string, string | undefined>): Con
31
36
  missing: string[];
32
37
  };
33
38
  export type State = {
34
- fired: number[];
35
39
  failuresInARow: number;
36
40
  lastHeartbeat: number;
37
41
  };
@@ -57,5 +61,18 @@ export declare function sources(d: Deps, c: Config): {
57
61
  /** One minute's work. It never throws: every failure is logged, and a failed
58
62
  * read counts toward the slower retry. */
59
63
  export declare function tick(d: Deps, state: State): Promise<void>;
60
- /** `factory poller`. */
61
- export declare function run(): Promise<number>;
64
+ /** What one scheduled pass reads from the settings: the board's number,
65
+ * and the column that approves a build, each null when the settings have
66
+ * none. */
67
+ export type OnceSettings = {
68
+ board: number | null;
69
+ ready: string | null;
70
+ };
71
+ export declare function onceSettings(s: Settings): OnceSettings;
72
+ /** One pass, for a scheduled run: the board and the Ready column come from
73
+ * the settings, the board's owner is the repository's, and only the token
74
+ * comes from the environment. 1 when it read nothing or could not read. */
75
+ export declare function once(s: OnceSettings, d: Deps): Promise<number>;
76
+ /** `factory poller`, and `factory poller --once`. The settings are loaded
77
+ * only for `--once`, since the hosted poller has no checkout. */
78
+ export declare function run(argv?: string[]): Promise<number>;