@yaks/machine 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 +243 -2
- package/_dist/comp.d.ts +5 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/connectors.d.ts +29 -0
- package/_dist/connectors.d.ts.map +1 -0
- package/_dist/effects.d.ts +5 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/execution.d.ts +6 -0
- package/_dist/execution.d.ts.map +1 -0
- package/_dist/graph.d.ts +4 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/mod.d.ts +12 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/recorded.d.ts +4 -0
- package/_dist/recorded.d.ts.map +1 -0
- package/_dist/room.d.ts +45 -0
- package/_dist/room.d.ts.map +1 -0
- package/_dist/targets.d.ts +27 -0
- package/_dist/targets.d.ts.map +1 -0
- package/_dist/tools.d.ts +51 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vocab.d.ts +5 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/workspace.d.ts +64 -0
- package/_dist/workspace.d.ts.map +1 -0
- package/_dist/workspaces.d.ts +10 -0
- package/_dist/workspaces.d.ts.map +1 -0
- package/comp.js +3 -0
- package/comp.js.map +1 -0
- package/comp.ts +3 -0
- package/connectors.js +149 -0
- package/connectors.js.map +1 -0
- package/connectors.ts +221 -0
- package/deno.json +1 -0
- package/effect-vocab.json +40 -0
- package/effects.js +71 -0
- package/effects.js.map +1 -0
- package/effects.ts +93 -0
- package/execution.js +239 -0
- package/execution.js.map +1 -0
- package/execution.ts +264 -0
- package/graph.js +70 -0
- package/graph.js.map +1 -0
- package/graph.ts +60 -0
- package/mod.js +5 -0
- package/mod.js.map +1 -0
- package/mod.ts +20 -0
- package/package.json +62 -4
- package/recorded.js +108 -0
- package/recorded.js.map +1 -0
- package/recorded.ts +74 -0
- package/room.js +160 -0
- package/room.js.map +1 -0
- package/room.ts +228 -0
- package/targets.js +127 -0
- package/targets.js.map +1 -0
- package/targets.ts +137 -0
- package/tools.js +346 -0
- package/tools.js.map +1 -0
- package/tools.ts +444 -0
- package/vocab.js +17 -0
- package/vocab.js.map +1 -0
- package/vocab.json +69 -0
- package/vocab.ts +13 -0
- package/workspace.js +79 -0
- package/workspace.js.map +1 -0
- package/workspace.ts +181 -0
- package/workspaces.js +38 -0
- package/workspaces.js.map +1 -0
- package/workspaces.ts +49 -0
package/room.ts
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// A machine's room: available memory and CPU, I/O and memory pressure.
|
|
2
|
+
// Read them in one control call and judge them with pure functions. Work
|
|
3
|
+
// asks its {@link admitter} first and waits for room when it does not fit,
|
|
4
|
+
// so the machine can finish what it already started.
|
|
5
|
+
|
|
6
|
+
import type { ComputerTransport } from './workspace.js'
|
|
7
|
+
import { localConnector } from './connectors.js'
|
|
8
|
+
import { applicationSlice } from '@yaks/process/isolate'
|
|
9
|
+
import { quote } from './workspace.js'
|
|
10
|
+
|
|
11
|
+
/** What a machine has room for: the bytes it could give without swapping,
|
|
12
|
+
* and the share of the last ten seconds some task waited
|
|
13
|
+
* for memory (PSI `some avg10`, a percentage). On Linux it also carries the
|
|
14
|
+
* share of the last minute some task waited for CPU or disk I/O. */
|
|
15
|
+
export type Room = {
|
|
16
|
+
available: number
|
|
17
|
+
pressure: number
|
|
18
|
+
cpuPressure?: number
|
|
19
|
+
ioPressure?: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** What one more run needs of a machine, and how long a run admitted holds a
|
|
23
|
+
* reservation before the machine's own reading counts it. */
|
|
24
|
+
export type Need = {
|
|
25
|
+
/** bytes a run takes at its peak */
|
|
26
|
+
memory: number
|
|
27
|
+
/** the memory pressure (PSI `some avg10`) at or past which nothing starts */
|
|
28
|
+
pressure: number
|
|
29
|
+
/** the sustained CPU pressure (PSI `some avg60`) that defers starts */
|
|
30
|
+
cpuPressure?: number
|
|
31
|
+
/** the sustained disk I/O pressure (PSI `some avg60`) that defers starts */
|
|
32
|
+
ioPressure?: number
|
|
33
|
+
/** ms a run admitted is reserved for: it starts small and grows */
|
|
34
|
+
settle: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** An agent run on the box holds 0.3 to 0.6 GB, and its test runs add to that
|
|
38
|
+
* for a while (T-1463 measured 3.6 GB at p90 when tests were heavier). Each
|
|
39
|
+
* start reserves this much until the readings carry its growth. */
|
|
40
|
+
export let NEED: Need = {
|
|
41
|
+
memory: 1.5 * 1024 ** 3,
|
|
42
|
+
pressure: 10,
|
|
43
|
+
cpuPressure: 30,
|
|
44
|
+
ioPressure: 20,
|
|
45
|
+
settle: 120_000,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Read the machine's pressure, then its memory, in one control call. The
|
|
49
|
+
* cgroup mount root covers the container. Host I/O pressure also covers work
|
|
50
|
+
* outside it, such as Proxmox snapshots on the same disk. */
|
|
51
|
+
export let SCRIPT = `cgroup=/sys/fs/cgroup
|
|
52
|
+
wait_pressure() {
|
|
53
|
+
label="$1"
|
|
54
|
+
file="$2"
|
|
55
|
+
if [ -r "$file" ] && read -r kind ten minute rest 2>/dev/null <"$file"; then
|
|
56
|
+
case "$kind:$minute" in
|
|
57
|
+
some:avg60=*) printf '%s: %s\\n' "$label" "\${minute#avg60=}" ;;
|
|
58
|
+
esac
|
|
59
|
+
fi
|
|
60
|
+
}
|
|
61
|
+
for resource in cpu io; do
|
|
62
|
+
path="$cgroup/$resource.pressure"
|
|
63
|
+
[ -r "$path" ] || path="/proc/pressure/$resource"
|
|
64
|
+
wait_pressure "$resource" "$path"
|
|
65
|
+
done
|
|
66
|
+
if [ -r "$cgroup/io.pressure" ]; then
|
|
67
|
+
wait_pressure io /proc/pressure/io
|
|
68
|
+
fi
|
|
69
|
+
pressure=$(cat "$cgroup/memory.pressure" 2>/dev/null) || pressure=''
|
|
70
|
+
case "$pressure" in
|
|
71
|
+
'some avg10='*) printf '%s\\n' "$pressure" ;;
|
|
72
|
+
*) cat /proc/pressure/memory || exit ;;
|
|
73
|
+
esac
|
|
74
|
+
if read -r limit 2>/dev/null <"$cgroup/memory.max" &&
|
|
75
|
+
read -r current 2>/dev/null <"$cgroup/memory.current"; then
|
|
76
|
+
printf 'CgroupMemory: %s %s\\n' "$limit" "$current"
|
|
77
|
+
fi
|
|
78
|
+
cat /proc/meminfo || exit`
|
|
79
|
+
|
|
80
|
+
// Slice capacity follows its capped ancestors. The parent can be short of
|
|
81
|
+
// room even when the slice could grow into its own cap.
|
|
82
|
+
let sliceRoom = (slice: string): string => `
|
|
83
|
+
group=$(systemctl --user show --property=ControlGroup --value ${
|
|
84
|
+
quote(slice)
|
|
85
|
+
} 2>/dev/null)
|
|
86
|
+
[ -n "$group" ] || exit 0
|
|
87
|
+
while [ -n "$group" ]; do
|
|
88
|
+
path="$cgroup$group"
|
|
89
|
+
if read -r limit <"$path/memory.max" && read -r used <"$path/memory.current"; then
|
|
90
|
+
awk -v limit="$limit" -v used="$used" '
|
|
91
|
+
$1 == "inactive_file" { cache = $2 }
|
|
92
|
+
$1 == "file_dirty" || $1 == "file_writeback" { dirty += $2 }
|
|
93
|
+
END {
|
|
94
|
+
clean = cache > dirty ? cache - dirty : 0
|
|
95
|
+
if (limit != "max") printf "SliceMemory: %.0f\\n", limit - used + clean
|
|
96
|
+
}
|
|
97
|
+
' "$path/memory.stat"
|
|
98
|
+
sed -n 's/^some avg10=\\([^ ]*\\) .*/SlicePressure: \\1/p' "$path/memory.pressure"
|
|
99
|
+
fi
|
|
100
|
+
wait_pressure cpu "$path/cpu.pressure"
|
|
101
|
+
wait_pressure io "$path/io.pressure"
|
|
102
|
+
group=\${group%/*}
|
|
103
|
+
done`
|
|
104
|
+
|
|
105
|
+
let mem = (text: string, field: string): number | undefined => {
|
|
106
|
+
let kb = text.match(new RegExp(`^${field}:[ \\t]+(\\d+) kB$`, 'm'))
|
|
107
|
+
if (kb) return Number(kb[1]) * 1024
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let available = (text: string): number => {
|
|
111
|
+
let reported = mem(text, 'MemAvailable')
|
|
112
|
+
if (reported != null) return reported
|
|
113
|
+
let free = mem(text, 'MemFree')
|
|
114
|
+
if (free != null) {
|
|
115
|
+
let cache = (mem(text, 'Buffers') ?? 0) + (mem(text, 'Cached') ?? 0)
|
|
116
|
+
return free + Math.max(0, cache - (mem(text, 'Shmem') ?? 0))
|
|
117
|
+
}
|
|
118
|
+
let group = text.match(/^CgroupMemory: (max|\d+) (\d+)$/m)
|
|
119
|
+
let total = mem(text, 'MemTotal')
|
|
120
|
+
let limit = group && group[1] != 'max' ? Number(group[1]) : total
|
|
121
|
+
if (group && limit != null) return Math.max(0, limit - Number(group[2]))
|
|
122
|
+
if (total != null) return 0
|
|
123
|
+
throw new Error('Machine memory reading is missing')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Read a machine's room from what {@link SCRIPT} prints. */
|
|
127
|
+
export let roomOf = (text: string): Room => {
|
|
128
|
+
let some = text.match(/^some[ \t]+avg10=(\d+(?:\.\d+)?)(?:[ \t]|$)/m)
|
|
129
|
+
if (!some) {
|
|
130
|
+
throw new Error('Machine memory pressure (PSI some avg10) is missing')
|
|
131
|
+
}
|
|
132
|
+
let limits = [...text.matchAll(/^SliceMemory: (-?\d+)$/gm)]
|
|
133
|
+
let pressures = [...text.matchAll(/^SlicePressure: (\d+(?:\.\d+)?)$/gm)]
|
|
134
|
+
let wait = (resource: 'cpu' | 'io') => {
|
|
135
|
+
let readings = [
|
|
136
|
+
...text.matchAll(new RegExp(`^${resource}: (\\d+(?:\\.\\d+)?)$`, 'gm')),
|
|
137
|
+
]
|
|
138
|
+
if (readings.length) {
|
|
139
|
+
return Math.max(...readings.map(([, value]) => Number(value)))
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let cpuPressure = wait('cpu'), ioPressure = wait('io')
|
|
143
|
+
return {
|
|
144
|
+
available: Math.max(
|
|
145
|
+
0,
|
|
146
|
+
Math.min(available(text), ...limits.map(([, bytes]) => Number(bytes))),
|
|
147
|
+
),
|
|
148
|
+
pressure: Math.max(
|
|
149
|
+
Number(some[1]),
|
|
150
|
+
...pressures.map(([, pressure]) => Number(pressure)),
|
|
151
|
+
),
|
|
152
|
+
...cpuPressure == null ? {} : { cpuPressure },
|
|
153
|
+
...ioPressure == null ? {} : { ioPressure },
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Read a computer's room, optionally within a systemd user slice. */
|
|
158
|
+
export let room = async (
|
|
159
|
+
transport: ComputerTransport,
|
|
160
|
+
slice?: string,
|
|
161
|
+
): Promise<Room> =>
|
|
162
|
+
roomOf(await transport.execute(slice ? SCRIPT + sliceRoom(slice) : SCRIPT))
|
|
163
|
+
|
|
164
|
+
/** Whether one more run fits, with `held` runs admitted but not yet grown. */
|
|
165
|
+
export let fits = (r: Room, need: Need = NEED, held = 0): boolean =>
|
|
166
|
+
r.pressure < need.pressure &&
|
|
167
|
+
(r.cpuPressure ?? 0) < (need.cpuPressure ?? Infinity) &&
|
|
168
|
+
(r.ioPressure ?? 0) < (need.ioPressure ?? Infinity) &&
|
|
169
|
+
r.available - held * need.memory >= need.memory
|
|
170
|
+
|
|
171
|
+
/** Read the room left for a start, reserving its need for `settle` ms if it
|
|
172
|
+
* fits. Calls are answered in order so a burst sees its own reservations. */
|
|
173
|
+
export let reserving = (
|
|
174
|
+
read: () => Promise<Room>,
|
|
175
|
+
need: Need = NEED,
|
|
176
|
+
now: () => number = Date.now,
|
|
177
|
+
): () => Promise<Room> => {
|
|
178
|
+
let held: number[] = []
|
|
179
|
+
let turn: Promise<unknown> = Promise.resolve()
|
|
180
|
+
let reserve = async () => {
|
|
181
|
+
let room = await read()
|
|
182
|
+
let t = now()
|
|
183
|
+
held = held.filter((at) => t - at < need.settle)
|
|
184
|
+
let remaining = {
|
|
185
|
+
...room,
|
|
186
|
+
available: Math.max(0, room.available - held.length * need.memory),
|
|
187
|
+
}
|
|
188
|
+
if (fits(remaining, need)) held.push(t)
|
|
189
|
+
return remaining
|
|
190
|
+
}
|
|
191
|
+
return () => {
|
|
192
|
+
let answer = turn.then(reserve)
|
|
193
|
+
turn = answer.catch(() => {})
|
|
194
|
+
return answer
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Whether a run can start now, with room reserved for recent starts.
|
|
200
|
+
*
|
|
201
|
+
* ```ts
|
|
202
|
+
* import { admitter } from '@yaks/machine/room'
|
|
203
|
+
*
|
|
204
|
+
* let admit = admitter(() =>
|
|
205
|
+
* Promise.resolve({ available: 9 * 1024 ** 3, pressure: 0 })
|
|
206
|
+
* )
|
|
207
|
+
* console.assert(await admit() && await admit() && !await admit())
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
export let admitter = (
|
|
211
|
+
read: () => Promise<Room>,
|
|
212
|
+
need: Need = NEED,
|
|
213
|
+
now: () => number = Date.now,
|
|
214
|
+
): () => Promise<boolean> => {
|
|
215
|
+
let reserve = reserving(read, need, now)
|
|
216
|
+
return async () => fits(await reserve(), need)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The room of the computer this process runs on. */
|
|
220
|
+
export let hereRoom = async (slice?: string): Promise<Room> =>
|
|
221
|
+
room(await localConnector().connect('local://'), slice)
|
|
222
|
+
|
|
223
|
+
/** The {@link admitter} of the computer this process runs on. */
|
|
224
|
+
export let here = (need: Need = NEED): () => Promise<boolean> =>
|
|
225
|
+
admitter(hereRoom, need)
|
|
226
|
+
|
|
227
|
+
/** The room in the systemd slice that holds managed runs. */
|
|
228
|
+
export let managedRoom = (): Promise<Room> => hereRoom(applicationSlice)
|
package/targets.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Resolve file-and-command targets once. A session default is only a convenience;
|
|
2
|
+
// process recovery always follows its original workspace.
|
|
3
|
+
import { line, sub } from "@yaks/query";
|
|
4
|
+
import { Refused } from "@yaks/graph";
|
|
5
|
+
import { token } from "@yaks/graph/guard";
|
|
6
|
+
let component = (value)=>value;
|
|
7
|
+
/**
|
|
8
|
+
* How a call names a workspace, and some it could name: the second half of
|
|
9
|
+
* every refusal for a call that named none, or named something else. The
|
|
10
|
+
* newest workspaces, one per directory, since many share one.
|
|
11
|
+
*/ export let naming = async (g)=>{
|
|
12
|
+
let newest = g.vocab.comp('created') ? line`&.order=-created.at` : line``;
|
|
13
|
+
let rows = await g.read(line`.workspace${sub(newest)}&.limit=40&*`);
|
|
14
|
+
let roots = new Map();
|
|
15
|
+
for (let row of rows){
|
|
16
|
+
let root = component(row.workspace)?.root;
|
|
17
|
+
if (row.released || typeof root != 'string' || roots.has(root)) continue;
|
|
18
|
+
roots.set(root, row.entity.eid);
|
|
19
|
+
}
|
|
20
|
+
let some = [
|
|
21
|
+
...roots
|
|
22
|
+
].slice(0, 5).map(([root, eid])=>`${eid} (${root})`);
|
|
23
|
+
return "workspace takes a workspace entity's eid" + (some.length ? `, such as ${some.join(', ')}` : '; this graph has none') + ". graph_query '.workspace.root=<directory>' finds the one at a directory";
|
|
24
|
+
};
|
|
25
|
+
/** The refusal for a call left to a default sandbox the host does not lend. */ export let unlent = async (g)=>new Refused('This host lends no default sandbox provider, so name a workspace or a ' + `sandbox provider it configures. ${await naming(g)}`);
|
|
26
|
+
/** Resolve an explicit target or the session default, never a local fallback. */ export let targetFor = async (g, session, args)=>{
|
|
27
|
+
let home = session ? component((await g.get([
|
|
28
|
+
session
|
|
29
|
+
], [
|
|
30
|
+
'home'
|
|
31
|
+
]))[0]?.home) : undefined;
|
|
32
|
+
let workspace = args.workspace ?? home?.workspace;
|
|
33
|
+
if (typeof workspace != 'string' || !workspace) {
|
|
34
|
+
throw new Refused(`No workspace named, and the session has no home: ${await naming(g)}`);
|
|
35
|
+
}
|
|
36
|
+
let row = (await g.get([
|
|
37
|
+
workspace
|
|
38
|
+
], [
|
|
39
|
+
'workspace'
|
|
40
|
+
]))[0];
|
|
41
|
+
let record = component(row?.workspace);
|
|
42
|
+
if (!record) {
|
|
43
|
+
throw new Refused(`Not a workspace: ${workspace}. ${await naming(g)}`);
|
|
44
|
+
}
|
|
45
|
+
let cwd = args.cwd ?? (args.workspace == null ? home?.cwd : undefined) ?? record.root;
|
|
46
|
+
if (typeof cwd != 'string' || !cwd.startsWith('/') || cwd.includes('\0')) {
|
|
47
|
+
throw new Refused('Workspace cwd must be an absolute path');
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
workspace,
|
|
51
|
+
cwd
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
/** A guarded default change. Omitting cwd explicitly clears the previous one. */ export let sessionHome = async (g, session, workspace, cwd)=>{
|
|
55
|
+
let row = (await g.get([
|
|
56
|
+
session
|
|
57
|
+
]))[0];
|
|
58
|
+
if (!row?.session) throw new Error('Not a session: ' + session);
|
|
59
|
+
let old = component(row.home);
|
|
60
|
+
await targetFor(g, undefined, {
|
|
61
|
+
workspace,
|
|
62
|
+
...cwd != null ? {
|
|
63
|
+
cwd
|
|
64
|
+
} : {}
|
|
65
|
+
});
|
|
66
|
+
return await g.apply([
|
|
67
|
+
{
|
|
68
|
+
entity: {
|
|
69
|
+
eid: session
|
|
70
|
+
},
|
|
71
|
+
home: {
|
|
72
|
+
workspace,
|
|
73
|
+
cwd: cwd ?? null
|
|
74
|
+
},
|
|
75
|
+
$was: {
|
|
76
|
+
home: {
|
|
77
|
+
workspace: token(old?.workspace ?? null),
|
|
78
|
+
cwd: token(old?.cwd ?? null)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
]);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Look once more at every workspace process with no `exit`. A look records the
|
|
86
|
+
* ending its receipt holds, so a run whose waiter stopped looking (a shell
|
|
87
|
+
* call's timeout, an aborted turn, the process that waited ending) gets its
|
|
88
|
+
* `exit` here. A workspace that refuses to connect (released, its placement
|
|
89
|
+
* unknown) has nothing to look at; any other failure goes to `failed`, and
|
|
90
|
+
* the other workspaces are still looked at. Answers how many were looked at.
|
|
91
|
+
*/ export let overdue = async (g, workspaces, failed = ()=>{})=>{
|
|
92
|
+
let held = new Map();
|
|
93
|
+
for (let row of (await g.read('.process&!exit'))){
|
|
94
|
+
let workspace = component(row.process)?.workspace;
|
|
95
|
+
if (typeof workspace != 'string') continue;
|
|
96
|
+
held.set(workspace, [
|
|
97
|
+
...held.get(workspace) ?? [],
|
|
98
|
+
row.entity.eid
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
let looked = 0;
|
|
102
|
+
for (let [workspace, ids] of held){
|
|
103
|
+
try {
|
|
104
|
+
let machine = await workspaces.connect(workspace);
|
|
105
|
+
for (let id of ids){
|
|
106
|
+
await machine.look(id);
|
|
107
|
+
looked++;
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (!(error instanceof Refused)) failed(error, workspace);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return looked;
|
|
114
|
+
};
|
|
115
|
+
/** Process operations ignore the caller's current home. */ export let processWorkspace = async (g, process)=>{
|
|
116
|
+
let row = (await g.get([
|
|
117
|
+
process
|
|
118
|
+
], [
|
|
119
|
+
'process'
|
|
120
|
+
]))[0];
|
|
121
|
+
let workspace = component(row?.process)?.workspace;
|
|
122
|
+
if (typeof workspace != 'string') {
|
|
123
|
+
throw new Refused('Process launch workspace is unknown: ' + process);
|
|
124
|
+
}
|
|
125
|
+
return workspace;
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=targets.js.map
|
package/targets.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"targets.js","sources":["./targets.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,0DAA0D;AAC1D,SAAS,IAAI,EAAE,GAAG,2BAAgC;AAClD,SAA6C,OAAO,2BAAgC;AACpF,SAAS,KA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AG;IAC9C,IAAI,YAAY,UAAU,IAAI,OAAO,GAAG;IACxC,IAAI,OAAO,aAAa,UAAU;IAClC,KAAK,GAAG,CAAC,WAAW;SAAI,KAAK,GAAG,CAAC,cAAc,EAAE;MAAE,IAAI,MAAM,CAAC,GAAG;KAAC;EACpE;EACA,IAAI,SAAS;EACb,KAAK,IAAI,CAAC,WAAW,IAAI,IAAI,KAAM;IACjC,IAAI;MACF,IAAI,UAAU,MAAM,WAAW,OAAO,CAAC;MACvC,KAAK,IAAI,MAAM,IAAK;QAClB,MAAM,QAAQ,IAAI,CAAC;QACnB;MACF;IACF,EAAE,OAAO,OAAO;MACd,IAAI,CAAC,CAAC,iBAAiB,OAAO,GAAG,OAAO,OAAO;IACjD;EACF;EACA,OAAO;AACT,EAAC;AAED,yDAAyD,GACzD,OAAO,IAAI,mBAAmB,OAC5B,GACA;EAEA,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAAC;GAAQ,EAAE;IAAC;GAAU,CAAC,CAAC,CAAC,EAAE;EAClD,IAAI,YAAY,UAAU,KAAK,UAAU;EACzC,IAAI,OAAO,aAAa,UAAU;IAChC,MAAM,IAAI,QAAQ,0CAA0C;EAC9D;EACA,OAAO;AACT,EAAC"}
|
package/targets.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Resolve file-and-command targets once. A session default is only a convenience;
|
|
2
|
+
// process recovery always follows its original workspace.
|
|
3
|
+
import { line, sub } from '@yaks/query'
|
|
4
|
+
import { type Bundle, type Comp, type Graph, Refused } from '@yaks/graph'
|
|
5
|
+
import { token } from '@yaks/graph/guard'
|
|
6
|
+
import type { Workspace } from './workspace.ts'
|
|
7
|
+
|
|
8
|
+
export type Workspaces = {
|
|
9
|
+
connect: (workspace: string) => Promise<Workspace>
|
|
10
|
+
}
|
|
11
|
+
export type Target = { workspace: string; cwd: string }
|
|
12
|
+
let component = (value: unknown) => value as Comp | undefined
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* How a call names a workspace, and some it could name: the second half of
|
|
16
|
+
* every refusal for a call that named none, or named something else. The
|
|
17
|
+
* newest workspaces, one per directory, since many share one.
|
|
18
|
+
*/
|
|
19
|
+
export let naming = async (g: Graph): Promise<string> => {
|
|
20
|
+
let newest = g.vocab.comp('created') ? line`&.order=-created.at` : line``
|
|
21
|
+
let rows = await g.read(line`.workspace${sub(newest)}&.limit=40&*`)
|
|
22
|
+
let roots = new Map<string, string>()
|
|
23
|
+
for (let row of rows) {
|
|
24
|
+
let root = component(row.workspace)?.root
|
|
25
|
+
if (row.released || typeof root != 'string' || roots.has(root)) continue
|
|
26
|
+
roots.set(root, row.entity.eid)
|
|
27
|
+
}
|
|
28
|
+
let some = [...roots].slice(0, 5).map(([root, eid]) => `${eid} (${root})`)
|
|
29
|
+
return "workspace takes a workspace entity's eid" +
|
|
30
|
+
(some.length ? `, such as ${some.join(', ')}` : '; this graph has none') +
|
|
31
|
+
". graph_query '.workspace.root=<directory>' finds the one at a directory"
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The refusal for a call left to a default sandbox the host does not lend. */
|
|
35
|
+
export let unlent = async (g: Graph): Promise<Refused> =>
|
|
36
|
+
new Refused(
|
|
37
|
+
'This host lends no default sandbox provider, so name a workspace or a ' +
|
|
38
|
+
`sandbox provider it configures. ${await naming(g)}`,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
/** Resolve an explicit target or the session default, never a local fallback. */
|
|
42
|
+
export let targetFor = async (
|
|
43
|
+
g: Graph,
|
|
44
|
+
session: string | undefined,
|
|
45
|
+
args: Record<string, unknown>,
|
|
46
|
+
): Promise<Target> => {
|
|
47
|
+
let home = session
|
|
48
|
+
? component((await g.get([session], ['home']))[0]?.home)
|
|
49
|
+
: undefined
|
|
50
|
+
let workspace = args.workspace ?? home?.workspace
|
|
51
|
+
if (typeof workspace != 'string' || !workspace) {
|
|
52
|
+
throw new Refused(
|
|
53
|
+
`No workspace named, and the session has no home: ${await naming(g)}`,
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
let row = (await g.get([workspace], ['workspace']))[0]
|
|
57
|
+
let record = component(row?.workspace)
|
|
58
|
+
if (!record) {
|
|
59
|
+
throw new Refused(`Not a workspace: ${workspace}. ${await naming(g)}`)
|
|
60
|
+
}
|
|
61
|
+
let cwd = args.cwd ?? (args.workspace == null ? home?.cwd : undefined) ??
|
|
62
|
+
record.root
|
|
63
|
+
if (typeof cwd != 'string' || !cwd.startsWith('/') || cwd.includes('\0')) {
|
|
64
|
+
throw new Refused('Workspace cwd must be an absolute path')
|
|
65
|
+
}
|
|
66
|
+
return { workspace, cwd }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A guarded default change. Omitting cwd explicitly clears the previous one. */
|
|
70
|
+
export let sessionHome = async (
|
|
71
|
+
g: Graph,
|
|
72
|
+
session: string,
|
|
73
|
+
workspace: string,
|
|
74
|
+
cwd?: string,
|
|
75
|
+
): Promise<Bundle[]> => {
|
|
76
|
+
let row = (await g.get([session]))[0]
|
|
77
|
+
if (!row?.session) throw new Error('Not a session: ' + session)
|
|
78
|
+
let old = component(row.home)
|
|
79
|
+
await targetFor(g, undefined, { workspace, ...cwd != null ? { cwd } : {} })
|
|
80
|
+
return await g.apply([{
|
|
81
|
+
entity: { eid: session },
|
|
82
|
+
home: { workspace, cwd: cwd ?? null },
|
|
83
|
+
$was: {
|
|
84
|
+
home: {
|
|
85
|
+
workspace: token(old?.workspace ?? null),
|
|
86
|
+
cwd: token(old?.cwd ?? null),
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
}])
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Look once more at every workspace process with no `exit`. A look records the
|
|
94
|
+
* ending its receipt holds, so a run whose waiter stopped looking (a shell
|
|
95
|
+
* call's timeout, an aborted turn, the process that waited ending) gets its
|
|
96
|
+
* `exit` here. A workspace that refuses to connect (released, its placement
|
|
97
|
+
* unknown) has nothing to look at; any other failure goes to `failed`, and
|
|
98
|
+
* the other workspaces are still looked at. Answers how many were looked at.
|
|
99
|
+
*/
|
|
100
|
+
export let overdue = async (
|
|
101
|
+
g: Graph,
|
|
102
|
+
workspaces: Workspaces,
|
|
103
|
+
failed: (error: unknown, workspace: string) => void = () => {},
|
|
104
|
+
): Promise<number> => {
|
|
105
|
+
let held = new Map<string, string[]>()
|
|
106
|
+
for (let row of await g.read('.process&!exit')) {
|
|
107
|
+
let workspace = component(row.process)?.workspace
|
|
108
|
+
if (typeof workspace != 'string') continue
|
|
109
|
+
held.set(workspace, [...held.get(workspace) ?? [], row.entity.eid])
|
|
110
|
+
}
|
|
111
|
+
let looked = 0
|
|
112
|
+
for (let [workspace, ids] of held) {
|
|
113
|
+
try {
|
|
114
|
+
let machine = await workspaces.connect(workspace)
|
|
115
|
+
for (let id of ids) {
|
|
116
|
+
await machine.look(id)
|
|
117
|
+
looked++
|
|
118
|
+
}
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (!(error instanceof Refused)) failed(error, workspace)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return looked
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Process operations ignore the caller's current home. */
|
|
127
|
+
export let processWorkspace = async (
|
|
128
|
+
g: Graph,
|
|
129
|
+
process: string,
|
|
130
|
+
): Promise<string> => {
|
|
131
|
+
let row = (await g.get([process], ['process']))[0]
|
|
132
|
+
let workspace = component(row?.process)?.workspace
|
|
133
|
+
if (typeof workspace != 'string') {
|
|
134
|
+
throw new Refused('Process launch workspace is unknown: ' + process)
|
|
135
|
+
}
|
|
136
|
+
return workspace
|
|
137
|
+
}
|