@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/tools.ts
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// File-and-command verbs over workspace targets; timeout never disposes a command.
|
|
2
|
+
import type { EffectAnswer } from 'jsr:@yaks/effects@0.2.36/invocation'
|
|
3
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
4
|
+
|
|
5
|
+
/** Structural tool interface, independent of a transcript runner. */
|
|
6
|
+
export type ToolContext = {
|
|
7
|
+
session: string
|
|
8
|
+
call: Bundle
|
|
9
|
+
entries: Bundle[]
|
|
10
|
+
signal?: AbortSignal
|
|
11
|
+
}
|
|
12
|
+
export type Tool = {
|
|
13
|
+
name: string
|
|
14
|
+
description: string
|
|
15
|
+
parameters: Record<string, unknown>
|
|
16
|
+
run: (
|
|
17
|
+
args: Record<string, unknown>,
|
|
18
|
+
ctx?: ToolContext,
|
|
19
|
+
) => Promise<string> | string
|
|
20
|
+
recover?: (
|
|
21
|
+
args: Record<string, unknown>,
|
|
22
|
+
ctx: ToolContext,
|
|
23
|
+
) => Promise<string | undefined> | string | undefined
|
|
24
|
+
/** A call to it is the model waiting: a transcript starts no turn for it
|
|
25
|
+
* while it runs (@yaks/session `PATIENCE`). */
|
|
26
|
+
waits?: boolean
|
|
27
|
+
}
|
|
28
|
+
import type { Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
29
|
+
import { token } from '@yaks/graph/guard'
|
|
30
|
+
import { processWorkspace, targetFor, type Workspaces } from './targets.js'
|
|
31
|
+
|
|
32
|
+
import type { Machine, Proc } from './mod.ts'
|
|
33
|
+
import { settle } from './workspace.js'
|
|
34
|
+
import { CallError } from '@yaks/tools/errors'
|
|
35
|
+
|
|
36
|
+
/** How the machine tools behave, all optional. */
|
|
37
|
+
export type MachineOpts = {
|
|
38
|
+
/** The durable invocation when a host calls outside a session runner. */
|
|
39
|
+
call?: string
|
|
40
|
+
/** how long `shell` waits for its command before answering the process id
|
|
41
|
+
* instead (ms, default 5000); a `timeout` argument overrides it per call */
|
|
42
|
+
budget?: number
|
|
43
|
+
/** how long `stop` gives SIGTERM before SIGKILL (ms, default 2000) */
|
|
44
|
+
grace?: number
|
|
45
|
+
/** how many lines of output an answer carries (default all) */
|
|
46
|
+
lines?: number
|
|
47
|
+
/** where every command starts, and what a relative file path is read from */
|
|
48
|
+
cwd?: (ctx?: ToolContext) => string | undefined | Promise<string | undefined>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// How long `shell` waits for its command unless told otherwise (ms).
|
|
52
|
+
let BUDGET = 5000
|
|
53
|
+
|
|
54
|
+
/** Whether a path names its own root.
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { assert } from '@std/assert'
|
|
58
|
+
* assert(rooted('/etc/hosts') && rooted('C:\\tmp') && !rooted('src/lib.rs'))
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export let rooted = (path: string): boolean => /^(\/|[A-Za-z]:[\\/])/.test(path)
|
|
62
|
+
|
|
63
|
+
let under = (dir: string | undefined, path: string) =>
|
|
64
|
+
dir == null || rooted(path) ? path : `${dir.replace(/\/+$/, '')}/${path}`
|
|
65
|
+
|
|
66
|
+
let code = (c: number | null) =>
|
|
67
|
+
c == null ? 'exited, code unknown' : `exited ${c}`
|
|
68
|
+
|
|
69
|
+
// What a path that is not a file names instead (`Stat`).
|
|
70
|
+
let SAID = {
|
|
71
|
+
missing: 'nothing is there',
|
|
72
|
+
dangling: 'it is a link to nothing',
|
|
73
|
+
directory: 'it is a directory',
|
|
74
|
+
other: 'it is not a regular file',
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let pid = (p: Proc) => p.pid ? ` (pid ${p.pid})` : ''
|
|
78
|
+
|
|
79
|
+
let str = (description: string) => ({ type: 'string', description })
|
|
80
|
+
let os = (description: string) => ({
|
|
81
|
+
...str(description),
|
|
82
|
+
pattern: '^[^\\u0000]*$',
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
/** A machine tool without its machine: what a door lists before it has one
|
|
86
|
+
* to run it on. */
|
|
87
|
+
export type MachineTool = Omit<Tool, 'run'>
|
|
88
|
+
|
|
89
|
+
/** The machine tools' declarations: `shell`, `wait`, `stop`, `read` and
|
|
90
|
+
* `write`. */
|
|
91
|
+
export let machineDeclared = (o: MachineOpts = {}): MachineTool[] => [{
|
|
92
|
+
name: 'shell',
|
|
93
|
+
description:
|
|
94
|
+
'Run a shell command. A command still running when the timeout passes ' +
|
|
95
|
+
'keeps running as a process: the result gives its id, and wait or stop ' +
|
|
96
|
+
'accepts that id.',
|
|
97
|
+
parameters: {
|
|
98
|
+
type: 'object',
|
|
99
|
+
properties: {
|
|
100
|
+
command: os('the command line, run by bash'),
|
|
101
|
+
timeout: {
|
|
102
|
+
type: 'number',
|
|
103
|
+
description: `milliseconds to wait for it (default ${
|
|
104
|
+
o.budget ?? BUDGET
|
|
105
|
+
})`,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
required: ['command'],
|
|
109
|
+
},
|
|
110
|
+
}, {
|
|
111
|
+
name: 'wait',
|
|
112
|
+
waits: true,
|
|
113
|
+
description:
|
|
114
|
+
'Wait for a process to exit. Returns its exit code and output, ' +
|
|
115
|
+
'or reports that it is still running when the timeout ' +
|
|
116
|
+
'passes.',
|
|
117
|
+
parameters: {
|
|
118
|
+
type: 'object',
|
|
119
|
+
properties: {
|
|
120
|
+
process: str('the process id'),
|
|
121
|
+
timeout: {
|
|
122
|
+
type: 'number',
|
|
123
|
+
description: 'milliseconds to wait (default 60000)',
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
required: ['process'],
|
|
127
|
+
},
|
|
128
|
+
}, {
|
|
129
|
+
name: 'stop',
|
|
130
|
+
description:
|
|
131
|
+
'Stop a process: SIGTERM, then SIGKILL if it is still running after ' +
|
|
132
|
+
'the grace period. Returns its exit code.',
|
|
133
|
+
parameters: {
|
|
134
|
+
type: 'object',
|
|
135
|
+
properties: {
|
|
136
|
+
process: str('the process id'),
|
|
137
|
+
grace: {
|
|
138
|
+
type: 'number',
|
|
139
|
+
description: 'milliseconds between the two signals (default 2000)',
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
required: ['process'],
|
|
143
|
+
},
|
|
144
|
+
}, {
|
|
145
|
+
name: 'read',
|
|
146
|
+
description: 'Read a text file.',
|
|
147
|
+
parameters: {
|
|
148
|
+
type: 'object',
|
|
149
|
+
properties: {
|
|
150
|
+
path: os('the file, from the working directory unless rooted'),
|
|
151
|
+
},
|
|
152
|
+
required: ['path'],
|
|
153
|
+
},
|
|
154
|
+
}, {
|
|
155
|
+
name: 'write',
|
|
156
|
+
description:
|
|
157
|
+
'Write a text file, replacing whatever the path held and making its ' +
|
|
158
|
+
'directory if there is none.',
|
|
159
|
+
parameters: {
|
|
160
|
+
type: 'object',
|
|
161
|
+
properties: {
|
|
162
|
+
path: os('the file, from the working directory unless rooted'),
|
|
163
|
+
content: str('the whole text of the file'),
|
|
164
|
+
},
|
|
165
|
+
required: ['path', 'content'],
|
|
166
|
+
},
|
|
167
|
+
}]
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The machine tools over a machine.
|
|
171
|
+
*
|
|
172
|
+
* A command that finishes within its timeout answers its code and output; one
|
|
173
|
+
* that does not answers its process id, still running, for `wait` and `stop`
|
|
174
|
+
* to name.
|
|
175
|
+
*/
|
|
176
|
+
export let machineTools = (
|
|
177
|
+
source: Machine | ((ctx?: ToolContext) => Promise<Machine>),
|
|
178
|
+
o: MachineOpts = {},
|
|
179
|
+
): Tool[] => {
|
|
180
|
+
if (typeof source == 'function') {
|
|
181
|
+
return machineDeclared(o).map((declaration) => ({
|
|
182
|
+
...declaration,
|
|
183
|
+
run: async (args, ctx) => {
|
|
184
|
+
let tool = machineTools(await source(ctx), o).find((t) =>
|
|
185
|
+
t.name == declaration.name
|
|
186
|
+
)!
|
|
187
|
+
return tool.run(args, ctx)
|
|
188
|
+
},
|
|
189
|
+
...declaration.name == 'shell'
|
|
190
|
+
? {
|
|
191
|
+
recover: async (args, ctx) =>
|
|
192
|
+
machineTools(await source(ctx), o)
|
|
193
|
+
.find((t) => t.name == 'shell')!.recover!(args, ctx),
|
|
194
|
+
}
|
|
195
|
+
: {},
|
|
196
|
+
}))
|
|
197
|
+
}
|
|
198
|
+
let m = source
|
|
199
|
+
let runs = machineOperations(m, o)
|
|
200
|
+
let lines = o.lines ?? Number.MAX_SAFE_INTEGER
|
|
201
|
+
let recover = async (_args: Record<string, unknown>, ctx?: ToolContext) => {
|
|
202
|
+
let call = ctx?.call.entity.eid ?? o.call
|
|
203
|
+
return await shellReceipt(m, call, lines) ??
|
|
204
|
+
'Shell execution has no process receipt; inspect before retrying.'
|
|
205
|
+
}
|
|
206
|
+
return machineDeclared(o).map((d) => ({
|
|
207
|
+
...d,
|
|
208
|
+
run: async (args, ctx) => (await runs[d.name](args, ctx)).body ?? '',
|
|
209
|
+
...d.name == 'shell' ? { recover } : {},
|
|
210
|
+
}))
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let processValue = (id: string, p: Proc): Comp => ({
|
|
214
|
+
process: id,
|
|
215
|
+
...p.pid == null ? {} : { pid: p.pid },
|
|
216
|
+
...p.exit ? { exit: p.exit } : {},
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
/** File and process operations over one workspace. */
|
|
220
|
+
export let machineOperations = (
|
|
221
|
+
m: Machine,
|
|
222
|
+
o: MachineOpts = {},
|
|
223
|
+
): Record<
|
|
224
|
+
string,
|
|
225
|
+
(
|
|
226
|
+
args: Record<string, unknown>,
|
|
227
|
+
ctx?: ToolContext,
|
|
228
|
+
) => Promise<EffectAnswer>
|
|
229
|
+
> => {
|
|
230
|
+
let lines = o.lines ?? Number.MAX_SAFE_INTEGER
|
|
231
|
+
let here = async (ctx?: ToolContext) => await o.cwd?.(ctx)
|
|
232
|
+
|
|
233
|
+
let told = async (id: string, head: string, p: Proc) => ({
|
|
234
|
+
body: [head, ...await m.tail(id, lines)].join('\n'),
|
|
235
|
+
value: processValue(id, p),
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
shell: async (args, ctx) => {
|
|
240
|
+
let ms = Number(args.timeout ?? o.budget ?? BUDGET)
|
|
241
|
+
// The timeout covers the whole call, the start included; a child that
|
|
242
|
+
// outlived it is reported as running even if it exits a moment later.
|
|
243
|
+
let end = Date.now() + ms
|
|
244
|
+
let id = await m.start(
|
|
245
|
+
String(args.command ?? ''),
|
|
246
|
+
await here(ctx),
|
|
247
|
+
ctx?.call.entity.eid ?? o.call,
|
|
248
|
+
ctx?.session,
|
|
249
|
+
)
|
|
250
|
+
let p = await settle(m, id, {
|
|
251
|
+
ms: end - Date.now(),
|
|
252
|
+
signal: ctx?.signal,
|
|
253
|
+
}) ?? {}
|
|
254
|
+
return {
|
|
255
|
+
body: await shellResult(
|
|
256
|
+
m,
|
|
257
|
+
id,
|
|
258
|
+
p,
|
|
259
|
+
lines,
|
|
260
|
+
ctx?.signal?.aborted ? undefined : ms,
|
|
261
|
+
),
|
|
262
|
+
value: processValue(id, p),
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
wait: async (args, ctx) => {
|
|
266
|
+
let id = String(args.process ?? '')
|
|
267
|
+
let ms = Number(args.timeout ?? 60_000)
|
|
268
|
+
let p = await settle(m, id, { ms, signal: ctx?.signal })
|
|
269
|
+
if (!p) return { body: `no such process: ${id}` }
|
|
270
|
+
return told(
|
|
271
|
+
id,
|
|
272
|
+
p.exit
|
|
273
|
+
? `process ${id} ${code(p.exit.code)}`
|
|
274
|
+
: `process ${id} still running${pid(p)}${
|
|
275
|
+
ctx?.signal?.aborted ? '' : ` after ${ms}ms`
|
|
276
|
+
}`,
|
|
277
|
+
p,
|
|
278
|
+
)
|
|
279
|
+
},
|
|
280
|
+
stop: async (args) => {
|
|
281
|
+
let id = String(args.process ?? '')
|
|
282
|
+
let p = await m.look(id)
|
|
283
|
+
if (!p) return { body: `no such process: ${id}` }
|
|
284
|
+
if (p.exit) {
|
|
285
|
+
return {
|
|
286
|
+
body: `process ${id} ${code(p.exit.code)} already`,
|
|
287
|
+
value: processValue(id, p),
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
let grace = Number(args.grace ?? o.grace ?? 2000)
|
|
291
|
+
await m.kill(id, 'SIGTERM')
|
|
292
|
+
p = await settle(m, id, { ms: grace })
|
|
293
|
+
if (!p?.exit) {
|
|
294
|
+
await m.kill(id, 'SIGKILL')
|
|
295
|
+
p = await settle(m, id, { ms: grace })
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
body: p?.exit
|
|
299
|
+
? `process ${id} ${code(p.exit.code)}`
|
|
300
|
+
: `process ${id} signalled, no exit recorded yet`,
|
|
301
|
+
value: processValue(id, p ?? {}),
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
read: async (args, ctx) => {
|
|
305
|
+
let cwd = await here(ctx)
|
|
306
|
+
if (Array.isArray(args.paths)) {
|
|
307
|
+
let paths = args.paths.map((path) => under(cwd, String(path)))
|
|
308
|
+
let stats = await m.stat(paths)
|
|
309
|
+
return { body: JSON.stringify(stats), value: { paths, stats } }
|
|
310
|
+
}
|
|
311
|
+
let path = under(cwd, String(args.path ?? ''))
|
|
312
|
+
try {
|
|
313
|
+
return { body: await m.read(path) }
|
|
314
|
+
} catch (e) {
|
|
315
|
+
// A path that names no file is the caller's to correct, however the
|
|
316
|
+
// machine said so; any other failure is the machine's own.
|
|
317
|
+
let [found] = await m.stat([path]).catch(() => [])
|
|
318
|
+
if (!found || found.kind == 'file') throw e
|
|
319
|
+
throw new CallError('path', `No file at ${path}: ${SAID[found.kind]}`)
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
write: async (args, ctx) => {
|
|
323
|
+
let path = String(args.path ?? '')
|
|
324
|
+
await m.write(
|
|
325
|
+
under(await here(ctx), path),
|
|
326
|
+
String(args.body ?? args.content ?? ''),
|
|
327
|
+
)
|
|
328
|
+
return { body: `wrote ${path}` }
|
|
329
|
+
},
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let shellResult = async (
|
|
334
|
+
m: Machine,
|
|
335
|
+
id: string,
|
|
336
|
+
p: Proc | null,
|
|
337
|
+
lines: number,
|
|
338
|
+
ms?: number,
|
|
339
|
+
) => {
|
|
340
|
+
let head = p?.exit
|
|
341
|
+
? `process ${id} ${code(p.exit.code)}`
|
|
342
|
+
: !p?.pid
|
|
343
|
+
? `process ${id} start unconfirmed — inspect before retrying`
|
|
344
|
+
: `process ${id} still running${pid(p)}${
|
|
345
|
+
ms == null ? '' : ` after ${ms}ms`
|
|
346
|
+
} — wait or stop it by that id`
|
|
347
|
+
return [head, ...await m.tail(id, lines)].join('\n')
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** Recover the command identified by its invocation, without starting one. */
|
|
351
|
+
export let shellReceipt = async (
|
|
352
|
+
m: Machine,
|
|
353
|
+
call?: string,
|
|
354
|
+
lines = Number.MAX_SAFE_INTEGER,
|
|
355
|
+
): Promise<string | undefined> => {
|
|
356
|
+
let id = call && await m.receipt?.(call)
|
|
357
|
+
if (!id) return
|
|
358
|
+
let process = await m.look(id)
|
|
359
|
+
if (process) return shellResult(m, id, process, lines)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Bind tools to graph targets. Each call persists its concrete target before
|
|
363
|
+
* touching a filesystem. Recovery refuses an invocation without that target. */
|
|
364
|
+
export let workspaceTools = (
|
|
365
|
+
g: Graph,
|
|
366
|
+
workspaces: Workspaces,
|
|
367
|
+
o: MachineOpts = {},
|
|
368
|
+
): Tool[] =>
|
|
369
|
+
machineDeclared(o).map((declaration) => {
|
|
370
|
+
let process = ['wait', 'stop'].includes(declaration.name)
|
|
371
|
+
let parameters = declaration.parameters as Record<string, unknown>
|
|
372
|
+
let properties = parameters.properties as Record<string, unknown>
|
|
373
|
+
return {
|
|
374
|
+
...declaration,
|
|
375
|
+
parameters: {
|
|
376
|
+
...parameters,
|
|
377
|
+
properties: {
|
|
378
|
+
...properties,
|
|
379
|
+
...!process
|
|
380
|
+
? {
|
|
381
|
+
workspace: str(
|
|
382
|
+
"A workspace entity's eid; otherwise the session home. " +
|
|
383
|
+
"graph_query '.workspace.root=<directory>' finds the one " +
|
|
384
|
+
'at a directory',
|
|
385
|
+
),
|
|
386
|
+
cwd: os(
|
|
387
|
+
'Absolute command directory; otherwise the selected workspace default',
|
|
388
|
+
),
|
|
389
|
+
}
|
|
390
|
+
: {},
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
run: async (args, ctx) => {
|
|
394
|
+
// A durable call may already have been targeted by an interrupted worker.
|
|
395
|
+
let recorded = ctx
|
|
396
|
+
? (await g.get([ctx.call.entity.eid], ['call']))[0]
|
|
397
|
+
: undefined
|
|
398
|
+
let prior = (recorded?.call as Comp | undefined)?.args as
|
|
399
|
+
| Record<string, unknown>
|
|
400
|
+
| undefined
|
|
401
|
+
let input = prior?.workspace ? prior : args
|
|
402
|
+
let target = process
|
|
403
|
+
? {
|
|
404
|
+
workspace: await processWorkspace(g, String(input.process)),
|
|
405
|
+
cwd: undefined,
|
|
406
|
+
}
|
|
407
|
+
: await targetFor(g, ctx?.session, input)
|
|
408
|
+
if (ctx && recorded) {
|
|
409
|
+
await g.apply([{
|
|
410
|
+
entity: ctx.call.entity,
|
|
411
|
+
call: { args: { ...input, ...target } },
|
|
412
|
+
$was: { call: { args: token(prior ?? null) } },
|
|
413
|
+
}])
|
|
414
|
+
}
|
|
415
|
+
let tools = machineTools(await workspaces.connect(target.workspace), {
|
|
416
|
+
...o,
|
|
417
|
+
cwd: () => target.cwd,
|
|
418
|
+
})
|
|
419
|
+
return await tools.find((t) => t.name == declaration.name)!.run(
|
|
420
|
+
input,
|
|
421
|
+
ctx,
|
|
422
|
+
)
|
|
423
|
+
},
|
|
424
|
+
...declaration.name == 'shell'
|
|
425
|
+
? {
|
|
426
|
+
recover: async (args: Record<string, unknown>, ctx: ToolContext) => {
|
|
427
|
+
if (
|
|
428
|
+
typeof args.workspace != 'string' || typeof args.cwd != 'string'
|
|
429
|
+
) {
|
|
430
|
+
return 'Shell execution has no recorded workspace target; inspect before retrying.'
|
|
431
|
+
}
|
|
432
|
+
let tools = machineTools(await workspaces.connect(args.workspace), {
|
|
433
|
+
...o,
|
|
434
|
+
cwd: () => String(args.cwd),
|
|
435
|
+
})
|
|
436
|
+
return await tools.find((t) => t.name == 'shell')!.recover!(
|
|
437
|
+
args,
|
|
438
|
+
ctx,
|
|
439
|
+
)
|
|
440
|
+
},
|
|
441
|
+
}
|
|
442
|
+
: {},
|
|
443
|
+
}
|
|
444
|
+
})
|
package/vocab.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Machine declarations, importable without a runtime or storage.
|
|
2
|
+
import doc from './vocab.json' with {
|
|
3
|
+
type: 'json'
|
|
4
|
+
};
|
|
5
|
+
import operations from './effect-vocab.json' with {
|
|
6
|
+
type: 'json'
|
|
7
|
+
};
|
|
8
|
+
import manifest from './package.json' with {
|
|
9
|
+
type: 'json'
|
|
10
|
+
};
|
|
11
|
+
/** The machine vocabulary. */ export let machineDoc = doc;
|
|
12
|
+
/** The package's description, from its manifest. */ export let description = manifest.description;
|
|
13
|
+
/** Every vocabulary document this package declares. */ export let docs = [
|
|
14
|
+
machineDoc,
|
|
15
|
+
operations
|
|
16
|
+
];
|
|
17
|
+
//# sourceMappingURL=vocab.js.map
|
package/vocab.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,iEAAiE;AAGjE,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AACpD,OAAO,gBAAgB,2BAA2B;EAAE,MAAM;AAAO,EAAC;AAClE,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D,4BAA4B,GAC5B,OAAO,IAAI,aAAuB,IAAe;AACjD,kDAAkD,GAClD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AACrD,qDAAqD,GACrD,OAAO,IAAI,OAAmB;EAAC;EAAY;CAAuB,CAAA"}
|
package/vocab.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$id": "urn:uuid:99ac13d4-626b-4f9f-aabf-133bfe489b17",
|
|
3
|
+
"$defs": {
|
|
4
|
+
"machine": {
|
|
5
|
+
"$id": "urn:uuid:ba9cbcbd-d254-404e-be03-c92ba3bb7353",
|
|
6
|
+
"component": true,
|
|
7
|
+
"journal": false,
|
|
8
|
+
"type": "object",
|
|
9
|
+
"embed": false,
|
|
10
|
+
"description": "an actual computer with a stable identity and an optional connector address; without a reachable address it is inventory, not an execution target",
|
|
11
|
+
"properties": {
|
|
12
|
+
"address": {
|
|
13
|
+
"$id": "urn:uuid:be053b0f-0b4b-41ba-a0af-325ae807ff4a",
|
|
14
|
+
"type": "string",
|
|
15
|
+
"description": "the connection address selecting a configured computer connector; changing it preserves computer identity"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"machine_browser": {
|
|
20
|
+
"$id": "urn:uuid:fe43a0c4-8665-456f-8548-8d05cf88934a",
|
|
21
|
+
"component": true,
|
|
22
|
+
"type": "object",
|
|
23
|
+
"embed": false,
|
|
24
|
+
"description": "a managed browser lent by a registered machine",
|
|
25
|
+
"properties": {
|
|
26
|
+
"owner": {
|
|
27
|
+
"$id": "urn:uuid:e2dc8c95-502d-4bb4-83eb-77e8646ebec2",
|
|
28
|
+
"type": "string",
|
|
29
|
+
"description": "the person who attached the browser to the space"
|
|
30
|
+
},
|
|
31
|
+
"key_hash": {
|
|
32
|
+
"$id": "urn:uuid:21b91f5f-1970-4520-a9e7-41171e1d3b93",
|
|
33
|
+
"type": "string",
|
|
34
|
+
"description": "SHA-256 verifier for this machine's outbound browser credential"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"required": ["owner", "key_hash"]
|
|
38
|
+
},
|
|
39
|
+
"workspace": {
|
|
40
|
+
"$id": "urn:uuid:26fd7959-8b68-4ea1-b4fa-de087c037440",
|
|
41
|
+
"component": true,
|
|
42
|
+
"journal": false,
|
|
43
|
+
"type": "object",
|
|
44
|
+
"embed": false,
|
|
45
|
+
"description": "a file-and-command target on a computer or in a sandbox; its root is a default directory, not an access boundary. Missing placement is unknown, never this host",
|
|
46
|
+
"properties": {
|
|
47
|
+
"machine": {
|
|
48
|
+
"$id": "urn:uuid:233fb637-dfbc-4107-9d9a-e99d1d0a25a2",
|
|
49
|
+
"type": "string",
|
|
50
|
+
"ref": "machine",
|
|
51
|
+
"death": "keep",
|
|
52
|
+
"description": "the computer whose filesystem or sandbox contains this target; absent only when a backend cannot disclose placement"
|
|
53
|
+
},
|
|
54
|
+
"root": {
|
|
55
|
+
"$id": "urn:uuid:62d5e08c-43f7-4543-aac1-9151fd6eb012",
|
|
56
|
+
"type": "string",
|
|
57
|
+
"description": "the default absolute directory in this filesystem; placement and root remain fixed for the resource lifetime"
|
|
58
|
+
},
|
|
59
|
+
"sandbox": {
|
|
60
|
+
"$id": "urn:uuid:2b19e9fa-7dbb-476e-915b-74eecbacca0a",
|
|
61
|
+
"type": "string",
|
|
62
|
+
"ref": "sandbox",
|
|
63
|
+
"death": "keep",
|
|
64
|
+
"description": "the enclosing sandbox whose filesystem interprets paths; on a sandbox workspace the sandbox is the entity itself, not a self reference"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
package/vocab.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Machine declarations, importable without a runtime or storage.
|
|
2
|
+
|
|
3
|
+
import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
4
|
+
import doc from './vocab.json' with { type: 'json' }
|
|
5
|
+
import operations from './effect-vocab.json' with { type: 'json' }
|
|
6
|
+
import manifest from './package.json' with { type: 'json' }
|
|
7
|
+
|
|
8
|
+
/** The machine vocabulary. */
|
|
9
|
+
export let machineDoc: VocabDoc = doc as VocabDoc
|
|
10
|
+
/** The package's description, from its manifest. */
|
|
11
|
+
export let description: string = manifest.description
|
|
12
|
+
/** Every vocabulary document this package declares. */
|
|
13
|
+
export let docs: VocabDoc[] = [machineDoc, operations as VocabDoc]
|
package/workspace.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// File-and-command targets are independent of computer connection and sandbox
|
|
2
|
+
// provisioning. A connector only reaches an explicitly selected computer.
|
|
3
|
+
import { sleep as nap } from "@yaks/fp";
|
|
4
|
+
/** Bytes from base64, whitespace and all. */ export let unbase64 = (text)=>Uint8Array.from(atob(text.replaceAll(/\s/g, '')), (c)=>c.charCodeAt(0));
|
|
5
|
+
/** UTF-8 text from base64, whitespace and all. */ export let decode = (text)=>new TextDecoder().decode(unbase64(text));
|
|
6
|
+
// One shell for every path, and no forks but for a link: `cd -P` resolves a
|
|
7
|
+
// directory's links, as `pwd -P` says, and `realpath` runs only for a path
|
|
8
|
+
// whose last part is itself a link. A line each: the kind, then the canonical
|
|
9
|
+
// path with `%` and newline escaped as a URI would.
|
|
10
|
+
let statScript = (paths)=>`here=$PWD
|
|
11
|
+
for f in ${paths.map(quote).join(' ')}; do
|
|
12
|
+
k=missing c=
|
|
13
|
+
if test -e "$f"; then
|
|
14
|
+
if test -f "$f"; then k=file; elif test -d "$f"; then k=directory; else k=other; fi
|
|
15
|
+
if test -L "$f"; then c=$(realpath -e -- "$f")
|
|
16
|
+
elif test $k = directory; then cd -P -- "$f" 2>/dev/null && c=$PWD
|
|
17
|
+
else case $f in */*) d=\${f%/*};; *) d=.;; esac
|
|
18
|
+
cd -P -- "\${d:-/}" 2>/dev/null && c=\${PWD%/}/\${f##*/}; fi
|
|
19
|
+
test -n "$c" || c=$(realpath -e -- "$f")
|
|
20
|
+
cd -- "$here"; c=\${c//%/%25}; c=\${c//$'\\n'/%0A}
|
|
21
|
+
elif test -L "$f"; then k=dangling; fi
|
|
22
|
+
printf '%s %s\\n' "$k" "$c"
|
|
23
|
+
done`;
|
|
24
|
+
/** `stat` for a computer that runs bash scripts, all the paths in one. */ export let shellStat = (execute)=>async (paths)=>{
|
|
25
|
+
if (!paths.length) return [];
|
|
26
|
+
let lines = (await execute(statScript(paths))).replace(/\n$/, '').split('\n');
|
|
27
|
+
if (lines.length != paths.length) {
|
|
28
|
+
throw new Error('Invalid workspace stat response');
|
|
29
|
+
}
|
|
30
|
+
return lines.map((line)=>{
|
|
31
|
+
let at = line.indexOf(' ');
|
|
32
|
+
let kind = line.slice(0, at), canonical = line.slice(at + 1);
|
|
33
|
+
if (kind == 'missing' || kind == 'dangling') return {
|
|
34
|
+
kind
|
|
35
|
+
};
|
|
36
|
+
if ((kind == 'file' || kind == 'directory' || kind == 'other') && canonical) return {
|
|
37
|
+
kind,
|
|
38
|
+
canonical: decodeURIComponent(canonical)
|
|
39
|
+
};
|
|
40
|
+
throw new Error('Invalid workspace stat response');
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
/** The process once it has ended, `ms` has passed or `signal` aborted,
|
|
44
|
+
* whichever is first; null for an id the machine does not know. Like checking
|
|
45
|
+
* a kettle, it looks often at first and then less: the first gap is the
|
|
46
|
+
* machine's `poll`, each next one half again as long, up to two seconds. A
|
|
47
|
+
* short command answers within a fraction of a second; a long wait costs a
|
|
48
|
+
* look every two seconds. */ export let settle = async (m, id, { ms = Infinity, signal, now = Date.now, sleep = nap } = {})=>{
|
|
49
|
+
let end = now() + ms, gap = m.poll ?? 100;
|
|
50
|
+
for(;;){
|
|
51
|
+
let p = await m.look(id);
|
|
52
|
+
if (!p || p.exit || signal?.aborted || now() >= end) return p;
|
|
53
|
+
await sleep(Math.max(0, Math.min(gap, end - now())), signal);
|
|
54
|
+
gap = Math.max(gap, Math.min(gap * 1.5, 2000));
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
/** The process once it has ended, or null for an id the machine does not
|
|
58
|
+
* know, handing `write` what it prints as it prints it: a terminal's view of
|
|
59
|
+
* a command on another computer. Each look reads what is new since the last;
|
|
60
|
+
* the first gap is the machine's `poll` and each quiet one half again as long,
|
|
61
|
+
* up to a second, so a run's last words come within a second of its end. An
|
|
62
|
+
* abort ends only the following, never the command. */ export let follow = async (m, id, write, { signal, sleep = nap } = {})=>{
|
|
63
|
+
let at = 0, gap = m.poll ?? 100;
|
|
64
|
+
for(;;){
|
|
65
|
+
// Look first: what an ended process printed is all there by its exit.
|
|
66
|
+
let p = await m.look(id);
|
|
67
|
+
let bytes = await m.output(id, at);
|
|
68
|
+
if (bytes.length) {
|
|
69
|
+
at += bytes.length;
|
|
70
|
+
await write(bytes);
|
|
71
|
+
gap = m.poll ?? 100;
|
|
72
|
+
}
|
|
73
|
+
if (!p || p.exit || signal?.aborted) return p;
|
|
74
|
+
await sleep(gap, signal);
|
|
75
|
+
gap = Math.min(gap * 1.5, 1000);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/** Quote one value for the POSIX shell. */ export let quote = (value)=>"'" + value.replaceAll("'", "'\\''") + "'";
|
|
79
|
+
//# sourceMappingURL=workspace.js.map
|
package/workspace.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace.js","sources":["./workspace.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,0EAA0E;AAC1E,SAAS,SAAS,GAAG,wBAA6B;AA0ClD,2CAA2C,GAC3C,OAAO,IAAI,WAAW,CAAC,OACrB,WAAW,IAAI,CAAC,KAAK,KAAK,UAAU,CAAC,OAAO,MAAM,CAAC,IAAM,EAAE,UAAU,CAAC,IAAG;AAE3E,gDAAgD,GAChD,OAAO,IAAI,SAAS,CAAC,OACnB,IAAI,cAAc,MAAM,CAAC,SAAS,OAAM;AAE1C,4EAA4E;AAC5E,2EAA2E;AAC3E,8EAA8E;AAC9E,oDAAoD;AACpD,IAAI,aAAa,CAAC,QAChB,CAAC;SACM,EAAE,MAAM,GAAG,CAAC,OAAO,IAAI,CAAC,KAAK;;;;;;;;;;;;IAYlC,CAAC;AAEL,wEAAwE,GACxE,OAAO,IAAI,YACT,CAAC,UACD,OAAO;IACL,IAAI,CAAC,MAAM,MAAM,EAAE,OAAO,EAAE;IAC5B,IAAI,QAAQ,CAAC,MAAM,QAAQ,WAAW,OAAO,EAAE,OAAO,CAAC,OAAO,IAC3D,KAAK,CAAC;IACT,IAAI,MAAM,MAAM,IAAI,MAAM,MAAM,EAAE;MAChC,MAAM,IAAI,MAAM;IAClB;IACA,OAAO,MAAM,GAAG,CAAC,CAAC;MAChB,IAAI,KAAK,KAAK,OAAO,CAAC;MACtB,IAAI,OAAO,KAAK,KAAK,CAAC,GAAG,KAAK,YAAY,KAAK,KAAK,CAAC,KAAK;MAC1D,IAAI,QAAQ,aAAa,QAAQ,YAAY,OAAO;QAAE;MAAK;MAC3D,IACE,CAAC,QAAQ,UAAU,QAAQ,eAAe,QAAQ,OAAO,KAAK,WAC9D,OAAO;QAAE;QAAM,WAAW,mBAAmB;MAAW;MAC1D,MAAM,IAAI,MAAM;IAClB;EACF,EAAC;AAaH;;;;;2BAK2B,GAC3B,OAAO,IAAI,SAAS,OAClB,GACA,IACA,EAAE,KAAK,QAAQ,EAAE,MAAM,EAAE,MAAM,KAAK,GAAG,EAAE,QAAQ,GAAG,EAAc,GAAG,CAAC,CAAC;EAEvE,IAAI,MAAM,QAAQ,IAAI,MAAM,EAAE,IAAI,IAAI;EACtC,OAAS;IACP,IAAI,IAAI,MAAM,EAAE,IAAI,CAAC;IACrB,IAAI,CAAC,KAAK,EAAE,IAAI,IAAI,QAAQ,WAAW,SAAS,KAAK,OAAO;IAC5D,MAAM,MAAM,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,KAAK,MAAM,SAAS;IACrD,MAAM,KAAK,GAAG,CAAC,KAAK,KAAK,GAAG,CAAC,MAAM,KAAK;EAC1C;AACF,EAAC;AAED;;;;;qDAKqD,GACrD,OAAO,IAAI,SAAS,OAClB,GACA,IACA,OACA,EAAE,MAAM,EAAE,QAAQ,GAAG,EAAwC,GAAG,CAAC,CAAC;EAElE,IAAI,KAAK,GAAG,MAAM,EAAE,IAAI,IAAI;EAC5B,OAAS;IACP,sEAAsE;IACtE,IAAI,IAAI,MAAM,EAAE,IAAI,CAAC;IACrB,IAAI,QAAQ,MAAM,EAAE,MAAM,CAAC,IAAI;IAC/B,IAAI,MAAM,MAAM,EAAE;MAChB,MAAM,MAAM,MAAM;MAClB,MAAM,MAAM;MACZ,MAAM,EAAE,IAAI,IAAI;IAClB;IACA,IAAI,CAAC,KAAK,EAAE,IAAI,IAAI,QAAQ,SAAS,OAAO;IAC5C,MAAM,MAAM,KAAK;IACjB,MAAM,KAAK,GAAG,CAAC,MAAM,KAAK;EAC5B;AACF,EAAC;AA4BD,yCAAyC,GACzC,OAAO,IAAI,QAAQ,CAAC,QAClB,MAAM,MAAM,UAAU,CAAC,KAAK,WAAW,IAAG"}
|