@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
package/dist/read.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/read.js ADDED
@@ -0,0 +1,297 @@
1
+ // `kryso status` and `kryso ledger` while a remote is set: the same questions, asked of the
2
+ // daemon's read API. bin/kryso execs this as `node read.js <status|ledger> <url> [arguments]`,
3
+ // so it owns the terminal and its signals as a driver would. Nothing here writes: a ledger verb
4
+ // that writes runs on the machine that runs the pass. The answers' shapes are the schemas in
5
+ // packages/core/schemas/api-*/, whose types are only types here, so the command still runs
6
+ // with no dependency installed.
7
+ import path from "node:path";
8
+ import { give, helpAnswer, refusal, say } from "./answer.js";
9
+ import { getRoute } from "./daemon.js";
10
+ import { EX_SOFTWARE, EX_UNAVAILABLE, EX_USAGE } from "./env.js";
11
+ import { outcomeTone } from "./style.js";
12
+ const STATUS_USAGE = `Usage: kryso status [repo-path]
13
+ kryso status --all
14
+
15
+ With a remote set, it prints the passes in progress and the running shift in one
16
+ repository the daemon is attached to: the one whose path holds repo-path, by the
17
+ daemon's paths (default the current directory). --all prints every attached
18
+ repository. --inspect reads the ledgers' files, so it needs this machine's
19
+ evidence, and so does --inspect --follow: run \`kryso remote --unset\` first.
20
+ `;
21
+ const LEDGER_USAGE = `Usage: kryso ledger read <repo> <pass>
22
+ kryso ledger tail <repo> <pass> [--after <n>] [--follow] [--interval <s>]
23
+
24
+ With a remote set, it prints a pass's ledger as the daemon reads it. The repo is
25
+ its id, and the pass is its id in \`kryso status\`: a worktree's name, or an
26
+ archived copy's.
27
+
28
+ read print one JSON document: where the pass stands, its ledger, checkpoint,
29
+ receipts and usage
30
+ tail print the ledger's lines after line --after (default 0, every line),
31
+ one per line. With --follow, print the lines written since, every
32
+ --interval seconds (default 2), asking for the lines after the last one
33
+ it printed. It asks again after a refused connection or a failed
34
+ answer, waiting longer each time, for up to 30 seconds in all, so a
35
+ daemon restart or a ledger that moves loses and repeats nothing
36
+
37
+ The verbs that write a ledger run on the machine that runs the pass, and
38
+ \`kryso remote --unset\` runs them here.
39
+ `;
40
+ /** How long --follow keeps asking again after the remote stops answering, in all. */
41
+ const GIVE_UP_MS = 30_000;
42
+ /** The end of a run: its exit status and one line for stderr, with its fix and usage. */
43
+ class Stop extends Error {
44
+ status;
45
+ options;
46
+ transient;
47
+ notFound;
48
+ constructor(status, message, options = {}) {
49
+ super(message);
50
+ this.status = status;
51
+ this.options = options;
52
+ this.transient = options.transient ?? false;
53
+ this.notFound = options.notFound ?? false;
54
+ }
55
+ }
56
+ /** A route's JSON body when the daemon answers 2xx, or the Stop the command ends on otherwise. */
57
+ async function getJson(base, route) {
58
+ const answer = await getRoute(base, route);
59
+ if (answer.kind === "unreachable")
60
+ throw new Stop(EX_UNAVAILABLE, `cannot reach the remote ${base}: ${answer.why}`, { transient: true });
61
+ const { status } = answer;
62
+ if (answer.kind === "not-json") {
63
+ throw new Stop(EX_UNAVAILABLE, `the remote ${base} answered ${route} with ${status} and no JSON, so it does not answer as a kryso daemon does`, { transient: status >= 500 });
64
+ }
65
+ if (status >= 200 && status < 300)
66
+ return answer.body;
67
+ const message = answer.body?.message;
68
+ const why = `the remote ${base}: ${typeof message === "string" ? message : `${status} for ${route}`}`;
69
+ throw new Stop(status === 404 || status >= 500 ? 1 : EX_USAGE, why, { transient: status >= 500, notFound: status === 404 });
70
+ }
71
+ const seg = (s) => encodeURIComponent(s);
72
+ function table(rows) {
73
+ const widths = rows[0].map((_, i) => Math.max(...rows.map((r) => r[i].length)));
74
+ return rows.map((r) => r.map((c, i) => (i === r.length - 1 ? c : c.padEnd(widths[i]))).join(" "));
75
+ }
76
+ const LIVE = { live: "ok", idle: "dim", unknown: "warn" };
77
+ const none = () => "";
78
+ const PASS_COLUMNS = [
79
+ { title: "PASS", cell: (p) => ({ text: p.id, tone: "value" }), unreadable: (id) => ({ text: id, tone: "value" }) },
80
+ { title: "ISSUE", cell: (p) => (p.issue ? `#${p.issue}` : "-"), unreadable: none },
81
+ { title: "STAGE", cell: (p) => p.stage ?? "-", unreadable: none },
82
+ { title: "STATUS", cell: (p) => p.status ?? "-", unreadable: none },
83
+ {
84
+ title: "LIVE",
85
+ cell: (p) => {
86
+ const text = p.live === true ? "live" : p.live === false ? "idle" : "unknown";
87
+ return { text, tone: LIVE[text] };
88
+ },
89
+ unreadable: none,
90
+ },
91
+ { title: "UPDATED", cell: (p) => ({ text: p.updated_at ?? "-", tone: "dim" }), unreadable: (_, error) => ({ text: `unreadable: ${error}`, tone: "fail" }) },
92
+ ];
93
+ const textOf = (c) => (typeof c === "string" ? c : c.text);
94
+ const within = (p, dir) => {
95
+ const rel = path.relative(dir, p);
96
+ return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
97
+ };
98
+ async function status(base, args) {
99
+ if (args[0] === "-h" || args[0] === "--help")
100
+ return helpAnswer(STATUS_USAGE, "status", "help");
101
+ if (args.includes("--inspect")) {
102
+ throw new Stop(EX_USAGE, `--inspect reads the ledgers' files, which are on the remote ${base}'s machine`, {
103
+ hint: "run `kryso remote --unset` and kryso status --inspect there, or read one pass with kryso ledger read",
104
+ join: ": ",
105
+ });
106
+ }
107
+ const all = args.includes("--all");
108
+ const rest = args.filter((a) => a !== "--all");
109
+ if (rest.length > 1 || rest[0]?.startsWith("-") || (all && rest.length > 0)) {
110
+ throw new Stop(EX_USAGE, `takes one repo-path or --all, got ${args.join(" ")}`, { usage: STATUS_USAGE });
111
+ }
112
+ const attached = (await getJson(base, "/repositories")).repositories;
113
+ let repos = attached;
114
+ if (!all) {
115
+ const target = path.resolve(rest[0] ?? process.cwd());
116
+ const holder = attached.filter((r) => within(target, r.path)).sort((a, b) => b.path.length - a.path.length)[0];
117
+ if (!holder) {
118
+ const paths = attached.map((r) => r.path).join(", ") || "none";
119
+ throw new Stop(1, `${target} is in no repository the remote ${base} is attached to (it has ${paths})`, { hint: "name one of those paths, or use --all" });
120
+ }
121
+ repos = [holder];
122
+ }
123
+ const out = [];
124
+ // The same report drawn: the brand line, then a pill for each repository, its shift as a line
125
+ // and its passes as a table.
126
+ const draws = [(s) => s.brand("status", `through ${base}`)];
127
+ for (const repo of repos) {
128
+ const shift = repo.running.length > 0 ? `shift ${repo.running.join(", ")} running` : "no shift running";
129
+ const iso = repo.isolated?.state === "running" ? `, an isolated run (docker pid ${repo.isolated.pid}) holds it` : "";
130
+ out.push(`${repo.id} (${repo.path}): ${repo.present ? shift + iso : "not on the daemon's disk"}`);
131
+ draws.push((s) => {
132
+ s.write("\n");
133
+ s.pill("primary", repo.id);
134
+ if (!repo.present)
135
+ return s.warn(`${s.val(repo.path)} is not on the daemon's disk`);
136
+ const running = repo.running.length > 0 ? `shift ${repo.running.map(s.val).join(", ")} running` : "no shift running";
137
+ const holds = repo.isolated?.state === "running" ? `, an isolated run (docker pid ${s.val(String(repo.isolated.pid))}) holds it` : "";
138
+ s.info(`${s.val(repo.path)}: ${running}${holds}`);
139
+ });
140
+ if (!repo.present)
141
+ continue;
142
+ const passes = (await getJson(base, `/repositories/${seg(repo.id)}/passes`)).passes.filter((p) => p.kind === "worktree");
143
+ if (passes.length === 0) {
144
+ out.push(" no pass in progress");
145
+ draws.push((s) => s.info("no pass in progress"));
146
+ continue;
147
+ }
148
+ const header = PASS_COLUMNS.map((c) => c.title);
149
+ const rows = passes.map((p) => PASS_COLUMNS.map((c) => ("error" in p ? c.unreadable(p.id, p.error) : c.cell(p))));
150
+ out.push(...table([header, ...rows.map((r) => r.map(textOf))]).map((l) => ` ${l.trimEnd()}`));
151
+ draws.push((s) => s.table([header, ...rows], { indent: 2, header: true }));
152
+ }
153
+ return { text: `${out.join("\n")}\n`, status: 0, draw: (s) => draws.forEach((d) => d(s)) };
154
+ }
155
+ /** One ledger line on stdout: the time dim, the stage bold and the outcome in its tone, drawn. */
156
+ function line(e) {
157
+ const evidence = e.evidence === "" ? "" : ` ${e.evidence}`;
158
+ // The palette's outcome map, ui_event's: an outcome it names takes its tone, any other is plain.
159
+ const named = outcomeTone(e.outcome);
160
+ const tone = named === "info" ? undefined : named;
161
+ say({
162
+ text: `${e.time} ${e.stage} ${e.outcome}${evidence}\n`,
163
+ draw: (s) => s.write(`${s.tint("dim", e.time)} ${s.tint("bold", e.stage)} ${tone ? s.tint(tone, e.outcome) : e.outcome}${evidence}\n`),
164
+ }, process.stdout);
165
+ }
166
+ /** A note about the remote on stderr: a warn line when it stops answering, an ok line when it
167
+ * answers again. */
168
+ const note = (text, draw) => say({ text: `kryso ledger: ${text}\n`, draw }, process.stderr);
169
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
170
+ /**
171
+ * Print the ledger's lines after `after`. Following, a transient failure is asked again after
172
+ * the last line printed, waiting twice as long each time up to 5 s, until the remote answers or
173
+ * 30 s have gone since the first failure. A pass the remote has stopped finding counts once one
174
+ * page has been read: its ledger directory is briefly gone while a writer moves it.
175
+ */
176
+ async function tail(base, route, after, follow, interval) {
177
+ let through = after;
178
+ let read = false;
179
+ let failingSince = null;
180
+ let wait = interval;
181
+ for (;;) {
182
+ try {
183
+ const page = await getJson(base, `${route}/ledger?after=${through}`);
184
+ if (failingSince !== null) {
185
+ note(`the remote answers again, from line ${through + 1}`, (s) => s.ok(`the remote answers again, from line ${s.val(String(through + 1))}`));
186
+ }
187
+ failingSince = null;
188
+ wait = interval;
189
+ read = true;
190
+ for (const e of page.events)
191
+ line(e);
192
+ through = page.through;
193
+ }
194
+ catch (e) {
195
+ const retry = follow && e instanceof Stop && (e.transient || (e.notFound && read));
196
+ if (!retry)
197
+ throw e;
198
+ const now = Date.now();
199
+ if (failingSince === null) {
200
+ failingSince = now;
201
+ const asking = `${e.message}, so kryso asks again for up to ${GIVE_UP_MS / 1000}s`;
202
+ note(asking, (s) => s.warn(asking));
203
+ }
204
+ else if (now - failingSince >= GIVE_UP_MS) {
205
+ throw new Stop(e.status, `${e.message}, and it has not answered for ${GIVE_UP_MS / 1000}s, so the tail stops after line ${through}`);
206
+ }
207
+ await sleep(wait);
208
+ wait = Math.min(wait * 2, 5000);
209
+ continue;
210
+ }
211
+ if (!follow)
212
+ return;
213
+ await sleep(interval);
214
+ }
215
+ }
216
+ async function ledger(base, args) {
217
+ const [verb, ...rest] = args;
218
+ if (verb === undefined)
219
+ return helpAnswer(LEDGER_USAGE, "ledger", "help", EX_USAGE);
220
+ if (verb === "-h" || verb === "--help" || verb === "help")
221
+ return helpAnswer(LEDGER_USAGE, "ledger", "help");
222
+ if (verb !== "read" && verb !== "tail") {
223
+ throw new Stop(EX_USAGE, `${verb}: with a remote set, ledger reads through ${base}, which serves read and tail`, {
224
+ hint: `${verb} runs on the machine that runs the pass, and \`kryso remote --unset\` runs it here`,
225
+ join: ". ",
226
+ });
227
+ }
228
+ const words = [];
229
+ let after = 0;
230
+ let follow = false;
231
+ let interval = 2000;
232
+ for (let i = 0; i < rest.length; i++) {
233
+ const a = rest[i];
234
+ if (verb === "tail" && (a === "--after" || a === "--interval")) {
235
+ const v = rest[++i];
236
+ if (v === undefined || !/^[0-9]+(\.[0-9]+)?$/.test(v) || (a === "--after" && !/^[0-9]+$/.test(v))) {
237
+ throw new Stop(EX_USAGE, `tail: ${a} takes ${a === "--after" ? "a line number" : "a number of seconds"}`);
238
+ }
239
+ if (a === "--after")
240
+ after = Number(v);
241
+ else
242
+ interval = Math.max(100, Number(v) * 1000);
243
+ }
244
+ else if (verb === "tail" && a === "--follow")
245
+ follow = true;
246
+ else if (a.startsWith("-"))
247
+ throw new Stop(EX_USAGE, `${verb}: unknown option '${a}'`, { usage: LEDGER_USAGE });
248
+ else
249
+ words.push(a);
250
+ }
251
+ if (words.length !== 2)
252
+ throw new Stop(EX_USAGE, `${verb} needs <repo> <pass>`, { usage: LEDGER_USAGE });
253
+ const route = `/repositories/${seg(words[0])}/passes/${seg(words[1])}`;
254
+ if (verb === "tail") {
255
+ await tail(base, route, after, follow, interval);
256
+ return null;
257
+ }
258
+ const [pass, led, checkpoint, receipts, usage] = await Promise.all([
259
+ getJson(base, route),
260
+ getJson(base, `${route}/ledger`),
261
+ getJson(base, `${route}/checkpoint`),
262
+ getJson(base, `${route}/receipts`),
263
+ getJson(base, `${route}/usage`),
264
+ ]);
265
+ const doc = {
266
+ pass,
267
+ ledger: { header: led.header, base: led.base, events: led.events, skipped: led.skipped, through: led.through },
268
+ checkpoint: checkpoint.checkpoint,
269
+ receipts: receipts.receipts,
270
+ receipt_times: receipts.receipt_times,
271
+ usage: { cost_usd: usage.cost_usd, forks: usage.forks, records: usage.records },
272
+ };
273
+ return { text: `${JSON.stringify(doc, null, 2)}\n`, status: 0 };
274
+ }
275
+ async function main() {
276
+ const [command, base, ...args] = process.argv.slice(2);
277
+ if ((command !== "status" && command !== "ledger") || !base) {
278
+ throw new Stop(EX_SOFTWARE, "dist/read.js is run by bin/kryso as `read.js <status|ledger> <url> [arguments]`");
279
+ }
280
+ const out = command === "status" ? await status(base, args) : await ledger(base, args);
281
+ if (out !== null) {
282
+ give(out);
283
+ process.exitCode = out.status;
284
+ }
285
+ }
286
+ main().catch((e) => {
287
+ if (e instanceof Stop) {
288
+ // The reader's usage errors have always ended with a blank line after the usage.
289
+ const { usage } = e.options;
290
+ give(refusal(e.status, `kryso ${process.argv[2]}`, e.message, { ...e.options, usage: usage && `${usage}\n` }));
291
+ process.exitCode = e.status;
292
+ }
293
+ else {
294
+ process.stderr.write(`kryso: ${e instanceof Error ? (e.stack ?? e.message) : String(e)}\n`);
295
+ process.exitCode = EX_SOFTWARE;
296
+ }
297
+ });
@@ -0,0 +1,5 @@
1
+ import type { Diagnosis } from "@krysoworks/core";
2
+ /** The repository group's readiness diagnoses for the repository at repo, its top-level path,
3
+ * which the dry runs run in, named by main, its main checkout, with the drivers at home. A driver
4
+ * that is missing is the install group's error, so its dry run is not tried. */
5
+ export declare function readiness(repo: string, main: string, home: string | undefined, offline: boolean, env: NodeJS.ProcessEnv): Diagnosis[];
@@ -0,0 +1,64 @@
1
+ // What diagnose.ts reads of whether a shift and a watch can start in one repository: the drivers'
2
+ // own dry runs, under the read-only switch that makes them change nothing (ADR 0044). Each stop
3
+ // line a dry run prints, with its exit status, is one diagnosis, so the preflight's rules stay in
4
+ // the drivers and none is copied here. Under the offline switch neither runs, since both read
5
+ // GitHub, and readiness is unknown.
6
+ import { startable } from "./commands.js";
7
+ import { capture } from "./proc.js";
8
+ /** How long a dry run gets before doctor stops waiting. A watch's runs its stages too, and took
9
+ * eleven seconds on this repository, so a run that hangs costs a plain doctor two minutes at most
10
+ * for each. */
11
+ const DRY_RUN_TIMEOUT = 120_000;
12
+ /** The lines a preflight stops on: ship-loop's and the runner's, and the watch's own. */
13
+ const STOP = /^(preflight|runner): |^warden: stop /;
14
+ /** The repository group's readiness diagnoses for the repository at repo, its top-level path,
15
+ * which the dry runs run in, named by main, its main checkout, with the drivers at home. A driver
16
+ * that is missing is the install group's error, so its dry run is not tried. */
17
+ export function readiness(repo, main, home, offline, env) {
18
+ const base = { group: "repository", repository: main };
19
+ if (offline) {
20
+ return [
21
+ {
22
+ ...base,
23
+ id: "repository.readiness",
24
+ severity: "info",
25
+ fix: "person",
26
+ unknown: true,
27
+ subject: main,
28
+ text: "whether a shift and a watch can start here is unknown, because --offline runs neither dry run",
29
+ },
30
+ ];
31
+ }
32
+ if (!home)
33
+ return [];
34
+ // Plain lines, whatever the terminal doctor itself runs on.
35
+ const plain = { ...env, NO_COLOR: "1", CLICOLOR_FORCE: "" };
36
+ const found = [];
37
+ for (const [name, args] of [
38
+ ["shift", ["--dry-run", "--read-only"]],
39
+ ["watch", ["--dry-run", "--read-only", "--repo", repo]],
40
+ ]) {
41
+ const target = startable(home, name, [...args], env);
42
+ if (!target)
43
+ continue;
44
+ const command = `kryso ${name} ${args.slice(0, 2).join(" ")}`;
45
+ const r = capture(target.program, [...target.head, ...args], plain, repo, DRY_RUN_TIMEOUT);
46
+ if (r.status === 0)
47
+ continue;
48
+ const exit = r.status === null ? `did not finish within ${DRY_RUN_TIMEOUT / 60_000} minutes` : `exits ${r.status}`;
49
+ const output = [...r.err.split("\n"), ...r.out.split("\n")].map((l) => l.trim()).filter(Boolean);
50
+ const stops = output.filter((l) => STOP.test(l));
51
+ const lines = stops.length > 0 ? stops : [output.at(-1) ?? "it printed nothing"];
52
+ for (const line of lines) {
53
+ found.push({
54
+ ...base,
55
+ id: "repository.preflight-stop",
56
+ severity: "warn",
57
+ fix: "person",
58
+ subject: command,
59
+ text: `${command} ${exit} here on ${line}, so a ${name} here stops the same way until a person puts that right`,
60
+ });
61
+ }
62
+ }
63
+ return found;
64
+ }
@@ -0,0 +1,6 @@
1
+ import { type Answer } from "./answer.js";
2
+ export declare const remoteFile: (env: NodeJS.ProcessEnv) => string;
3
+ /** The remote's URL, or null when none is set. An empty file is none. */
4
+ export declare function readRemote(env: NodeJS.ProcessEnv): string | null;
5
+ export declare const REMOTE_USAGE = "Usage: kryso remote [<url> | --unset]\n\n <url> read through the kryso daemon at this http or https URL, so\n `kryso status` and `kryso ledger` ask its API, not this\n machine's evidence\n --unset read this machine's evidence again\n (none) print the remote, or say there is none and exit 1\n";
6
+ export declare function remote(args: string[], env: NodeJS.ProcessEnv): Answer;
package/dist/remote.js ADDED
@@ -0,0 +1,85 @@
1
+ // `kryso remote <url>`: the one setting the command keeps (story 9). It names a kryso daemon,
2
+ // and while it is set `kryso status` and `kryso ledger` read through that daemon's API instead of
3
+ // this machine's evidence. It lives beside the command's other files, in
4
+ // ${XDG_CONFIG_HOME:-~/.config}/kryso/remote, one line.
5
+ import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
6
+ import path from "node:path";
7
+ import { helpAnswer, refusal } from "./answer.js";
8
+ import { configDir, EX_SOFTWARE, EX_USAGE } from "./env.js";
9
+ export const remoteFile = (env) => path.join(configDir(env), "remote");
10
+ /** The remote's URL, or null when none is set. An empty file is none. */
11
+ export function readRemote(env) {
12
+ try {
13
+ const url = readFileSync(remoteFile(env), "utf8").trim();
14
+ return url === "" ? null : url;
15
+ }
16
+ catch {
17
+ return null;
18
+ }
19
+ }
20
+ export const REMOTE_USAGE = `Usage: kryso remote [<url> | --unset]
21
+
22
+ <url> read through the kryso daemon at this http or https URL, so
23
+ \`kryso status\` and \`kryso ledger\` ask its API, not this
24
+ machine's evidence
25
+ --unset read this machine's evidence again
26
+ (none) print the remote, or say there is none and exit 1
27
+ `;
28
+ /** A URL the daemon can be reached at: http or https, with no query, fragment or credentials. */
29
+ function check(raw) {
30
+ let url;
31
+ try {
32
+ url = new URL(raw);
33
+ }
34
+ catch {
35
+ return null;
36
+ }
37
+ if (url.protocol !== "http:" && url.protocol !== "https:")
38
+ return null;
39
+ if (url.search || url.hash || url.username || url.password)
40
+ return null;
41
+ return url.href.replace(/\/+$/, "");
42
+ }
43
+ export function remote(args, env) {
44
+ const file = remoteFile(env);
45
+ const [arg, ...more] = args;
46
+ if (arg === "-h" || arg === "--help")
47
+ return helpAnswer(REMOTE_USAGE, "remote", "help");
48
+ if (more.length > 0)
49
+ return refusal(EX_USAGE, "kryso remote", `takes one URL, got ${args.length} arguments`, { usage: REMOTE_USAGE });
50
+ if (arg === undefined) {
51
+ const url = readRemote(env);
52
+ if (url)
53
+ return { text: `${url}\n`, status: 0, draw: (s) => (s.brand("remote"), s.ok(`status and ledger read through ${s.val(url)}`)) };
54
+ return {
55
+ text: "kryso remote: none is set, so status and ledger read this machine's evidence\n",
56
+ status: 1,
57
+ draw: (s) => (s.brand("remote"), s.warn("none is set, so status and ledger read this machine's evidence")),
58
+ };
59
+ }
60
+ try {
61
+ if (arg === "--unset") {
62
+ rmSync(file, { force: true });
63
+ return {
64
+ text: "kryso remote: unset, so status and ledger read this machine's evidence\n",
65
+ status: 0,
66
+ draw: (s) => (s.brand("remote"), s.ok("unset, so status and ledger read this machine's evidence")),
67
+ };
68
+ }
69
+ const url = check(arg);
70
+ if (!url)
71
+ return refusal(EX_USAGE, "kryso remote", `${JSON.stringify(arg)} is not an http or https URL with no query, fragment or credentials`);
72
+ mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
73
+ const tmp = `${file}.${process.pid}`;
74
+ writeFileSync(tmp, `${url}\n`);
75
+ renameSync(tmp, file);
76
+ return {
77
+ text: `kryso remote: status and ledger read through ${url}\n`,
78
+ status: 0,
79
+ draw: (s) => (s.brand("remote"), s.ok(`status and ledger read through ${s.val(url)}`)),
80
+ };
81
+ }
82
+ catch (e) {
83
+ return refusal(EX_SOFTWARE, "kryso remote", `cannot write ${file}: ${e.message}`);
84
+ }
85
+ }
@@ -0,0 +1 @@
1
+ export {};