@niwa-labs/workflow 0.0.0-stage → 0.0.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 (114) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -2
  3. package/package.json +55 -4
  4. package/src/actions.ts +408 -0
  5. package/src/agent.ts +211 -0
  6. package/src/ask.ts +84 -0
  7. package/src/bridge.ts +710 -0
  8. package/src/catalogue.ts +74 -0
  9. package/src/check.ts +880 -0
  10. package/src/child.ts +1378 -0
  11. package/src/cockpit/actions.ts +128 -0
  12. package/src/cockpit/catalogue-actions.ts +97 -0
  13. package/src/cockpit/dashboard.tsx +134 -0
  14. package/src/cockpit/groups.ts +45 -0
  15. package/src/cockpit/index.tsx +50 -0
  16. package/src/cockpit/launch.tsx +136 -0
  17. package/src/cockpit/plan.ts +101 -0
  18. package/src/cockpit/run-commands.tsx +102 -0
  19. package/src/cockpit/run-page.tsx +140 -0
  20. package/src/cockpit/run-summary.ts +16 -0
  21. package/src/cockpit/runs.tsx +73 -0
  22. package/src/cockpit/statistics.tsx +176 -0
  23. package/src/cockpit/track.tsx +159 -0
  24. package/src/cockpit/workflows.tsx +383 -0
  25. package/src/describe-cache.ts +122 -0
  26. package/src/describe.ts +174 -0
  27. package/src/guide.ts +213 -0
  28. package/src/index.ts +2813 -0
  29. package/src/inspect.ts +235 -0
  30. package/src/installation.ts +90 -0
  31. package/src/language.ts +217 -0
  32. package/src/records-entry.ts +63 -0
  33. package/src/records.ts +574 -0
  34. package/src/registry.ts +250 -0
  35. package/src/revise.ts +89 -0
  36. package/src/runner.ts +339 -0
  37. package/src/script-types.ts +549 -0
  38. package/src/stats.ts +460 -0
  39. package/src/status.ts +512 -0
  40. package/src/store.ts +259 -0
  41. package/src/subscriptions.ts +102 -0
  42. package/src/waiting.ts +61 -0
  43. package/test/cockpit-actions.test.ts +20 -0
  44. package/test/cockpit-groups.test.ts +42 -0
  45. package/test/cockpit-plan.test.ts +118 -0
  46. package/test/fixtures/branches-host.ts +58 -0
  47. package/test/fixtures/branches-script.js +23 -0
  48. package/test/fixtures/bridge-host.ts +23 -0
  49. package/test/fixtures/bridge-keys-script.js +15 -0
  50. package/test/fixtures/bridge-wait-script.js +8 -0
  51. package/test/fixtures/cancel-host.ts +58 -0
  52. package/test/fixtures/cancel-script.js +12 -0
  53. package/test/fixtures/crash-host.ts +61 -0
  54. package/test/fixtures/crash-script.js +14 -0
  55. package/test/fixtures/ping-records.ts +20 -0
  56. package/test/fixtures/revision-host.ts +57 -0
  57. package/test/fixtures/revision-script.js +11 -0
  58. package/test/fixtures/running-host.ts +61 -0
  59. package/test/fixtures/running-script.js +12 -0
  60. package/test/fixtures/with-data/flow.ts +2 -0
  61. package/test/fixtures/with-data/table.json +1 -0
  62. package/test/git-repo.ts +45 -0
  63. package/test/git-worktree.test.ts +413 -0
  64. package/test/question-format.test.ts +279 -0
  65. package/test/workflow-agent-caps.test.ts +91 -0
  66. package/test/workflow-agent-lifetime.test.ts +93 -0
  67. package/test/workflow-agent-options.test.ts +216 -0
  68. package/test/workflow-agent-schema.test.ts +386 -0
  69. package/test/workflow-agent-typings.test.ts +241 -0
  70. package/test/workflow-ask-resume.test.ts +145 -0
  71. package/test/workflow-branch-keys.test.ts +133 -0
  72. package/test/workflow-bridge-inproc.test.ts +230 -0
  73. package/test/workflow-bridge.test.ts +141 -0
  74. package/test/workflow-builtin-candidate.test.ts +230 -0
  75. package/test/workflow-builtin.test.ts +197 -0
  76. package/test/workflow-cancel-agent.test.ts +144 -0
  77. package/test/workflow-cancel.test.ts +166 -0
  78. package/test/workflow-chain.test.ts +84 -0
  79. package/test/workflow-check-options.test.ts +144 -0
  80. package/test/workflow-check-uses.test.ts +148 -0
  81. package/test/workflow-check.test.ts +282 -0
  82. package/test/workflow-contract.test.ts +295 -0
  83. package/test/workflow-crash-resume.test.ts +141 -0
  84. package/test/workflow-deadline.test.ts +102 -0
  85. package/test/workflow-describe-cache.test.ts +240 -0
  86. package/test/workflow-directory.test.ts +73 -0
  87. package/test/workflow-emits.test.ts +261 -0
  88. package/test/workflow-group-keys.test.ts +72 -0
  89. package/test/workflow-harnesses.test.ts +262 -0
  90. package/test/workflow-inspect.test.ts +152 -0
  91. package/test/workflow-language-types.test.ts +148 -0
  92. package/test/workflow-manifest.test.ts +93 -0
  93. package/test/workflow-manual-pause.test.ts +116 -0
  94. package/test/workflow-owners.test.ts +154 -0
  95. package/test/workflow-pause.test.ts +200 -0
  96. package/test/workflow-phases.test.ts +445 -0
  97. package/test/workflow-plugin-entries.test.ts +59 -0
  98. package/test/workflow-record-refs.test.ts +310 -0
  99. package/test/workflow-replay.test.ts +206 -0
  100. package/test/workflow-resume-once.test.ts +188 -0
  101. package/test/workflow-revision.test.ts +216 -0
  102. package/test/workflow-scope.test.ts +505 -0
  103. package/test/workflow-script-defaults.test.ts +119 -0
  104. package/test/workflow-script-error.test.ts +228 -0
  105. package/test/workflow-script-libraries.test.ts +88 -0
  106. package/test/workflow-script-packages.test.ts +119 -0
  107. package/test/workflow-stdout.test.ts +89 -0
  108. package/test/workflow-stop.test.ts +299 -0
  109. package/test/workflow-store.test.ts +220 -0
  110. package/test/workflow-visible.test.ts +136 -0
  111. package/test/workflow-wait-version.test.ts +54 -0
  112. package/test/workflow-wait.test.ts +224 -0
  113. package/test/workflow-waiting-agent.test.ts +64 -0
  114. package/tsconfig.json +15 -0
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The actions a script may use: what the host describes, narrowed to what the caller may call.
3
+ *
4
+ * `callableBy` is the one place that decides which actions a caller can call: the ones its grant
5
+ * lets it call, an action it must ask for included. The check and the guide both ask it.
6
+ */
7
+
8
+ import { z } from "zod";
9
+
10
+ import { JsonSchemaValue, actionName, actionRef } from "@niwa-labs/core";
11
+ import { isOwnerGrant, matches } from "@niwa-labs/core";
12
+ import type { ActionContext, ActionDescription, ActionName, Grant, JsonSchema } from "@niwa-labs/core";
13
+ import type { OptionsBlock } from "@niwa-labs/harness";
14
+ import { harnessList } from "@niwa-labs/sessions/records";
15
+
16
+ import type { HarnessBlock } from "./script-types.ts";
17
+
18
+ /** niwa's description of itself, as far as a script needs it. */
19
+ const describeRef = actionRef("niwa.describe", {
20
+ description: "What niwa is: its actions and its record types, with their schemas.",
21
+ input: z.object({}),
22
+ output: z.object({
23
+ actions: z.array(
24
+ z.object({
25
+ name: z.string(),
26
+ description: z.string(),
27
+ recovery: z.enum(["repeat", "repeat-with-key", "do-not-repeat"]),
28
+ effect: z.enum(["read", "record", "write", "irreversible"]),
29
+ world: z.enum(["niwa", "local", "external"]).optional(),
30
+ idempotent: z.boolean().optional(),
31
+ destructive: z.boolean().optional(),
32
+ input: JsonSchemaValue,
33
+ output: JsonSchemaValue,
34
+ }),
35
+ ),
36
+ }),
37
+ });
38
+
39
+ /**
40
+ * The actions the caller may call, out of the ones the host serves: those a call rule of its grant
41
+ * covers, for some target at least. An irreversible action is covered only by a rule that names it.
42
+ */
43
+ export function callableBy(
44
+ actions: readonly Pick<ActionDescription, "name" | "effect">[],
45
+ rights: Grant | "unlimited",
46
+ ): ActionName[] {
47
+ if (rights === "unlimited" || isOwnerGrant(rights)) return actions.map((action) => action.name);
48
+ return actions
49
+ .filter(({ name, effect }) =>
50
+ rights.rules.some((held) => held.verb === "call" && matches(held.name, name) && (effect !== "irreversible" || String(held.name) === String(name))),
51
+ )
52
+ .map((action) => action.name);
53
+ }
54
+
55
+ /** The options block of each harness, by its name, with its schema, out of what `harness.list` answers: a harness with none is not in it. */
56
+ function blocksByHarness(answer: { items: ReadonlyArray<{ name: string; block?: OptionsBlock; options?: JsonSchema }> }): Map<string, HarnessBlock> {
57
+ return new Map(
58
+ answer.items.flatMap((one) => (one.block === undefined || one.options === undefined ? [] : [[one.name, { block: one.block, schema: one.options }] as const])),
59
+ );
60
+ }
61
+
62
+ /**
63
+ * The actions of the host, the ones of them the caller may call, and the options block of each harness as the sessions
64
+ * module binds it. A host with no sessions module binds no harness, so no harness takes a block.
65
+ */
66
+ export async function catalogueFor(
67
+ ctx: ActionContext,
68
+ held: Grant | "unlimited",
69
+ ): Promise<{ actions: ActionDescription[]; callable: ActionName[]; rights: Grant | "unlimited"; blocks: ReadonlyMap<string, HarnessBlock> }> {
70
+ const described = await ctx.step(describeRef, {});
71
+ const actions = described.actions.map((action): ActionDescription => ({ ...action, name: actionName(action.name) }));
72
+ const blocks = actions.some((action) => action.name === harnessList.name) ? blocksByHarness(await ctx.step(harnessList, {})) : new Map<string, HarnessBlock>();
73
+ return { actions, callable: callableBy(actions, held), rights: held, blocks };
74
+ }