@seasb/metafab 2.0.0-next.1 → 2.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 (127) hide show
  1. package/README.md +33 -3
  2. package/dist/cli.js +31 -3
  3. package/dist/cli.js.map +1 -1
  4. package/dist/commands/automations-list.d.ts +2 -0
  5. package/dist/commands/automations-list.js +81 -0
  6. package/dist/commands/automations-list.js.map +1 -0
  7. package/dist/commands/automations-plan.d.ts +2 -0
  8. package/dist/commands/automations-plan.js +146 -0
  9. package/dist/commands/automations-plan.js.map +1 -0
  10. package/dist/commands/automations-prompt.d.ts +2 -0
  11. package/dist/commands/automations-prompt.js +123 -0
  12. package/dist/commands/automations-prompt.js.map +1 -0
  13. package/dist/commands/automations-stub.d.ts +2 -0
  14. package/dist/commands/automations-stub.js +125 -0
  15. package/dist/commands/automations-stub.js.map +1 -0
  16. package/dist/commands/automations-sync.d.ts +2 -0
  17. package/dist/commands/automations-sync.js +104 -0
  18. package/dist/commands/automations-sync.js.map +1 -0
  19. package/dist/commands/create.js +18 -10
  20. package/dist/commands/create.js.map +1 -1
  21. package/dist/core/adopt.d.ts +4 -3
  22. package/dist/core/adopt.js +17 -10
  23. package/dist/core/adopt.js.map +1 -1
  24. package/dist/core/automations/account.d.ts +7 -0
  25. package/dist/core/automations/account.js +27 -0
  26. package/dist/core/automations/account.js.map +1 -0
  27. package/dist/core/automations/amp-schedule.d.ts +50 -0
  28. package/dist/core/automations/amp-schedule.js +272 -0
  29. package/dist/core/automations/amp-schedule.js.map +1 -0
  30. package/dist/core/automations/cron.d.ts +15 -0
  31. package/dist/core/automations/cron.js +75 -0
  32. package/dist/core/automations/cron.js.map +1 -0
  33. package/dist/core/automations/desired.d.ts +28 -0
  34. package/dist/core/automations/desired.js +95 -0
  35. package/dist/core/automations/desired.js.map +1 -0
  36. package/dist/core/automations/index.d.ts +10 -0
  37. package/dist/core/automations/index.js +11 -0
  38. package/dist/core/automations/index.js.map +1 -0
  39. package/dist/core/automations/load.d.ts +9 -0
  40. package/dist/core/automations/load.js +141 -0
  41. package/dist/core/automations/load.js.map +1 -0
  42. package/dist/core/automations/orca.d.ts +126 -0
  43. package/dist/core/automations/orca.js +513 -0
  44. package/dist/core/automations/orca.js.map +1 -0
  45. package/dist/core/automations/problems.d.ts +32 -0
  46. package/dist/core/automations/problems.js +110 -0
  47. package/dist/core/automations/problems.js.map +1 -0
  48. package/dist/core/automations/prompt.d.ts +38 -0
  49. package/dist/core/automations/prompt.js +211 -0
  50. package/dist/core/automations/prompt.js.map +1 -0
  51. package/dist/core/automations/stub.d.ts +40 -0
  52. package/dist/core/automations/stub.js +96 -0
  53. package/dist/core/automations/stub.js.map +1 -0
  54. package/dist/core/automations/types.d.ts +53 -0
  55. package/dist/core/automations/types.js +3 -0
  56. package/dist/core/automations/types.js.map +1 -0
  57. package/dist/core/automations/validate.d.ts +45 -0
  58. package/dist/core/automations/validate.js +594 -0
  59. package/dist/core/automations/validate.js.map +1 -0
  60. package/dist/core/build/agents.d.ts +16 -3
  61. package/dist/core/build/agents.js +125 -5
  62. package/dist/core/build/agents.js.map +1 -1
  63. package/dist/core/build/plan.js +51 -28
  64. package/dist/core/build/plan.js.map +1 -1
  65. package/dist/core/check/drift.js +5 -5
  66. package/dist/core/check/drift.js.map +1 -1
  67. package/dist/core/config/parse.js +7 -0
  68. package/dist/core/config/parse.js.map +1 -1
  69. package/dist/core/config/types.d.ts +5 -0
  70. package/dist/core/config/validate.d.ts +2 -1
  71. package/dist/core/config/validate.js +40 -1
  72. package/dist/core/config/validate.js.map +1 -1
  73. package/dist/core/edit/create.d.ts +14 -6
  74. package/dist/core/edit/create.js +124 -34
  75. package/dist/core/edit/create.js.map +1 -1
  76. package/dist/core/edit/map.js +3 -3
  77. package/dist/core/edit/map.js.map +1 -1
  78. package/dist/core/edit/pipeline.js +13 -9
  79. package/dist/core/edit/pipeline.js.map +1 -1
  80. package/dist/core/edit/skill.d.ts +5 -0
  81. package/dist/core/edit/skill.js +9 -0
  82. package/dist/core/edit/skill.js.map +1 -1
  83. package/dist/core/env/declarations.d.ts +2 -1
  84. package/dist/core/env/declarations.js +4 -3
  85. package/dist/core/env/declarations.js.map +1 -1
  86. package/dist/core/env/render.d.ts +12 -0
  87. package/dist/core/env/render.js +51 -0
  88. package/dist/core/env/render.js.map +1 -1
  89. package/dist/core/env/validate.js +76 -5
  90. package/dist/core/env/validate.js.map +1 -1
  91. package/dist/core/factory/discovery.d.ts +12 -2
  92. package/dist/core/factory/discovery.js +119 -2
  93. package/dist/core/factory/discovery.js.map +1 -1
  94. package/dist/core/factory/factory-yaml.d.ts +2 -1
  95. package/dist/core/factory/factory-yaml.js +4 -3
  96. package/dist/core/factory/factory-yaml.js.map +1 -1
  97. package/dist/core/factory/format.d.ts +26 -0
  98. package/dist/core/factory/format.js +34 -0
  99. package/dist/core/factory/format.js.map +1 -0
  100. package/dist/core/factory/load.d.ts +2 -2
  101. package/dist/core/factory/load.js +129 -16
  102. package/dist/core/factory/load.js.map +1 -1
  103. package/dist/core/factory/types.d.ts +24 -1
  104. package/dist/core/factory/validate.d.ts +31 -5
  105. package/dist/core/factory/validate.js +155 -23
  106. package/dist/core/factory/validate.js.map +1 -1
  107. package/dist/core/hooks/script.d.ts +22 -0
  108. package/dist/core/hooks/script.js +38 -2
  109. package/dist/core/hooks/script.js.map +1 -1
  110. package/dist/core/import/run.js +6 -2
  111. package/dist/core/import/run.js.map +1 -1
  112. package/dist/core/publish/package-json.d.ts +2 -1
  113. package/dist/core/publish/package-json.js.map +1 -1
  114. package/dist/core/publish/readme.js +13 -3
  115. package/dist/core/publish/readme.js.map +1 -1
  116. package/dist/core/publish/stage.js +18 -3
  117. package/dist/core/publish/stage.js.map +1 -1
  118. package/dist/core/sources/npm.js +3 -2
  119. package/dist/core/sources/npm.js.map +1 -1
  120. package/dist/core/sources/types.d.ts +2 -1
  121. package/dist/core/sources/validate.js +3 -2
  122. package/dist/core/sources/validate.js.map +1 -1
  123. package/dist/core/summary.js +49 -13
  124. package/dist/core/summary.js.map +1 -1
  125. package/package.json +2 -2
  126. package/resources/metafab-automations-skill/SKILL.md +53 -0
  127. package/resources/metafab-skill/SKILL.md +13 -6
@@ -0,0 +1,110 @@
1
+ import { normalizeProblemPath } from "../../util/fs.js";
2
+ import { FORMAT_FEATURES } from "../factory/format.js";
3
+ import { TARGETS } from "./types.js";
4
+ const AUTOMATIONS_DIR = ".factory/automations";
5
+ const AUTOMATION_FILE = ".factory/automations/";
6
+ /** The `automations` commands need a format that has `feature`. */
7
+ export function needsFormat(feature) {
8
+ return {
9
+ level: "error",
10
+ file: ".factory/automations",
11
+ message: `.factory/automations/ needs format: ${FORMAT_FEATURES[feature]}`,
12
+ fix: `Set format: ${FORMAT_FEATURES[feature]} in .factory/factory.yaml`,
13
+ };
14
+ }
15
+ /** A name that is not an automation's. */
16
+ export function missingAutomation(name) {
17
+ return {
18
+ level: "error",
19
+ file: `.factory/automations/${name}`,
20
+ message: `No automation named ${name}`,
21
+ fix: "Run metafab automations list to see the automations",
22
+ };
23
+ }
24
+ /** A disabled automation never runs, so no prompt is rendered for it. */
25
+ export function turnedOff(automation) {
26
+ return {
27
+ level: "error",
28
+ file: automation.file,
29
+ message: `${automation.name} is turned off`,
30
+ fix: `Set enabled: true in ${automation.file}`,
31
+ };
32
+ }
33
+ /** A `--target` that is not one of this version's targets. */
34
+ export function unknownTarget(value) {
35
+ return {
36
+ level: "error",
37
+ file: normalizeProblemPath("metafab"),
38
+ message: `Unknown target "${value}"`,
39
+ fix: `Use one of: ${TARGETS.join(", ")}`,
40
+ };
41
+ }
42
+ /** A target's account is not set, so the prompt cannot check who runs it. */
43
+ export function missingAccount(target) {
44
+ const setting = target === "amp" ? "AMP_USER_EMAIL" : "git config user.email";
45
+ return {
46
+ level: "error",
47
+ file: normalizeProblemPath("metafab"),
48
+ message: `Cannot tell which account this run acts as: ${setting} is not set`,
49
+ fix: target === "amp"
50
+ ? "Set AMP_USER_EMAIL to the account that runs this automation"
51
+ : "Set user.email in Git config, such as git config user.email <email>",
52
+ };
53
+ }
54
+ /** The automation is registered in another person's account. */
55
+ export function otherOwner(automation, account) {
56
+ return {
57
+ level: "error",
58
+ file: automation.file,
59
+ message: `${automation.name} belongs to ${automation.owner}, and this run acts as ${account}`,
60
+ fix: `Set owner to "${account}" in ${automation.file}, or run it as ${automation.owner}`,
61
+ };
62
+ }
63
+ /**
64
+ * `plan amp` needs `AMP_USER_EMAIL`: outside an Amp thread nothing else names
65
+ * the account that would hold the schedules.
66
+ */
67
+ export function missingAmpAccount() {
68
+ return {
69
+ level: "error",
70
+ file: normalizeProblemPath("metafab"),
71
+ message: "The Amp account is unknown: AMP_USER_EMAIL is not set, so this is not an Amp thread",
72
+ fix: "Run metafab automations plan amp inside an Amp thread",
73
+ };
74
+ }
75
+ /** Neither `--branch` nor `origin/HEAD` named a trunk. */
76
+ export function noTrunk() {
77
+ return {
78
+ level: "error",
79
+ file: normalizeProblemPath("metafab"),
80
+ message: "Cannot tell which branch this run switches to",
81
+ fix: "Pass --branch <trunk>",
82
+ };
83
+ }
84
+ /** A `--branch` that `git check-ref-format --branch` refuses. */
85
+ export function badBranch(branch) {
86
+ return {
87
+ level: "error",
88
+ file: normalizeProblemPath("metafab"),
89
+ message: `${branch} is not a branch name`,
90
+ fix: "Pass --branch <trunk> with a branch name, such as main",
91
+ };
92
+ }
93
+ /** Every error `check` reports in `.factory/automations/`. */
94
+ export function automationErrors(factory) {
95
+ return factory.problems.filter((problem) => problem.level === "error" &&
96
+ (problem.file === AUTOMATIONS_DIR || problem.file.startsWith(AUTOMATION_FILE)));
97
+ }
98
+ /**
99
+ * The repository has no checkout Orca can run in: a bare repository, or a
100
+ * linked worktree whose main checkout does not hold the factory.
101
+ */
102
+ export function noMainCheckout() {
103
+ return {
104
+ level: "error",
105
+ file: normalizeProblemPath("metafab"),
106
+ message: "This repository has no main checkout for Orca to run in",
107
+ fix: "Run the sync from a normal clone",
108
+ };
109
+ }
110
+ //# sourceMappingURL=problems.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"problems.js","sourceRoot":"","sources":["../../../src/core/automations/problems.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAGvD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAGrC,MAAM,eAAe,GAAG,sBAAsB,CAAC;AAC/C,MAAM,eAAe,GAAG,uBAAuB,CAAC;AAEhD,mEAAmE;AACnE,MAAM,UAAU,WAAW,CAAC,OAAsB;IAChD,OAAO;QACL,KAAK,EAAE,OAAO;QACd,IAAI,EAAE,sBAAsB;QAC5B,OAAO,EAAE,uCAAuC,eAAe,CAAC,OAAO,CAAC,EAAE;QAC1E,GAAG,EAAE,eAAe,eAAe,CAAC,OAAO,CAAC,2BAA2B;KACxE,CAAC;AACJ,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,OAAO;QACL,KAAK,EAAE,OAAO;QACd,IAAI,EAAE,wBAAwB,IAAI,EAAE;QACpC,OAAO,EAAE,uBAAuB,IAAI,EAAE;QACtC,GAAG,EAAE,qDAAqD;KAC3D,CAAC;AACJ,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,SAAS,CAAC,UAAsB;IAC9C,OAAO;QACL,KAAK,EAAE,OAAO;QACd,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,OAAO,EAAE,GAAG,UAAU,CAAC,IAAI,gBAAgB;QAC3C,GAAG,EAAE,wBAAwB,UAAU,CAAC,IAAI,EAAE;KAC/C,CAAC;AACJ,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,aAAa,CAAC,KAAa;IACzC,OAAO;QACL,KAAK,EAAE,OAAO;QACd,IAAI,EAAE,oBAAoB,CAAC,SAAS,CAAC;QACrC,OAAO,EAAE,mBAAmB,KAAK,GAAG;QAC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AAS,CAAC,MAAc;IACtC,OAAO;QACL,KAAK,EAAE,OAAO;QACd,IAAI,EAAE,oBAAoB,CAAC,SAAS,CAAC;QACrC,OAAO,EAAE,GAAG,MAAM,uBAAuB;QACzC,GAAG,EAAE,wDAAwD;KAC9D,CAAC;AACJ,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,OAAO,OAAO,CAAC,QAAQ,CAAC,MAAM,CAC5B,CAAC,OAAO,EAAE,EAAE,CACV,OAAO,CAAC,KAAK,KAAK,OAAO;QACzB,CAAC,OAAO,CAAC,IAAI,KAAK,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,CACjF,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc;IAC5B,OAAO;QACL,KAAK,EAAE,OAAO;QACd,IAAI,EAAE,oBAAoB,CAAC,SAAS,CAAC;QACrC,OAAO,EAAE,yDAAyD;QAClE,GAAG,EAAE,kCAAkC;KACxC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { Problem } from "../../errors.js";
2
+ import type { EnvSelection } from "../env/types.js";
3
+ import type { Factory } from "../factory/types.js";
4
+ import type { Automation } from "./types.js";
5
+ /**
6
+ * The automations that run after `name`: enabled ones whose `after` names it,
7
+ * sorted by name. A disabled automation is never started by a chain, so it is
8
+ * not listed.
9
+ */
10
+ export declare function afterOf(name: string, list: readonly Automation[]): string[];
11
+ /**
12
+ * The text of one run, as the spec's The prompt says: the fixed first line, the
13
+ * rendered run instructions when the factory has them, the automation's name as
14
+ * a heading with the rendered body directly below it, and the line naming the
15
+ * automations that start when this run ends. It ends with one newline.
16
+ *
17
+ * A selection that leaves the body with no text is an error: the run has
18
+ * nothing to do, and the caller reports it as `has nothing to do in this
19
+ * environment`.
20
+ */
21
+ export declare function renderPrompt(factory: Factory, automation: Automation, selection: EnvSelection, after: readonly string[]): Promise<string>;
22
+ /**
23
+ * The problems that stop a prompt: errors in a file the prompt depends on,
24
+ * plus a loop that passes through the automation. Every other problem is
25
+ * ignored, so one broken automation never stops another run.
26
+ *
27
+ * The dependency set is the automation's own file, `run.md`, the automation.md
28
+ * of each enabled automation on its after line (the ones the prompt tells the
29
+ * run to start), and every file reached by an import while the body or `run.md`
30
+ * is rendered. An imported file is kept under every name it is known by: the
31
+ * route the validator reports problems under, the name on the import node, and
32
+ * the realpath name the import cache records, which differ when a symbolic link
33
+ * is on the route.
34
+ *
35
+ * `check`'s scanners report what the loader cannot, such as an unresolved
36
+ * conflict marker, so their problems for the same files stop the prompt too.
37
+ */
38
+ export declare function promptProblems(name: string, factory: Factory): Promise<Problem[]>;
@@ -0,0 +1,211 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { ExitCode, MetafabError } from "../../errors.js";
4
+ import { toLF } from "../../util/text.js";
5
+ import { dedupeReadErrors, textContent } from "../build/skill.js";
6
+ import { scanFactoryFiles } from "../check/markers.js";
7
+ import { parseMarkdown } from "../env/directives.js";
8
+ import { createImportCache } from "../env/imports.js";
9
+ import { markdownParts } from "../env/markdown.js";
10
+ import { renderDocument, rendersContent } from "../env/render.js";
11
+ import { automationFile } from "./validate.js";
12
+ /** The prompt's fixed first line, and the last line when a run follows. */
13
+ const FIRST_LINE = "Run the factory automation";
14
+ const AFTER_LINE = "When this run ends, start each automation that runs after it, as the run instructions say:";
15
+ /**
16
+ * The automations that run after `name`: enabled ones whose `after` names it,
17
+ * sorted by name. A disabled automation is never started by a chain, so it is
18
+ * not listed.
19
+ */
20
+ export function afterOf(name, list) {
21
+ return list
22
+ .filter((automation) => automation.enabled &&
23
+ automation.triggers.some((trigger) => trigger.kind === "after" && trigger.names.includes(name)))
24
+ .map((automation) => automation.name)
25
+ .sort();
26
+ }
27
+ /**
28
+ * The text of one run, as the spec's The prompt says: the fixed first line, the
29
+ * rendered run instructions when the factory has them, the automation's name as
30
+ * a heading with the rendered body directly below it, and the line naming the
31
+ * automations that start when this run ends. It ends with one newline.
32
+ *
33
+ * A selection that leaves the body with no text is an error: the run has
34
+ * nothing to do, and the caller reports it as `has nothing to do in this
35
+ * environment`.
36
+ */
37
+ export async function renderPrompt(factory, automation, selection, after) {
38
+ const variables = factory.factoryYaml?.env ?? [];
39
+ const imports = createImportCache(factory.dir);
40
+ const runText = await renderRunFile(factory, variables, selection, imports);
41
+ const body = await parseBody(automation, variables, imports);
42
+ const ownHeading = `# ${automation.name}`;
43
+ if (!rendersContent(body, { variables, selection }, ownHeading)) {
44
+ const message = `${automation.name} has nothing to do in this environment`;
45
+ throw new MetafabError(message, {
46
+ exitCode: ExitCode.Failed,
47
+ problems: [
48
+ {
49
+ level: "error",
50
+ file: automation.file,
51
+ line: automation.bodyLine,
52
+ message,
53
+ fix: "Add text for this environment, or select one where the body has some",
54
+ },
55
+ ],
56
+ });
57
+ }
58
+ const sections = [
59
+ `${FIRST_LINE} \`${automation.name}\` unattended. Nobody is watching this session.`,
60
+ ];
61
+ const run = section(runText);
62
+ if (run !== "")
63
+ sections.push(run);
64
+ // The template puts the name on its own line directly above the body, so a
65
+ // body that starts with the same heading leaves it out rather than show it
66
+ // twice, and the blank lines around it are trimmed rather than kept.
67
+ const text = section(withoutOwnHeading(renderDocument(body, { variables, selection }), ownHeading));
68
+ sections.push(`${ownHeading}\n${text}`);
69
+ if (after.length > 0) {
70
+ const names = after.map((name) => `\`${name}\``).join(", ");
71
+ sections.push(`${AFTER_LINE} ${names}.`);
72
+ }
73
+ return `${sections.join("\n\n")}\n`;
74
+ }
75
+ /**
76
+ * The problems that stop a prompt: errors in a file the prompt depends on,
77
+ * plus a loop that passes through the automation. Every other problem is
78
+ * ignored, so one broken automation never stops another run.
79
+ *
80
+ * The dependency set is the automation's own file, `run.md`, the automation.md
81
+ * of each enabled automation on its after line (the ones the prompt tells the
82
+ * run to start), and every file reached by an import while the body or `run.md`
83
+ * is rendered. An imported file is kept under every name it is known by: the
84
+ * route the validator reports problems under, the name on the import node, and
85
+ * the realpath name the import cache records, which differ when a symbolic link
86
+ * is on the route.
87
+ *
88
+ * `check`'s scanners report what the loader cannot, such as an unresolved
89
+ * conflict marker, so their problems for the same files stop the prompt too.
90
+ */
91
+ export async function promptProblems(name, factory) {
92
+ const automation = factory.automations?.list.find((entry) => entry.name === name);
93
+ if (automation === undefined)
94
+ return [];
95
+ const dependencies = await promptDependencies(factory, automation);
96
+ const scanned = await scanFactoryFiles(factory.root);
97
+ return dedupeReadErrors([...factory.problems, ...scanned]).filter((problem) => problem.level === "error" &&
98
+ (dependencies.has(problem.file) || cycleThrough(problem, name)));
99
+ }
100
+ /** True for a loop problem whose loop names the automation. */
101
+ function cycleThrough(problem, name) {
102
+ if (!problem.message.startsWith("cycle: "))
103
+ return false;
104
+ return problem.message.slice("cycle: ".length).split(" → ").includes(name);
105
+ }
106
+ async function promptDependencies(factory, automation) {
107
+ const dependencies = new Set([automation.file]);
108
+ const automations = factory.automations;
109
+ if (automations === undefined)
110
+ return dependencies;
111
+ if (automations.runFile !== undefined)
112
+ dependencies.add(automations.runFile);
113
+ // The after line names the automations that start when this run ends, so
114
+ // those are the automation.md files the prompt depends on: not the ones this
115
+ // automation itself runs after.
116
+ for (const name of afterOf(automation.name, automations.list)) {
117
+ dependencies.add(automationFile(name));
118
+ }
119
+ const variables = factory.factoryYaml?.env ?? [];
120
+ const imports = createImportCache(factory.dir);
121
+ const collect = async (file, route, text, firstLine) => {
122
+ const document = await parseMarkdown(text, { file, firstLine, variables, imports, route });
123
+ for (const node of importNodes(document.nodes)) {
124
+ if (node.file !== undefined)
125
+ dependencies.add(node.file);
126
+ }
127
+ };
128
+ const run = await readRunFile(factory);
129
+ if (run !== undefined && automations.runFile !== undefined) {
130
+ await collect(automations.runFile, "automations/run.md", run.text, run.firstLine);
131
+ }
132
+ await collect(automation.file, `automations/${automation.name}/automation.md`, automation.body, automation.bodyLine);
133
+ for (const loaded of imports.files.values())
134
+ dependencies.add(loaded.file);
135
+ return dependencies;
136
+ }
137
+ /** Every import node below these nodes, nested imports included. */
138
+ function* importNodes(nodes) {
139
+ for (const node of nodes) {
140
+ if (node.kind === "text")
141
+ continue;
142
+ if (node.kind === "import") {
143
+ yield node;
144
+ yield* importNodes(node.children);
145
+ continue;
146
+ }
147
+ const children = node.kind === "when" ? node.children : node.branches.flatMap((branch) => branch.children);
148
+ yield* importNodes(children);
149
+ }
150
+ }
151
+ async function parseBody(automation, variables, imports) {
152
+ return parseMarkdown(automation.body, {
153
+ file: automation.file,
154
+ firstLine: automation.bodyLine,
155
+ variables,
156
+ imports,
157
+ route: `automations/${automation.name}/automation.md`,
158
+ });
159
+ }
160
+ /** `run.md` without its frontmatter, as an import is read. */
161
+ async function readRunFile(factory) {
162
+ let data;
163
+ try {
164
+ data = await readFile(path.join(factory.dir, "automations", "run.md"));
165
+ }
166
+ catch {
167
+ return undefined;
168
+ }
169
+ const decoded = textContent(data);
170
+ if (decoded === undefined)
171
+ return undefined;
172
+ const text = toLF(decoded.startsWith("\uFEFF") ? decoded.slice(1) : decoded);
173
+ const parts = markdownParts(text);
174
+ return { text: parts.body, firstLine: parts.firstLine };
175
+ }
176
+ /** `run.md` rendered like an import: directives applied, frontmatter gone. */
177
+ async function renderRunFile(factory, variables, selection, imports) {
178
+ const run = await readRunFile(factory);
179
+ if (run === undefined)
180
+ return "";
181
+ const document = await parseMarkdown(run.text, {
182
+ file: factory.automations?.runFile ?? ".factory/automations/run.md",
183
+ firstLine: run.firstLine,
184
+ variables,
185
+ imports,
186
+ route: "automations/run.md",
187
+ });
188
+ return renderDocument(document, { variables, selection });
189
+ }
190
+ /**
191
+ * The rendered body without its first non-blank line when that line is the
192
+ * heading the prompt prints, so the name appears once. Any other heading, such
193
+ * as `# Other` or `## <name>`, is the body's own and is kept.
194
+ */
195
+ function withoutOwnHeading(text, ownHeading) {
196
+ const lines = text.split("\n");
197
+ const index = lines.findIndex((line) => line.trim() !== "");
198
+ const first = lines[index];
199
+ if (first === undefined || first.trimEnd() !== ownHeading)
200
+ return text;
201
+ lines.splice(index, 1);
202
+ return lines.join("\n");
203
+ }
204
+ /**
205
+ * A section of the prompt: no leading blank lines, and no trailing whitespace,
206
+ * so the blank line between sections is the only one there.
207
+ */
208
+ function section(text) {
209
+ return text.replace(/^\n+/, "").trimEnd();
210
+ }
211
+ //# sourceMappingURL=prompt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../../src/core/automations/prompt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEzD,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAIlE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,2EAA2E;AAC3E,MAAM,UAAU,GAAG,4BAA4B,CAAC;AAChD,MAAM,UAAU,GACd,4FAA4F,CAAC;AAE/F;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,IAAY,EAAE,IAA2B;IAC/D,OAAO,IAAI;SACR,MAAM,CACL,CAAC,UAAU,EAAE,EAAE,CACb,UAAU,CAAC,OAAO;QAClB,UAAU,CAAC,QAAQ,CAAC,IAAI,CACtB,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IA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AI,CAAC,CAAC;AAC1B,CAAC;AAED;;;GAGG;AACH,SAAS,OAAO,CAAC,IAAY;IAC3B,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,40 @@
1
+ import type { Automation, Target } from "./types.js";
2
+ export interface StubInput {
3
+ /** `name` in `factory.yaml`, for the marker. */
4
+ factoryName: string;
5
+ automation: Automation;
6
+ target: Target;
7
+ /** The trunk the run switches to, without `origin/`. */
8
+ trunk: string;
9
+ /** The factory root relative to the repository root, POSIX, `.` at the root. */
10
+ factoryRelDir: string;
11
+ cliVersion: string;
12
+ }
13
+ /** The first line of a stub: the marker sync uses to recognise its registrations. */
14
+ export declare function stubMarker(name: string, factoryName: string): string;
15
+ /**
16
+ * The text a target registers for one automation, as the spec's The stub says.
17
+ * It holds no part of the body: the run renders the prompt when it starts, so a
18
+ * body edit takes effect without a sync.
19
+ *
20
+ * For `checkout: main` the clean-checkout and detach steps are left out for
21
+ * Orca, which runs in the main checkout as it is. Amp always has them, because
22
+ * an orb is its own clone.
23
+ */
24
+ export declare function renderStub(input: StubInput): string;
25
+ /**
26
+ * `git check-ref-format --branch` accepts the value, so a stub's `git switch
27
+ * --detach origin/<trunk>` is never given a branch name Git refuses.
28
+ */
29
+ export declare function isBranchName(repoRoot: string, value: string): Promise<boolean>;
30
+ /**
31
+ * The remote's default branch: `git symbolic-ref --short
32
+ * refs/remotes/origin/HEAD`, without `origin/`. Undefined when Git cannot say,
33
+ * as outside a repository or when no remote HEAD is set.
34
+ */
35
+ export declare function defaultTrunk(repoRoot: string): Promise<string | undefined>;
36
+ /**
37
+ * The factory root relative to the repository root, POSIX and `.` at the root,
38
+ * as the Git hooks name it: `git rev-parse --show-prefix` in the factory root.
39
+ */
40
+ export declare function relativeFactoryDir(root: string): Promise<string>;
@@ -0,0 +1,96 @@
1
+ import { runGit } from "../../util/git.js";
2
+ import { toLF } from "../../util/text.js";
3
+ import { metafabInvocation, shQuote } from "../hooks/script.js";
4
+ /** The first line of a stub: the marker sync uses to recognise its registrations. */
5
+ export function stubMarker(name, factoryName) {
6
+ return `Run the factory automation \`${name}\` from the \`${factoryName}\` factory.`;
7
+ }
8
+ const CLEAN_STEP = "Run `git status --porcelain --untracked-files=all`. If it prints anything or fails, report that this run needs a clean checkout, and stop.";
9
+ const PROMPT_STEP = "If it fails, report what it printed, and stop. Otherwise follow what it printed: those are this run's instructions.";
10
+ /**
11
+ * The text a target registers for one automation, as the spec's The stub says.
12
+ * It holds no part of the body: the run renders the prompt when it starts, so a
13
+ * body edit takes effect without a sync.
14
+ *
15
+ * For `checkout: main` the clean-checkout and detach steps are left out for
16
+ * Orca, which runs in the main checkout as it is. Amp always has them, because
17
+ * an orb is its own clone.
18
+ */
19
+ export function renderStub(input) {
20
+ const { factoryName, automation, target, trunk, factoryRelDir, cliVersion } = input;
21
+ const directory = shQuote(factoryRelDir);
22
+ const mainCli = `"$main"/${directory}/node_modules/.bin/metafab`;
23
+ const args = `automations prompt ${automation.name} --target ${target}`;
24
+ const lookup = metafabInvocation(mainCli, cliVersion, args).join("; ");
25
+ const prompt = `Run \`(cd "$(git rev-parse --show-toplevel)"/${directory} && ${lookup}; fi)\`. ${PROMPT_STEP}`;
26
+ const steps = [];
27
+ if (automation.checkout === "worktree" || target === "amp") {
28
+ steps.push(CLEAN_STEP);
29
+ steps.push(`Run \`git fetch origin && git switch --detach origin/${shQuote(trunk)}\`.`);
30
+ }
31
+ steps.push(prompt);
32
+ const lines = [
33
+ stubMarker(automation.name, factoryName),
34
+ "",
35
+ ...steps.map((step, index) => `${index + 1}. ${step}`),
36
+ ];
37
+ return `${lines.join("\n")}\n`;
38
+ }
39
+ /**
40
+ * `git check-ref-format --branch` accepts the value, so a stub's `git switch
41
+ * --detach origin/<trunk>` is never given a branch name Git refuses.
42
+ */
43
+ export async function isBranchName(repoRoot, value) {
44
+ try {
45
+ const result = await runGit(["check-ref-format", "--branch", value], { cwd: repoRoot });
46
+ return result.exitCode === 0;
47
+ }
48
+ catch {
49
+ return false;
50
+ }
51
+ }
52
+ /**
53
+ * The remote's default branch: `git symbolic-ref --short
54
+ * refs/remotes/origin/HEAD`, without `origin/`. Undefined when Git cannot say,
55
+ * as outside a repository or when no remote HEAD is set.
56
+ */
57
+ export async function defaultTrunk(repoRoot) {
58
+ let stdout;
59
+ try {
60
+ const result = await runGit(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], {
61
+ cwd: repoRoot,
62
+ });
63
+ if (result.exitCode !== 0)
64
+ return undefined;
65
+ stdout = result.stdout;
66
+ }
67
+ catch {
68
+ return undefined;
69
+ }
70
+ const value = stdout.trim();
71
+ if (value === "")
72
+ return undefined;
73
+ return value.startsWith("origin/") ? value.slice("origin/".length) : value;
74
+ }
75
+ /**
76
+ * The factory root relative to the repository root, POSIX and `.` at the root,
77
+ * as the Git hooks name it: `git rev-parse --show-prefix` in the factory root.
78
+ */
79
+ export async function relativeFactoryDir(root) {
80
+ let stdout;
81
+ try {
82
+ const result = await runGit(["rev-parse", "--show-prefix"], { cwd: root });
83
+ if (result.exitCode !== 0)
84
+ return ".";
85
+ stdout = result.stdout;
86
+ }
87
+ catch {
88
+ return ".";
89
+ }
90
+ // Remove only what Git adds: the terminating newline, and the separator after
91
+ // the last directory. Trimming would eat a leading space in the directory's
92
+ // own name.
93
+ const prefix = toLF(stdout).replace(/\n$/, "").replace(/\/+$/, "");
94
+ return prefix === "" ? "." : prefix;
95
+ }
96
+ //# sourceMappingURL=stub.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stub.js","sourceRoot":"","sources":["../../../src/core/automations/stub.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAehE,qFAAq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CAAC;QACH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,CAAC,WAAW,EAAE,eAAe,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3E,IAAI,MAAM,CAAC,QAAQ,KAAK,CAAC;YAAE,OAAO,GAAG,CAAC;QACtC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IACzB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAC;IACb,CAAC;IAED,8EAA8E;IAC9E,4EAA4E;IAC5E,YAAY;IACZ,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACnE,OAAO,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AACtC,CAAC"}
@@ -0,0 +1,53 @@
1
+ import type { Problem } from "../../errors.js";
2
+ /** One way an automation starts: a schedule, or the end of another automation. */
3
+ export type Trigger = {
4
+ kind: "schedule";
5
+ cron: string;
6
+ tz: string;
7
+ line: number;
8
+ } | {
9
+ kind: "after";
10
+ names: string[];
11
+ line: number;
12
+ };
13
+ export interface Automation {
14
+ name: string;
15
+ /** `.factory/automations/<name>/automation.md`, always with `/`. */
16
+ file: string;
17
+ /** `"*"` or an email, trimmed. */
18
+ owner: string;
19
+ description?: string;
20
+ enabled: boolean;
21
+ checkout: "worktree" | "main";
22
+ triggers: Trigger[];
23
+ /** Raw, LF, below the frontmatter. */
24
+ body: string;
25
+ /** First body line, for directive problems. */
26
+ bodyLine: number;
27
+ /** Line of the `triggers` key, where a loop names this automation. */
28
+ triggersLine: number;
29
+ }
30
+ /** Where one `automation.md` body starts, for checking it on its own. */
31
+ export interface AutomationBody {
32
+ name: string;
33
+ file: string;
34
+ /** First body line, for directive problems. */
35
+ bodyLine: number;
36
+ }
37
+ export interface Automations {
38
+ /** Sorted by name. */
39
+ list: Automation[];
40
+ /**
41
+ * Every `automation.md` body whose frontmatter parsed, in name order, so its
42
+ * directives and emptiness are checked even when the frontmatter yields no
43
+ * runnable automation.
44
+ */
45
+ bodies: AutomationBody[];
46
+ /** `.factory/automations/run.md`, when the factory has one. */
47
+ runFile?: string;
48
+ problems: Problem[];
49
+ }
50
+ /** A harness Metafab can register automations in. */
51
+ export type Target = "orca" | "amp";
52
+ /** The targets this version supports, in the order help and errors list them. */
53
+ export declare const TARGETS: readonly Target[];
@@ -0,0 +1,3 @@
1
+ /** The targets this version supports, in the order help and errors list them. */
2
+ export const TARGETS = ["orca", "amp"];
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/automations/types.ts"],"names":[],"mappings":"AAkDA,iFAAiF;AACjF,MAAM,CAAC,MAAM,OAAO,GAAsB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { Problem } from "../../errors.js";
2
+ import type { FrontmatterResult } from "../../util/frontmatter.js";
3
+ import type { Automation, AutomationBody } from "./types.js";
4
+ /** The parsed `automation.md` frontmatter, body and all. */
5
+ export type AutomationFrontmatter = Extract<FrontmatterResult, {
6
+ ok: true;
7
+ }>;
8
+ export declare const AUTOMATION_KEYS: readonly ["owner", "description", "enabled", "checkout", "triggers"];
9
+ export declare function automationFile(name: string): string;
10
+ /** Rule 2's name check for an automation directory, with rule 5's fix. */
11
+ export declare function automationNameProblems(name: string): Problem[];
12
+ /** Rule 3: a file directly in `automations/` that is not `run.md`. */
13
+ export declare function automationFileProblem(file: string): Problem;
14
+ /** Rule 2: a directory directly in `automations/` with no `automation.md`. */
15
+ export declare function automationMissingFileProblem(name: string): Problem;
16
+ /** Rule 1: a `SKILL.md` anywhere below `automations/`. */
17
+ export declare function automationSkillFileProblem(file: string): Problem;
18
+ export interface AutomationParse {
19
+ automation?: Automation;
20
+ /** The body to check on its own, whatever the frontmatter says. */
21
+ body: AutomationBody;
22
+ problems: Problem[];
23
+ }
24
+ /**
25
+ * Reads one `automation.md` frontmatter into an `Automation`, reporting every
26
+ * frontmatter, trigger and cron problem. Rules that need the other automations
27
+ * are in `automationProblems`. The automation is returned when its `owner` is
28
+ * valid, so a broken cron or trigger still leaves it a named `after` target.
29
+ * The body is returned either way, so it is checked even when there is no
30
+ * runnable automation.
31
+ */
32
+ export declare function parseAutomation(name: string, parsed: AutomationFrontmatter): AutomationParse;
33
+ /**
34
+ * Rules that need every automation: an `after` name is another automation and
35
+ * not itself, an owner chain runs in its parent's account, and no `after`
36
+ * chain loops.
37
+ */
38
+ export declare function automationProblems(automations: readonly Automation[]): Problem[];
39
+ /**
40
+ * Each loop in the `after` graph once, as the path of an automation's children:
41
+ * an edge runs from a named parent to the automation that starts after it.
42
+ * Depth-first from each automation in name order, keeping the first path that
43
+ * returns to the start.
44
+ */
45
+ export declare function findCycles(automations: readonly Automation[]): string[][];