@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.
- package/LICENSE +202 -0
- package/README.md +1059 -2
- package/_dist/account-forward.d.ts +12 -0
- package/_dist/account-forward.d.ts.map +1 -0
- package/_dist/accounts.d.ts +23 -0
- package/_dist/accounts.d.ts.map +1 -0
- package/_dist/answer.d.ts +88 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/apply.d.ts +17 -0
- package/_dist/apply.d.ts.map +1 -0
- package/_dist/args.d.ts +86 -0
- package/_dist/args.d.ts.map +1 -0
- package/_dist/commands.d.ts +15 -0
- package/_dist/commands.d.ts.map +1 -0
- package/_dist/complete.d.ts +15 -0
- package/_dist/complete.d.ts.map +1 -0
- package/_dist/config.d.ts +37 -0
- package/_dist/config.d.ts.map +1 -0
- package/_dist/dependencies.d.ts +6 -0
- package/_dist/dependencies.d.ts.map +1 -0
- package/_dist/facets.d.ts +71 -0
- package/_dist/facets.d.ts.map +1 -0
- package/_dist/grammar.d.ts +5 -0
- package/_dist/grammar.d.ts.map +1 -0
- package/_dist/host.d.ts +164 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/install.d.ts +43 -0
- package/_dist/install.d.ts.map +1 -0
- package/_dist/main.d.ts +16 -0
- package/_dist/main.d.ts.map +1 -0
- package/_dist/memo.d.ts +60 -0
- package/_dist/memo.d.ts.map +1 -0
- package/_dist/mod.d.ts +46 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/page.d.ts +73 -0
- package/_dist/page.d.ts.map +1 -0
- package/_dist/platform.d.ts +27 -0
- package/_dist/platform.d.ts.map +1 -0
- package/_dist/reader.d.ts +10 -0
- package/_dist/reader.d.ts.map +1 -0
- package/_dist/release.d.ts +48 -0
- package/_dist/release.d.ts.map +1 -0
- package/_dist/remote.d.ts +13 -0
- package/_dist/remote.d.ts.map +1 -0
- package/_dist/reported.d.ts +11 -0
- package/_dist/reported.d.ts.map +1 -0
- package/_dist/roles.d.ts +2 -0
- package/_dist/roles.d.ts.map +1 -0
- package/_dist/roster.d.ts +41 -0
- package/_dist/roster.d.ts.map +1 -0
- package/_dist/rpc.d.ts +29 -0
- package/_dist/rpc.d.ts.map +1 -0
- package/_dist/run.d.ts +164 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/runtime.d.ts +4 -0
- package/_dist/runtime.d.ts.map +1 -0
- package/_dist/searching.d.ts +7 -0
- package/_dist/searching.d.ts.map +1 -0
- package/_dist/show.d.ts +22 -0
- package/_dist/show.d.ts.map +1 -0
- package/_dist/store.d.ts +25 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/terminal.d.ts +17 -0
- package/_dist/terminal.d.ts.map +1 -0
- package/_dist/tool.d.ts +55 -0
- package/_dist/tool.d.ts.map +1 -0
- package/_dist/tty.d.ts +3 -0
- package/_dist/tty.d.ts.map +1 -0
- package/_dist/vault.d.ts +9 -0
- package/_dist/vault.d.ts.map +1 -0
- package/_dist/words.d.ts +11 -0
- package/_dist/words.d.ts.map +1 -0
- package/_dist/yak.d.ts +10 -0
- package/_dist/yak.d.ts.map +1 -0
- package/account-forward.js +40 -0
- package/account-forward.js.map +1 -0
- package/account-forward.ts +50 -0
- package/accounts.js +162 -0
- package/accounts.js.map +1 -0
- package/accounts.ts +228 -0
- package/answer.js +394 -0
- package/answer.js.map +1 -0
- package/answer.ts +569 -0
- package/apply.js +49 -0
- package/apply.js.map +1 -0
- package/apply.ts +54 -0
- package/args.js +420 -0
- package/args.js.map +1 -0
- package/args.ts +494 -0
- package/code.js +209 -0
- package/code.js.map +1 -0
- package/code.ts +227 -0
- package/commands.js +131 -0
- package/commands.js.map +1 -0
- package/commands.ts +142 -0
- package/complete.js +104 -0
- package/complete.js.map +1 -0
- package/complete.ts +152 -0
- package/config.js +125 -0
- package/config.js.map +1 -0
- package/config.ts +180 -0
- package/declarations.js +67 -0
- package/declarations.js.map +1 -0
- package/declarations.ts +84 -0
- package/deno.json +1 -0
- package/dependencies.js +126 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +141 -0
- package/derive.js +102 -0
- package/derive.js.map +1 -0
- package/derive.ts +125 -0
- package/drain.js +83 -0
- package/drain.js.map +1 -0
- package/drain.ts +115 -0
- package/duties.ts +17 -0
- package/exports.json +1486 -0
- package/external.js +373 -0
- package/external.js.map +1 -0
- package/external.ts +435 -0
- package/facets.js +214 -0
- package/facets.js.map +1 -0
- package/facets.ts +232 -0
- package/feedback.js +54 -0
- package/feedback.js.map +1 -0
- package/feedback.ts +57 -0
- package/files.js +84 -0
- package/files.js.map +1 -0
- package/files.ts +112 -0
- package/get_starts.js +31 -0
- package/get_starts.js.map +1 -0
- package/get_starts.ts +34 -0
- package/git-credential.js +79 -0
- package/git-credential.js.map +1 -0
- package/git-credential.ts +77 -0
- package/grammar.js +4 -0
- package/grammar.js.map +1 -0
- package/grammar.ts +5 -0
- package/held.js +51 -0
- package/held.js.map +1 -0
- package/held.ts +66 -0
- package/host.js +1174 -0
- package/host.js.map +1 -0
- package/host.ts +1343 -0
- package/init.js +138 -0
- package/init.js.map +1 -0
- package/init.ts +139 -0
- package/install.js +216 -0
- package/install.js.map +1 -0
- package/install.ts +223 -0
- package/lifetime.js +56 -0
- package/lifetime.js.map +1 -0
- package/lifetime.ts +66 -0
- package/local-page.ts +114 -0
- package/local.js +417 -0
- package/local.js.map +1 -0
- package/local.ts +537 -0
- package/lockfile.js +20 -0
- package/lockfile.js.map +1 -0
- package/lockfile.ts +32 -0
- package/machines.js +33 -0
- package/machines.js.map +1 -0
- package/machines.ts +51 -0
- package/made/4f855610bdabcd736100d34e4e467ea5.json +1 -0
- package/main.js +534 -0
- package/main.js.map +1 -0
- package/main.ts +547 -0
- package/mcp.js +108 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +113 -0
- package/member.js +103 -0
- package/member.js.map +1 -0
- package/member.ts +133 -0
- package/memo.js +503 -0
- package/memo.js.map +1 -0
- package/memo.ts +634 -0
- package/mod.js +46 -0
- package/mod.js.map +1 -0
- package/mod.ts +99 -0
- package/operations.js +53 -0
- package/operations.js.map +1 -0
- package/operations.ts +68 -0
- package/package.json +115 -4
- package/page.js +211 -0
- package/page.js.map +1 -0
- package/page.ts +308 -0
- package/parser.js +45 -0
- package/parser.js.map +1 -0
- package/parser.ts +77 -0
- package/people-search.js +112 -0
- package/people-search.js.map +1 -0
- package/people-search.ts +162 -0
- package/platform.js +184 -0
- package/platform.js.map +1 -0
- package/platform.ts +224 -0
- package/platform_effects.js +5 -0
- package/platform_effects.js.map +1 -0
- package/platform_effects.ts +13 -0
- package/prepare.js +40 -0
- package/prepare.js.map +1 -0
- package/prepare.ts +46 -0
- package/read_activity.js +54 -0
- package/read_activity.js.map +1 -0
- package/read_activity.ts +61 -0
- package/read_thread.js +387 -0
- package/read_thread.js.map +1 -0
- package/read_thread.ts +506 -0
- package/read_worker.ts +164 -0
- package/reader.js +48 -0
- package/reader.js.map +1 -0
- package/reader.ts +57 -0
- package/release.js +82 -0
- package/release.js.map +1 -0
- package/release.json +127 -0
- package/release.ts +98 -0
- package/remote-page.ts +70 -0
- package/remote.js +96 -0
- package/remote.js.map +1 -0
- package/remote.ts +153 -0
- package/report.js +83 -0
- package/report.js.map +1 -0
- package/report.ts +101 -0
- package/reported.js +32 -0
- package/reported.js.map +1 -0
- package/reported.ts +38 -0
- package/restart.js +349 -0
- package/restart.js.map +1 -0
- package/restart.ts +478 -0
- package/restore.js +32 -0
- package/restore.js.map +1 -0
- package/restore.ts +38 -0
- package/roles.js +32 -0
- package/roles.js.map +1 -0
- package/roles.ts +49 -0
- package/roster.js +65 -0
- package/roster.js.map +1 -0
- package/roster.ts +84 -0
- package/rpc.js +113 -0
- package/rpc.js.map +1 -0
- package/rpc.ts +159 -0
- package/run.js +533 -0
- package/run.js.map +1 -0
- package/run.ts +741 -0
- package/runtime.js +381 -0
- package/runtime.js.map +1 -0
- package/runtime.ts +433 -0
- package/sandboxes.js +26 -0
- package/sandboxes.js.map +1 -0
- package/sandboxes.ts +30 -0
- package/searching.js +33 -0
- package/searching.js.map +1 -0
- package/searching.ts +50 -0
- package/show.js +109 -0
- package/show.js.map +1 -0
- package/show.ts +144 -0
- package/shown.js +13 -0
- package/shown.js.map +1 -0
- package/shown.ts +44 -0
- package/stall.js +90 -0
- package/stall.js.map +1 -0
- package/stall.ts +156 -0
- package/stall_profile.js +113 -0
- package/stall_profile.js.map +1 -0
- package/stall_profile.ts +142 -0
- package/stall_wait.js +183 -0
- package/stall_wait.js.map +1 -0
- package/stall_wait.ts +249 -0
- package/stall_wait_worker.ts +11 -0
- package/start.js +349 -0
- package/start.js.map +1 -0
- package/start.ts +343 -0
- package/starvation.js +147 -0
- package/starvation.js.map +1 -0
- package/starvation.ts +224 -0
- package/store.js +118 -0
- package/store.js.map +1 -0
- package/store.ts +193 -0
- package/subcommands.js +62 -0
- package/subcommands.js.map +1 -0
- package/subcommands.ts +77 -0
- package/terminal.js +93 -0
- package/terminal.js.map +1 -0
- package/terminal.ts +128 -0
- package/text.js +20 -0
- package/text.js.map +1 -0
- package/text.ts +37 -0
- package/tool.js +76 -0
- package/tool.js.map +1 -0
- package/tool.ts +157 -0
- package/trace_control.js +231 -0
- package/trace_control.js.map +1 -0
- package/trace_control.ts +233 -0
- package/traces.js +138 -0
- package/traces.js.map +1 -0
- package/traces.ts +160 -0
- package/tracker.js +22 -0
- package/tracker.js.map +1 -0
- package/tracker.ts +27 -0
- package/tty.js +82 -0
- package/tty.js.map +1 -0
- package/tty.ts +142 -0
- package/unit_failure.js +178 -0
- package/unit_failure.js.map +1 -0
- package/unit_failure.ts +191 -0
- package/upkeep_thread.js +32 -0
- package/upkeep_thread.js.map +1 -0
- package/upkeep_thread.ts +35 -0
- package/upkeep_worker.ts +27 -0
- package/vault.js +186 -0
- package/vault.js.map +1 -0
- package/vault.ts +220 -0
- package/vocab-ids.js +63 -0
- package/vocab-ids.js.map +1 -0
- package/vocab-ids.ts +62 -0
- package/vocab.json +35 -0
- package/warm.js +138 -0
- package/warm.js.map +1 -0
- package/warm.ts +171 -0
- package/watch_revision.js +25 -0
- package/watch_revision.js.map +1 -0
- package/watch_revision.ts +30 -0
- package/words.js +29 -0
- package/words.js.map +1 -0
- package/words.ts +41 -0
- package/workspaces.js +40 -0
- package/workspaces.js.map +1 -0
- package/workspaces.ts +58 -0
- package/yak-unit-failure.conf +6 -0
- package/yak-unit-handoff.conf +2 -0
- package/yak.js +123 -0
- package/yak.js.map +1 -0
- package/yak.ts +136 -0
package/starvation.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// The box can hold a read that its query would not: it keeps the read's
|
|
2
|
+
// worker waiting for a CPU, or keeps the disk so busy that the few pages the
|
|
3
|
+
// read needs take seconds. Every reader waiting then sees the same incident,
|
|
4
|
+
// so it is reported once per box incident, with who was using the CPU or the
|
|
5
|
+
// disk, and never as each read's own slowness.
|
|
6
|
+
import * as disk from '@yaks/file/io'
|
|
7
|
+
import * as system from '@yaks/process/system'
|
|
8
|
+
|
|
9
|
+
let WINDOW = 30_000
|
|
10
|
+
let SAMPLE = 1000
|
|
11
|
+
|
|
12
|
+
let pause = (ms: number) =>
|
|
13
|
+
new Promise<void>((resolve) => setTimeout(resolve, ms))
|
|
14
|
+
|
|
15
|
+
let children = async (path: string): Promise<string[]> => {
|
|
16
|
+
let out: string[] = []
|
|
17
|
+
try {
|
|
18
|
+
for await (let entry of disk.readDir(path)) {
|
|
19
|
+
if (entry.isDirectory) out.push(`${path}/${entry.name}`)
|
|
20
|
+
}
|
|
21
|
+
} catch { /* A transient scope can end between samples. */ }
|
|
22
|
+
return out
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What each of `units` used over one sample, ranked: the units that used any,
|
|
26
|
+
* the four that used most. `use` says one unit's amount. */
|
|
27
|
+
let ranked = (
|
|
28
|
+
units: string[],
|
|
29
|
+
before: number[],
|
|
30
|
+
after: number[],
|
|
31
|
+
use: (amount: number) => string,
|
|
32
|
+
): string[] =>
|
|
33
|
+
units.map((unit, i) => ({
|
|
34
|
+
unit: unit.slice(unit.lastIndexOf('/') + 1),
|
|
35
|
+
used: after[i] - before[i],
|
|
36
|
+
})).filter(({ used }) => used > 0).sort((a, b) => b.used - a.used)
|
|
37
|
+
.slice(0, 4).map(({ unit, used }) => `${unit} (${use(used)})`)
|
|
38
|
+
|
|
39
|
+
let cpuUsage = (path: string) =>
|
|
40
|
+
disk.readTextFile(`${path}/cpu.stat`).then(
|
|
41
|
+
(text) => Number(/^usage_usec (\d+)$/m.exec(text)?.[1]),
|
|
42
|
+
() => NaN,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
// Sample only after a measured scheduler delay. Systemd's cgroup counters
|
|
46
|
+
// include every descendant of a unit, including its short-lived subprocesses.
|
|
47
|
+
let cpuSample = async () => {
|
|
48
|
+
let group = /^0::(.*)$/m.exec(await disk.readTextFile('/proc/self/cgroup'))
|
|
49
|
+
?.[1].match(/^.*?\/user@\d+\.service(?:\/|$)/)?.[0].replace(/\/$/, '')
|
|
50
|
+
if (!group) return 'CPU attribution unavailable for this cgroup'
|
|
51
|
+
let root = `/sys/fs/cgroup${group}`
|
|
52
|
+
let scopes = (await children(root)).filter((path) =>
|
|
53
|
+
!path.endsWith('/init.scope')
|
|
54
|
+
)
|
|
55
|
+
let units = (await Promise.all(
|
|
56
|
+
scopes.map((path) => path.endsWith('.slice') ? children(path) : [path]),
|
|
57
|
+
)).flat()
|
|
58
|
+
let before = await Promise.all(units.map(cpuUsage))
|
|
59
|
+
let at = performance.now()
|
|
60
|
+
await pause(SAMPLE)
|
|
61
|
+
let after = await Promise.all(units.map(cpuUsage))
|
|
62
|
+
return cpuConsumers(units, before, after, performance.now() - at)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export let cpuConsumers = (
|
|
66
|
+
units: string[],
|
|
67
|
+
before: number[],
|
|
68
|
+
after: number[],
|
|
69
|
+
ms: number,
|
|
70
|
+
): string => {
|
|
71
|
+
let top = ranked(
|
|
72
|
+
units,
|
|
73
|
+
before.map((usec) => usec / 1e6),
|
|
74
|
+
after.map((usec) => usec / 1e6),
|
|
75
|
+
(cpu) => `${cpu.toFixed(2)} CPU s`,
|
|
76
|
+
)
|
|
77
|
+
return top.length
|
|
78
|
+
? `Top local cgroups over the next ${(ms / 1000).toFixed(1)} s: ` +
|
|
79
|
+
top.join(', ')
|
|
80
|
+
: 'No local cgroup CPU use could be attributed after the delay'
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** What a process has moved to and from storage, in bytes, and the unit it
|
|
84
|
+
* runs in; undefined for one this user cannot read or that has ended. */
|
|
85
|
+
let processIo = async (pid: string) => {
|
|
86
|
+
try {
|
|
87
|
+
let [io, cgroup] = await Promise.all([
|
|
88
|
+
disk.readTextFile(`/proc/${pid}/io`),
|
|
89
|
+
disk.readTextFile(`/proc/${pid}/cgroup`),
|
|
90
|
+
])
|
|
91
|
+
let bytes = (name: string) =>
|
|
92
|
+
Number(new RegExp(`^${name}: (\\d+)$`, 'm').exec(io)?.[1])
|
|
93
|
+
return {
|
|
94
|
+
unit: /^0::(.*)$/m.exec(cgroup)?.[1] ?? `pid ${pid}`,
|
|
95
|
+
bytes: bytes('read_bytes') + bytes('write_bytes'),
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
return undefined
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let ioByUnit = async (pids: string[]) => {
|
|
103
|
+
let units = new Map<string, number>()
|
|
104
|
+
for (let io of await Promise.all(pids.map(processIo))) {
|
|
105
|
+
if (io && Number.isFinite(io.bytes)) {
|
|
106
|
+
units.set(io.unit, (units.get(io.unit) ?? 0) + io.bytes)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return units
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// The units' io.stat is not kept below the slices, so a unit's disk use is
|
|
113
|
+
// the sum of its processes' (/proc/<pid>/io): what they moved to and from
|
|
114
|
+
// storage, page cache hits left out.
|
|
115
|
+
let diskSample = async () => {
|
|
116
|
+
let pids: string[] = []
|
|
117
|
+
for await (let entry of disk.readDir('/proc')) {
|
|
118
|
+
if (/^\d+$/.test(entry.name)) pids.push(entry.name)
|
|
119
|
+
}
|
|
120
|
+
let before = await ioByUnit(pids)
|
|
121
|
+
let at = performance.now()
|
|
122
|
+
await pause(SAMPLE)
|
|
123
|
+
let after = await ioByUnit(pids)
|
|
124
|
+
let units = [...after.keys()].filter((unit) => before.has(unit))
|
|
125
|
+
return diskConsumers(
|
|
126
|
+
units,
|
|
127
|
+
units.map((unit) => before.get(unit)!),
|
|
128
|
+
units.map((unit) => after.get(unit)!),
|
|
129
|
+
performance.now() - at,
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export let diskConsumers = (
|
|
134
|
+
units: string[],
|
|
135
|
+
before: number[],
|
|
136
|
+
after: number[],
|
|
137
|
+
ms: number,
|
|
138
|
+
): string => {
|
|
139
|
+
let top = ranked(
|
|
140
|
+
units,
|
|
141
|
+
before,
|
|
142
|
+
after,
|
|
143
|
+
(bytes) => `${(bytes / 1e6).toFixed(1)} MB`,
|
|
144
|
+
)
|
|
145
|
+
return top.length
|
|
146
|
+
? `Top local units by disk over the next ${(ms / 1000).toFixed(1)} s: ` +
|
|
147
|
+
top.join(', ')
|
|
148
|
+
: 'No local disk use could be attributed after the delay'
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// All serving processes (including two during handover) share this stamp.
|
|
152
|
+
// Keep the inode so a new observer always locks the same file.
|
|
153
|
+
let claim = (name: string) => async (at: number) => {
|
|
154
|
+
let runtime = system.env.get('XDG_RUNTIME_DIR')
|
|
155
|
+
if (!runtime) return undefined
|
|
156
|
+
let path = `${runtime}/yak-${name}`
|
|
157
|
+
let file = await disk.open(path, { create: true, write: true, mode: 0o600 })
|
|
158
|
+
try {
|
|
159
|
+
await file.lock(true)
|
|
160
|
+
let last = Number(await disk.readTextFile(path))
|
|
161
|
+
if (at <= last) return false
|
|
162
|
+
await disk.writeTextFile(path, String(at))
|
|
163
|
+
return at - last >= WINDOW
|
|
164
|
+
} finally {
|
|
165
|
+
file.close()
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** What one kind of starvation is: what its report says, and how it finds
|
|
170
|
+
* who was using what the read waited for. */
|
|
171
|
+
export type Starvation = {
|
|
172
|
+
name: string
|
|
173
|
+
fault: string
|
|
174
|
+
said: string
|
|
175
|
+
sample: () => Promise<string>
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export let cpuKind: Starvation = {
|
|
179
|
+
name: 'CPUStarvation',
|
|
180
|
+
fault: 'box:cpu-starvation',
|
|
181
|
+
said: 'The box kept a read worker waiting for a CPU',
|
|
182
|
+
sample: cpuSample,
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export let diskKind: Starvation = {
|
|
186
|
+
name: 'DiskStarvation',
|
|
187
|
+
fault: 'box:disk-starvation',
|
|
188
|
+
said: 'The box kept the disk too busy to serve a read worker',
|
|
189
|
+
sample: diskSample,
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Reports `kind` once per box incident: a reader that sees it again within
|
|
193
|
+
* 30 s, or after another process reported it, says nothing. */
|
|
194
|
+
export let starvationWatch = (
|
|
195
|
+
kind: Starvation,
|
|
196
|
+
{
|
|
197
|
+
now = Date.now,
|
|
198
|
+
reserve = claim(kind.fault.slice(kind.fault.indexOf(':') + 1)),
|
|
199
|
+
sample = kind.sample,
|
|
200
|
+
}: {
|
|
201
|
+
now?: () => number
|
|
202
|
+
reserve?: (at: number) => Promise<boolean | undefined>
|
|
203
|
+
sample?: () => Promise<string>
|
|
204
|
+
} = {},
|
|
205
|
+
) => {
|
|
206
|
+
let seen = -Infinity, checked = -Infinity
|
|
207
|
+
return async (evidence: string, report: (error: Error) => void) => {
|
|
208
|
+
let at = now()
|
|
209
|
+
let quiet = at - seen >= WINDOW
|
|
210
|
+
seen = at
|
|
211
|
+
if (at - checked < SAMPLE) return
|
|
212
|
+
checked = at
|
|
213
|
+
let dedup = await reserve(at).catch(() => undefined)
|
|
214
|
+
if (!(dedup ?? quiet)) return
|
|
215
|
+
let activity = await sample().catch(() => 'attribution unavailable')
|
|
216
|
+
report(Object.assign(
|
|
217
|
+
new Error(kind.said, { cause: new Error(`${evidence}\n${activity}`) }),
|
|
218
|
+
{ name: kind.name, fault: kind.fault },
|
|
219
|
+
))
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export let cpuStarvation = starvationWatch(cpuKind)
|
|
224
|
+
export let diskStarvation = starvationWatch(diskKind)
|
package/store.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import * as system from "@yaks/process/system";
|
|
2
|
+
import * as disk from "@yaks/file/io";
|
|
3
|
+
import { sha256 } from "@yaks/graph/sha256";
|
|
4
|
+
let env = (name)=>{
|
|
5
|
+
try {
|
|
6
|
+
return system.env.get(name);
|
|
7
|
+
} catch {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
/** Where this operating system keeps a program's configuration. */ export let configDir = (vars = env)=>{
|
|
12
|
+
let home = vars('HOME') ?? vars('USERPROFILE') ?? '.';
|
|
13
|
+
if (system.build.os == 'windows') {
|
|
14
|
+
return vars('APPDATA') ?? `${home}/AppData/Roaming`;
|
|
15
|
+
}
|
|
16
|
+
if (system.build.os == 'darwin') return `${home}/Library/Preferences`;
|
|
17
|
+
return vars('XDG_CONFIG_HOME') ?? `${home}/.config`;
|
|
18
|
+
};
|
|
19
|
+
/** This program's own directory under it. */ export let stateDir = (vars = env)=>vars('YAKS_HOME') ?? `${configDir(vars)}/yaks`;
|
|
20
|
+
export let rosters = (files = disk)=>{
|
|
21
|
+
let read = (path)=>{
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(files.readTextFileSync(path));
|
|
24
|
+
} catch {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
let write = (dir, path, body)=>{
|
|
29
|
+
files.mkdirSync(dir, {
|
|
30
|
+
recursive: true,
|
|
31
|
+
mode: 0o700
|
|
32
|
+
});
|
|
33
|
+
files.writeTextFileSync(path, JSON.stringify(body, null, 2) + '\n', {
|
|
34
|
+
mode: 0o644
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
// Each host has its own file: reading one roster never parses every other
|
|
38
|
+
// server's tools and vocabulary. The filename holds no host text or secret.
|
|
39
|
+
let rosterPath = (host, dir)=>`${dir}/tools/${sha256(host)}.json`;
|
|
40
|
+
// A file removed, or already gone.
|
|
41
|
+
let gone = (path)=>{
|
|
42
|
+
try {
|
|
43
|
+
files.removeSync(path);
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if (!(e instanceof disk.errors.NotFound)) throw e;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
let saved = (dir, path, roster, fresh = false)=>{
|
|
49
|
+
let temp = `${path}.${crypto.randomUUID()}`;
|
|
50
|
+
try {
|
|
51
|
+
write(`${dir}/tools`, temp, roster);
|
|
52
|
+
if (!fresh) files.renameSync(temp, path);
|
|
53
|
+
else {
|
|
54
|
+
try {
|
|
55
|
+
files.linkSync(temp, path);
|
|
56
|
+
} catch (e) {
|
|
57
|
+
if (!(e instanceof disk.errors.AlreadyExists)) throw e;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
} finally{
|
|
61
|
+
gone(temp);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
// Move every old cache entry once. Existing destinations win after an
|
|
65
|
+
// interrupted move; only a completed move removes the source.
|
|
66
|
+
let migrated = (dir)=>{
|
|
67
|
+
let legacy = `${dir}/tools.json`;
|
|
68
|
+
try {
|
|
69
|
+
files.statSync(legacy);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
if (e instanceof disk.errors.NotFound) return;
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
for (let [host, roster] of Object.entries(read(legacy))){
|
|
75
|
+
let path = rosterPath(host, dir);
|
|
76
|
+
try {
|
|
77
|
+
files.statSync(path);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
if (!(e instanceof disk.errors.NotFound)) throw e;
|
|
80
|
+
saved(dir, path, roster, true);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
files.removeSync(legacy);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
if (!(e instanceof disk.errors.NotFound)) throw e;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
/** The cached tool list for a host, if there is one. */ let cached = async (host, dir = stateDir())=>{
|
|
90
|
+
await migrated(dir);
|
|
91
|
+
let kept = read(rosterPath(host, dir));
|
|
92
|
+
return Array.isArray(kept.tools) ? kept : null;
|
|
93
|
+
};
|
|
94
|
+
/** Cache one. */ let remember = async (host, roster, dir = stateDir())=>{
|
|
95
|
+
await migrated(dir);
|
|
96
|
+
saved(dir, rosterPath(host, dir), roster);
|
|
97
|
+
};
|
|
98
|
+
/** Drop one — the tool list changed, so the next command calls `tools/list`
|
|
99
|
+
* again. */ let forget = async (host, dir = stateDir())=>{
|
|
100
|
+
await migrated(dir);
|
|
101
|
+
try {
|
|
102
|
+
files.removeSync(rosterPath(host, dir));
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (!(e instanceof disk.errors.NotFound)) throw e;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
cached,
|
|
109
|
+
remember,
|
|
110
|
+
forget
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
export { accountToken as tokenFor } from "./accounts.js";
|
|
114
|
+
let roster = rosters();
|
|
115
|
+
export let cached = roster.cached;
|
|
116
|
+
export let remember = roster.remember;
|
|
117
|
+
export let forget = roster.forget;
|
|
118
|
+
//# sourceMappingURL=store.js.map
|
package/store.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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,WAAW,MAAM;IACjC,OAAO,MAAM,OAAO,CAAC,KAAK,KAAK,IAAI,OAAiB;EACtD;EAEA,eAAe,GACf,IAAI,WAAW,OACb,MACA,QACA,MAAc,UAAU;IAExB,MAAM,SAAS;IACf,MAAM,KAAK,WAAW,MAAM,MAAM;EACpC;EAEA;YACU,GACV,IAAI,SAAS,OACX,MACA,MAAc,UAAU;IAExB,MAAM,SAAS;IACf,IAAI;MACF,MAAM,UAAU,CAAC,WAAW,MAAM;IACpC,EAAE,OAAO,GAAG;MACV,IAAI,CAAC,CAAC,aAAa,KAAK,MAAM,CAAC,QAAQ,GAAG,MAAM;IAClD;EACF;EACA,OAAO;IAAE;IAAQ;IAAU;EAAO;AACpC,EAAC;AAED,SAAS,gBAAgB,QAAQ,wBAAuB;AACxD,IAAI,SAAkB;AACtB,OAAO,IAAI,SAA4B,OAAO,MAAM,CAAA;AACpD,OAAO,IAAI,WAAgC,OAAO,QAAQ,CAAA;AAC1D,OAAO,IAAI,SAA4B,OAAO,MAAM,CAAA"}
|
package/store.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import * as system from '@yaks/process/system'
|
|
2
|
+
import * as disk from '@yaks/file/io'
|
|
3
|
+
// What this machine remembers between commands: the bearer token `yak login`
|
|
4
|
+
// saved, and the tool list a host last served.
|
|
5
|
+
//
|
|
6
|
+
// Two files, not one, and the reason is the difference between them. The token
|
|
7
|
+
// is a secret and is written 0600 in a directory of its own at 0700; the cache
|
|
8
|
+
// is a copy of something the server tells anyone who asks. Keeping them apart
|
|
9
|
+
// means loosening the cache's permissions can never loosen the token's, and a
|
|
10
|
+
// cache that has to be deleted takes nothing else with it.
|
|
11
|
+
//
|
|
12
|
+
// The cache is keyed by host and stamped with the roster version (T-34277) —
|
|
13
|
+
// the eight hex characters the `about` tool names its tool list by. Nothing
|
|
14
|
+
// here checks that version, because checking would cost the round trip the
|
|
15
|
+
// cache exists to save. The cache is dropped instead on the two signals that
|
|
16
|
+
// arrive for free — a tool result carrying the roster notice, and an `about`
|
|
17
|
+
// result naming a different version.
|
|
18
|
+
|
|
19
|
+
import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
20
|
+
import { sha256 } from '@yaks/graph/sha256'
|
|
21
|
+
import type { Listed } from './tool.ts'
|
|
22
|
+
|
|
23
|
+
/** The tools a host served, and the version stamp of that list — and the
|
|
24
|
+
* vocabulary its answers are drawn with, once one needed drawing
|
|
25
|
+
* (platform.ts `vocabOf`), dropped with the list it came beside. */
|
|
26
|
+
export type Roster = {
|
|
27
|
+
version?: string
|
|
28
|
+
protocol?: string
|
|
29
|
+
tools: Listed[]
|
|
30
|
+
vocab?: VocabDoc
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Where a variable is read from: this process's environment by default, or
|
|
34
|
+
* a function a test passes so it never sets one every other test shares. */
|
|
35
|
+
export type Env = (name: string) => string | undefined
|
|
36
|
+
|
|
37
|
+
let env: Env = (name) => {
|
|
38
|
+
try {
|
|
39
|
+
return system.env.get(name)
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Where this operating system keeps a program's configuration. */
|
|
46
|
+
export let configDir = (vars: Env = env): string => {
|
|
47
|
+
let home = vars('HOME') ?? vars('USERPROFILE') ?? '.'
|
|
48
|
+
if (system.build.os == 'windows') {
|
|
49
|
+
return vars('APPDATA') ?? `${home}/AppData/Roaming`
|
|
50
|
+
}
|
|
51
|
+
if (system.build.os == 'darwin') return `${home}/Library/Preferences`
|
|
52
|
+
return vars('XDG_CONFIG_HOME') ?? `${home}/.config`
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** This program's own directory under it. */
|
|
56
|
+
export let stateDir = (vars: Env = env): string =>
|
|
57
|
+
vars('YAKS_HOME') ?? `${configDir(vars)}/yaks`
|
|
58
|
+
|
|
59
|
+
/** The filesystem operations roster persistence needs. The migration and
|
|
60
|
+
* per-host cache use the same operations on a disk or an in-memory filesystem. */
|
|
61
|
+
export type RosterFiles = {
|
|
62
|
+
readTextFileSync: (path: string) => string
|
|
63
|
+
mkdirSync: (path: string, options: disk.MkdirOptions) => void
|
|
64
|
+
writeTextFileSync: (
|
|
65
|
+
path: string,
|
|
66
|
+
body: string,
|
|
67
|
+
options: disk.WriteFileOptions,
|
|
68
|
+
) => void
|
|
69
|
+
renameSync: (from: string, to: string) => void
|
|
70
|
+
linkSync: (from: string, to: string) => void
|
|
71
|
+
removeSync: (path: string) => void
|
|
72
|
+
statSync: (path: string) => unknown
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A roster cache over a filesystem. Destinations survive a retried migration,
|
|
76
|
+
* and a forgotten host cannot return from the legacy combined file. */
|
|
77
|
+
/** Where each host's roster is kept between commands. */
|
|
78
|
+
export type Rosters = {
|
|
79
|
+
cached: (host: string, dir?: string) => Promise<Roster | null>
|
|
80
|
+
remember: (host: string, roster: Roster, dir?: string) => Promise<void>
|
|
81
|
+
forget: (host: string, dir?: string) => Promise<void>
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export let rosters = (files: RosterFiles = disk): Rosters => {
|
|
85
|
+
let read = (path: string): Record<string, unknown> => {
|
|
86
|
+
try {
|
|
87
|
+
return JSON.parse(files.readTextFileSync(path))
|
|
88
|
+
} catch {
|
|
89
|
+
return {}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
let write = (dir: string, path: string, body: unknown) => {
|
|
93
|
+
files.mkdirSync(dir, { recursive: true, mode: 0o700 })
|
|
94
|
+
files.writeTextFileSync(path, JSON.stringify(body, null, 2) + '\n', {
|
|
95
|
+
mode: 0o644,
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Each host has its own file: reading one roster never parses every other
|
|
100
|
+
// server's tools and vocabulary. The filename holds no host text or secret.
|
|
101
|
+
let rosterPath = (host: string, dir: string): string =>
|
|
102
|
+
`${dir}/tools/${sha256(host)}.json`
|
|
103
|
+
// A file removed, or already gone.
|
|
104
|
+
let gone = (path: string) => {
|
|
105
|
+
try {
|
|
106
|
+
files.removeSync(path)
|
|
107
|
+
} catch (e) {
|
|
108
|
+
if (!(e instanceof disk.errors.NotFound)) throw e
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
let saved = (dir: string, path: string, roster: unknown, fresh = false) => {
|
|
112
|
+
let temp = `${path}.${crypto.randomUUID()}`
|
|
113
|
+
try {
|
|
114
|
+
write(`${dir}/tools`, temp, roster)
|
|
115
|
+
if (!fresh) files.renameSync(temp, path)
|
|
116
|
+
else {
|
|
117
|
+
try {
|
|
118
|
+
files.linkSync(temp, path)
|
|
119
|
+
} catch (e) {
|
|
120
|
+
if (!(e instanceof disk.errors.AlreadyExists)) throw e
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} finally {
|
|
124
|
+
gone(temp)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Move every old cache entry once. Existing destinations win after an
|
|
128
|
+
// interrupted move; only a completed move removes the source.
|
|
129
|
+
let migrated = (dir: string): void => {
|
|
130
|
+
let legacy = `${dir}/tools.json`
|
|
131
|
+
try {
|
|
132
|
+
files.statSync(legacy)
|
|
133
|
+
} catch (e) {
|
|
134
|
+
if (e instanceof disk.errors.NotFound) return
|
|
135
|
+
throw e
|
|
136
|
+
}
|
|
137
|
+
for (let [host, roster] of Object.entries(read(legacy))) {
|
|
138
|
+
let path = rosterPath(host, dir)
|
|
139
|
+
try {
|
|
140
|
+
files.statSync(path)
|
|
141
|
+
} catch (e) {
|
|
142
|
+
if (!(e instanceof disk.errors.NotFound)) throw e
|
|
143
|
+
saved(dir, path, roster, true)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
files.removeSync(legacy)
|
|
148
|
+
} catch (e) {
|
|
149
|
+
if (!(e instanceof disk.errors.NotFound)) throw e
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The cached tool list for a host, if there is one. */
|
|
154
|
+
let cached = async (
|
|
155
|
+
host: string,
|
|
156
|
+
dir: string = stateDir(),
|
|
157
|
+
): Promise<Roster | null> => {
|
|
158
|
+
await migrated(dir)
|
|
159
|
+
let kept = read(rosterPath(host, dir))
|
|
160
|
+
return Array.isArray(kept.tools) ? kept as Roster : null
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Cache one. */
|
|
164
|
+
let remember = async (
|
|
165
|
+
host: string,
|
|
166
|
+
roster: Roster,
|
|
167
|
+
dir: string = stateDir(),
|
|
168
|
+
): Promise<void> => {
|
|
169
|
+
await migrated(dir)
|
|
170
|
+
saved(dir, rosterPath(host, dir), roster)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Drop one — the tool list changed, so the next command calls `tools/list`
|
|
174
|
+
* again. */
|
|
175
|
+
let forget = async (
|
|
176
|
+
host: string,
|
|
177
|
+
dir: string = stateDir(),
|
|
178
|
+
): Promise<void> => {
|
|
179
|
+
await migrated(dir)
|
|
180
|
+
try {
|
|
181
|
+
files.removeSync(rosterPath(host, dir))
|
|
182
|
+
} catch (e) {
|
|
183
|
+
if (!(e instanceof disk.errors.NotFound)) throw e
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return { cached, remember, forget }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { accountToken as tokenFor } from './accounts.js'
|
|
190
|
+
let roster: Rosters = rosters()
|
|
191
|
+
export let cached: Rosters['cached'] = roster.cached
|
|
192
|
+
export let remember: Rosters['remember'] = roster.remember
|
|
193
|
+
export let forget: Rosters['forget'] = roster.forget
|
package/subcommands.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The tools of the graph a config names that are offered on a command line,
|
|
2
|
+
// as the subcommands a person types: the list `cli` gathers when the command
|
|
3
|
+
// named a config (run.ts `more`). They are read off the plugins' words
|
|
4
|
+
// (./words.ts) and their terminal controls (`./cli`), so listing them opens
|
|
5
|
+
// nothing and loads none of the code that opens a graph: a usage page, a help
|
|
6
|
+
// page and a mistyped word cost a command no more than the declarations. The
|
|
7
|
+
// one a command line runs is asked of the `yak serve` running over that graph
|
|
8
|
+
// (./warm.ts), and only where none can answer it imports the code that opens
|
|
9
|
+
// the graph here (./local.ts).
|
|
10
|
+
import { read, subpath } from "./config.js";
|
|
11
|
+
import { declarations } from "./declarations.js";
|
|
12
|
+
import { prepare } from "./prepare.js";
|
|
13
|
+
import { traceCommand } from "./trace_control.js";
|
|
14
|
+
import { warm } from "./warm.js";
|
|
15
|
+
/** The subcommands of the graph `c.config` names: `yak trace`, its plugins'
|
|
16
|
+
* terminal controls, then every tool it offers on a command line. */ export let commands = async (c)=>{
|
|
17
|
+
let config = read(c.config);
|
|
18
|
+
let said = await declarations(config, true);
|
|
19
|
+
let listing = {
|
|
20
|
+
config,
|
|
21
|
+
said
|
|
22
|
+
};
|
|
23
|
+
let local = (command, selected)=>prepare(config, command, selected, !c.json);
|
|
24
|
+
let tools = said.tools.map((declared)=>({
|
|
25
|
+
...declared,
|
|
26
|
+
globals: said.controls.find(({ plugin })=>plugin == said.owners[declared.name])?.value?.globals?.[declared.name],
|
|
27
|
+
// A tool arrives declaring its arguments as JSON Schema — the same
|
|
28
|
+
// document `tools/list` sends — so a command typed against a local graph
|
|
29
|
+
// and the same command typed against an MCP server are written
|
|
30
|
+
// identically. Nothing is converted here: a transport that wants them in
|
|
31
|
+
// another form restates them on its own side (@yaks/mcp `core`).
|
|
32
|
+
run: async (args)=>{
|
|
33
|
+
let plugin = said.owners[declared.name];
|
|
34
|
+
let control = said.controls.find((v)=>v.plugin == plugin)?.value;
|
|
35
|
+
if (control?.tools?.includes(declared.name)) {
|
|
36
|
+
let cli = await subpath(plugin, 'cli');
|
|
37
|
+
let code = await cli?.tools?.[declared.name]?.(args, c);
|
|
38
|
+
if (code !== undefined) return code;
|
|
39
|
+
}
|
|
40
|
+
let code = await warm(c, config, declared, args);
|
|
41
|
+
if (code !== undefined) return code;
|
|
42
|
+
return (await local(declared)).tool(c, declared, args, listing);
|
|
43
|
+
}
|
|
44
|
+
}));
|
|
45
|
+
let controls = said.controls.flatMap(({ plugin, value })=>(value?.commands ?? []).map((declared, i)=>({
|
|
46
|
+
...declared,
|
|
47
|
+
run: async (args, context)=>{
|
|
48
|
+
let ready = await local(declared, [
|
|
49
|
+
plugin,
|
|
50
|
+
'cli'
|
|
51
|
+
]);
|
|
52
|
+
let control = (await subpath(plugin, 'cli')).commands[i];
|
|
53
|
+
return ready.control(context, declared, control.run, args, listing);
|
|
54
|
+
}
|
|
55
|
+
})));
|
|
56
|
+
return [
|
|
57
|
+
traceCommand(config),
|
|
58
|
+
...controls,
|
|
59
|
+
...tools
|
|
60
|
+
];
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=subcommands.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subcommands.js","sources":["./subcommands.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,uEAAuE;AACvE,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,+BAA+B;AAE/B,SAAsB,IAAI,EAAE,OAAO,sBAAqB;AAExD,SAA4B,YAAY,4BAA2B;AACnE,SAAS,OAAO,uBAAsB;AAEtC,SAAS,YAAY,6BAA4B;AACjD,SAAS,IAAI,oBAAmB;AAUhC;mEACmE,GACnE,OAAO,IAAI,WAAW,OAAO;EAC3B,IAAI,SAAS,KAAK,EAAE,MAAM;EAC1B,IAAI,OAAO,MAAM,aAAa,QAAQ;EACtC,IAAI,UAAmB;IAAE;IAAQ;EAAK;EACtC,IAAI,QAAQ,CACV,SACA,WACG,QAAQ,QAAQ,SAAS,UAAU,CAAC,EAAE,IAAI;EAC/C,IAAI,QAAQ,KAAK,KAAK,CAAC,GAAG,CAAC,CAAC,WAAa,CAAC;MACxC,GAAG,QAAQ;MACX,SAAS,KAAK,QA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
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// The tools of the graph a config names that are offered on a command line,
|
|
2
|
+
// as the subcommands a person types: the list `cli` gathers when the command
|
|
3
|
+
// named a config (run.ts `more`). They are read off the plugins' words
|
|
4
|
+
// (./words.ts) and their terminal controls (`./cli`), so listing them opens
|
|
5
|
+
// nothing and loads none of the code that opens a graph: a usage page, a help
|
|
6
|
+
// page and a mistyped word cost a command no more than the declarations. The
|
|
7
|
+
// one a command line runs is asked of the `yak serve` running over that graph
|
|
8
|
+
// (./warm.ts), and only where none can answer it imports the code that opens
|
|
9
|
+
// the graph here (./local.ts).
|
|
10
|
+
|
|
11
|
+
import { type Config, read, subpath } from './config.js'
|
|
12
|
+
import type { CliFacet } from './host.ts'
|
|
13
|
+
import { type Declarations, declarations } from './declarations.js'
|
|
14
|
+
import { prepare } from './prepare.js'
|
|
15
|
+
import type { Command, Ctx } from './run.ts'
|
|
16
|
+
import { traceCommand } from './trace_control.js'
|
|
17
|
+
import { warm } from './warm.js'
|
|
18
|
+
|
|
19
|
+
/** What a listing read before any graph was open: the config, its plugins'
|
|
20
|
+
* words, and the terminal controls each plugin's `./cli` holds, beside the
|
|
21
|
+
* plugin it came from. */
|
|
22
|
+
export type Listing = {
|
|
23
|
+
config: Config
|
|
24
|
+
said: Declarations
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The subcommands of the graph `c.config` names: `yak trace`, its plugins'
|
|
28
|
+
* terminal controls, then every tool it offers on a command line. */
|
|
29
|
+
export let commands = async (c: Ctx): Promise<Command[]> => {
|
|
30
|
+
let config = read(c.config!)
|
|
31
|
+
let said = await declarations(config, true)
|
|
32
|
+
let listing: Listing = { config, said }
|
|
33
|
+
let local = (
|
|
34
|
+
command: Pick<Command, 'name' | 'roles'>,
|
|
35
|
+
selected?: readonly [plugin: string, facet: 'cli' | 'tools'],
|
|
36
|
+
) => prepare(config, command, selected, !c.json)
|
|
37
|
+
let tools = said.tools.map((declared) => ({
|
|
38
|
+
...declared,
|
|
39
|
+
globals: said.controls.find(({ plugin }) =>
|
|
40
|
+
plugin == said.owners[declared.name]
|
|
41
|
+
)?.value?.globals?.[declared.name],
|
|
42
|
+
// A tool arrives declaring its arguments as JSON Schema — the same
|
|
43
|
+
// document `tools/list` sends — so a command typed against a local graph
|
|
44
|
+
// and the same command typed against an MCP server are written
|
|
45
|
+
// identically. Nothing is converted here: a transport that wants them in
|
|
46
|
+
// another form restates them on its own side (@yaks/mcp `core`).
|
|
47
|
+
run: async (args: Record<string, unknown>): Promise<number> => {
|
|
48
|
+
let plugin = said.owners[declared.name]
|
|
49
|
+
let control = said.controls.find((v) => v.plugin == plugin)?.value
|
|
50
|
+
if (control?.tools?.includes(declared.name)) {
|
|
51
|
+
let cli = await subpath<CliFacet>(plugin, 'cli')
|
|
52
|
+
let code = await cli?.tools?.[declared.name]?.(args, c)
|
|
53
|
+
if (code !== undefined) return code
|
|
54
|
+
}
|
|
55
|
+
let code = await warm(c, config, declared, args)
|
|
56
|
+
if (code !== undefined) return code
|
|
57
|
+
return (await local(declared)).tool(c, declared, args, listing)
|
|
58
|
+
},
|
|
59
|
+
}))
|
|
60
|
+
let controls: Command[] = said.controls.flatMap(({ plugin, value }) =>
|
|
61
|
+
(value?.commands ?? []).map((declared, i) => ({
|
|
62
|
+
...declared,
|
|
63
|
+
run: async (args: Record<string, unknown>, context: Ctx) => {
|
|
64
|
+
let ready = await local(declared, [plugin, 'cli'])
|
|
65
|
+
let control = (await subpath<CliFacet>(plugin, 'cli'))!.commands![i]
|
|
66
|
+
return ready.control(
|
|
67
|
+
context,
|
|
68
|
+
declared,
|
|
69
|
+
control.run,
|
|
70
|
+
args,
|
|
71
|
+
listing,
|
|
72
|
+
)
|
|
73
|
+
},
|
|
74
|
+
}))
|
|
75
|
+
)
|
|
76
|
+
return [traceCommand(config), ...controls, ...tools]
|
|
77
|
+
}
|