@yaks/cli 0.0.0-stage → 0.2.36

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 (331) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1059 -2
  3. package/_dist/account-forward.d.ts +12 -0
  4. package/_dist/account-forward.d.ts.map +1 -0
  5. package/_dist/accounts.d.ts +23 -0
  6. package/_dist/accounts.d.ts.map +1 -0
  7. package/_dist/answer.d.ts +88 -0
  8. package/_dist/answer.d.ts.map +1 -0
  9. package/_dist/apply.d.ts +17 -0
  10. package/_dist/apply.d.ts.map +1 -0
  11. package/_dist/args.d.ts +86 -0
  12. package/_dist/args.d.ts.map +1 -0
  13. package/_dist/commands.d.ts +15 -0
  14. package/_dist/commands.d.ts.map +1 -0
  15. package/_dist/complete.d.ts +15 -0
  16. package/_dist/complete.d.ts.map +1 -0
  17. package/_dist/config.d.ts +37 -0
  18. package/_dist/config.d.ts.map +1 -0
  19. package/_dist/dependencies.d.ts +6 -0
  20. package/_dist/dependencies.d.ts.map +1 -0
  21. package/_dist/facets.d.ts +71 -0
  22. package/_dist/facets.d.ts.map +1 -0
  23. package/_dist/grammar.d.ts +5 -0
  24. package/_dist/grammar.d.ts.map +1 -0
  25. package/_dist/host.d.ts +164 -0
  26. package/_dist/host.d.ts.map +1 -0
  27. package/_dist/install.d.ts +43 -0
  28. package/_dist/install.d.ts.map +1 -0
  29. package/_dist/main.d.ts +16 -0
  30. package/_dist/main.d.ts.map +1 -0
  31. package/_dist/memo.d.ts +60 -0
  32. package/_dist/memo.d.ts.map +1 -0
  33. package/_dist/mod.d.ts +46 -0
  34. package/_dist/mod.d.ts.map +1 -0
  35. package/_dist/page.d.ts +73 -0
  36. package/_dist/page.d.ts.map +1 -0
  37. package/_dist/platform.d.ts +27 -0
  38. package/_dist/platform.d.ts.map +1 -0
  39. package/_dist/reader.d.ts +10 -0
  40. package/_dist/reader.d.ts.map +1 -0
  41. package/_dist/release.d.ts +48 -0
  42. package/_dist/release.d.ts.map +1 -0
  43. package/_dist/remote.d.ts +13 -0
  44. package/_dist/remote.d.ts.map +1 -0
  45. package/_dist/reported.d.ts +11 -0
  46. package/_dist/reported.d.ts.map +1 -0
  47. package/_dist/roles.d.ts +2 -0
  48. package/_dist/roles.d.ts.map +1 -0
  49. package/_dist/roster.d.ts +41 -0
  50. package/_dist/roster.d.ts.map +1 -0
  51. package/_dist/rpc.d.ts +29 -0
  52. package/_dist/rpc.d.ts.map +1 -0
  53. package/_dist/run.d.ts +164 -0
  54. package/_dist/run.d.ts.map +1 -0
  55. package/_dist/runtime.d.ts +4 -0
  56. package/_dist/runtime.d.ts.map +1 -0
  57. package/_dist/searching.d.ts +7 -0
  58. package/_dist/searching.d.ts.map +1 -0
  59. package/_dist/show.d.ts +22 -0
  60. package/_dist/show.d.ts.map +1 -0
  61. package/_dist/store.d.ts +25 -0
  62. package/_dist/store.d.ts.map +1 -0
  63. package/_dist/terminal.d.ts +17 -0
  64. package/_dist/terminal.d.ts.map +1 -0
  65. package/_dist/tool.d.ts +55 -0
  66. package/_dist/tool.d.ts.map +1 -0
  67. package/_dist/tty.d.ts +3 -0
  68. package/_dist/tty.d.ts.map +1 -0
  69. package/_dist/vault.d.ts +9 -0
  70. package/_dist/vault.d.ts.map +1 -0
  71. package/_dist/words.d.ts +11 -0
  72. package/_dist/words.d.ts.map +1 -0
  73. package/_dist/yak.d.ts +10 -0
  74. package/_dist/yak.d.ts.map +1 -0
  75. package/account-forward.js +40 -0
  76. package/account-forward.js.map +1 -0
  77. package/account-forward.ts +50 -0
  78. package/accounts.js +162 -0
  79. package/accounts.js.map +1 -0
  80. package/accounts.ts +228 -0
  81. package/answer.js +394 -0
  82. package/answer.js.map +1 -0
  83. package/answer.ts +569 -0
  84. package/apply.js +49 -0
  85. package/apply.js.map +1 -0
  86. package/apply.ts +54 -0
  87. package/args.js +420 -0
  88. package/args.js.map +1 -0
  89. package/args.ts +494 -0
  90. package/code.js +209 -0
  91. package/code.js.map +1 -0
  92. package/code.ts +227 -0
  93. package/commands.js +131 -0
  94. package/commands.js.map +1 -0
  95. package/commands.ts +142 -0
  96. package/complete.js +104 -0
  97. package/complete.js.map +1 -0
  98. package/complete.ts +152 -0
  99. package/config.js +125 -0
  100. package/config.js.map +1 -0
  101. package/config.ts +180 -0
  102. package/declarations.js +67 -0
  103. package/declarations.js.map +1 -0
  104. package/declarations.ts +84 -0
  105. package/deno.json +1 -0
  106. package/dependencies.js +126 -0
  107. package/dependencies.js.map +1 -0
  108. package/dependencies.ts +141 -0
  109. package/derive.js +102 -0
  110. package/derive.js.map +1 -0
  111. package/derive.ts +125 -0
  112. package/drain.js +83 -0
  113. package/drain.js.map +1 -0
  114. package/drain.ts +115 -0
  115. package/duties.ts +17 -0
  116. package/exports.json +1486 -0
  117. package/external.js +373 -0
  118. package/external.js.map +1 -0
  119. package/external.ts +435 -0
  120. package/facets.js +214 -0
  121. package/facets.js.map +1 -0
  122. package/facets.ts +232 -0
  123. package/feedback.js +54 -0
  124. package/feedback.js.map +1 -0
  125. package/feedback.ts +57 -0
  126. package/files.js +84 -0
  127. package/files.js.map +1 -0
  128. package/files.ts +112 -0
  129. package/get_starts.js +31 -0
  130. package/get_starts.js.map +1 -0
  131. package/get_starts.ts +34 -0
  132. package/git-credential.js +79 -0
  133. package/git-credential.js.map +1 -0
  134. package/git-credential.ts +77 -0
  135. package/grammar.js +4 -0
  136. package/grammar.js.map +1 -0
  137. package/grammar.ts +5 -0
  138. package/held.js +51 -0
  139. package/held.js.map +1 -0
  140. package/held.ts +66 -0
  141. package/host.js +1174 -0
  142. package/host.js.map +1 -0
  143. package/host.ts +1343 -0
  144. package/init.js +138 -0
  145. package/init.js.map +1 -0
  146. package/init.ts +139 -0
  147. package/install.js +216 -0
  148. package/install.js.map +1 -0
  149. package/install.ts +223 -0
  150. package/lifetime.js +56 -0
  151. package/lifetime.js.map +1 -0
  152. package/lifetime.ts +66 -0
  153. package/local-page.ts +114 -0
  154. package/local.js +417 -0
  155. package/local.js.map +1 -0
  156. package/local.ts +537 -0
  157. package/lockfile.js +20 -0
  158. package/lockfile.js.map +1 -0
  159. package/lockfile.ts +32 -0
  160. package/machines.js +33 -0
  161. package/machines.js.map +1 -0
  162. package/machines.ts +51 -0
  163. package/made/4f855610bdabcd736100d34e4e467ea5.json +1 -0
  164. package/main.js +534 -0
  165. package/main.js.map +1 -0
  166. package/main.ts +547 -0
  167. package/mcp.js +108 -0
  168. package/mcp.js.map +1 -0
  169. package/mcp.ts +113 -0
  170. package/member.js +103 -0
  171. package/member.js.map +1 -0
  172. package/member.ts +133 -0
  173. package/memo.js +503 -0
  174. package/memo.js.map +1 -0
  175. package/memo.ts +634 -0
  176. package/mod.js +46 -0
  177. package/mod.js.map +1 -0
  178. package/mod.ts +99 -0
  179. package/operations.js +53 -0
  180. package/operations.js.map +1 -0
  181. package/operations.ts +68 -0
  182. package/package.json +115 -4
  183. package/page.js +211 -0
  184. package/page.js.map +1 -0
  185. package/page.ts +308 -0
  186. package/parser.js +45 -0
  187. package/parser.js.map +1 -0
  188. package/parser.ts +77 -0
  189. package/people-search.js +112 -0
  190. package/people-search.js.map +1 -0
  191. package/people-search.ts +162 -0
  192. package/platform.js +184 -0
  193. package/platform.js.map +1 -0
  194. package/platform.ts +224 -0
  195. package/platform_effects.js +5 -0
  196. package/platform_effects.js.map +1 -0
  197. package/platform_effects.ts +13 -0
  198. package/prepare.js +40 -0
  199. package/prepare.js.map +1 -0
  200. package/prepare.ts +46 -0
  201. package/read_activity.js +54 -0
  202. package/read_activity.js.map +1 -0
  203. package/read_activity.ts +61 -0
  204. package/read_thread.js +387 -0
  205. package/read_thread.js.map +1 -0
  206. package/read_thread.ts +506 -0
  207. package/read_worker.ts +164 -0
  208. package/reader.js +48 -0
  209. package/reader.js.map +1 -0
  210. package/reader.ts +57 -0
  211. package/release.js +82 -0
  212. package/release.js.map +1 -0
  213. package/release.json +127 -0
  214. package/release.ts +98 -0
  215. package/remote-page.ts +70 -0
  216. package/remote.js +96 -0
  217. package/remote.js.map +1 -0
  218. package/remote.ts +153 -0
  219. package/report.js +83 -0
  220. package/report.js.map +1 -0
  221. package/report.ts +101 -0
  222. package/reported.js +32 -0
  223. package/reported.js.map +1 -0
  224. package/reported.ts +38 -0
  225. package/restart.js +349 -0
  226. package/restart.js.map +1 -0
  227. package/restart.ts +478 -0
  228. package/restore.js +32 -0
  229. package/restore.js.map +1 -0
  230. package/restore.ts +38 -0
  231. package/roles.js +32 -0
  232. package/roles.js.map +1 -0
  233. package/roles.ts +49 -0
  234. package/roster.js +65 -0
  235. package/roster.js.map +1 -0
  236. package/roster.ts +84 -0
  237. package/rpc.js +113 -0
  238. package/rpc.js.map +1 -0
  239. package/rpc.ts +159 -0
  240. package/run.js +533 -0
  241. package/run.js.map +1 -0
  242. package/run.ts +741 -0
  243. package/runtime.js +381 -0
  244. package/runtime.js.map +1 -0
  245. package/runtime.ts +433 -0
  246. package/sandboxes.js +26 -0
  247. package/sandboxes.js.map +1 -0
  248. package/sandboxes.ts +30 -0
  249. package/searching.js +33 -0
  250. package/searching.js.map +1 -0
  251. package/searching.ts +50 -0
  252. package/show.js +109 -0
  253. package/show.js.map +1 -0
  254. package/show.ts +144 -0
  255. package/shown.js +13 -0
  256. package/shown.js.map +1 -0
  257. package/shown.ts +44 -0
  258. package/stall.js +90 -0
  259. package/stall.js.map +1 -0
  260. package/stall.ts +156 -0
  261. package/stall_profile.js +113 -0
  262. package/stall_profile.js.map +1 -0
  263. package/stall_profile.ts +142 -0
  264. package/stall_wait.js +183 -0
  265. package/stall_wait.js.map +1 -0
  266. package/stall_wait.ts +249 -0
  267. package/stall_wait_worker.ts +11 -0
  268. package/start.js +349 -0
  269. package/start.js.map +1 -0
  270. package/start.ts +343 -0
  271. package/starvation.js +147 -0
  272. package/starvation.js.map +1 -0
  273. package/starvation.ts +224 -0
  274. package/store.js +118 -0
  275. package/store.js.map +1 -0
  276. package/store.ts +193 -0
  277. package/subcommands.js +62 -0
  278. package/subcommands.js.map +1 -0
  279. package/subcommands.ts +77 -0
  280. package/terminal.js +93 -0
  281. package/terminal.js.map +1 -0
  282. package/terminal.ts +128 -0
  283. package/text.js +20 -0
  284. package/text.js.map +1 -0
  285. package/text.ts +37 -0
  286. package/tool.js +76 -0
  287. package/tool.js.map +1 -0
  288. package/tool.ts +157 -0
  289. package/trace_control.js +231 -0
  290. package/trace_control.js.map +1 -0
  291. package/trace_control.ts +233 -0
  292. package/traces.js +138 -0
  293. package/traces.js.map +1 -0
  294. package/traces.ts +160 -0
  295. package/tracker.js +22 -0
  296. package/tracker.js.map +1 -0
  297. package/tracker.ts +27 -0
  298. package/tty.js +82 -0
  299. package/tty.js.map +1 -0
  300. package/tty.ts +142 -0
  301. package/unit_failure.js +178 -0
  302. package/unit_failure.js.map +1 -0
  303. package/unit_failure.ts +191 -0
  304. package/upkeep_thread.js +32 -0
  305. package/upkeep_thread.js.map +1 -0
  306. package/upkeep_thread.ts +35 -0
  307. package/upkeep_worker.ts +27 -0
  308. package/vault.js +186 -0
  309. package/vault.js.map +1 -0
  310. package/vault.ts +220 -0
  311. package/vocab-ids.js +63 -0
  312. package/vocab-ids.js.map +1 -0
  313. package/vocab-ids.ts +62 -0
  314. package/vocab.json +35 -0
  315. package/warm.js +138 -0
  316. package/warm.js.map +1 -0
  317. package/warm.ts +171 -0
  318. package/watch_revision.js +25 -0
  319. package/watch_revision.js.map +1 -0
  320. package/watch_revision.ts +30 -0
  321. package/words.js +29 -0
  322. package/words.js.map +1 -0
  323. package/words.ts +41 -0
  324. package/workspaces.js +40 -0
  325. package/workspaces.js.map +1 -0
  326. package/workspaces.ts +58 -0
  327. package/yak-unit-failure.conf +6 -0
  328. package/yak-unit-handoff.conf +2 -0
  329. package/yak.js +123 -0
  330. package/yak.js.map +1 -0
  331. package/yak.ts +136 -0
package/show.js ADDED
@@ -0,0 +1,109 @@
1
+ // What reaches the terminal: the tool listing and the help pages, both
2
+ // generated from a tool's own input schema, and the boundary everything
3
+ // printed crosses first.
4
+ //
5
+ // Anything printed here may have come from a remote server, so it passes
6
+ // through `safe` first: control characters are stripped, tabs become spaces
7
+ // chosen here rather than cursor movement chosen by the terminal, and only
8
+ // `\n` survives. A tool's output is content, never an escape sequence.
9
+ import { clearable } from "./args.js";
10
+ import { commandOf, titleOf, typeOf } from "./tool.js";
11
+ // deno-lint-ignore no-control-regex -- control characters are the subject
12
+ let ctrl = /[\x00-\x1f\x7f-\x9f]/g;
13
+ /** Text from a server, with every escape sequence a terminal could act on
14
+ * removed. */ export let safe = (text)=>text.replaceAll('\t', ' ').replace(ctrl, (c)=>c == '\n' ? c : '');
15
+ let pad = (s, n)=>s.padEnd(n);
16
+ /** Every tool, one per line, with the short label that says what it is. */ export let toolLines = (tools)=>{
17
+ let wide = Math.max(0, ...tools.map((t)=>commandOf(t).length));
18
+ return tools.map((t)=>` ${pad(commandOf(t), wide)} ${titleOf(t)}`.trimEnd()).join('\n');
19
+ };
20
+ // `--name <type>`, in brackets when the argument is optional.
21
+ let slot = (name, p, need, reserved)=>{
22
+ let said = reserved ? `${name}=<${typeOf(p)}>` : `--${name} ${typeOf(p) == 'boolean' ? '' : `<${typeOf(p)}>`}`.trim();
23
+ return need ? said : `[${said}]`;
24
+ };
25
+ /**
26
+ * The argument part of the command line to type: the positional arguments in
27
+ * order, then every other property as an option, in brackets where it is
28
+ * optional. The tool's own schema is the whole grammar, so this is the only
29
+ * place that decides how a tool is written on a command line.
30
+ */ export let sketch = (t, omit = [], reserved = [])=>{
31
+ let schema = t.inputSchema ?? {};
32
+ let props = schema.properties ?? {};
33
+ let need = new Set(schema.required ?? []);
34
+ let positional = t.positional ?? [];
35
+ let names = positional.map((n)=>n.replace(/\.\.\.$/, ''));
36
+ let forward = t.forward;
37
+ let named = Object.keys(props).filter((n)=>!names.includes(n) && n != forward && !omit.includes(n));
38
+ return [
39
+ ...positional.map((n, i)=>need.has(names[i]) ? `<${n}>` : `[${n}]`),
40
+ ...forward ? [
41
+ '[arguments ...]'
42
+ ] : [],
43
+ ...named.map((n)=>slot(n, props[n], need.has(n), reserved.includes(n))),
44
+ ...clearable(t).length ? [
45
+ '[--clear <field>]'
46
+ ] : []
47
+ ].join(' ');
48
+ };
49
+ /** The whole command line to type, without the program's own name. */ export let lineOf = (t, omit = [], reserved = [])=>`${commandOf(t)} ${sketch(t, omit, reserved)}`.trimEnd();
50
+ /** One tool's help page: the command line to type, what it is for, and a row
51
+ * per argument with its type, whether it is required, and the description the
52
+ * schema gives it. */ export let toolHelp = (t, program = 'yak', reserved = [])=>{
53
+ let schema = t.inputSchema ?? {};
54
+ let props = schema.properties ?? {};
55
+ let need = new Set(schema.required ?? []);
56
+ let names = Object.keys(props);
57
+ let head = `${program} ${lineOf(t, [], reserved)}`.trimEnd();
58
+ let label = (n)=>reserved.includes(n) ? `${n}=` : `--${n}`;
59
+ let wide = Math.max(0, ...names.map((n)=>label(n).length));
60
+ let kind = Math.max(0, ...names.map((n)=>typeOf(props[n]).length));
61
+ let rows = names.map((n)=>{
62
+ let p = props[n];
63
+ let about = [
64
+ p.description ?? '',
65
+ p.enum ? `one of ${p.enum.join(', ')}` : '',
66
+ p.default !== undefined ? `default ${JSON.stringify(p.default)}` : ''
67
+ ].filter(Boolean).join(' — ');
68
+ return ` ${pad(label(n), wide)} ${pad(typeOf(p), kind)} ${pad(need.has(n) ? 'required' : '', 8)} ${about}`.trimEnd();
69
+ });
70
+ return [
71
+ head,
72
+ ...t.description ? [
73
+ '',
74
+ wrap(t.description, 76, ' ')
75
+ ] : [],
76
+ ...rows.length ? [
77
+ '',
78
+ ...rows
79
+ ] : [
80
+ '',
81
+ ' (no arguments)'
82
+ ],
83
+ ...clearable(t).length ? [
84
+ ` --clear <field> Clear ${clearable(t).join(', ')}; repeat for more.`
85
+ ] : [],
86
+ ...names.some((n)=>typeOf(props[n]) == 'string') ? [
87
+ '',
88
+ ` A value written @path is read from that file, and - from stdin,`,
89
+ ` so text of several lines (a list, a body) needs no shell quoting.`
90
+ ] : []
91
+ ].join('\n');
92
+ };
93
+ /** Wrap prose to a width, so a description written for a model is readable in
94
+ * a terminal. */ export let wrap = (text, width, lead = '')=>{
95
+ let out = [];
96
+ for (let para of text.split('\n')){
97
+ let line = lead;
98
+ for (let word of para.split(/\s+/).filter(Boolean)){
99
+ if (line.length > lead.length && line.length + 1 + word.length > width) {
100
+ out.push(line);
101
+ line = lead;
102
+ }
103
+ line += (line.length > lead.length ? ' ' : '') + word;
104
+ }
105
+ out.push(line);
106
+ }
107
+ return out.join('\n');
108
+ };
109
+ //# sourceMappingURL=show.js.map
package/show.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"show.js","sources":["./show.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,wEAAwE;AACxE,yBAAyB;AACzB,EAAE;AACF,yEAAyE;AACzE,4EAA4E;AAC5E,2EAA2E;AAC3E,uEAAuE;AAEvE,SAAS,SAAS,oBAAiC;AACnD,SAAS,SAAS,EAA0B,OAAO,EAAE,MAAM,oBAAmB;AAE9E,0EAA0E;AAC1E,IAAI,OAAO;AAEX;YACY,GACZ,OAAO,IAAI,OAAO,CAAC,OACjB,KAAK,UAAU,CAAC,MAAM,MAAM,OAAO,CAAC,MAAM,CAAC,IAAM,KAAK,OAAO,IAAI,IAAG;AAEtE,IAAI,MAAM,CAAC,GAAW,IAAsB,EAAE,MAAM,CAAC;AAErD,yEAAyE,GACzE,OAAO,IAAI,YAAY,CACrB;EAEA,IAAI,OAAO,KAAK,GAAG,CAAC,MAAM,MAAM,GAAG,CAAC,CAAC,IAAM,UAAU,GAAG,MAAM;EAC9D,OAAO,MACJ,GAAG,CAAC,CAAC,IAAM,CAAC,EAAE,EAAE,IAAI,UAAU,IAAI,MAAM,EAAE,EAAE,QAAQ,IAAI,CAAC,OAAO,IAChE,IAAI,CAAC;AACV,EAAC;AAED,8DAA8D;AAC9D,IAAI,OAAO,CACT,MACA,GACA,MACA;EAEA,IAAI,OAAO,WACP,GAAG,KAAK,EAAE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E,OAAO,CAAC,OAAO;EACtB;EACA,OAAO;IACL;OACI,EAAE,WAAW,GAAG;MAAC;MAAI,KAAK,EAAE,WAAW,EAAE,IAAI;KAAM,GAAG,EAAE;OACxD,KAAK,MAAM,GAAG;MAAC;SAAO;KAAK,GAAG;MAAC;MAAI;KAAmB;OACtD,UAAU,GAAG,MAAM,GACnB;MACA,CAAC,yBAAyB,EAAE,UAAU,GAAG,IAAI,CAAC,MAAM,kBAAkB,CAAC;KACxE,GACC,EAAE;OACF,MAAM,IAAI,CAAC,CAAC,IAAM,OAAO,KAAK,CAAC,EAAE,KAAK,YACtC;MACA;MACA,CAAC,iEAAiE,CAAC;MACnE,CAAC,mEAAmE,CAAC;KACtE,GACC,EAAE;GACP,CAAC,IAAI,CAAC;AACT,EAAC;AAED;eACe,GACf,OAAO,IAAI,OAAO,CAAC,MAAc,OAAe,OAAO,EAAE;EACvD,IAAI,MAAgB,EAAE;EACtB,KAAK,IAAI,QAAQ,KAAK,KAAK,CAAC,MAAO;IACjC,IAAI,OAAO;IACX,KAAK,IAAI,QAAQ,KAAK,KAAK,CAAC,OAAO,MAAM,CAAC,SAAU;MAClD,IAAI,KAAK,MAAM,GAAG,KAAK,MAAM,IAAI,KAAK,MAAM,GAAG,IAAI,KAAK,MAAM,GAAG,OAAO;QACtE,IAAI,IAAI,CAAC;QACT,OAAO;MACT;MACA,QAAQ,CAAC,KAAK,MAAM,GAAG,KAAK,MAAM,GAAG,MAAM,EAAE,IAAI;IACnD;IACA,IAAI,IAAI,CAAC;EACX;EACA,OAAO,IAAI,IAAI,CAAC;AAClB,EAAC"}
package/show.ts ADDED
@@ -0,0 +1,144 @@
1
+ // What reaches the terminal: the tool listing and the help pages, both
2
+ // generated from a tool's own input schema, and the boundary everything
3
+ // printed crosses first.
4
+ //
5
+ // Anything printed here may have come from a remote server, so it passes
6
+ // through `safe` first: control characters are stripped, tabs become spaces
7
+ // chosen here rather than cursor movement chosen by the terminal, and only
8
+ // `\n` survives. A tool's output is content, never an escape sequence.
9
+
10
+ import { clearable, type Grammar } from './args.js'
11
+ import { commandOf, type Prop, type Schema, titleOf, typeOf } from './tool.js'
12
+
13
+ // deno-lint-ignore no-control-regex -- control characters are the subject
14
+ let ctrl = /[\x00-\x1f\x7f-\x9f]/g
15
+
16
+ /** Text from a server, with every escape sequence a terminal could act on
17
+ * removed. */
18
+ export let safe = (text: string): string =>
19
+ text.replaceAll('\t', ' ').replace(ctrl, (c) => c == '\n' ? c : '')
20
+
21
+ let pad = (s: string, n: number): string => s.padEnd(n)
22
+
23
+ /** Every tool, one per line, with the short label that says what it is. */
24
+ export let toolLines = (
25
+ tools: (Grammar & { title?: string; description?: string })[],
26
+ ): string => {
27
+ let wide = Math.max(0, ...tools.map((t) => commandOf(t).length))
28
+ return tools
29
+ .map((t) => ` ${pad(commandOf(t), wide)} ${titleOf(t)}`.trimEnd())
30
+ .join('\n')
31
+ }
32
+
33
+ // `--name <type>`, in brackets when the argument is optional.
34
+ let slot = (
35
+ name: string,
36
+ p: Prop | undefined,
37
+ need: boolean,
38
+ reserved: boolean,
39
+ ): string => {
40
+ let said = reserved
41
+ ? `${name}=<${typeOf(p)}>`
42
+ : `--${name} ${typeOf(p) == 'boolean' ? '' : `<${typeOf(p)}>`}`
43
+ .trim()
44
+ return need ? said : `[${said}]`
45
+ }
46
+
47
+ /**
48
+ * The argument part of the command line to type: the positional arguments in
49
+ * order, then every other property as an option, in brackets where it is
50
+ * optional. The tool's own schema is the whole grammar, so this is the only
51
+ * place that decides how a tool is written on a command line.
52
+ */
53
+ export let sketch = (
54
+ t: Grammar,
55
+ omit: readonly string[] = [],
56
+ reserved: readonly string[] = [],
57
+ ): string => {
58
+ let schema = (t.inputSchema ?? {}) as Schema
59
+ let props = schema.properties ?? {}
60
+ let need = new Set(schema.required ?? [])
61
+ let positional = t.positional ?? []
62
+ let names = positional.map((n) => n.replace(/\.\.\.$/, ''))
63
+ let forward = t.forward
64
+ let named = Object.keys(props).filter(
65
+ (n) => !names.includes(n) && n != forward && !omit.includes(n),
66
+ )
67
+ return [
68
+ ...positional.map((n, i) => need.has(names[i]) ? `<${n}>` : `[${n}]`),
69
+ ...(forward ? ['[arguments ...]'] : []),
70
+ ...named.map((n) => slot(n, props[n], need.has(n), reserved.includes(n))),
71
+ ...(clearable(t).length ? ['[--clear <field>]'] : []),
72
+ ].join(' ')
73
+ }
74
+
75
+ /** The whole command line to type, without the program's own name. */
76
+ export let lineOf = (
77
+ t: Grammar,
78
+ omit: readonly string[] = [],
79
+ reserved: readonly string[] = [],
80
+ ): string => `${commandOf(t)} ${sketch(t, omit, reserved)}`.trimEnd()
81
+
82
+ /** One tool's help page: the command line to type, what it is for, and a row
83
+ * per argument with its type, whether it is required, and the description the
84
+ * schema gives it. */
85
+ export let toolHelp = (
86
+ t: Grammar & { description?: string },
87
+ program = 'yak',
88
+ reserved: readonly string[] = [],
89
+ ): string => {
90
+ let schema = (t.inputSchema ?? {}) as Schema
91
+ let props = schema.properties ?? {}
92
+ let need = new Set(schema.required ?? [])
93
+ let names = Object.keys(props)
94
+ let head = `${program} ${lineOf(t, [], reserved)}`.trimEnd()
95
+ let label = (n: string) => reserved.includes(n) ? `${n}=` : `--${n}`
96
+ let wide = Math.max(0, ...names.map((n) => label(n).length))
97
+ let kind = Math.max(0, ...names.map((n) => typeOf(props[n]).length))
98
+ let rows = names.map((n) => {
99
+ let p = props[n]
100
+ let about = [
101
+ p.description ?? '',
102
+ p.enum ? `one of ${p.enum.join(', ')}` : '',
103
+ p.default !== undefined ? `default ${JSON.stringify(p.default)}` : '',
104
+ ].filter(Boolean).join(' — ')
105
+ return ` ${pad(label(n), wide)} ${pad(typeOf(p), kind)} ${
106
+ pad(need.has(n) ? 'required' : '', 8)
107
+ } ${about}`.trimEnd()
108
+ })
109
+ return [
110
+ head,
111
+ ...(t.description ? ['', wrap(t.description, 76, ' ')] : []),
112
+ ...(rows.length ? ['', ...rows] : ['', ' (no arguments)']),
113
+ ...(clearable(t).length
114
+ ? [
115
+ ` --clear <field> Clear ${clearable(t).join(', ')}; repeat for more.`,
116
+ ]
117
+ : []),
118
+ ...(names.some((n) => typeOf(props[n]) == 'string')
119
+ ? [
120
+ '',
121
+ ` A value written @path is read from that file, and - from stdin,`,
122
+ ` so text of several lines (a list, a body) needs no shell quoting.`,
123
+ ]
124
+ : []),
125
+ ].join('\n')
126
+ }
127
+
128
+ /** Wrap prose to a width, so a description written for a model is readable in
129
+ * a terminal. */
130
+ export let wrap = (text: string, width: number, lead = ''): string => {
131
+ let out: string[] = []
132
+ for (let para of text.split('\n')) {
133
+ let line = lead
134
+ for (let word of para.split(/\s+/).filter(Boolean)) {
135
+ if (line.length > lead.length && line.length + 1 + word.length > width) {
136
+ out.push(line)
137
+ line = lead
138
+ }
139
+ line += (line.length > lead.length ? ' ' : '') + word
140
+ }
141
+ out.push(line)
142
+ }
143
+ return out.join('\n')
144
+ }
package/shown.js ADDED
@@ -0,0 +1,13 @@
1
+ // What a command line shows: an answer drawn with its config's plugin views
2
+ // (tty.ts `show`). Finding a plugin's views reads the disk (./config.ts).
3
+ import { terminal } from "./answer.js";
4
+ import { pluginTui, pluginViews } from "./config.js";
5
+ import { show } from "./tty.js";
6
+ /** An answer drawn as a command line shows one, with the views of `plugins`
7
+ * ({@link show}): held or painted in their terminal components where `c`
8
+ * holds or paints, printed otherwise. `from` is where the drawing reads what
9
+ * it needs beside the answer. */ export let shown = async (c, plugins, views, vocab, said, from, tool, observe)=>show(c, views, vocab, said, c.tui || c.tty ? {
10
+ views: await terminal(plugins, pluginViews, pluginTui, observe),
11
+ config: c.config
12
+ } : {}, from, tool);
13
+ //# sourceMappingURL=shown.js.map
package/shown.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shown.js","sources":["./shown.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,0EAA0E;AAM1E,SAAkC,QAAQ,sBAA+B;AACzE,SAAS,SAAS,EAAE,WAAW,sBAAqB;AACpD,SAAS,IAAI,mBAAkB;AAE/B;;;+BAG+B,GAC/B,OAAO,IAAI,QAAQ,OACjB,GAMA,SACA,OACA,OACA,MACA,MACA,MACA,UAEA,KACE,GACA,OACA,OACA,MACA,EAAE,GAAG,IAAI,EAAE,GAAG,GACV;IACA,OAAO,MAAM,SAAS,SAAS,aAAa,WAAW;IACvD,QAAQ,EAAE,MAAM;EAClB,IACE,CAAC,GACL,MACA,MACD"}
package/shown.ts ADDED
@@ -0,0 +1,44 @@
1
+ // What a command line shows: an answer drawn with its config's plugin views
2
+ // (tty.ts `show`). Finding a plugin's views reads the disk (./config.ts).
3
+
4
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
5
+ import type { Registry } from 'jsr:@yaks/render@0.2.36'
6
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36/runtime'
7
+ import type { AnatomyObserver } from 'jsr:@yaks/code@0.2.36/anatomy'
8
+ import { type Drawn, type Source, terminal, type Tty } from './answer.js'
9
+ import { pluginTui, pluginViews } from './config.js'
10
+ import { show } from './tty.js'
11
+
12
+ /** An answer drawn as a command line shows one, with the views of `plugins`
13
+ * ({@link show}): held or painted in their terminal components where `c`
14
+ * holds or paints, printed otherwise. `from` is where the drawing reads what
15
+ * it needs beside the answer. */
16
+ export let shown = async (
17
+ c: {
18
+ tui: boolean
19
+ tty?: Tty
20
+ out: (line: string) => void
21
+ config?: string
22
+ },
23
+ plugins: string[],
24
+ views: Registry,
25
+ vocab: Vocab,
26
+ said: Bundle[],
27
+ from: Source,
28
+ tool: Drawn,
29
+ observe?: AnatomyObserver,
30
+ ): Promise<void> =>
31
+ show(
32
+ c,
33
+ views,
34
+ vocab,
35
+ said,
36
+ c.tui || c.tty
37
+ ? {
38
+ views: await terminal(plugins, pluginViews, pluginTui, observe),
39
+ config: c.config,
40
+ }
41
+ : {},
42
+ from,
43
+ tool,
44
+ )
package/stall.js ADDED
@@ -0,0 +1,90 @@
1
+ import * as system from "@yaks/process/system";
2
+ import * as disk from "@yaks/file/io";
3
+ let readBytes = (thread)=>{
4
+ try {
5
+ let text = disk.readTextFileSync(`/proc/${thread}/io`);
6
+ let bytes = Number(/^read_bytes: (\d+)$/m.exec(text)?.[1]);
7
+ return Number.isFinite(bytes) ? bytes : undefined;
8
+ } catch {
9
+ return undefined;
10
+ }
11
+ };
12
+ /** What `thread` (a path under /proc: `thread-self`, or `<pid>/task/<tid>`)
13
+ * has spent; undefined where the platform does not say. */ export let spent = (thread)=>{
14
+ try {
15
+ let [cpu, wait] = disk.readTextFileSync(`/proc/${thread}/schedstat`).split(' ').map(Number);
16
+ if (!Number.isFinite(wait)) return undefined;
17
+ let read = readBytes(thread);
18
+ return {
19
+ cpu: cpu / 1e6,
20
+ wait: wait / 1e6,
21
+ ...read == undefined ? {} : {
22
+ read
23
+ }
24
+ };
25
+ } catch {
26
+ return undefined;
27
+ }
28
+ };
29
+ /** This thread's path under /proc, as {@link spent} takes it from another
30
+ * thread of the same process; undefined where the platform has none. */ export let thread = ()=>{
31
+ try {
32
+ return disk.readLinkSync('/proc/thread-self');
33
+ } catch {
34
+ return undefined;
35
+ }
36
+ };
37
+ /// since({ cpu: 1, wait: 2 }, { cpu: 4, wait: 7 }) -> { cpu: 3, wait: 5 }
38
+ /// since({ cpu: 1, wait: 2, read: 10 }, { cpu: 4, wait: 7, read: 30 }) -> { cpu: 3, wait: 5, read: 20 }
39
+ /// since(undefined, { cpu: 4, wait: 7 }) -> undefined
40
+ /** What was spent between two readings of one thread. */ export let since = (a, b)=>a && b ? {
41
+ cpu: b.cpu - a.cpu,
42
+ wait: b.wait - a.wait,
43
+ ...a.read != undefined && b.read != undefined ? {
44
+ read: b.read - a.read
45
+ } : {}
46
+ } : undefined;
47
+ /// own(10_000, { cpu: 2000, wait: 7000 }) -> 3000
48
+ /// own(10_000, undefined) -> 10_000
49
+ /** A span's own time: `ms` of wall clock less what the thread waited for a
50
+ * CPU in it. Where the platform does not say, all of it. */ export let own = (ms, s)=>ms - (s?.wait ?? 0);
51
+ let secs = (ms)=>`${(ms / 1000).toFixed(1)} s`;
52
+ /// how('its worker', 10_000, { cpu: 1500, wait: 7000 }) -> 'in 10.0 s its worker ran 1.5 s on a CPU, waited 7.0 s for one and was off one 1.5 s'
53
+ /// how('its worker', 10_000, { cpu: 500, wait: 0, read: 81920 }) -> 'in 10.0 s its worker ran 0.5 s on a CPU, waited 0.0 s for one and was off one 9.5 s, and had the disk read 0.1 MB'
54
+ /// how('its worker', 10_000) -> 'the platform does not say how its worker spent 10.0 s'
55
+ /** How `who` spent `ms` of wall clock. Time off a CPU not waiting for one is
56
+ * a call that blocked it: a lock, the disk, memory paged back in. */ export let how = (who, ms, s)=>s ? `in ${secs(ms)} ${who} ran ${secs(s.cpu)} on a CPU, waited ` + `${secs(s.wait)} for one and was off one ${secs(off(ms, s))}` + (s.read == undefined ? '' : `, and had the disk read ${(s.read / 1e6).toFixed(1)} MB`) : `the platform does not say how ${who} spent ${secs(ms)}`;
57
+ /// off(10_000, { cpu: 1500, wait: 7000 }) -> 1500
58
+ /** The ms of `ms` a thread spent off a CPU, not waiting for one: blocked. */ export let off = (ms, s)=>Math.max(0, ms - s.cpu - s.wait);
59
+ /** Watches this thread's event loop with a tick every `every` ms. A tick that
60
+ * comes `after` ms late or more of its own time means the thread was held by
61
+ * its own work or a call that blocked: that is told to `report`, with how it
62
+ * spent the time. One late only because the box kept it waiting for a CPU is
63
+ * a line to `note`. Returns its stop. */ export let stalls = ({ report, note = console.warn, every = 1000, after = 5000, read = ()=>spent('thread-self'), now = ()=>performance.now(), sample, tick = (fn, ms)=>{
64
+ let id = system.setInterval(fn, ms);
65
+ system.unrefTimer(id);
66
+ return ()=>system.clearInterval(id);
67
+ } })=>{
68
+ let last = now(), was = read();
69
+ return tick(()=>{
70
+ let at = now(), is = read();
71
+ let gap = at - last, s = since(was, is);
72
+ last = at;
73
+ was = is;
74
+ let late = gap - every;
75
+ let held = late >= after && own(late, s) >= after;
76
+ let sampled = sample?.(gap, held);
77
+ if (late < after) return;
78
+ let said = `the main thread was held ${secs(late)}`;
79
+ let why = how('the main thread', gap, s);
80
+ if (own(late, s) < after) return note(`${said}, by the box: ${why}`);
81
+ let tell = (stack)=>report(Object.assign(new Error(said, {
82
+ cause: new Error(why + (stack ? `\nsampled while held:\n${stack}` : ''))
83
+ }), {
84
+ name: 'Stalled'
85
+ }));
86
+ if (!sampled) return tell();
87
+ void sampled.then(tell, (error)=>tell(`stack sampling failed: ${String(error)}`));
88
+ }, every);
89
+ };
90
+ //# sourceMappingURL=stall.js.map
package/stall.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stall.js","sources":["./stall.ts"],"names":[],"mappings":"AAAA,YAAY,wCAA6C;AACzD,YAAY,+BAAoC;AAahD,IAAI,YAAY,CAAC;EACf,IAAI;IACF,IAAI,OAAO,KAAK,gBAAgB,CAAC,CAAC,MAAM,EAAE,OAAO,GAAG,CAAC;IACrD,IAAI,QAAQ,OAAO,uBAAuB,IAAI,CAAC,OAAO,CAAC,EAAE;IACzD,OAAO,OAAO,QAAQ,CAAC,SAAS,QAAQ;EAC1C,EAAE,OAAM;IACN,OAAO;EACT;AACF;AAEA;yDACyD,GACzD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI;IACF,IAAI,CAAC,KAAK,KAAK,GAAG,KAAK,gBAAgB,CAAC,CAAC,MAAM,EAAE,OAAO,UAAU,CAAC,EAChE,KAAK,CAAC,KAAK,GAAG,CAAC;IAClB,IAAI,CAAC,OAAO,QAAQ,CAAC,OAAO,OAAO;IACnC,IAAI,OAAO,UAAU;IACrB,OAAO;MACL,KAAK,MAAM;MACX,MAAM,OAAO;MACb,GAAG,QAAQ,YAAY,CAAC,IAAI;QAAE;MAAK,CAAC;IACtC;EACF,EAAE,OAAM;IACN,OAAO;EACT;AACF,EAAC;AAED;sEACsE,GACtE,OAAO,IAAI,SAAS;EAClB,IAAI;IACF,OAAO,KAAK,YAAY,CAAC;EAC3B,EAAE,OAAM;IACN,OAAO;EACT;AACF,EAAC;AAED,0EAA0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,QAAQ,IAAI,EACnB,QAAQ,IAAI,EACZ,QAAQ,IAAI,EACZ,OAAO,IAAM,MAAM,cAAc,EACjC,MAAM,IAAM,YAAY,GAAG,EAAE,EAC7B,MAAM,EACN,OAAO,CAAC,IAAgB;EACtB,IAAI,KAAK,OAAO,WAAW,CAAC,IAAI;EAChC,OAAO,UAAU,CAAC;EAClB,OAAO,IAAM,OAAO,aAAa,CAAC;AACpC,CAAC,EAeF;EACC,IAAI,OAAO,OAAO,MAAM;EACxB,OAAO,KAAK;IACV,IAAI,KAAK,OAAO,KAAK;IACrB,IAAI,MAAM,KAAK,MAAM,IAAI,MAAM,KAAK;IACpC,OAAO;IACP,MAAM;IACN,IAAI,OAAO,MAAM;IACjB,IAAI,OAAO,QAAQ,SAAS,IAAI,MAAM,MAAM;IAC5C,IAAI,UAAU,SAAS,KAAK;IAC5B,IAAI,OAAO,OAAO;IAClB,IAAI,OAAO,CAAC,yBAAyB,EAAE,KAAK,OAAO;IACnD,IAAI,MAAM,IAAI,mBAAmB,KAAK;IACtC,IAAI,IAAI,MAAM,KAAK,OAAO,OAAO,KAAK,GAAG,KAAK,cAAc,EAAE,KAAK;IACnE,IAAI,OAAO,CAAC,QACV,OAAO,OAAO,MAAM,CAClB,IAAI,MAAM,MAAM;QACd,OAAO,IAAI,MACT,MAAM,CAAC,QAAQ,CAAC,uBAAuB,EAAE,OAAO,GAAG,EAAE;MAEzD,IACA;QAAE,MAAM;MAAU;IAEtB,IAAI,CAAC,SAAS,OAAO;IACrB,KAAK,QAAQ,IAAI,CACf,MACA,CAAC,QAAU,KAAK,CAAC,uBAAuB,EAAE,OAAO,QAAQ;EAE7D,GAAG;AACL,EAAC"}
package/stall.ts ADDED
@@ -0,0 +1,156 @@
1
+ import * as system from '@yaks/process/system'
2
+ import * as disk from '@yaks/file/io'
3
+ // How long a thread was held, and whether the box held it. Linux keeps, for
4
+ // each thread, the time it ran on a CPU and the time it waited, runnable, for
5
+ // one (/proc/<thread>/schedstat). A span's own time is its wall clock less
6
+ // that wait: what it would have taken on an idle box. A busy box stretches
7
+ // every thread's wall clock by its wait and by nothing else, so a span judged
8
+ // by its own time reports what the code did, never how busy the box was.
9
+
10
+ /** What a thread has spent since it started, in ms: on a CPU, and waiting
11
+ * for one; and the bytes it had storage read for it (page cache hits left
12
+ * out), where the platform says. */
13
+ export type Spent = { cpu: number; wait: number; read?: number }
14
+
15
+ let readBytes = (thread: string): number | undefined => {
16
+ try {
17
+ let text = disk.readTextFileSync(`/proc/${thread}/io`)
18
+ let bytes = Number(/^read_bytes: (\d+)$/m.exec(text)?.[1])
19
+ return Number.isFinite(bytes) ? bytes : undefined
20
+ } catch {
21
+ return undefined
22
+ }
23
+ }
24
+
25
+ /** What `thread` (a path under /proc: `thread-self`, or `<pid>/task/<tid>`)
26
+ * has spent; undefined where the platform does not say. */
27
+ export let spent = (thread: string): Spent | undefined => {
28
+ try {
29
+ let [cpu, wait] = disk.readTextFileSync(`/proc/${thread}/schedstat`)
30
+ .split(' ').map(Number)
31
+ if (!Number.isFinite(wait)) return undefined
32
+ let read = readBytes(thread)
33
+ return {
34
+ cpu: cpu / 1e6,
35
+ wait: wait / 1e6,
36
+ ...read == undefined ? {} : { read },
37
+ }
38
+ } catch {
39
+ return undefined
40
+ }
41
+ }
42
+
43
+ /** This thread's path under /proc, as {@link spent} takes it from another
44
+ * thread of the same process; undefined where the platform has none. */
45
+ export let thread = (): string | undefined => {
46
+ try {
47
+ return disk.readLinkSync('/proc/thread-self')
48
+ } catch {
49
+ return undefined
50
+ }
51
+ }
52
+
53
+ /// since({ cpu: 1, wait: 2 }, { cpu: 4, wait: 7 }) -> { cpu: 3, wait: 5 }
54
+ /// since({ cpu: 1, wait: 2, read: 10 }, { cpu: 4, wait: 7, read: 30 }) -> { cpu: 3, wait: 5, read: 20 }
55
+ /// since(undefined, { cpu: 4, wait: 7 }) -> undefined
56
+ /** What was spent between two readings of one thread. */
57
+ export let since = (a?: Spent, b?: Spent): Spent | undefined =>
58
+ a && b
59
+ ? {
60
+ cpu: b.cpu - a.cpu,
61
+ wait: b.wait - a.wait,
62
+ ...a.read != undefined && b.read != undefined
63
+ ? { read: b.read - a.read }
64
+ : {},
65
+ }
66
+ : undefined
67
+
68
+ /// own(10_000, { cpu: 2000, wait: 7000 }) -> 3000
69
+ /// own(10_000, undefined) -> 10_000
70
+ /** A span's own time: `ms` of wall clock less what the thread waited for a
71
+ * CPU in it. Where the platform does not say, all of it. */
72
+ export let own = (ms: number, s?: Spent): number => ms - (s?.wait ?? 0)
73
+
74
+ let secs = (ms: number) => `${(ms / 1000).toFixed(1)} s`
75
+
76
+ /// how('its worker', 10_000, { cpu: 1500, wait: 7000 }) -> 'in 10.0 s its worker ran 1.5 s on a CPU, waited 7.0 s for one and was off one 1.5 s'
77
+ /// how('its worker', 10_000, { cpu: 500, wait: 0, read: 81920 }) -> 'in 10.0 s its worker ran 0.5 s on a CPU, waited 0.0 s for one and was off one 9.5 s, and had the disk read 0.1 MB'
78
+ /// how('its worker', 10_000) -> 'the platform does not say how its worker spent 10.0 s'
79
+ /** How `who` spent `ms` of wall clock. Time off a CPU not waiting for one is
80
+ * a call that blocked it: a lock, the disk, memory paged back in. */
81
+ export let how = (who: string, ms: number, s?: Spent): string =>
82
+ s
83
+ ? `in ${secs(ms)} ${who} ran ${secs(s.cpu)} on a CPU, waited ` +
84
+ `${secs(s.wait)} for one and was off one ${secs(off(ms, s))}` +
85
+ (s.read == undefined
86
+ ? ''
87
+ : `, and had the disk read ${(s.read / 1e6).toFixed(1)} MB`)
88
+ : `the platform does not say how ${who} spent ${secs(ms)}`
89
+
90
+ /// off(10_000, { cpu: 1500, wait: 7000 }) -> 1500
91
+ /** The ms of `ms` a thread spent off a CPU, not waiting for one: blocked. */
92
+ export let off = (ms: number, s: Spent): number =>
93
+ Math.max(0, ms - s.cpu - s.wait)
94
+
95
+ /** Watches this thread's event loop with a tick every `every` ms. A tick that
96
+ * comes `after` ms late or more of its own time means the thread was held by
97
+ * its own work or a call that blocked: that is told to `report`, with how it
98
+ * spent the time. One late only because the box kept it waiting for a CPU is
99
+ * a line to `note`. Returns its stop. */
100
+ export let stalls = ({
101
+ report,
102
+ note = console.warn,
103
+ every = 1000,
104
+ after = 5000,
105
+ read = () => spent('thread-self'),
106
+ now = () => performance.now(),
107
+ sample,
108
+ tick = (fn: () => void, ms: number) => {
109
+ let id = system.setInterval(fn, ms)
110
+ system.unrefTimer(id)
111
+ return () => system.clearInterval(id)
112
+ },
113
+ }: {
114
+ report: (error: Error) => void
115
+ note?: (line: string) => void
116
+ every?: number
117
+ after?: number
118
+ read?: () => Spent | undefined
119
+ now?: () => number
120
+ /** Rotate sparse stack samples; a held tick receives its running stacks. */
121
+ sample?: (
122
+ ms: number,
123
+ held: boolean,
124
+ ) => Promise<string | undefined> | undefined
125
+ /** run `fn` every `ms` and return its stop */
126
+ tick?: (fn: () => void, ms: number) => () => void
127
+ }): () => void => {
128
+ let last = now(), was = read()
129
+ return tick(() => {
130
+ let at = now(), is = read()
131
+ let gap = at - last, s = since(was, is)
132
+ last = at
133
+ was = is
134
+ let late = gap - every
135
+ let held = late >= after && own(late, s) >= after
136
+ let sampled = sample?.(gap, held)
137
+ if (late < after) return
138
+ let said = `the main thread was held ${secs(late)}`
139
+ let why = how('the main thread', gap, s)
140
+ if (own(late, s) < after) return note(`${said}, by the box: ${why}`)
141
+ let tell = (stack?: string) =>
142
+ report(Object.assign(
143
+ new Error(said, {
144
+ cause: new Error(
145
+ why + (stack ? `\nsampled while held:\n${stack}` : ''),
146
+ ),
147
+ }),
148
+ { name: 'Stalled' },
149
+ ))
150
+ if (!sampled) return tell()
151
+ void sampled.then(
152
+ tell,
153
+ (error) => tell(`stack sampling failed: ${String(error)}`),
154
+ )
155
+ }, every)
156
+ }