@namzu/sdk 69.2.0 → 69.4.0
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/CHANGELOG.md +25 -0
- package/dist/authorization/readonly-commands.d.ts.map +1 -1
- package/dist/authorization/readonly-commands.js +30 -2
- package/dist/authorization/readonly-commands.js.map +1 -1
- package/dist/constants/plugin/index.d.ts +8 -0
- package/dist/constants/plugin/index.d.ts.map +1 -1
- package/dist/constants/plugin/index.js +8 -0
- package/dist/constants/plugin/index.js.map +1 -1
- package/dist/plugin/common-layout/index.d.ts +5 -0
- package/dist/plugin/common-layout/index.d.ts.map +1 -0
- package/dist/plugin/common-layout/index.js +3 -0
- package/dist/plugin/common-layout/index.js.map +1 -0
- package/dist/plugin/common-layout/mcp.d.ts +21 -0
- package/dist/plugin/common-layout/mcp.d.ts.map +1 -0
- package/dist/plugin/common-layout/mcp.js +305 -0
- package/dist/plugin/common-layout/mcp.js.map +1 -0
- package/dist/plugin/common-layout/paths.d.ts +27 -0
- package/dist/plugin/common-layout/paths.d.ts.map +1 -0
- package/dist/plugin/common-layout/paths.js +48 -0
- package/dist/plugin/common-layout/paths.js.map +1 -0
- package/dist/plugin/common-layout/read.d.ts +25 -0
- package/dist/plugin/common-layout/read.d.ts.map +1 -0
- package/dist/plugin/common-layout/read.js +1155 -0
- package/dist/plugin/common-layout/read.js.map +1 -0
- package/dist/plugin/common-layout/types.d.ts +66 -0
- package/dist/plugin/common-layout/types.d.ts.map +1 -0
- package/dist/plugin/common-layout/types.js +2 -0
- package/dist/plugin/common-layout/types.js.map +1 -0
- package/dist/plugin/common-layout-names.d.ts +65 -0
- package/dist/plugin/common-layout-names.d.ts.map +1 -0
- package/dist/plugin/common-layout-names.js +94 -0
- package/dist/plugin/common-layout-names.js.map +1 -0
- package/dist/plugin/hook-module/capabilities.d.ts +41 -0
- package/dist/plugin/hook-module/capabilities.d.ts.map +1 -0
- package/dist/plugin/hook-module/capabilities.js +438 -0
- package/dist/plugin/hook-module/capabilities.js.map +1 -0
- package/dist/plugin/hook-module/consent.d.ts +39 -0
- package/dist/plugin/hook-module/consent.d.ts.map +1 -0
- package/dist/plugin/hook-module/consent.js +59 -0
- package/dist/plugin/hook-module/consent.js.map +1 -0
- package/dist/plugin/hook-module/events.d.ts +33 -0
- package/dist/plugin/hook-module/events.d.ts.map +1 -0
- package/dist/plugin/hook-module/events.js +149 -0
- package/dist/plugin/hook-module/events.js.map +1 -0
- package/dist/plugin/hook-module/host.d.ts +79 -0
- package/dist/plugin/hook-module/host.d.ts.map +1 -0
- package/dist/plugin/hook-module/host.js +546 -0
- package/dist/plugin/hook-module/host.js.map +1 -0
- package/dist/plugin/hook-module/index.d.ts +11 -0
- package/dist/plugin/hook-module/index.d.ts.map +1 -0
- package/dist/plugin/hook-module/index.js +6 -0
- package/dist/plugin/hook-module/index.js.map +1 -0
- package/dist/plugin/hook-module/scan.d.ts +99 -0
- package/dist/plugin/hook-module/scan.d.ts.map +1 -0
- package/dist/plugin/hook-module/scan.js +789 -0
- package/dist/plugin/hook-module/scan.js.map +1 -0
- package/dist/plugin/hook-module/worker-source.d.ts +31 -0
- package/dist/plugin/hook-module/worker-source.d.ts.map +1 -0
- package/dist/plugin/hook-module/worker-source.js +498 -0
- package/dist/plugin/hook-module/worker-source.js.map +1 -0
- package/dist/plugin/index.d.ts +5 -2
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +4 -1
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/lifecycle.d.ts +57 -0
- package/dist/plugin/lifecycle.d.ts.map +1 -1
- package/dist/plugin/lifecycle.js +92 -11
- package/dist/plugin/lifecycle.js.map +1 -1
- package/dist/plugin/loader.d.ts +30 -0
- package/dist/plugin/loader.d.ts.map +1 -1
- package/dist/plugin/loader.js +76 -19
- package/dist/plugin/loader.js.map +1 -1
- package/dist/plugin/shell-hook-env.d.ts +7 -0
- package/dist/plugin/shell-hook-env.d.ts.map +1 -1
- package/dist/plugin/shell-hook-env.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +10 -1
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +81 -70
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/prompt/coding-agent-doctrine.d.ts +1 -1
- package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
- package/dist/prompt/coding-agent-doctrine.js +1 -1
- package/dist/provider/side-request-reasoning.d.ts.map +1 -1
- package/dist/provider/side-request-reasoning.js +5 -1
- package/dist/provider/side-request-reasoning.js.map +1 -1
- package/dist/public-runtime.d.ts +3 -2
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +2 -2
- package/dist/public-runtime.js.map +1 -1
- package/dist/runtime/query/auto-review/classifier.d.ts.map +1 -1
- package/dist/runtime/query/auto-review/classifier.js +13 -2
- package/dist/runtime/query/auto-review/classifier.js.map +1 -1
- package/dist/runtime/query/auto-review/failure.d.ts +12 -5
- package/dist/runtime/query/auto-review/failure.d.ts.map +1 -1
- package/dist/runtime/query/auto-review/failure.js +42 -7
- package/dist/runtime/query/auto-review/failure.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +22 -6
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/turn/check-scope.d.ts +18 -0
- package/dist/runtime/query/turn/check-scope.d.ts.map +1 -0
- package/dist/runtime/query/turn/check-scope.js +90 -0
- package/dist/runtime/query/turn/check-scope.js.map +1 -0
- package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.js +13 -0
- package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
- package/dist/skills/loader.d.ts +8 -2
- package/dist/skills/loader.d.ts.map +1 -1
- package/dist/skills/loader.js +22 -11
- package/dist/skills/loader.js.map +1 -1
- package/dist/skills/registry.d.ts.map +1 -1
- package/dist/skills/registry.js +9 -1
- package/dist/skills/registry.js.map +1 -1
- package/dist/tools/builtins/job.d.ts.map +1 -1
- package/dist/tools/builtins/job.js +4 -1
- package/dist/tools/builtins/job.js.map +1 -1
- package/dist/tools/builtins/wait-for-job.d.ts.map +1 -1
- package/dist/tools/builtins/wait-for-job.js +2 -1
- package/dist/tools/builtins/wait-for-job.js.map +1 -1
- package/dist/types/plugin/index.d.ts +73 -0
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +28 -6
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/skills/index.d.ts +6 -0
- package/dist/types/skills/index.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/authorization/readonly-commands.ts +30 -2
- package/src/constants/plugin/index.ts +9 -0
- package/src/plugin/common-layout/index.ts +19 -0
- package/src/plugin/common-layout/mcp.ts +362 -0
- package/src/plugin/common-layout/paths.ts +70 -0
- package/src/plugin/common-layout/read.ts +1282 -0
- package/src/plugin/common-layout/types.ts +77 -0
- package/src/plugin/common-layout-names.ts +108 -0
- package/src/plugin/hook-module/capabilities.ts +500 -0
- package/src/plugin/hook-module/consent.ts +96 -0
- package/src/plugin/hook-module/events.ts +177 -0
- package/src/plugin/hook-module/host.ts +649 -0
- package/src/plugin/hook-module/index.ts +52 -0
- package/src/plugin/hook-module/scan.ts +1004 -0
- package/src/plugin/hook-module/worker-source.ts +500 -0
- package/src/plugin/index.ts +13 -1
- package/src/plugin/lifecycle.ts +179 -17
- package/src/plugin/loader.ts +97 -18
- package/src/plugin/shell-hook-env.ts +7 -0
- package/src/plugin/shell-hook.ts +91 -70
- package/src/prompt/coding-agent-doctrine.ts +1 -1
- package/src/provider/side-request-reasoning.ts +5 -1
- package/src/public-runtime.ts +35 -0
- package/src/runtime/query/auto-review/classifier.ts +14 -2
- package/src/runtime/query/auto-review/failure.ts +52 -7
- package/src/runtime/query/review-policy.ts +30 -6
- package/src/runtime/query/turn/check-scope.ts +90 -0
- package/src/runtime/query/turn/outcome-ledger.ts +11 -0
- package/src/skills/loader.ts +26 -9
- package/src/skills/registry.ts +9 -1
- package/src/tools/builtins/job.ts +4 -1
- package/src/tools/builtins/wait-for-job.ts +3 -1
- package/src/types/plugin/index.ts +81 -24
- package/src/types/skills/index.ts +7 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The program a hooks module runs in, as the text of a worker thread.
|
|
3
|
+
*
|
|
4
|
+
* It is text, not a file, so it runs the same from the SDK's own build, from a
|
|
5
|
+
* bundle and from a test, with nothing to locate on disk. It is plain script:
|
|
6
|
+
* `require` is the worker's own (there is no import in it), and it holds no
|
|
7
|
+
* dependency.
|
|
8
|
+
*
|
|
9
|
+
* The module itself runs in a `node:vm` context of its own: the language's
|
|
10
|
+
* builtins, `console`, and nothing else. No `require`, no `process`, no timers,
|
|
11
|
+
* no network, no web classes. What a module can do beyond computing goes through
|
|
12
|
+
* `$`, and `$` is a set of messages to the host, which decides.
|
|
13
|
+
*
|
|
14
|
+
* Nothing the worker made in its own realm is handed to the module. Everything
|
|
15
|
+
* the module can touch (`$`, `console`, the event, `next`, its signal, the
|
|
16
|
+
* errors it catches) is made by a bootstrap script that runs inside the context,
|
|
17
|
+
* and the worker talks to that script in strings only, so no object the module
|
|
18
|
+
* can reach has the worker's `Function` behind its `constructor`. The context
|
|
19
|
+
* is made from a sandbox with no prototype and with code generation from
|
|
20
|
+
* strings off, and the worker's own function constructors are replaced by ones
|
|
21
|
+
* that throw, so a reference that still leaked would not reach `process`. It is
|
|
22
|
+
* a worker thread, not a process boundary, and a hostile module is not
|
|
23
|
+
* contained against a flaw in V8 itself; a module runs only after the person
|
|
24
|
+
* has been told what it does, and what it calls on `$` is checked before it
|
|
25
|
+
* runs.
|
|
26
|
+
*
|
|
27
|
+
* Messages to the worker: `load`, `dispatch`, `overrun`, `grace-over`, `abort`,
|
|
28
|
+
* `call-result`. Messages from it: `loaded`, `result`, `call`, `log`.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** Runs inside the module's context, and is the only code there besides the module. */
|
|
32
|
+
const BOOTSTRAP_SOURCE = String.raw`
|
|
33
|
+
(function (send, evaluate) {
|
|
34
|
+
'use strict'
|
|
35
|
+
const MAX_LOG = 4000
|
|
36
|
+
|
|
37
|
+
const post = (message) => {
|
|
38
|
+
try {
|
|
39
|
+
send(JSON.stringify(message))
|
|
40
|
+
return true
|
|
41
|
+
} catch (error) {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const text = (value) => {
|
|
47
|
+
if (typeof value === 'string') return value
|
|
48
|
+
try {
|
|
49
|
+
const json = JSON.stringify(value)
|
|
50
|
+
return json === undefined ? String(value) : json
|
|
51
|
+
} catch (error) {
|
|
52
|
+
return String(value)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const log = (level, args) => {
|
|
56
|
+
post({ t: 'log', level, text: args.map(text).join(' ').slice(0, MAX_LOG) })
|
|
57
|
+
}
|
|
58
|
+
const messageOf = (error) =>
|
|
59
|
+
error !== null && typeof error === 'object' && typeof error.message === 'string'
|
|
60
|
+
? error.message
|
|
61
|
+
: String(error)
|
|
62
|
+
|
|
63
|
+
const consoleShim = {}
|
|
64
|
+
for (const level of ['log', 'info', 'warn', 'error', 'debug']) {
|
|
65
|
+
consoleShim[level] = (...args) => log(level, args)
|
|
66
|
+
}
|
|
67
|
+
globalThis.console = Object.freeze(consoleShim)
|
|
68
|
+
|
|
69
|
+
const BUDGET = Object.freeze({ ms: 10000, catchMs: 1000, lingerMs: 5000 })
|
|
70
|
+
|
|
71
|
+
let pluginName = ''
|
|
72
|
+
let pluginRoot = ''
|
|
73
|
+
let dollar
|
|
74
|
+
let registrations = []
|
|
75
|
+
let registering = true
|
|
76
|
+
let nextCall = 1
|
|
77
|
+
const pendingCalls = new Map()
|
|
78
|
+
const dispatches = new Map()
|
|
79
|
+
|
|
80
|
+
function call(noun, method, args) {
|
|
81
|
+
return new Promise((resolve, reject) => {
|
|
82
|
+
const id = nextCall++
|
|
83
|
+
pendingCalls.set(id, { resolve, reject })
|
|
84
|
+
if (!post({ t: 'call', cid: id, noun, method, args })) {
|
|
85
|
+
pendingCalls.delete(id)
|
|
86
|
+
reject(new Error('the arguments of $.' + noun + '.' + method + ' are not plain data'))
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function unavailable(path) {
|
|
92
|
+
return () => {
|
|
93
|
+
throw new Error(path + ' is not available in Namzu')
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const locked = {
|
|
98
|
+
set() {
|
|
99
|
+
throw new TypeError('$ cannot be changed')
|
|
100
|
+
},
|
|
101
|
+
defineProperty() {
|
|
102
|
+
throw new TypeError('$ cannot be changed')
|
|
103
|
+
},
|
|
104
|
+
deleteProperty() {
|
|
105
|
+
throw new TypeError('$ cannot be changed')
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// One noun of $: the calls Namzu answers go to the host, any other name throws where it is called.
|
|
110
|
+
function noun(name, methods) {
|
|
111
|
+
const target = {}
|
|
112
|
+
for (const method of methods) {
|
|
113
|
+
target[method] = (...args) => call(name, method, args)
|
|
114
|
+
}
|
|
115
|
+
Object.freeze(target)
|
|
116
|
+
return new Proxy(target, {
|
|
117
|
+
get(object, key) {
|
|
118
|
+
if (typeof key !== 'string') return undefined
|
|
119
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) return object[key]
|
|
120
|
+
return unavailable('$.' + name + '.' + key)
|
|
121
|
+
},
|
|
122
|
+
...locked,
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function makeDollar(methodsByNoun) {
|
|
127
|
+
const nouns = { plugin: Object.freeze({ name: pluginName, root: pluginRoot }) }
|
|
128
|
+
for (const key of Object.keys(methodsByNoun)) nouns[key] = noun(key, methodsByNoun[key])
|
|
129
|
+
Object.freeze(nouns)
|
|
130
|
+
return new Proxy(nouns, {
|
|
131
|
+
get(object, key) {
|
|
132
|
+
if (typeof key !== 'string') return undefined
|
|
133
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) return object[key]
|
|
134
|
+
return noun(key, [])
|
|
135
|
+
},
|
|
136
|
+
...locked,
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function deepFreeze(value, seen) {
|
|
141
|
+
if (value === null || typeof value !== 'object' || seen.has(value)) return value
|
|
142
|
+
seen.add(value)
|
|
143
|
+
for (const key of Object.keys(value)) deepFreeze(value[key], seen)
|
|
144
|
+
return Object.freeze(value)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const isRegExp = (value) => Object.prototype.toString.call(value) === '[object RegExp]'
|
|
148
|
+
|
|
149
|
+
// A matcher is an object of fields of the event: a scalar is ===, a RegExp tests the value as
|
|
150
|
+
// a string, an array matches if any element does, an object is a partial of an object.
|
|
151
|
+
function matchValue(matcher, value) {
|
|
152
|
+
if (isRegExp(matcher)) {
|
|
153
|
+
matcher.lastIndex = 0
|
|
154
|
+
return typeof value === 'string' || typeof value === 'number' ? matcher.test(String(value)) : false
|
|
155
|
+
}
|
|
156
|
+
if (Array.isArray(matcher)) return matcher.some((item) => matchValue(item, value))
|
|
157
|
+
if (matcher !== null && typeof matcher === 'object') {
|
|
158
|
+
if (value === null || typeof value !== 'object') return false
|
|
159
|
+
return Object.keys(matcher).every((key) => matchValue(matcher[key], value[key]))
|
|
160
|
+
}
|
|
161
|
+
return matcher === value
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function matches(matcher, event) {
|
|
165
|
+
if (matcher === undefined) return true
|
|
166
|
+
return matchValue(matcher, event)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The signal a hook is given: the parts of an AbortSignal a hook reads, made here.
|
|
170
|
+
function makeSignal() {
|
|
171
|
+
const listeners = []
|
|
172
|
+
const signal = {
|
|
173
|
+
aborted: false,
|
|
174
|
+
reason: undefined,
|
|
175
|
+
onabort: null,
|
|
176
|
+
addEventListener(type, listener, options) {
|
|
177
|
+
if (type === 'abort' && typeof listener === 'function') {
|
|
178
|
+
listeners.push({ listener, once: options !== null && typeof options === 'object' && options.once === true })
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
removeEventListener(type, listener) {
|
|
182
|
+
for (let i = listeners.length - 1; i >= 0; i--) {
|
|
183
|
+
if (type === 'abort' && listeners[i].listener === listener) listeners.splice(i, 1)
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
throwIfAborted() {
|
|
187
|
+
if (signal.aborted) throw signal.reason
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
const abort = (reason) => {
|
|
191
|
+
if (signal.aborted) return
|
|
192
|
+
signal.aborted = true
|
|
193
|
+
signal.reason = reason === undefined ? new Error('aborted') : reason
|
|
194
|
+
const event = { type: 'abort', target: signal }
|
|
195
|
+
const calling = []
|
|
196
|
+
if (typeof signal.onabort === 'function') calling.push(signal.onabort)
|
|
197
|
+
for (let i = 0; i < listeners.length; i++) {
|
|
198
|
+
calling.push(listeners[i].listener)
|
|
199
|
+
}
|
|
200
|
+
for (let i = listeners.length - 1; i >= 0; i--) if (listeners[i].once) listeners.splice(i, 1)
|
|
201
|
+
for (const listener of calling) {
|
|
202
|
+
try {
|
|
203
|
+
listener.call(signal, event)
|
|
204
|
+
} catch (error) {
|
|
205
|
+
log('error', ['an abort listener threw: ' + messageOf(error)])
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return { signal: Object.freeze(signal), abort }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function loadProgram(program) {
|
|
213
|
+
const cache = new Map()
|
|
214
|
+
const load = (rel) => {
|
|
215
|
+
const cached = cache.get(rel)
|
|
216
|
+
if (cached !== undefined) return cached
|
|
217
|
+
const exports = Object.create(null)
|
|
218
|
+
cache.set(rel, exports)
|
|
219
|
+
const source = program.files[rel]
|
|
220
|
+
if (typeof source !== 'string') throw new Error('no such file in the module: ' + rel)
|
|
221
|
+
const compiled = evaluate(rel, source)
|
|
222
|
+
if (typeof compiled === 'string') throw new Error(compiled)
|
|
223
|
+
compiled(exports, load)
|
|
224
|
+
return exports
|
|
225
|
+
}
|
|
226
|
+
return load(program.entry)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function load(message) {
|
|
230
|
+
pluginName = message.name
|
|
231
|
+
pluginRoot = message.root
|
|
232
|
+
dollar = makeDollar(message.nouns)
|
|
233
|
+
const options = deepFreeze(message.options || {}, new Set())
|
|
234
|
+
const exports = loadProgram(message.program)
|
|
235
|
+
const register = typeof exports.register === 'function' ? exports.register : exports.default
|
|
236
|
+
if (typeof register !== 'function') {
|
|
237
|
+
throw new Error('the module does not export a function named register')
|
|
238
|
+
}
|
|
239
|
+
const on = (pattern, second, third) => {
|
|
240
|
+
if (!registering) throw new Error('on() is called while register() runs, not after')
|
|
241
|
+
const hasMatcher = third !== undefined
|
|
242
|
+
const hook = hasMatcher ? third : second
|
|
243
|
+
if (typeof pattern !== 'string') throw new TypeError('the event name passed to on() is not a string')
|
|
244
|
+
if (typeof hook !== 'function') throw new TypeError('on("' + pattern + '") needs a function as its hook')
|
|
245
|
+
const entry = { pattern, matcher: hasMatcher ? second : undefined, hasMatcher, hook, catcher: undefined }
|
|
246
|
+
registrations.push(entry)
|
|
247
|
+
return Object.freeze({
|
|
248
|
+
catch(handler) {
|
|
249
|
+
if (!registering) throw new Error('.catch() is set while register() runs, not after')
|
|
250
|
+
if (typeof handler !== 'function') throw new TypeError('.catch() takes a function')
|
|
251
|
+
if (entry.catcher !== undefined) throw new Error('on("' + pattern + '") already has a .catch')
|
|
252
|
+
entry.catcher = handler
|
|
253
|
+
},
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
const answered = register(on, options)
|
|
257
|
+
if (answered !== null && typeof answered === 'object' && typeof answered.then === 'function') {
|
|
258
|
+
await answered
|
|
259
|
+
}
|
|
260
|
+
registering = false
|
|
261
|
+
post({
|
|
262
|
+
t: 'loaded',
|
|
263
|
+
registrations: registrations.map((entry) => ({ pattern: entry.pattern, hasMatcher: entry.hasMatcher })),
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async function dispatch(message) {
|
|
268
|
+
const { signal, abort } = makeSignal()
|
|
269
|
+
const state = { abort, overrun: undefined, rejectOverrun: undefined, graceOver: undefined, rejectGrace: undefined }
|
|
270
|
+
state.overrun = new Promise((_, reject) => {
|
|
271
|
+
state.rejectOverrun = reject
|
|
272
|
+
})
|
|
273
|
+
state.overrun.catch(() => undefined)
|
|
274
|
+
state.graceOver = new Promise((_, reject) => {
|
|
275
|
+
state.rejectGrace = reject
|
|
276
|
+
})
|
|
277
|
+
state.graceOver.catch(() => undefined)
|
|
278
|
+
dispatches.set(message.id, state)
|
|
279
|
+
const failures = []
|
|
280
|
+
const event = deepFreeze(message.e, new Set())
|
|
281
|
+
|
|
282
|
+
const run = async (position, current) => {
|
|
283
|
+
if (position >= message.regs.length) return {}
|
|
284
|
+
const entry = registrations[message.regs[position]]
|
|
285
|
+
if (!matches(entry.matcher, current)) return run(position + 1, current)
|
|
286
|
+
let called = false
|
|
287
|
+
let beneath
|
|
288
|
+
const next = (changed) => {
|
|
289
|
+
if (!called) {
|
|
290
|
+
called = true
|
|
291
|
+
beneath = run(position + 1, changed === undefined ? current : changed)
|
|
292
|
+
}
|
|
293
|
+
return beneath
|
|
294
|
+
}
|
|
295
|
+
const nextFor = (extra) => {
|
|
296
|
+
const api = (changed) => next(changed)
|
|
297
|
+
Object.assign(api, {
|
|
298
|
+
signal,
|
|
299
|
+
event: message.name,
|
|
300
|
+
origin: Object.freeze({ plugin: pluginName }),
|
|
301
|
+
budget: BUDGET,
|
|
302
|
+
to: (changed) => next(changed),
|
|
303
|
+
is: (pattern) =>
|
|
304
|
+
pattern === message.name ||
|
|
305
|
+
pattern === '*' ||
|
|
306
|
+
(pattern.endsWith('.*') && message.name.startsWith(pattern.slice(0, -1))),
|
|
307
|
+
...extra,
|
|
308
|
+
})
|
|
309
|
+
Object.defineProperty(api, 'called', { get: () => called })
|
|
310
|
+
return api
|
|
311
|
+
}
|
|
312
|
+
let value
|
|
313
|
+
try {
|
|
314
|
+
value = await Promise.race([
|
|
315
|
+
Promise.resolve().then(() => entry.hook(dollar, current, nextFor({}))),
|
|
316
|
+
state.overrun,
|
|
317
|
+
])
|
|
318
|
+
if (value === undefined) throw new Error('the hook returned nothing')
|
|
319
|
+
return value
|
|
320
|
+
} catch (error) {
|
|
321
|
+
const why = entry.pattern + ': ' + messageOf(error)
|
|
322
|
+
if (entry.catcher !== undefined) {
|
|
323
|
+
try {
|
|
324
|
+
const answer = await Promise.race([
|
|
325
|
+
Promise.resolve().then(() => entry.catcher(dollar, current, nextFor({ error: messageOf(error) }))),
|
|
326
|
+
state.graceOver,
|
|
327
|
+
])
|
|
328
|
+
if (answer !== undefined) return answer
|
|
329
|
+
} catch (inner) {
|
|
330
|
+
failures.push(why + ' (and its .catch: ' + messageOf(inner) + ')')
|
|
331
|
+
return called ? beneath : run(position + 1, current)
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
failures.push(why)
|
|
335
|
+
// The failed hook is absent: what is beneath it answers.
|
|
336
|
+
return called ? beneath : run(position + 1, current)
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
try {
|
|
341
|
+
const value = await run(0, event)
|
|
342
|
+
if (!post({ t: 'result', id: message.id, ok: true, value, failures })) {
|
|
343
|
+
post({ t: 'result', id: message.id, ok: false, message: 'its answer is not plain data', failures })
|
|
344
|
+
}
|
|
345
|
+
} catch (error) {
|
|
346
|
+
post({ t: 'result', id: message.id, ok: false, message: messageOf(error), failures })
|
|
347
|
+
} finally {
|
|
348
|
+
dispatches.delete(message.id)
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function receive(json) {
|
|
353
|
+
const message = JSON.parse(json)
|
|
354
|
+
switch (message.t) {
|
|
355
|
+
case 'load':
|
|
356
|
+
load(message).catch((error) => post({ t: 'loaded', error: messageOf(error) }))
|
|
357
|
+
return
|
|
358
|
+
case 'dispatch':
|
|
359
|
+
dispatch(message)
|
|
360
|
+
return
|
|
361
|
+
case 'overrun': {
|
|
362
|
+
const state = dispatches.get(message.id)
|
|
363
|
+
if (state !== undefined) {
|
|
364
|
+
state.abort()
|
|
365
|
+
state.rejectOverrun(new Error('exceeded ' + BUDGET.ms + 'ms budget'))
|
|
366
|
+
}
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
case 'grace-over': {
|
|
370
|
+
const state = dispatches.get(message.id)
|
|
371
|
+
if (state !== undefined) state.rejectGrace(new Error('its .catch did not answer within the grace'))
|
|
372
|
+
return
|
|
373
|
+
}
|
|
374
|
+
case 'abort': {
|
|
375
|
+
const state = dispatches.get(message.id)
|
|
376
|
+
if (state !== undefined) {
|
|
377
|
+
state.abort()
|
|
378
|
+
state.rejectOverrun(new Error('the turn was cancelled'))
|
|
379
|
+
state.rejectGrace(new Error('the turn was cancelled'))
|
|
380
|
+
}
|
|
381
|
+
return
|
|
382
|
+
}
|
|
383
|
+
case 'call-result': {
|
|
384
|
+
const pending = pendingCalls.get(message.cid)
|
|
385
|
+
if (pending === undefined) return
|
|
386
|
+
pendingCalls.delete(message.cid)
|
|
387
|
+
if (message.ok) pending.resolve(message.value)
|
|
388
|
+
else pending.reject(new Error(message.message))
|
|
389
|
+
return
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return receive
|
|
395
|
+
})
|
|
396
|
+
`
|
|
397
|
+
|
|
398
|
+
export const HOOK_WORKER_SOURCE =
|
|
399
|
+
`const BOOTSTRAP = ${JSON.stringify('(' + BOOTSTRAP_SOURCE.trim() + ')')}\n` +
|
|
400
|
+
String.raw`
|
|
401
|
+
'use strict'
|
|
402
|
+
const { parentPort } = require('node:worker_threads')
|
|
403
|
+
const vm = require('node:vm')
|
|
404
|
+
|
|
405
|
+
// Whatever of the worker's own realm might still be reached must not be able to make code: its
|
|
406
|
+
// function constructors throw.
|
|
407
|
+
{
|
|
408
|
+
const refuse = function () {
|
|
409
|
+
throw new Error('code cannot be made from text here')
|
|
410
|
+
}
|
|
411
|
+
const protos = [
|
|
412
|
+
Function.prototype,
|
|
413
|
+
Object.getPrototypeOf(async function () {}),
|
|
414
|
+
Object.getPrototypeOf(function* () {}),
|
|
415
|
+
Object.getPrototypeOf(async function* () {}),
|
|
416
|
+
]
|
|
417
|
+
for (const proto of protos) {
|
|
418
|
+
try {
|
|
419
|
+
Object.defineProperty(proto, 'constructor', { value: refuse, writable: false, configurable: false })
|
|
420
|
+
} catch (error) {
|
|
421
|
+
// Already fixed: nothing more to do.
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const send = (json) => {
|
|
427
|
+
try {
|
|
428
|
+
parentPort.postMessage(JSON.parse(json))
|
|
429
|
+
} catch (error) {
|
|
430
|
+
// A message that cannot be sent is dropped; the host's budget answers for it.
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const post = (message) => parentPort.postMessage(message)
|
|
434
|
+
const messageOf = (error) =>
|
|
435
|
+
error !== null && typeof error === 'object' && typeof error.message === 'string'
|
|
436
|
+
? error.message
|
|
437
|
+
: String(error)
|
|
438
|
+
|
|
439
|
+
// Source text in, a function of the context out; a failure comes back as its message, never as an object.
|
|
440
|
+
let context
|
|
441
|
+
const evaluate = (rel, source) => {
|
|
442
|
+
try {
|
|
443
|
+
const script = new vm.Script('(function (__exports, __import) {"use strict";' + source + '\n})', {
|
|
444
|
+
filename: 'plugin:' + rel,
|
|
445
|
+
})
|
|
446
|
+
return script.runInContext(context)
|
|
447
|
+
} catch (error) {
|
|
448
|
+
return String(messageOf(error))
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function freezeIntrinsics() {
|
|
453
|
+
vm.runInContext(
|
|
454
|
+
'(function () {' +
|
|
455
|
+
'const keep = new Set(["Object", "Error"]);' +
|
|
456
|
+
'for (const name of ["Object", "Array", "Function", "String", "Number", "Boolean", "Symbol", "Promise", "RegExp", "Date", "Map", "Set", "WeakMap", "WeakSet", "Error", "TypeError", "RangeError", "SyntaxError", "BigInt", "ArrayBuffer", "Uint8Array"]) {' +
|
|
457
|
+
'const ctor = globalThis[name];' +
|
|
458
|
+
'if (!ctor) continue;' +
|
|
459
|
+
'if (ctor.prototype && !keep.has(name)) Object.freeze(ctor.prototype);' +
|
|
460
|
+
'Object.freeze(ctor);' +
|
|
461
|
+
'}' +
|
|
462
|
+
'for (const name of ["JSON", "Math", "Reflect"]) { if (globalThis[name]) Object.freeze(globalThis[name]); }' +
|
|
463
|
+
'for (const key of Reflect.ownKeys(globalThis)) {' +
|
|
464
|
+
'const d = Object.getOwnPropertyDescriptor(globalThis, key);' +
|
|
465
|
+
'if (d && d.configurable && "value" in d) Object.defineProperty(globalThis, key, { value: d.value, writable: false, configurable: false, enumerable: d.enumerable });' +
|
|
466
|
+
'}' +
|
|
467
|
+
'})()',
|
|
468
|
+
context,
|
|
469
|
+
)
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
let receive
|
|
473
|
+
|
|
474
|
+
function start() {
|
|
475
|
+
// No prototype on the sandbox, so nothing of the worker's realm is found through the global.
|
|
476
|
+
context = vm.createContext(Object.create(null), {
|
|
477
|
+
name: 'plugin',
|
|
478
|
+
codeGeneration: { strings: false, wasm: false },
|
|
479
|
+
})
|
|
480
|
+
receive = vm.runInContext(BOOTSTRAP, context, { filename: 'plugin:bootstrap' })(send, evaluate)
|
|
481
|
+
freezeIntrinsics()
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
parentPort.on('message', (message) => {
|
|
485
|
+
try {
|
|
486
|
+
if (message.t === 'load') start()
|
|
487
|
+
if (receive !== undefined) receive(JSON.stringify(message))
|
|
488
|
+
} catch (error) {
|
|
489
|
+
if (message.t === 'load') post({ t: 'loaded', error: messageOf(error) })
|
|
490
|
+
else post({ t: 'log', level: 'error', text: 'the hooks worker failed on a message: ' + messageOf(error) })
|
|
491
|
+
}
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
process.on('unhandledRejection', (reason) =>
|
|
495
|
+
post({ t: 'log', level: 'error', text: 'an unhandled rejection: ' + messageOf(reason) }),
|
|
496
|
+
)
|
|
497
|
+
process.on('uncaughtException', (error) =>
|
|
498
|
+
post({ t: 'log', level: 'error', text: 'an uncaught error: ' + messageOf(error) }),
|
|
499
|
+
)
|
|
500
|
+
`
|
package/src/plugin/index.ts
CHANGED
|
@@ -2,12 +2,24 @@ export {
|
|
|
2
2
|
assertEnableable,
|
|
3
3
|
discoverPlugins,
|
|
4
4
|
loadPluginManifest,
|
|
5
|
+
loadPluginManifestWithFindings,
|
|
5
6
|
discoverAllPluginDirs,
|
|
6
7
|
parsePluginManifestText,
|
|
7
8
|
} from './loader.js'
|
|
8
9
|
export { definePlugin } from './define.js'
|
|
9
10
|
export type { DefinedPlugin, DefinePluginOptions } from './define.js'
|
|
10
|
-
export type {
|
|
11
|
+
export type {
|
|
12
|
+
LoadedPluginManifest,
|
|
13
|
+
PluginDiscoveryOptions,
|
|
14
|
+
PluginEnablementCapabilities,
|
|
15
|
+
} from './loader.js'
|
|
16
|
+
export * from './common-layout/index.js'
|
|
17
|
+
export * from './hook-module/index.js'
|
|
18
|
+
export {
|
|
19
|
+
COMMON_MARKETPLACE_FILE,
|
|
20
|
+
COMMON_MANIFEST_DIR as COMMON_MARKETPLACE_DIR,
|
|
21
|
+
isOfficialLookingMarketplaceName,
|
|
22
|
+
} from './common-layout-names.js'
|
|
11
23
|
export {
|
|
12
24
|
boundCompactionSummary,
|
|
13
25
|
boundHookContext,
|