@krysoworks/kryso 0.0.2 → 2.0.0-rc.1

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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +146 -6
  3. package/bin/kryso +31 -0
  4. package/bin/ship-it-status +14 -0
  5. package/bin/ship-ledger +14 -0
  6. package/bin/ship-loop +14 -0
  7. package/bin/ship-pass +14 -0
  8. package/bin/warden +14 -0
  9. package/dist/answer.d.ts +38 -0
  10. package/dist/answer.js +48 -0
  11. package/dist/bytes.d.ts +5 -0
  12. package/dist/bytes.js +35 -0
  13. package/dist/cleanup.d.ts +1 -0
  14. package/dist/cleanup.js +629 -0
  15. package/dist/commands.d.ts +48 -0
  16. package/dist/commands.js +293 -0
  17. package/dist/completions.d.ts +24 -0
  18. package/dist/completions.js +239 -0
  19. package/dist/daemon.d.ts +15 -0
  20. package/dist/daemon.js +23 -0
  21. package/dist/diagnose.d.ts +47 -0
  22. package/dist/diagnose.js +314 -0
  23. package/dist/doctor.d.ts +1 -0
  24. package/dist/doctor.js +241 -0
  25. package/dist/env.d.ts +9 -0
  26. package/dist/env.js +17 -0
  27. package/dist/evidence.d.ts +39 -0
  28. package/dist/evidence.js +327 -0
  29. package/dist/home.d.ts +3 -0
  30. package/dist/home.js +27 -0
  31. package/dist/install.d.ts +59 -0
  32. package/dist/install.js +137 -0
  33. package/dist/isolate.d.ts +28 -0
  34. package/dist/isolate.js +402 -0
  35. package/dist/leftovers.d.ts +5 -0
  36. package/dist/leftovers.js +156 -0
  37. package/dist/machine.d.ts +6 -0
  38. package/dist/machine.js +185 -0
  39. package/dist/palette.d.ts +147 -0
  40. package/dist/palette.js +167 -0
  41. package/dist/plan.d.ts +1 -0
  42. package/dist/plan.js +133 -0
  43. package/dist/proc.d.ts +23 -0
  44. package/dist/proc.js +23 -0
  45. package/dist/read.d.ts +1 -0
  46. package/dist/read.js +297 -0
  47. package/dist/readiness.d.ts +5 -0
  48. package/dist/readiness.js +64 -0
  49. package/dist/remote.d.ts +6 -0
  50. package/dist/remote.js +85 -0
  51. package/dist/self-update.d.ts +1 -0
  52. package/dist/self-update.js +224 -0
  53. package/dist/semver.d.ts +7 -0
  54. package/dist/semver.js +42 -0
  55. package/dist/style.d.ts +71 -0
  56. package/dist/style.js +187 -0
  57. package/dist/update.d.ts +47 -0
  58. package/dist/update.js +190 -0
  59. package/dist/work.d.ts +25 -0
  60. package/dist/work.js +449 -0
  61. package/dist/workflows.d.ts +4 -0
  62. package/dist/workflows.js +73 -0
  63. package/docker/entrypoint.sh +44 -0
  64. package/docker/runner.Dockerfile +39 -0
  65. package/package.json +55 -22
  66. package/scripts/install-skills.sh +607 -0
  67. package/skills/code-review/SKILL.md +231 -0
  68. package/skills/kryso/SKILL.md +107 -0
  69. package/skills/kryso/golden/help.rounded.ansi +23 -0
  70. package/skills/kryso/golden/help.square.ansi +23 -0
  71. package/skills/kryso/golden/help.txt +23 -0
  72. package/skills/kryso/golden/table-narrow.json +11 -0
  73. package/skills/kryso/golden/table-narrow.rounded.ansi +4 -0
  74. package/skills/kryso/golden/table-narrow.square.ansi +4 -0
  75. package/skills/kryso/golden/table.ansi +5 -0
  76. package/skills/kryso/golden/table.json +11 -0
  77. package/skills/kryso/ledger.sh +2146 -0
  78. package/skills/kryso/palette.json +57 -0
  79. package/skills/kryso/reference/resuming.md +30 -0
  80. package/skills/kryso/reference/routing.md +23 -0
  81. package/skills/kryso/reference/stage-1-claim.md +79 -0
  82. package/skills/kryso/reference/stage-2-build.md +133 -0
  83. package/skills/kryso/reference/stage-3-prove.md +51 -0
  84. package/skills/kryso/reference/stage-4-pr.md +76 -0
  85. package/skills/kryso/reference/stage-5-reconcile.md +81 -0
  86. package/skills/kryso/reference/stage-6-merge-or-park.md +73 -0
  87. package/skills/kryso/reference/stage-7-close-out.md +62 -0
  88. package/skills/kryso/reference/terminal.md +7 -0
  89. package/skills/kryso/ship-it-status.sh +1503 -0
  90. package/skills/kryso/ui.py +274 -0
  91. package/skills/kryso/ui.sh +609 -0
  92. package/skills/kryso-build/SKILL.md +168 -0
  93. package/skills/kryso-shift/SKILL.md +167 -0
  94. package/skills/kryso-shift/archive.py +2784 -0
  95. package/skills/kryso-shift/archive_checker.py +462 -0
  96. package/skills/kryso-shift/archive_checker_lambda.py +155 -0
  97. package/skills/kryso-shift/archive_health.py +364 -0
  98. package/skills/kryso-shift/archive_launchd.py +140 -0
  99. package/skills/kryso-shift/archive_remote.py +990 -0
  100. package/skills/kryso-shift/archiving-checker.md +140 -0
  101. package/skills/kryso-shift/archiving-keep.md +115 -0
  102. package/skills/kryso-shift/archiving-recovery.md +112 -0
  103. package/skills/kryso-shift/archiving-remote.md +166 -0
  104. package/skills/kryso-shift/archiving.md +146 -0
  105. package/skills/kryso-shift/coverage.schema.json +225 -0
  106. package/skills/kryso-shift/deadline.sh +254 -0
  107. package/skills/kryso-shift/feedback.schema.json +35 -0
  108. package/skills/kryso-shift/feedback.sh +228 -0
  109. package/skills/kryso-shift/history-manifest.schema.json +331 -0
  110. package/skills/kryso-shift/keep-after.sh +55 -0
  111. package/skills/kryso-shift/measure.sh +611 -0
  112. package/skills/kryso-shift/merge-ready.sh +207 -0
  113. package/skills/kryso-shift/outcome.schema.json +16 -0
  114. package/skills/kryso-shift/pass-rules.md +8 -0
  115. package/skills/kryso-shift/profile.schema.json +42 -0
  116. package/skills/kryso-shift/reader.sh +692 -0
  117. package/skills/kryso-shift/reads.sh +151 -0
  118. package/skills/kryso-shift/reference/allowance.md +21 -0
  119. package/skills/kryso-shift/reference/driver.md +95 -0
  120. package/skills/kryso-shift/reference/measuring.md +67 -0
  121. package/skills/kryso-shift/reference/outputs.md +21 -0
  122. package/skills/kryso-shift/reference/pass-script.md +115 -0
  123. package/skills/kryso-shift/reference/reconcile-and-merge.md +55 -0
  124. package/skills/kryso-shift/reference/second-reader.md +9 -0
  125. package/skills/kryso-shift/reference/sweeps.md +83 -0
  126. package/skills/kryso-shift/repair.schema.json +14 -0
  127. package/skills/kryso-shift/report-template.html +679 -0
  128. package/skills/kryso-shift/report.py +389 -0
  129. package/skills/kryso-shift/report_fixture.py +65 -0
  130. package/skills/kryso-shift/reporting.md +58 -0
  131. package/skills/kryso-shift/result.sh +91 -0
  132. package/skills/kryso-shift/runner.sh +1178 -0
  133. package/skills/kryso-shift/ship-loop.sh +3977 -0
  134. package/skills/kryso-shift/ship-pass.sh +4987 -0
  135. package/skills/kryso-shift/steps.jq +19 -0
  136. package/skills/kryso-shift/workflow.mjs +830 -0
  137. package/skills/kryso-shift/workflows/default.json +22 -0
  138. package/skills/kryso-shift/workflows/ralph.json +13 -0
  139. package/skills/kryso-watch/SKILL.md +100 -0
  140. package/skills/kryso-watch/answer.schema.json +23 -0
  141. package/skills/kryso-watch/base-refs.sh +44 -0
  142. package/skills/kryso-watch/board.sh +58 -0
  143. package/skills/kryso-watch/reference/config.md +23 -0
  144. package/skills/kryso-watch/reference/outputs.md +30 -0
  145. package/skills/kryso-watch/reference/report.md +29 -0
  146. package/skills/kryso-watch/reference/scheduler.md +32 -0
  147. package/skills/kryso-watch/reference/settlement.md +29 -0
  148. package/skills/kryso-watch/reference/shift.md +44 -0
  149. package/skills/kryso-watch/reference/shim-limits.md +22 -0
  150. package/skills/kryso-watch/reference/triage.md +26 -0
  151. package/skills/kryso-watch/schema.jq +40 -0
  152. package/skills/kryso-watch/settle.schema.json +36 -0
  153. package/skills/kryso-watch/stage-common.sh +187 -0
  154. package/skills/kryso-watch/stages/report.sh +525 -0
  155. package/skills/kryso-watch/stages/settle.sh +722 -0
  156. package/skills/kryso-watch/stages/shift.sh +466 -0
  157. package/skills/kryso-watch/stages/triage.sh +379 -0
  158. package/skills/kryso-watch/tracker.sh +27 -0
  159. package/skills/kryso-watch/triage.schema.json +77 -0
  160. package/skills/kryso-watch/warden.launchd.plist +50 -0
  161. package/skills/kryso-watch/warden.schema.json +98 -0
  162. package/skills/kryso-watch/warden.sh +1004 -0
  163. package/bin/kryso.js +0 -11
@@ -0,0 +1,293 @@
1
+ // The kryso command table: which bash driver or script each subcommand runs, and the help
2
+ // that lists them. Bash stays the engine (ADR 0022). A subcommand adds nothing of its own to
3
+ // the driver's arguments, so `kryso pass 42 --base main` is `ship-pass.sh 42 --base main`, and
4
+ // the driver's own help, exit codes and output are the command's.
5
+ import { existsSync, readFileSync } from "node:fs";
6
+ import path from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+ import { helpAnswer, refusal } from "./answer.js";
9
+ import { EX_UNAVAILABLE, EX_USAGE } from "./env.js";
10
+ import { IMAGE } from "./isolate.js";
11
+ import { completions } from "./completions.js";
12
+ import { checkoutRecord } from "./install.js";
13
+ import { remote } from "./remote.js";
14
+ export const isAnswer = (t) => "text" in t;
15
+ /** A subcommand's target among the drivers at home, when it can start: kryso resolves it and every
16
+ * file it names is there. A driver that is missing is the install group's error in kryso doctor. */
17
+ export function startable(home, name, args, env) {
18
+ const target = COMMANDS.find((c) => c.name === name)?.target(home, args, env);
19
+ if (!target || isAnswer(target))
20
+ return undefined;
21
+ return target.head.filter((w) => path.isAbsolute(w)).every((f) => existsSync(f)) ? target : undefined;
22
+ }
23
+ export function version() {
24
+ const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
25
+ return pkg.version;
26
+ }
27
+ const bash = (home, script, before = [], verbs = 0) => ({
28
+ program: "bash",
29
+ head: [path.join(home, script), ...before],
30
+ verbs,
31
+ script,
32
+ });
33
+ const python = (home, script) => ({
34
+ program: "python3",
35
+ head: [path.join(home, script)],
36
+ verbs: 0,
37
+ script,
38
+ });
39
+ /** A command kryso implements itself: a script in dist/ that node runs in kryso's process. */
40
+ const nodeScript = (file) => ({
41
+ program: "node",
42
+ head: [path.join(path.dirname(fileURLToPath(import.meta.url)), file)],
43
+ verbs: 0,
44
+ script: `dist/${file}`,
45
+ });
46
+ const ISOLATE_USAGE = `Usage: kryso isolate build [docker build options]
47
+
48
+ build build the kryso-runner image from docker/runner.Dockerfile, tagged
49
+ with this package's version, for \`kryso shift --isolate docker\`
50
+ and \`kryso pass --isolate docker\`
51
+ `;
52
+ /** \`docker build\` of the runner image, run by the launcher so its output and exit are docker's.
53
+ * The image's files are beside this package (the npm package, or a checkout install's copy in
54
+ * ~/.agents/kryso), or at the repository's root in the workspace. */
55
+ function buildImage(home) {
56
+ const beside = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "docker");
57
+ const dir = [beside, path.join(home, "docker")].find((d) => existsSync(path.join(d, "runner.Dockerfile")));
58
+ if (!dir) {
59
+ return refusal(EX_UNAVAILABLE, "kryso isolate", `no docker/runner.Dockerfile beside this kryso or in ${home}`, { hint: "reinstall it from a checkout or the npm package" });
60
+ }
61
+ const dockerfile = path.join(dir, "runner.Dockerfile");
62
+ return {
63
+ program: "docker",
64
+ head: ["build", "-t", `${IMAGE}:${version()}`, "-f", dockerfile, dir],
65
+ verbs: 1,
66
+ script: "docker/runner.Dockerfile",
67
+ };
68
+ }
69
+ const INSTALLER = "scripts/install-skills.sh";
70
+ /** install-skills.sh: beside the drivers in a package or the workspace, or else in the checkout a
71
+ * checkout install came from. That install's copy in ~/.agents/kryso carries bin/ and dist/ but
72
+ * no scripts/, and its drivers are the installed skills, so home is ~/.agents, which has none. */
73
+ function installer(home) {
74
+ if (existsSync(path.join(home, INSTALLER)))
75
+ return bash(home, INSTALLER, [], 1);
76
+ const record = checkoutRecord();
77
+ if (record && existsSync(path.join(record.checkout, INSTALLER)))
78
+ return bash(record.checkout, INSTALLER, [], 1);
79
+ if (record) {
80
+ return refusal(EX_UNAVAILABLE, "kryso skills", `no ${INSTALLER} in the checkout ${record.checkout} that installed this kryso`, {
81
+ hint: "clone the repository there again, or run scripts/install-skills.sh from another checkout",
82
+ });
83
+ }
84
+ return refusal(EX_UNAVAILABLE, "kryso skills", `no ${INSTALLER} in ${home}`, { hint: "reinstall it from a checkout or the npm package" });
85
+ }
86
+ const SKILLS_USAGE = `Usage: kryso skills install [options] [skill ...]
87
+
88
+ install install this package's skills for the current user, and link the
89
+ commands onto PATH (install-skills.sh). Its options are in
90
+ \`kryso skills install --help\`
91
+ `;
92
+ export const COMMANDS = [
93
+ {
94
+ name: "pass",
95
+ group: "work",
96
+ summary: "one issue, from claimed to merged or parked",
97
+ runs: "ship-pass.sh",
98
+ target: (home) => bash(home, "skills/kryso-shift/ship-pass.sh"),
99
+ isolates: true,
100
+ },
101
+ {
102
+ name: "shift",
103
+ group: "work",
104
+ summary: "passes over a queue, a workset or an epic",
105
+ runs: "ship-loop.sh",
106
+ target: (home) => bash(home, "skills/kryso-shift/ship-loop.sh"),
107
+ isolates: true,
108
+ },
109
+ {
110
+ name: "watch",
111
+ group: "work",
112
+ summary: "one watch of a tracker, or the schedule's watches",
113
+ runs: "warden.sh",
114
+ target: (home) => bash(home, "skills/kryso-watch/warden.sh"),
115
+ },
116
+ {
117
+ // Not `reads`: attach follows the files of this machine's runs, with or without a remote.
118
+ name: "attach",
119
+ group: "work",
120
+ summary: "follow a running shift or pass here, and log it",
121
+ runs: "ship-it-status.sh --attach",
122
+ target: (home) => bash(home, "skills/kryso/ship-it-status.sh", ["--attach"]),
123
+ },
124
+ {
125
+ name: "status",
126
+ group: "evidence",
127
+ summary: "the passes in progress, and their ledgers",
128
+ runs: "ship-it-status.sh",
129
+ target: (home) => bash(home, "skills/kryso/ship-it-status.sh"),
130
+ reads: true,
131
+ },
132
+ {
133
+ name: "ledger",
134
+ group: "evidence",
135
+ summary: "read and write a pass's ledger and receipts",
136
+ runs: "ledger.sh",
137
+ target: (home) => bash(home, "skills/kryso/ledger.sh"),
138
+ reads: true,
139
+ },
140
+ {
141
+ name: "report",
142
+ group: "evidence",
143
+ summary: "the run metrics report, in JSON and HTML",
144
+ runs: "report.py",
145
+ target: (home) => python(home, "skills/kryso-shift/report.py"),
146
+ },
147
+ {
148
+ name: "measure",
149
+ group: "evidence",
150
+ summary: "freeze a measurement manifest, then analyse it",
151
+ runs: "measure.sh",
152
+ target: (home) => bash(home, "skills/kryso-shift/measure.sh"),
153
+ },
154
+ {
155
+ name: "archive",
156
+ group: "evidence",
157
+ summary: "the evidence archive and its remote",
158
+ runs: "archive.py, keep-after.sh",
159
+ target: (home, args) => args[0] === "keep-after" ? bash(home, "skills/kryso-shift/keep-after.sh", [], 1) : python(home, "skills/kryso-shift/archive.py"),
160
+ verbs: ["keep-after"],
161
+ verbHelp: true,
162
+ checks: [[], ["keep-after"]],
163
+ },
164
+ {
165
+ name: "integration",
166
+ group: "setup",
167
+ summary: "make an epic's integration branch",
168
+ runs: "ship-loop.sh integration",
169
+ target: (home) => bash(home, "skills/kryso-shift/ship-loop.sh", ["integration"]),
170
+ },
171
+ {
172
+ // The resolver sits beside the drivers, which run it before a claim, so the command and a
173
+ // pass read a workflow through the one module (ADR 0046).
174
+ name: "workflow",
175
+ group: "setup",
176
+ summary: "the workflow a pass here runs, resolved at a ref",
177
+ runs: "workflow.mjs",
178
+ target: (home) => ({ program: "node", head: [path.join(home, "skills/kryso-shift/workflow.mjs")], verbs: 0, script: "skills/kryso-shift/workflow.mjs" }),
179
+ },
180
+ {
181
+ name: "isolate",
182
+ group: "setup",
183
+ summary: "build the image --isolate docker runs in",
184
+ runs: "docker build",
185
+ target: (home, args) => {
186
+ if (args[0] === "build")
187
+ return buildImage(home);
188
+ if (args[0] === "-h" || args[0] === "--help")
189
+ return helpAnswer(ISOLATE_USAGE, "isolate", "help");
190
+ if (args.length === 0)
191
+ return helpAnswer(ISOLATE_USAGE, "isolate", "help", EX_USAGE);
192
+ return refusal(EX_USAGE, "kryso isolate", `unknown verb '${args[0]}'`, { usage: ISOLATE_USAGE });
193
+ },
194
+ checks: [["build"]],
195
+ },
196
+ {
197
+ name: "remote",
198
+ group: "setup",
199
+ summary: "read status and ledger through a kryso daemon",
200
+ runs: "kryso itself",
201
+ target: (_home, args, env) => remote(args, env),
202
+ checks: [],
203
+ },
204
+ {
205
+ name: "skills",
206
+ group: "setup",
207
+ summary: "install the skills and link the commands",
208
+ runs: "install-skills.sh",
209
+ target: (home, args) => {
210
+ if (args[0] === "install")
211
+ return installer(home);
212
+ if (args[0] === "-h" || args[0] === "--help")
213
+ return helpAnswer(SKILLS_USAGE, "skills", "help");
214
+ if (args.length === 0)
215
+ return helpAnswer(SKILLS_USAGE, "skills", "help", EX_USAGE);
216
+ return refusal(EX_USAGE, "kryso skills", `unknown verb '${args[0]}'`, { usage: SKILLS_USAGE });
217
+ },
218
+ checks: [["install"]],
219
+ },
220
+ {
221
+ name: "doctor",
222
+ group: "setup",
223
+ summary: "what stops kryso working here, and what is left over",
224
+ runs: "doctor.js",
225
+ target: () => nodeScript("doctor.js"),
226
+ withoutDrivers: true,
227
+ },
228
+ {
229
+ name: "cleanup",
230
+ group: "setup",
231
+ summary: "remove what doctor names as cleanup's, after asking",
232
+ runs: "cleanup.js",
233
+ target: () => nodeScript("cleanup.js"),
234
+ withoutDrivers: true,
235
+ },
236
+ {
237
+ name: "self-update",
238
+ group: "setup",
239
+ summary: "update kryso, or --check for a newer version",
240
+ runs: "self-update.js",
241
+ target: () => nodeScript("self-update.js"),
242
+ withoutDrivers: true,
243
+ },
244
+ {
245
+ name: "completions",
246
+ group: "setup",
247
+ summary: "print the completion script for bash, zsh or fish",
248
+ runs: "kryso itself",
249
+ target: (_home, args, env) => completions(args, env),
250
+ withoutDrivers: true,
251
+ checks: [],
252
+ },
253
+ ];
254
+ export const find = (name) => COMMANDS.find((c) => c.name === name);
255
+ const GROUPS = [
256
+ ["work", "Work"],
257
+ ["evidence", "Evidence"],
258
+ ["setup", "Setup"],
259
+ ];
260
+ /** The help's width: every line fits 80 columns, as a person reads it on any terminal. */
261
+ const HELP_WIDTH = 80;
262
+ /** Words after a lead, filled to HELP_WIDTH, each line after the first under the first's text. */
263
+ function fill(lead, text) {
264
+ const lines = [];
265
+ let line = lead;
266
+ for (const word of text.split(" ")) {
267
+ if (line.length > lead.length && line.length + 1 + word.length > HELP_WIDTH) {
268
+ lines.push(line);
269
+ line = " ".repeat(lead.length) + word;
270
+ }
271
+ else {
272
+ line += line.length > lead.length ? ` ${word}` : word;
273
+ }
274
+ }
275
+ lines.push(line);
276
+ return lines;
277
+ }
278
+ export function help() {
279
+ const width = Math.max(...COMMANDS.map((c) => c.name.length)) + 2;
280
+ const lines = [
281
+ "Usage: kryso <command> [arguments]",
282
+ "",
283
+ ...fill("", "KrysoWorks works a repository's backlog while you are away. Each command runs one of its bash drivers with your arguments. `kryso <command> --help` prints the driver's own help."),
284
+ ];
285
+ for (const [group, title] of GROUPS) {
286
+ lines.push("", `${title}:`);
287
+ for (const c of COMMANDS.filter((x) => x.group === group)) {
288
+ lines.push(...fill(` ${c.name.padEnd(width)}`, `${c.summary} (${c.runs})`));
289
+ }
290
+ }
291
+ lines.push("", "Options:", " -h, --help print this help", " -V, --version print the version, and how kryso was installed", "");
292
+ return lines.join("\n");
293
+ }
@@ -0,0 +1,24 @@
1
+ import { type Answer } from "./answer.js";
2
+ export declare const SHELLS: readonly ["bash", "zsh", "fish"];
3
+ export declare const USAGE = "Usage: kryso completions <bash|zsh|fish>\n\n bash print the completion script for bash\n zsh print the completion script for zsh\n fish print the completion script for fish\n\nA script asks `kryso completions complete -- <words>` for the words that can\nfollow the ones typed, one a line, each with a tab and its description when it\nhas one. kryso reads them from each command's help when you press Tab, so a\ndriver's new option completes with the script you have.\n";
4
+ /** What a help text names: the verbs, the options for the command and for each verb, the
5
+ * values an option lists as a|b|c, and the options that take an argument. */
6
+ export interface HelpWords {
7
+ verbs: string[];
8
+ flags: string[];
9
+ verbFlags: Map<string, string[]>;
10
+ values: Map<string, string[]>;
11
+ takes: Set<string>;
12
+ }
13
+ /** Reads the words of one help text for `kryso <command>`. */
14
+ export declare function readHelp(text: string, command: string): HelpWords;
15
+ /** A word to offer, with its description when it has one. */
16
+ interface Word {
17
+ word: string;
18
+ about?: string;
19
+ }
20
+ /** The words that can follow the typed ones: `kryso` itself is not among them. */
21
+ export declare function wordsAfter(typed: string[], env: NodeJS.ProcessEnv): Word[];
22
+ /** `kryso completions <shell>`, or `kryso completions complete -- <words>`. */
23
+ export declare function completions(args: string[], env: NodeJS.ProcessEnv): Answer;
24
+ export {};
@@ -0,0 +1,239 @@
1
+ // Shell completion. `kryso completions <shell>` prints a script for bash, zsh or fish, and the
2
+ // script asks `kryso completions complete -- <words>` for the words that can follow at each Tab.
3
+ // The words come from the command table and from each command's own help, read when the Tab is
4
+ // pressed, so an option a driver adds completes with no change here and no new script.
5
+ import { spawnSync } from "node:child_process";
6
+ import { helpAnswer, refusal } from "./answer.js";
7
+ import { COMMANDS, find, isAnswer } from "./commands.js";
8
+ import { EX_USAGE } from "./env.js";
9
+ import { findHome } from "./home.js";
10
+ import { MODES } from "./isolate.js";
11
+ export const SHELLS = ["bash", "zsh", "fish"];
12
+ export const USAGE = `Usage: kryso completions <bash|zsh|fish>
13
+
14
+ bash print the completion script for bash
15
+ zsh print the completion script for zsh
16
+ fish print the completion script for fish
17
+
18
+ A script asks \`kryso completions complete -- <words>\` for the words that can
19
+ follow the ones typed, one a line, each with a tab and its description when it
20
+ has one. kryso reads them from each command's help when you press Tab, so a
21
+ driver's new option completes with the script you have.
22
+ `;
23
+ const FLAG = /^--?[A-Za-z][\w-]*$/;
24
+ const VERB = /^[a-z][a-z0-9-]*$/;
25
+ const LISTED = /^[a-z][a-z0-9-]*(?:\|[a-z][a-z0-9-]*)+$/;
26
+ /** A verb at the start of a line in a list of them: ` open <wt> ...`, ` collect text`. */
27
+ const VERB_LINE = /^ {2,4}([a-z][a-z0-9-]*)(?: {2,}| <| \[|$)/;
28
+ /** An option line: an option at most eight columns in, then its argument and description. */
29
+ const OPTION_LINE = /^ {1,8}(-[^\s-]|--[A-Za-z])/;
30
+ /** The words of a line's first column, before the gap of two spaces or more. */
31
+ const column = (line) => (line.trim().split(/\s{2,}/)[0] ?? "").split(/\s+/);
32
+ const unbracket = (token) => token.replace(/^[[(]+/, "").replace(/[\])]+$/, "");
33
+ /** Reads the words of one help text for `kryso <command>`. */
34
+ export function readHelp(text, command) {
35
+ const found = { verbs: [], flags: [], verbFlags: new Map(), values: new Map(), takes: new Set() };
36
+ const bare = new Set();
37
+ const add = (list, word) => {
38
+ if (!list.includes(word))
39
+ list.push(word);
40
+ };
41
+ const forVerb = (verb) => {
42
+ if (!found.verbFlags.has(verb))
43
+ found.verbFlags.set(verb, []);
44
+ return found.verbFlags.get(verb);
45
+ };
46
+ /** The options among the tokens, into the list, with what follows each one. */
47
+ const options = (tokens, into) => {
48
+ tokens.forEach((raw, i) => {
49
+ const token = unbracket(raw.replace(/,$/, ""));
50
+ if (!FLAG.test(token))
51
+ return;
52
+ add(into, token);
53
+ const next = raw.endsWith("]") || raw.endsWith(",") ? "" : (tokens[i + 1] ?? "");
54
+ const value = unbracket(next);
55
+ if (LISTED.test(value))
56
+ found.values.set(token, value.split("|"));
57
+ if (next && !next.startsWith("-") && !next.startsWith("[") && next !== "|")
58
+ found.takes.add(token);
59
+ else
60
+ bare.add(token);
61
+ });
62
+ };
63
+ const usage = new RegExp(`^(?:Usage:\\s+|\\s+)kryso\\s+${command.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:\\s+(.*))?$`);
64
+ let listsVerbs = false;
65
+ for (const line of text.split("\n")) {
66
+ const u = usage.exec(line);
67
+ if (u) {
68
+ const tokens = (u[1] ?? "").split(/\s+/).filter(Boolean);
69
+ if (tokens.includes("<verb>"))
70
+ listsVerbs = true;
71
+ const verb = tokens[0] !== undefined && VERB.test(tokens[0]) ? tokens[0] : undefined;
72
+ if (verb)
73
+ add(found.verbs, verb);
74
+ options(verb ? tokens.slice(1) : tokens, verb ? forVerb(verb) : found.flags);
75
+ continue;
76
+ }
77
+ if (OPTION_LINE.test(line)) {
78
+ options(column(line), found.flags);
79
+ continue;
80
+ }
81
+ const verb = listsVerbs ? VERB_LINE.exec(line)?.[1] : undefined;
82
+ if (verb) {
83
+ add(found.verbs, verb);
84
+ options(column(line).slice(1), forVerb(verb));
85
+ }
86
+ }
87
+ // An option shown both with an argument and without one, as shift's --adopt is, may stand
88
+ // alone, so the shell offers the options after it.
89
+ for (const flag of bare)
90
+ found.takes.delete(flag);
91
+ return found;
92
+ }
93
+ const plain = (words) => words.map((word) => ({ word }));
94
+ /** The help of `kryso <command> [verb] --help`: kryso's own text, or what the driver prints. */
95
+ function helpText(command, home, verb, env) {
96
+ if (!home && !command.withoutDrivers)
97
+ return "";
98
+ const args = [...verb, "--help"];
99
+ let target;
100
+ try {
101
+ target = command.target(home, args, env);
102
+ }
103
+ catch {
104
+ return "";
105
+ }
106
+ if (isAnswer(target))
107
+ return target.text;
108
+ const r = spawnSync(target.program, [...target.head, ...args.slice(target.verbs)], {
109
+ env,
110
+ encoding: "utf8",
111
+ stdio: ["ignore", "pipe", "pipe"],
112
+ timeout: 5000,
113
+ });
114
+ return `${r.stdout ?? ""}${r.stderr ?? ""}`;
115
+ }
116
+ /** The words that can follow the typed ones: `kryso` itself is not among them. */
117
+ export function wordsAfter(typed, env) {
118
+ if (typed.length === 0) {
119
+ return [...COMMANDS.map((c) => ({ word: c.name, about: c.summary })), { word: "--help" }, { word: "--version" }];
120
+ }
121
+ const [name, ...after] = typed;
122
+ const command = name === undefined ? undefined : find(name);
123
+ if (!command)
124
+ return [];
125
+ if (command.name === "completions")
126
+ return typed.length === 1 ? plain(SHELLS) : [];
127
+ let rest = after;
128
+ const first = rest.length === 0 && command.isolates ? ["--isolate"] : [];
129
+ if (command.isolates && rest[0] === "--isolate") {
130
+ if (rest.length === 1)
131
+ return plain(MODES);
132
+ rest = rest.slice(2);
133
+ }
134
+ else if (command.isolates && rest[0]?.startsWith("--isolate=")) {
135
+ rest = rest.slice(1);
136
+ }
137
+ let home = "";
138
+ try {
139
+ home = findHome(env);
140
+ }
141
+ catch {
142
+ // No drivers: the commands and kryso's own words still complete.
143
+ }
144
+ const help = readHelp(helpText(command, home, [], env), command.name);
145
+ for (const verb of command.verbs ?? [])
146
+ if (!help.verbs.includes(verb))
147
+ help.verbs.push(verb);
148
+ let flags = help.flags;
149
+ let { values, takes } = help;
150
+ const verb = rest[0] !== undefined && help.verbs.includes(rest[0]) ? rest[0] : undefined;
151
+ if (verb) {
152
+ rest = rest.slice(1);
153
+ flags = [...(help.verbFlags.get(verb) ?? [])];
154
+ if (command.verbHelp) {
155
+ const own = readHelp(helpText(command, home, [verb], env), command.name);
156
+ for (const f of [...(own.verbFlags.get(verb) ?? []), ...own.flags])
157
+ if (!flags.includes(f))
158
+ flags.push(f);
159
+ values = new Map([...values, ...own.values]);
160
+ takes = new Set([...takes, ...own.takes]);
161
+ }
162
+ }
163
+ else if (rest.length === 0 && help.verbs.length > 0) {
164
+ return plain([...help.verbs, ...flags]);
165
+ }
166
+ const previous = rest.at(-1);
167
+ if (previous !== undefined && values.has(previous))
168
+ return plain(values.get(previous));
169
+ if (previous !== undefined && takes.has(previous))
170
+ return [];
171
+ return plain([...first, ...flags]);
172
+ }
173
+ const SCRIPTS = {
174
+ bash: `# kryso completion for bash: put this file in
175
+ # ~/.local/share/bash-completion/completions/kryso, or source it from ~/.bashrc.
176
+ _kryso() {
177
+ local cur=\${COMP_WORDS[COMP_CWORD]} IFS=$'\\n' words
178
+ words=$(kryso completions complete -- "\${COMP_WORDS[@]:1:COMP_CWORD-1}" 2>/dev/null | cut -f1)
179
+ COMPREPLY=($(compgen -W "$words" -- "$cur"))
180
+ }
181
+ complete -o default -F _kryso kryso
182
+ `,
183
+ zsh: `#compdef kryso
184
+ # kryso completion for zsh: put this file as _kryso in a directory on $fpath before compinit
185
+ # runs, or source it from ~/.zshrc after compinit.
186
+ _kryso() {
187
+ local -a lines described
188
+ local line
189
+ lines=("\${(@f)$(kryso completions complete -- "\${(@)words[2,CURRENT-1]}" 2>/dev/null)}")
190
+ for line in $lines; do
191
+ if [[ $line == *$'\\t'* ]]; then
192
+ described+=("\${\${line%%$'\\t'*}//:/\\\\:}:\${line#*$'\\t'}")
193
+ else
194
+ described+=("\${line//:/\\\\:}")
195
+ fi
196
+ done
197
+ if (( \${#described} )); then
198
+ _describe kryso described
199
+ else
200
+ _files
201
+ fi
202
+ }
203
+ if [[ $funcstack[1] == _kryso ]]; then
204
+ _kryso "$@"
205
+ else
206
+ compdef _kryso kryso
207
+ fi
208
+ `,
209
+ fish: `# kryso completion for fish: put this file in ~/.config/fish/completions/kryso.fish.
210
+ function __kryso_complete
211
+ set -l typed (commandline -opc)
212
+ set -l words (kryso completions complete -- $typed[2..-1] 2>/dev/null)
213
+ if test (count $words) -gt 0
214
+ printf '%s\\n' $words
215
+ else
216
+ __fish_complete_path (commandline -ct)
217
+ end
218
+ end
219
+ complete -c kryso -f -a '(__kryso_complete)'
220
+ `,
221
+ };
222
+ /** `kryso completions <shell>`, or `kryso completions complete -- <words>`. */
223
+ export function completions(args, env) {
224
+ const [first, ...rest] = args;
225
+ if (first === "complete") {
226
+ const typed = rest[0] === "--" ? rest.slice(1) : rest;
227
+ const text = wordsAfter(typed, env)
228
+ .map((w) => (w.about ? `${w.word}\t${w.about}\n` : `${w.word}\n`))
229
+ .join("");
230
+ return { text, status: 0 };
231
+ }
232
+ if (first === undefined)
233
+ return helpAnswer(USAGE, "completions", "help", EX_USAGE);
234
+ if (first === "-h" || first === "--help")
235
+ return helpAnswer(USAGE, "completions", "help");
236
+ if (SHELLS.includes(first) && rest.length === 0)
237
+ return { text: SCRIPTS[first], status: 0 };
238
+ return refusal(EX_USAGE, "kryso completions", `no script for '${first}'`, { usage: USAGE });
239
+ }
@@ -0,0 +1,15 @@
1
+ /** What a GET of a route came to: the status and its JSON body, a status with no JSON body, or
2
+ * why the daemon was not reached at all. */
3
+ export type DaemonAnswer = {
4
+ kind: "json";
5
+ status: number;
6
+ body: unknown;
7
+ } | {
8
+ kind: "not-json";
9
+ status: number;
10
+ } | {
11
+ kind: "unreachable";
12
+ why: string;
13
+ };
14
+ /** GET route of the daemon at base, giving up after timeout milliseconds when one is named. */
15
+ export declare function getRoute(base: string, route: string, timeout?: number): Promise<DaemonAnswer>;
package/dist/daemon.js ADDED
@@ -0,0 +1,23 @@
1
+ // The CLI's one client of a kryso daemon's API: a GET of one route, and what came back. It
2
+ // never throws and never decides what an answer means, so kryso status and kryso ledger
3
+ // (read.ts) turn a result into their exit statuses, and kryso doctor turns the same result into
4
+ // a diagnosis.
5
+ /** GET route of the daemon at base, giving up after timeout milliseconds when one is named. */
6
+ export async function getRoute(base, route, timeout) {
7
+ let res;
8
+ try {
9
+ res = await fetch(`${base}${route}`, { headers: { Accept: "application/json" }, ...(timeout === undefined ? {} : { signal: AbortSignal.timeout(timeout) }) });
10
+ }
11
+ catch (e) {
12
+ if (e.name === "TimeoutError")
13
+ return { kind: "unreachable", why: `nothing within ${timeout / 1000} seconds` };
14
+ const cause = e.cause;
15
+ return { kind: "unreachable", why: cause?.code ?? cause?.message ?? e.message };
16
+ }
17
+ try {
18
+ return { kind: "json", status: res.status, body: await res.json() };
19
+ }
20
+ catch {
21
+ return { kind: "not-json", status: res.status };
22
+ }
23
+ }
@@ -0,0 +1,47 @@
1
+ import type { Diagnosis, Doctor } from "@krysoworks/core";
2
+ export type { Diagnosis, Doctor };
3
+ export interface Scope {
4
+ /** The repositories to read, by their top-level paths. */
5
+ repositories: string[];
6
+ /** The directory the run started in, named when it is in no repository, which one info line
7
+ * then says. */
8
+ outside?: string;
9
+ /** The paths a list named that are no repository, one info line each. */
10
+ gone?: Gone[];
11
+ /** Why the repositories sessions have used could not be listed, when they could not. */
12
+ unlisted?: string;
13
+ }
14
+ /** A path a list named that is no repository: where it was listed, and why it is none. */
15
+ export interface Gone {
16
+ path: string;
17
+ listed: string;
18
+ why: string;
19
+ }
20
+ export interface Switches {
21
+ /** Read no registry, remote or GitHub. */
22
+ offline: boolean;
23
+ /** How many dated skill backups to keep, five unless a kryso cleanup run names another. */
24
+ keepBackups?: number;
25
+ /** Run each repository's shift and watch dry runs for its readiness lines, and read its
26
+ * workflows and their pins. True unless a reader that has no use for them, kryso cleanup, turns
27
+ * them off. */
28
+ readiness?: boolean;
29
+ /** The keep configuration kryso cleanup --config names, read in place of the one the keep
30
+ * launch agent names. */
31
+ keepConfig?: string;
32
+ /** How many days evidence stays before a copy the remote holds is cleanup's, 90 unless a kryso
33
+ * cleanup run names another. */
34
+ retentionDays?: number;
35
+ }
36
+ /** The repository of the current directory, by its top-level path, or the directory itself
37
+ * when it is in no repository. */
38
+ export declare function here(env: NodeJS.ProcessEnv): Scope;
39
+ /** The main checkout of the repository a path is in: the directory its git common directory
40
+ * sits in, as kryso status --all names a repository. */
41
+ export declare function mainOf(dir: string, env: NodeJS.ProcessEnv): string | undefined;
42
+ /** Every repository a session has used, by kryso status's own rule, and every one the watch
43
+ * schedule's list names, each once by its main checkout. */
44
+ export declare function everywhere(env: NodeJS.ProcessEnv): Scope;
45
+ /** The groups in the order they are reported. */
46
+ export declare const GROUPS: ("install" | "work" | "evidence" | "remote" | "repository" | "images" | "tools" | "locks" | "branches")[];
47
+ export declare function diagnose(scope: Scope, switches: Switches, env: NodeJS.ProcessEnv): Doctor;