@namzu/sdk 64.0.0 → 64.2.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 +49 -0
- package/dist/agents/file-definitions.d.ts +10 -0
- package/dist/agents/file-definitions.d.ts.map +1 -1
- package/dist/agents/file-definitions.js +10 -2
- package/dist/agents/file-definitions.js.map +1 -1
- package/dist/plugin/file-change-hooks.d.ts +53 -0
- package/dist/plugin/file-change-hooks.d.ts.map +1 -0
- package/dist/plugin/file-change-hooks.js +111 -0
- package/dist/plugin/file-change-hooks.js.map +1 -0
- package/dist/plugin/file-change-watch.d.ts +111 -0
- package/dist/plugin/file-change-watch.d.ts.map +1 -0
- package/dist/plugin/file-change-watch.js +324 -0
- package/dist/plugin/file-change-watch.js.map +1 -0
- package/dist/plugin/index.d.ts +10 -1
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +7 -1
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/lifecycle.d.ts +60 -1
- package/dist/plugin/lifecycle.d.ts.map +1 -1
- package/dist/plugin/lifecycle.js +118 -7
- package/dist/plugin/lifecycle.js.map +1 -1
- package/dist/plugin/loader.d.ts.map +1 -1
- package/dist/plugin/loader.js +24 -1
- package/dist/plugin/loader.js.map +1 -1
- package/dist/plugin/session-hook-env.d.ts +140 -0
- package/dist/plugin/session-hook-env.d.ts.map +1 -0
- package/dist/plugin/session-hook-env.js +458 -0
- package/dist/plugin/session-hook-env.js.map +1 -0
- package/dist/plugin/shell-hook-env.d.ts +107 -0
- package/dist/plugin/shell-hook-env.d.ts.map +1 -0
- package/dist/plugin/shell-hook-env.js +272 -0
- package/dist/plugin/shell-hook-env.js.map +1 -0
- package/dist/plugin/shell-hook.d.ts +105 -2
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +282 -24
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/plugin/worktree-hook-answer.d.ts +26 -0
- package/dist/plugin/worktree-hook-answer.d.ts.map +1 -0
- package/dist/plugin/worktree-hook-answer.js +132 -0
- package/dist/plugin/worktree-hook-answer.js.map +1 -0
- package/dist/public-runtime.d.ts +4 -4
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +4 -4
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +7 -1
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/executor.d.ts +8 -1
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +4 -0
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +9 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +20 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +8 -1
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +6 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/session/workspace/hooked-driver.d.ts +72 -0
- package/dist/session/workspace/hooked-driver.d.ts.map +1 -0
- package/dist/session/workspace/hooked-driver.js +204 -0
- package/dist/session/workspace/hooked-driver.js.map +1 -0
- package/dist/session/workspace/index.d.ts +2 -0
- package/dist/session/workspace/index.d.ts.map +1 -1
- package/dist/session/workspace/index.js +1 -0
- package/dist/session/workspace/index.js.map +1 -1
- package/dist/skills/index.d.ts +1 -1
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +1 -1
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/loader.d.ts +22 -0
- package/dist/skills/loader.d.ts.map +1 -1
- package/dist/skills/loader.js +41 -0
- package/dist/skills/loader.js.map +1 -1
- package/dist/tools/builtins/bash-cwd.d.ts +6 -1
- package/dist/tools/builtins/bash-cwd.d.ts.map +1 -1
- package/dist/tools/builtins/bash-cwd.js +27 -2
- package/dist/tools/builtins/bash-cwd.js.map +1 -1
- package/dist/tools/builtins/bash.js +1 -1
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +114 -17
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/types/plugin/index.d.ts +75 -2
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js +5 -0
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/session/records.d.ts +73 -1
- package/dist/types/session/records.d.ts.map +1 -1
- package/dist/types/session/records.js +25 -0
- package/dist/types/session/records.js.map +1 -1
- package/dist/types/skills/index.d.ts +8 -0
- package/dist/types/skills/index.d.ts.map +1 -1
- package/dist/types/skills/index.js.map +1 -1
- package/dist/types/tool/index.d.ts +97 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/workspace/ref.d.ts +6 -0
- package/dist/types/workspace/ref.d.ts.map +1 -1
- package/dist/utils/frontmatter.d.ts +16 -0
- package/dist/utils/frontmatter.d.ts.map +1 -1
- package/dist/utils/frontmatter.js +31 -1
- package/dist/utils/frontmatter.js.map +1 -1
- package/package.json +1 -1
- package/src/agents/file-definitions.ts +17 -2
- package/src/plugin/file-change-hooks.ts +131 -0
- package/src/plugin/file-change-watch.ts +367 -0
- package/src/plugin/index.ts +39 -1
- package/src/plugin/lifecycle.ts +162 -8
- package/src/plugin/loader.ts +22 -1
- package/src/plugin/session-hook-env.ts +475 -0
- package/src/plugin/shell-hook-env.ts +335 -0
- package/src/plugin/shell-hook.ts +416 -24
- package/src/plugin/worktree-hook-answer.ts +142 -0
- package/src/public-runtime.ts +33 -0
- package/src/public-types.ts +20 -1
- package/src/runtime/query/executor.ts +12 -0
- package/src/runtime/query/index.ts +28 -0
- package/src/runtime/query/tooling.ts +19 -1
- package/src/session/workspace/hooked-driver.ts +272 -0
- package/src/session/workspace/index.ts +7 -0
- package/src/skills/index.ts +3 -0
- package/src/skills/loader.ts +47 -0
- package/src/tools/builtins/bash-cwd.ts +25 -1
- package/src/tools/builtins/bash.ts +1 -1
- package/src/tools/builtins/skill.ts +138 -27
- package/src/types/plugin/index.ts +84 -1
- package/src/types/session/records.ts +28 -0
- package/src/types/skills/index.ts +9 -0
- package/src/types/tool/index.ts +105 -0
- package/src/types/workspace/ref.ts +6 -0
- package/src/utils/frontmatter.ts +52 -1
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Watching the files hooks asked about, and nothing else.
|
|
3
|
+
*
|
|
4
|
+
* A `file_changed` hook names the files it cares about in its matcher
|
|
5
|
+
* (`.envrc|package.json`), and may hand back more (`watchPaths`). This file
|
|
6
|
+
* turns those paths into events. No watcher exists until a path is given, and
|
|
7
|
+
* `setPaths([])` closes them all.
|
|
8
|
+
*
|
|
9
|
+
* ## How
|
|
10
|
+
*
|
|
11
|
+
* The platform's own `fs.watch`, on the folder that holds each file rather
|
|
12
|
+
* than on the file: an editor that saves by writing a new file and renaming
|
|
13
|
+
* it over the old one would leave a watch on the file pointing at a file that
|
|
14
|
+
* is gone, and a watch on a folder also sees a file appear and disappear.
|
|
15
|
+
* One watch serves every file in a folder.
|
|
16
|
+
*
|
|
17
|
+
* ## Settling
|
|
18
|
+
*
|
|
19
|
+
* What the platform reports is noisy: an editor's save is several events, and
|
|
20
|
+
* the names differ between systems. Every event for a watched file only
|
|
21
|
+
* restarts a timer; when the file has been still for `settleMs` (half a
|
|
22
|
+
* second) the file is looked at once, and the event is `add` (it did not exist
|
|
23
|
+
* and does now), `unlink` (it did and does not) or `change` (it exists, and
|
|
24
|
+
* did). A file that did not exist before and does not now is no event.
|
|
25
|
+
*
|
|
26
|
+
* ## Limits
|
|
27
|
+
*
|
|
28
|
+
* At most {@link MAX_WATCHED_PATHS} files are watched. A longer list is cut
|
|
29
|
+
* (the first ones are kept, in the order given) and the cut is reported once.
|
|
30
|
+
* A watch that cannot be started or breaks (the folder does not exist, no
|
|
31
|
+
* permission, the system's watch limit) is reported once for that folder and
|
|
32
|
+
* the others carry on; the watch is not retried until the list is set again.
|
|
33
|
+
* Watches never keep the process alive.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { type FSWatcher, statSync, watch } from 'node:fs'
|
|
37
|
+
import { basename, dirname, isAbsolute, resolve } from 'node:path'
|
|
38
|
+
|
|
39
|
+
import type { PluginFileChange } from '../types/plugin/index.js'
|
|
40
|
+
|
|
41
|
+
/** The most files watched at once, matcher paths and handed-back paths together. */
|
|
42
|
+
export const MAX_WATCHED_PATHS = 64
|
|
43
|
+
/** How long a file must be still before its change is reported, in milliseconds. */
|
|
44
|
+
export const FILE_SETTLE_MS = 500
|
|
45
|
+
|
|
46
|
+
export interface FileChange {
|
|
47
|
+
readonly path: string
|
|
48
|
+
readonly change: PluginFileChange
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The platform's folder watch, as far as this file uses it. */
|
|
52
|
+
export type WatchDirectory = (
|
|
53
|
+
directory: string,
|
|
54
|
+
listener: (name: string | null) => void,
|
|
55
|
+
onError: (error: Error) => void,
|
|
56
|
+
) => { close(): void }
|
|
57
|
+
|
|
58
|
+
export interface FileChangeWatcherOptions {
|
|
59
|
+
readonly onChange: (change: FileChange) => void
|
|
60
|
+
/** Called once for each distinct problem, in a sentence. */
|
|
61
|
+
readonly onProblem?: (message: string) => void
|
|
62
|
+
readonly settleMs?: number
|
|
63
|
+
readonly maxPaths?: number
|
|
64
|
+
/** A seam for tests; default `fs.watch`, not persistent. */
|
|
65
|
+
readonly watchDirectory?: WatchDirectory
|
|
66
|
+
/** A seam for tests; default a `stat`. */
|
|
67
|
+
readonly exists?: (path: string) => boolean
|
|
68
|
+
/**
|
|
69
|
+
* A seam for tests: a string that differs whenever the file's content or
|
|
70
|
+
* identity does (default a `stat`'s inode, size and modification time;
|
|
71
|
+
* `'absent'` when the file is not there).
|
|
72
|
+
*/
|
|
73
|
+
readonly signature?: (path: string) => string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const defaultWatchDirectory: WatchDirectory = (directory, listener, onError) => {
|
|
77
|
+
const watcher: FSWatcher = watch(directory, { persistent: false }, (_type, name) => {
|
|
78
|
+
listener(name === null || name === undefined ? null : String(name))
|
|
79
|
+
})
|
|
80
|
+
watcher.on('error', onError)
|
|
81
|
+
return watcher
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const defaultExists = (path: string): boolean => {
|
|
85
|
+
try {
|
|
86
|
+
statSync(path)
|
|
87
|
+
return true
|
|
88
|
+
} catch {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const defaultSignature = (path: string): string => {
|
|
94
|
+
try {
|
|
95
|
+
const stat = statSync(path)
|
|
96
|
+
return `${stat.ino}:${stat.size}:${stat.mtimeMs}`
|
|
97
|
+
} catch {
|
|
98
|
+
return 'absent'
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The case-insensitive file systems compare names without regard to case. */
|
|
103
|
+
const FOLDS_CASE = process.platform === 'win32' || process.platform === 'darwin'
|
|
104
|
+
const fold = (name: string): string => (FOLDS_CASE ? name.toLowerCase() : name)
|
|
105
|
+
|
|
106
|
+
interface Watched {
|
|
107
|
+
existed: boolean
|
|
108
|
+
timer: ReturnType<typeof setTimeout> | undefined
|
|
109
|
+
/** Runs of a hook for this file now under way; its own writes are not news. */
|
|
110
|
+
holding: number
|
|
111
|
+
/** What the file looked like when the last hook run for it ended. */
|
|
112
|
+
acknowledged: string | undefined
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export class FileChangeWatcher {
|
|
116
|
+
private readonly files = new Map<string, Watched>()
|
|
117
|
+
private readonly folders = new Map<string, { close(): void }>()
|
|
118
|
+
private readonly failed = new Set<string>()
|
|
119
|
+
/** Folders that did not exist when asked for, each with a watch on the nearest folder that does. */
|
|
120
|
+
private readonly waiting = new Map<string, { close(): void } | undefined>()
|
|
121
|
+
private readonly reported = new Set<string>()
|
|
122
|
+
private closed = false
|
|
123
|
+
|
|
124
|
+
constructor(private readonly options: FileChangeWatcherOptions) {}
|
|
125
|
+
|
|
126
|
+
/** The files watched now, absolute. */
|
|
127
|
+
get watching(): readonly string[] {
|
|
128
|
+
return [...this.files.keys()]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Watch exactly these files from now on. A relative path is under `base`.
|
|
133
|
+
* A file already watched keeps its state; one no longer named is let go.
|
|
134
|
+
*/
|
|
135
|
+
setPaths(paths: readonly string[], base: string): void {
|
|
136
|
+
if (this.closed) return
|
|
137
|
+
const limit = this.options.maxPaths ?? MAX_WATCHED_PATHS
|
|
138
|
+
const wanted: string[] = []
|
|
139
|
+
for (const path of paths) {
|
|
140
|
+
const absolute = isAbsolute(path) ? resolve(path) : resolve(base, path)
|
|
141
|
+
if (!wanted.includes(absolute)) wanted.push(absolute)
|
|
142
|
+
}
|
|
143
|
+
if (wanted.length > limit) {
|
|
144
|
+
this.report(
|
|
145
|
+
`Hooks asked to watch ${wanted.length} files; only the first ${limit} are watched.`,
|
|
146
|
+
)
|
|
147
|
+
wanted.length = limit
|
|
148
|
+
}
|
|
149
|
+
const exists = this.options.exists ?? defaultExists
|
|
150
|
+
for (const [path, state] of this.files) {
|
|
151
|
+
if (wanted.includes(path)) continue
|
|
152
|
+
if (state.timer !== undefined) clearTimeout(state.timer)
|
|
153
|
+
this.files.delete(path)
|
|
154
|
+
}
|
|
155
|
+
for (const path of wanted) {
|
|
156
|
+
if (!this.files.has(path)) {
|
|
157
|
+
this.files.set(path, {
|
|
158
|
+
existed: exists(path),
|
|
159
|
+
timer: undefined,
|
|
160
|
+
holding: 0,
|
|
161
|
+
acknowledged: undefined,
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const folders = new Set(wanted.map((path) => dirname(path)))
|
|
166
|
+
for (const [folder, handle] of this.folders) {
|
|
167
|
+
if (folders.has(folder)) continue
|
|
168
|
+
handle.close()
|
|
169
|
+
this.folders.delete(folder)
|
|
170
|
+
}
|
|
171
|
+
for (const [folder] of this.waiting) {
|
|
172
|
+
if (!folders.has(folder)) this.stopWaiting(folder)
|
|
173
|
+
}
|
|
174
|
+
this.failed.clear()
|
|
175
|
+
for (const folder of folders) {
|
|
176
|
+
if (!this.folders.has(folder) && !this.waiting.has(folder)) this.start(folder)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* A hook run for `path` begins: what it writes to that file is its own, not
|
|
182
|
+
* a change to report. Paired with {@link release}.
|
|
183
|
+
*/
|
|
184
|
+
hold(path: string): void {
|
|
185
|
+
const state = this.files.get(path)
|
|
186
|
+
if (state === undefined) return
|
|
187
|
+
state.holding += 1
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The hook run for `path` ended. The file as it is now is what that run
|
|
192
|
+
* left, so a settle that finds it unchanged is the run's own write and
|
|
193
|
+
* reports nothing; a change by anyone after this moment is reported.
|
|
194
|
+
*/
|
|
195
|
+
release(path: string): void {
|
|
196
|
+
const state = this.files.get(path)
|
|
197
|
+
if (state === undefined) return
|
|
198
|
+
state.holding = Math.max(0, state.holding - 1)
|
|
199
|
+
if (state.holding > 0) return
|
|
200
|
+
state.acknowledged = (this.options.signature ?? this.defaultSignatureFor())(path)
|
|
201
|
+
state.existed = (this.options.exists ?? defaultExists)(path)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private defaultSignatureFor(): (path: string) => string {
|
|
205
|
+
const exists = this.options.exists
|
|
206
|
+
return exists === undefined
|
|
207
|
+
? defaultSignature
|
|
208
|
+
: (path) => (exists(path) ? `present:${path}` : 'absent')
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
close(): void {
|
|
212
|
+
this.closed = true
|
|
213
|
+
for (const state of this.files.values()) {
|
|
214
|
+
if (state.timer !== undefined) clearTimeout(state.timer)
|
|
215
|
+
}
|
|
216
|
+
this.files.clear()
|
|
217
|
+
for (const handle of this.folders.values()) handle.close()
|
|
218
|
+
this.folders.clear()
|
|
219
|
+
for (const [folder] of [...this.waiting]) this.stopWaiting(folder)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private report(message: string): void {
|
|
223
|
+
if (this.reported.has(message)) return
|
|
224
|
+
this.reported.add(message)
|
|
225
|
+
this.options.onProblem?.(message)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private start(folder: string): void {
|
|
229
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory
|
|
230
|
+
try {
|
|
231
|
+
const handle = watchDirectory(
|
|
232
|
+
folder,
|
|
233
|
+
(name) => this.touched(folder, name),
|
|
234
|
+
(error) => {
|
|
235
|
+
this.drop(folder)
|
|
236
|
+
this.report(`Watching files in ${folder} stopped: ${error.message}.`)
|
|
237
|
+
},
|
|
238
|
+
)
|
|
239
|
+
this.folders.set(folder, handle)
|
|
240
|
+
} catch (error) {
|
|
241
|
+
this.failed.add(folder)
|
|
242
|
+
const reason = error instanceof Error ? error.message : String(error)
|
|
243
|
+
const missing = /ENOENT/.test(reason)
|
|
244
|
+
this.report(
|
|
245
|
+
`Files in ${folder} cannot be watched${missing ? ' yet' : ''} (${reason})${missing ? '; they are watched once the folder exists' : ''}.`,
|
|
246
|
+
)
|
|
247
|
+
if (missing) this.waitFor(folder)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* A folder that does not exist yet: watch the nearest one that does, and
|
|
253
|
+
* try the folder again whenever something happens there. When the folder
|
|
254
|
+
* appears its files are looked at, so a file made right after it is seen.
|
|
255
|
+
*/
|
|
256
|
+
private waitFor(folder: string): void {
|
|
257
|
+
this.stopWaiting(folder)
|
|
258
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory
|
|
259
|
+
let handle: { close(): void } | undefined
|
|
260
|
+
let ancestor = dirname(folder)
|
|
261
|
+
while (handle === undefined && ancestor !== folder) {
|
|
262
|
+
try {
|
|
263
|
+
handle = watchDirectory(
|
|
264
|
+
ancestor,
|
|
265
|
+
() => this.retry(folder),
|
|
266
|
+
() => undefined,
|
|
267
|
+
)
|
|
268
|
+
} catch {
|
|
269
|
+
const up = dirname(ancestor)
|
|
270
|
+
if (up === ancestor) break
|
|
271
|
+
ancestor = up
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
this.waiting.set(folder, handle)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private stopWaiting(folder: string): void {
|
|
278
|
+
const handle = this.waiting.get(folder)
|
|
279
|
+
this.waiting.delete(folder)
|
|
280
|
+
try {
|
|
281
|
+
handle?.close()
|
|
282
|
+
} catch {
|
|
283
|
+
// Already closed.
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private retry(folder: string): void {
|
|
288
|
+
if (this.closed || !this.waiting.has(folder)) return
|
|
289
|
+
const watchDirectory = this.options.watchDirectory ?? defaultWatchDirectory
|
|
290
|
+
let handle: { close(): void }
|
|
291
|
+
try {
|
|
292
|
+
handle = watchDirectory(
|
|
293
|
+
folder,
|
|
294
|
+
(name) => this.touched(folder, name),
|
|
295
|
+
(error) => {
|
|
296
|
+
this.drop(folder)
|
|
297
|
+
this.report(`Watching files in ${folder} stopped: ${error.message}.`)
|
|
298
|
+
},
|
|
299
|
+
)
|
|
300
|
+
} catch {
|
|
301
|
+
// Not there yet: move the watch down to the nearest folder that exists now.
|
|
302
|
+
this.waitFor(folder)
|
|
303
|
+
return
|
|
304
|
+
}
|
|
305
|
+
this.stopWaiting(folder)
|
|
306
|
+
this.folders.set(folder, handle)
|
|
307
|
+
for (const path of this.files.keys()) {
|
|
308
|
+
if (dirname(path) === folder) this.schedule(path)
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private drop(folder: string): void {
|
|
313
|
+
const handle = this.folders.get(folder)
|
|
314
|
+
this.folders.delete(folder)
|
|
315
|
+
try {
|
|
316
|
+
handle?.close()
|
|
317
|
+
} catch {
|
|
318
|
+
// Already closed by the error that brought us here.
|
|
319
|
+
}
|
|
320
|
+
this.failed.add(folder)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** An event in `folder`: for one name, or for every file in it when the system gave no name. */
|
|
324
|
+
private touched(folder: string, name: string | null): void {
|
|
325
|
+
if (this.closed) return
|
|
326
|
+
for (const path of this.files.keys()) {
|
|
327
|
+
if (dirname(path) !== folder) continue
|
|
328
|
+
if (name !== null && fold(basename(path)) !== fold(name)) continue
|
|
329
|
+
this.schedule(path)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private schedule(path: string): void {
|
|
334
|
+
const state = this.files.get(path)
|
|
335
|
+
if (state === undefined) return
|
|
336
|
+
if (state.timer !== undefined) clearTimeout(state.timer)
|
|
337
|
+
const timer = setTimeout(() => this.settle(path), this.options.settleMs ?? FILE_SETTLE_MS)
|
|
338
|
+
timer.unref?.()
|
|
339
|
+
state.timer = timer
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private settle(path: string): void {
|
|
343
|
+
const state = this.files.get(path)
|
|
344
|
+
if (state === undefined || this.closed) return
|
|
345
|
+
state.timer = undefined
|
|
346
|
+
// A hook run for this file is under way: whatever it writes is its own.
|
|
347
|
+
// `release` looks at the file when the run ends.
|
|
348
|
+
if (state.holding > 0) return
|
|
349
|
+
const exists = (this.options.exists ?? defaultExists)(path)
|
|
350
|
+
const before = state.existed
|
|
351
|
+
state.existed = exists
|
|
352
|
+
if (state.acknowledged !== undefined) {
|
|
353
|
+
const now = (this.options.signature ?? this.defaultSignatureFor())(path)
|
|
354
|
+
if (now === state.acknowledged) return
|
|
355
|
+
state.acknowledged = undefined
|
|
356
|
+
}
|
|
357
|
+
const change: PluginFileChange | undefined =
|
|
358
|
+
before && exists
|
|
359
|
+
? 'change'
|
|
360
|
+
: !before && exists
|
|
361
|
+
? 'add'
|
|
362
|
+
: before && !exists
|
|
363
|
+
? 'unlink'
|
|
364
|
+
: undefined
|
|
365
|
+
if (change !== undefined) this.options.onChange({ path, change })
|
|
366
|
+
}
|
|
367
|
+
}
|
package/src/plugin/index.ts
CHANGED
|
@@ -21,7 +21,7 @@ export {
|
|
|
21
21
|
MAX_LAST_MESSAGE_HOOK_CHARS,
|
|
22
22
|
sessionStartHookContext,
|
|
23
23
|
} from './hook-context.js'
|
|
24
|
-
export { PluginLifecycleManager } from './lifecycle.js'
|
|
24
|
+
export { isHookEntriesPath, PluginLifecycleManager } from './lifecycle.js'
|
|
25
25
|
export { PluginResolver } from './resolver.js'
|
|
26
26
|
export {
|
|
27
27
|
attachShellHooks,
|
|
@@ -38,6 +38,17 @@ export {
|
|
|
38
38
|
shellHookMatches,
|
|
39
39
|
shellHookVerdict,
|
|
40
40
|
} from './shell-hook.js'
|
|
41
|
+
export {
|
|
42
|
+
assignedWithin,
|
|
43
|
+
isPluginFolderVariable,
|
|
44
|
+
isShellOwnVariable,
|
|
45
|
+
MOVED_FOLDER_ENDINGS,
|
|
46
|
+
referencedVariables,
|
|
47
|
+
SHELL_HOOK_RESERVED_ENV,
|
|
48
|
+
shellHookEnvNameProblem,
|
|
49
|
+
shellHookEnvProblems,
|
|
50
|
+
substitutePluginFolders,
|
|
51
|
+
} from './shell-hook-env.js'
|
|
41
52
|
export {
|
|
42
53
|
SHELL_HOOK_WRITTEN_TOOL_NAMES,
|
|
43
54
|
type ShellHookToolNames,
|
|
@@ -58,3 +69,30 @@ export type {
|
|
|
58
69
|
ShellHookOutcome,
|
|
59
70
|
ShellHooksConfig,
|
|
60
71
|
} from './shell-hook.js'
|
|
72
|
+
export {
|
|
73
|
+
fileChangedMatcherNote,
|
|
74
|
+
fileChangedMatcherPaths,
|
|
75
|
+
fileChangedMatches,
|
|
76
|
+
} from './shell-hook.js'
|
|
77
|
+
export {
|
|
78
|
+
HOOK_ENV_EVENTS,
|
|
79
|
+
MAX_HOOK_ENV_FILE_BYTES,
|
|
80
|
+
MAX_HOOK_ENV_VALUE_CHARS,
|
|
81
|
+
MAX_HOOK_ENV_VARIABLES,
|
|
82
|
+
refusedHookEnvName,
|
|
83
|
+
SessionHookEnv,
|
|
84
|
+
} from './session-hook-env.js'
|
|
85
|
+
export type { HookEnvEvent, SessionHookEnvOptions } from './session-hook-env.js'
|
|
86
|
+
export { FILE_SETTLE_MS, FileChangeWatcher, MAX_WATCHED_PATHS } from './file-change-watch.js'
|
|
87
|
+
export type {
|
|
88
|
+
FileChange,
|
|
89
|
+
FileChangeWatcherOptions,
|
|
90
|
+
WatchDirectory,
|
|
91
|
+
} from './file-change-watch.js'
|
|
92
|
+
export { FileChangeHooks } from './file-change-hooks.js'
|
|
93
|
+
export type { FileChangeHooksOptions } from './file-change-hooks.js'
|
|
94
|
+
export {
|
|
95
|
+
WORKTREE_HOOK_SHOWN_CHARS,
|
|
96
|
+
worktreeHookPrinted,
|
|
97
|
+
worktreeHookVerdict,
|
|
98
|
+
} from './worktree-hook-answer.js'
|
package/src/plugin/lifecycle.ts
CHANGED
|
@@ -31,6 +31,7 @@ import type {
|
|
|
31
31
|
PluginHookResult,
|
|
32
32
|
PluginLifecycleEvent,
|
|
33
33
|
PluginMCPServerConfig,
|
|
34
|
+
PluginManifest,
|
|
34
35
|
PluginScope,
|
|
35
36
|
} from '../types/plugin/index.js'
|
|
36
37
|
import { assertPluginHookEvent } from '../types/plugin/index.js'
|
|
@@ -156,6 +157,11 @@ function assertPluginTool(
|
|
|
156
157
|
}
|
|
157
158
|
}
|
|
158
159
|
|
|
160
|
+
/** A manifest `hooks` path that names a file of hook entries (not a module). */
|
|
161
|
+
export function isHookEntriesPath(path: string): boolean {
|
|
162
|
+
return path.toLowerCase().endsWith('.json')
|
|
163
|
+
}
|
|
164
|
+
|
|
159
165
|
function immutableManifest(manifest: PluginDefinition['manifest']): PluginDefinition['manifest'] {
|
|
160
166
|
const mcpServers = manifest.mcpServers?.map((server) =>
|
|
161
167
|
Object.freeze({
|
|
@@ -177,6 +183,9 @@ function immutableManifest(manifest: PluginDefinition['manifest']): PluginDefini
|
|
|
177
183
|
...(manifest.tools ? { tools: Object.freeze([...manifest.tools]) } : {}),
|
|
178
184
|
...(manifest.skills ? { skills: Object.freeze([...manifest.skills]) } : {}),
|
|
179
185
|
...(manifest.hooks ? { hooks: Object.freeze([...manifest.hooks]) } : {}),
|
|
186
|
+
...(manifest.hookEntries
|
|
187
|
+
? { hookEntries: Object.freeze(structuredClone(manifest.hookEntries)) }
|
|
188
|
+
: {}),
|
|
180
189
|
...(mcpServers ? { mcpServers: Object.freeze(mcpServers) } : {}),
|
|
181
190
|
...(manifest.connectors ? { connectors: Object.freeze([...manifest.connectors]) } : {}),
|
|
182
191
|
...(manifest.personas ? { personas: Object.freeze([...manifest.personas]) } : {}),
|
|
@@ -209,6 +218,20 @@ export interface PluginLifecycleManagerConfig {
|
|
|
209
218
|
skillRegistry?: SkillRegistry
|
|
210
219
|
log: Logger
|
|
211
220
|
hookTimeoutMs?: number
|
|
221
|
+
/**
|
|
222
|
+
* Hooks a host reads for a plugin when it is enabled, beyond the JavaScript
|
|
223
|
+
* modules its manifest lists (shell, http and prompt entries a plugin ships,
|
|
224
|
+
* for one). They are attached under the plugin's id the moment it is
|
|
225
|
+
* enabled and removed with its other hooks when it is disabled, uninstalled
|
|
226
|
+
* or its enable is rolled back, so they live exactly as long as the plugin
|
|
227
|
+
* is on. Not called for a plugin defined in code. A throw fails the enable.
|
|
228
|
+
*/
|
|
229
|
+
hooksFor?: (plugin: {
|
|
230
|
+
readonly id: PluginId
|
|
231
|
+
readonly manifest: PluginManifest
|
|
232
|
+
readonly rootDir: string
|
|
233
|
+
readonly scope: PluginScope
|
|
234
|
+
}) => readonly PluginHookDefinition[] | Promise<readonly PluginHookDefinition[]>
|
|
212
235
|
/**
|
|
213
236
|
* Where each MCP server's reconnect policy is registered, so an operator
|
|
214
237
|
* can retune it while a turn is live.
|
|
@@ -247,6 +270,19 @@ export interface PluginLifecycleManagerConfig {
|
|
|
247
270
|
onMCPToolDrift?: MCPToolDiscoveryOptions['onDrift']
|
|
248
271
|
}
|
|
249
272
|
|
|
273
|
+
/**
|
|
274
|
+
* What {@link PluginLifecycleManager.attachHooks} gives back: a way to take
|
|
275
|
+
* off exactly the hooks that call attached.
|
|
276
|
+
*/
|
|
277
|
+
export interface HookAttachment {
|
|
278
|
+
/** How many hooks the call attached. */
|
|
279
|
+
readonly size: number
|
|
280
|
+
/** Take every hook of this attachment off; returns how many were still attached. Safe to call again. */
|
|
281
|
+
readonly remove: () => number
|
|
282
|
+
/** Take off the hook at this position of the call's list; false when it is gone already or there is none. */
|
|
283
|
+
readonly removeAt: (index: number) => boolean
|
|
284
|
+
}
|
|
285
|
+
|
|
250
286
|
export class PluginLifecycleManager {
|
|
251
287
|
private pluginRegistry: PluginRegistry
|
|
252
288
|
/** Each installed plugin owns its own file source and one source per MCP server. */
|
|
@@ -265,14 +301,21 @@ export class PluginLifecycleManager {
|
|
|
265
301
|
priority: number
|
|
266
302
|
seq: number
|
|
267
303
|
statusMessage?: string
|
|
304
|
+
/** Read by the host from entries the plugin ships, not a module the plugin runs. */
|
|
305
|
+
shell?: true
|
|
306
|
+
/** On `file_changed`: the paths the hook's matcher names. */
|
|
307
|
+
watches?: readonly string[]
|
|
268
308
|
}>
|
|
269
309
|
> = new Map()
|
|
310
|
+
private hookSeq = 0
|
|
311
|
+
private readonly hooksChangedListeners = new Set<() => void>()
|
|
270
312
|
private pluginContributions: Map<PluginId, PluginContributionRecord> = new Map()
|
|
271
313
|
/** Reserve each id before async admission so lifecycle calls cannot overlap. */
|
|
272
314
|
private readonly lifecycleTails = new Map<PluginId, Promise<void>>()
|
|
273
315
|
/** Immutable executable admissions owned by this manager, never by the mutable registry. */
|
|
274
316
|
private pluginAdmissions: Map<PluginId, PluginAdmission> = new Map()
|
|
275
317
|
private hookTimeoutMs: number
|
|
318
|
+
private readonly hooksFor: PluginLifecycleManagerConfig['hooksFor']
|
|
276
319
|
private readonly configRegistry: ConfigRegistry | undefined
|
|
277
320
|
private readonly scopeRoots: Readonly<Record<PluginScope, string>>
|
|
278
321
|
private log: Logger
|
|
@@ -299,6 +342,7 @@ export class PluginLifecycleManager {
|
|
|
299
342
|
this.skillRegistry = config.skillRegistry
|
|
300
343
|
this.scopeRoots = Object.freeze({ ...config.scopeRoots })
|
|
301
344
|
this.hookTimeoutMs = config.hookTimeoutMs ?? HOOK_TIMEOUT_MS
|
|
345
|
+
this.hooksFor = config.hooksFor
|
|
302
346
|
this.configRegistry = config.configRegistry
|
|
303
347
|
this.mcpToolPolicies = config.mcpToolPolicies
|
|
304
348
|
this.onMCPToolDrift = config.onMCPToolDrift
|
|
@@ -411,24 +455,98 @@ export class PluginLifecycleManager {
|
|
|
411
455
|
* registration order.
|
|
412
456
|
*/
|
|
413
457
|
registerHook(pluginId: PluginId, hook: PluginHookDefinition): void {
|
|
458
|
+
this.addHook(pluginId, hook, false)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Attach hooks that can be taken off again, and nothing else.
|
|
463
|
+
*
|
|
464
|
+
* Hooks left the manager only by plugin id: everything one id attached went
|
|
465
|
+
* together, and a hook attached for a while (a skill's, for as long as it is
|
|
466
|
+
* used) had no way to leave on its own. This returns a handle that removes
|
|
467
|
+
* exactly the hooks this call attached, whoever else registered under the
|
|
468
|
+
* same id, and a handle for one hook of them. A hook that is removed while
|
|
469
|
+
* an event is being answered still answers that event: the event started
|
|
470
|
+
* with the hooks it found.
|
|
471
|
+
*
|
|
472
|
+
* All or nothing: when one definition is refused (an event nothing emits),
|
|
473
|
+
* none of them is attached.
|
|
474
|
+
*/
|
|
475
|
+
attachHooks(pluginId: PluginId, hooks: readonly PluginHookDefinition[]): HookAttachment {
|
|
476
|
+
for (const hook of hooks) assertPluginHookEvent(hook.event)
|
|
477
|
+
const records = hooks.map((hook) => this.addHook(pluginId, hook, false))
|
|
478
|
+
const removeRecords = (gone: ReadonlySet<object>): number => {
|
|
479
|
+
let removed = 0
|
|
480
|
+
for (const [event, handlers] of this.hookHandlers) {
|
|
481
|
+
const kept = handlers.filter((handler) => !gone.has(handler))
|
|
482
|
+
removed += handlers.length - kept.length
|
|
483
|
+
if (kept.length === handlers.length) continue
|
|
484
|
+
if (kept.length === 0) this.hookHandlers.delete(event)
|
|
485
|
+
else this.hookHandlers.set(event, kept)
|
|
486
|
+
}
|
|
487
|
+
if (removed > 0) this.notifyHooksChanged()
|
|
488
|
+
return removed
|
|
489
|
+
}
|
|
490
|
+
return {
|
|
491
|
+
size: records.length,
|
|
492
|
+
remove: () => removeRecords(new Set(records)),
|
|
493
|
+
removeAt: (index) => {
|
|
494
|
+
const record = records[index]
|
|
495
|
+
return record === undefined ? false : removeRecords(new Set([record])) > 0
|
|
496
|
+
},
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
private addHook(pluginId: PluginId, hook: PluginHookDefinition, shell: boolean): object {
|
|
414
501
|
// Refused here rather than left to never fire: a hook module still
|
|
415
502
|
// naming `run_start` would otherwise attach to an event nothing emits,
|
|
416
503
|
// and the operator would learn of the rename only by its silence.
|
|
417
504
|
assertPluginHookEvent(hook.event)
|
|
418
505
|
const handlers = this.hookHandlers.get(hook.event) ?? []
|
|
419
|
-
|
|
506
|
+
const record = {
|
|
420
507
|
pluginId,
|
|
421
508
|
handler: hook.handler,
|
|
422
509
|
priority: hook.priority ?? DEFAULT_HOOK_PRIORITY,
|
|
423
510
|
...(hook.statusMessage !== undefined ? { statusMessage: hook.statusMessage } : {}),
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
511
|
+
...(shell ? { shell: true as const } : {}),
|
|
512
|
+
...(hook.watches !== undefined ? { watches: hook.watches } : {}),
|
|
513
|
+
// Registration order, so the sort has something stable to fall back
|
|
514
|
+
// on when priorities are equal. A counter, not the list's length: a
|
|
515
|
+
// hook taken off again would otherwise hand its number to the next.
|
|
516
|
+
seq: this.hookSeq++,
|
|
517
|
+
}
|
|
518
|
+
handlers.push(record)
|
|
428
519
|
// Sorted on insert rather than on dispatch: a chain runs on every
|
|
429
520
|
// tool call, and the order only changes when a plugin comes or goes.
|
|
430
521
|
handlers.sort((a, b) => a.priority - b.priority || a.seq - b.seq)
|
|
431
522
|
this.hookHandlers.set(hook.event, handlers)
|
|
523
|
+
this.notifyHooksChanged()
|
|
524
|
+
return record
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Called after a hook was attached or taken off, however it happened (a
|
|
529
|
+
* plugin enabled or disabled, a skill's hooks attached, a handle removed).
|
|
530
|
+
* For a host that keeps something in step with the hooks, such as the files
|
|
531
|
+
* `file_changed` hooks watch. Returns what ends the subscription.
|
|
532
|
+
*/
|
|
533
|
+
onHooksChanged(listener: () => void): () => void {
|
|
534
|
+
this.hooksChangedListeners.add(listener)
|
|
535
|
+
return () => {
|
|
536
|
+
this.hooksChangedListeners.delete(listener)
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
private notifyHooksChanged(): void {
|
|
541
|
+
for (const listener of [...this.hooksChangedListeners]) {
|
|
542
|
+
try {
|
|
543
|
+
listener()
|
|
544
|
+
} catch (error) {
|
|
545
|
+
this.log.warn('A hooks-changed listener threw', {
|
|
546
|
+
'exception.message': toErrorMessage(error),
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
}
|
|
432
550
|
}
|
|
433
551
|
|
|
434
552
|
/**
|
|
@@ -436,10 +554,29 @@ export class PluginLifecycleManager {
|
|
|
436
554
|
* and its event, in the order answers are read. For a host that shows the
|
|
437
555
|
* operator what is attached; it carries no handler.
|
|
438
556
|
*/
|
|
439
|
-
listHooks(): readonly {
|
|
440
|
-
|
|
557
|
+
listHooks(): readonly {
|
|
558
|
+
readonly pluginId: PluginId
|
|
559
|
+
readonly event: PluginHookEvent
|
|
560
|
+
/** Set when the hook was read from entries the plugin ships and not from a module it runs. */
|
|
561
|
+
readonly shell?: true
|
|
562
|
+
/** On `file_changed`: the paths the hook's matcher names. */
|
|
563
|
+
readonly watches?: readonly string[]
|
|
564
|
+
}[] {
|
|
565
|
+
const out: {
|
|
566
|
+
pluginId: PluginId
|
|
567
|
+
event: PluginHookEvent
|
|
568
|
+
shell?: true
|
|
569
|
+
watches?: readonly string[]
|
|
570
|
+
}[] = []
|
|
441
571
|
for (const [event, handlers] of this.hookHandlers) {
|
|
442
|
-
for (const handler of handlers)
|
|
572
|
+
for (const handler of handlers) {
|
|
573
|
+
out.push({
|
|
574
|
+
pluginId: handler.pluginId,
|
|
575
|
+
event,
|
|
576
|
+
...(handler.shell === true ? { shell: true as const } : {}),
|
|
577
|
+
...(handler.watches !== undefined ? { watches: handler.watches } : {}),
|
|
578
|
+
})
|
|
579
|
+
}
|
|
443
580
|
}
|
|
444
581
|
return out
|
|
445
582
|
}
|
|
@@ -739,6 +876,9 @@ export class PluginLifecycleManager {
|
|
|
739
876
|
for (const hook of admission.inCode.hooks) this.registerHook(pluginId, hook)
|
|
740
877
|
} else if (manifest.hooks && manifest.hooks.length > 0) {
|
|
741
878
|
for (const hookPath of manifest.hooks) {
|
|
879
|
+
// A `.json` path is a file of hook entries, read by the host through
|
|
880
|
+
// `hooksFor`; it is not a module.
|
|
881
|
+
if (isHookEntriesPath(hookPath)) continue
|
|
742
882
|
const absolutePath = await resolveWithinReal(admission.rootDir, hookPath)
|
|
743
883
|
const fileUrl = pathToFileURL(absolutePath).href
|
|
744
884
|
const mod = (await import(fileUrl)) as {
|
|
@@ -757,6 +897,18 @@ export class PluginLifecycleManager {
|
|
|
757
897
|
}
|
|
758
898
|
}
|
|
759
899
|
|
|
900
|
+
// Entries the host reads from the plugin folder: attached now, removed
|
|
901
|
+
// with the rest of this plugin's hooks.
|
|
902
|
+
if (!admission.inCode && this.hooksFor) {
|
|
903
|
+
const shipped = await this.hooksFor({
|
|
904
|
+
id: pluginId,
|
|
905
|
+
manifest,
|
|
906
|
+
rootDir: admission.rootDir,
|
|
907
|
+
scope: admission.scope,
|
|
908
|
+
})
|
|
909
|
+
for (const hook of shipped) this.addHook(pluginId, hook, true)
|
|
910
|
+
}
|
|
911
|
+
|
|
760
912
|
// Start MCP servers and adapt their tools
|
|
761
913
|
if (manifest.mcpServers && manifest.mcpServers.length > 0) {
|
|
762
914
|
for (const serverConfig of manifest.mcpServers) {
|
|
@@ -945,6 +1097,7 @@ export class PluginLifecycleManager {
|
|
|
945
1097
|
this.hookHandlers.set(event, filtered)
|
|
946
1098
|
}
|
|
947
1099
|
}
|
|
1100
|
+
this.notifyHooksChanged()
|
|
948
1101
|
}
|
|
949
1102
|
|
|
950
1103
|
private async stopMcpContributions(
|
|
@@ -1028,6 +1181,7 @@ export class PluginLifecycleManager {
|
|
|
1028
1181
|
this.hookHandlers.set(event, filtered)
|
|
1029
1182
|
}
|
|
1030
1183
|
}
|
|
1184
|
+
this.notifyHooksChanged()
|
|
1031
1185
|
|
|
1032
1186
|
this.pluginContributions.delete(pluginId)
|
|
1033
1187
|
this.instructions.unregister(
|