@namzu/sdk 64.0.0 → 64.1.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.
Files changed (127) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/dist/agents/file-definitions.d.ts +10 -0
  3. package/dist/agents/file-definitions.d.ts.map +1 -1
  4. package/dist/agents/file-definitions.js +10 -2
  5. package/dist/agents/file-definitions.js.map +1 -1
  6. package/dist/plugin/file-change-hooks.d.ts +53 -0
  7. package/dist/plugin/file-change-hooks.d.ts.map +1 -0
  8. package/dist/plugin/file-change-hooks.js +111 -0
  9. package/dist/plugin/file-change-hooks.js.map +1 -0
  10. package/dist/plugin/file-change-watch.d.ts +111 -0
  11. package/dist/plugin/file-change-watch.d.ts.map +1 -0
  12. package/dist/plugin/file-change-watch.js +324 -0
  13. package/dist/plugin/file-change-watch.js.map +1 -0
  14. package/dist/plugin/index.d.ts +10 -1
  15. package/dist/plugin/index.d.ts.map +1 -1
  16. package/dist/plugin/index.js +7 -1
  17. package/dist/plugin/index.js.map +1 -1
  18. package/dist/plugin/lifecycle.d.ts +60 -1
  19. package/dist/plugin/lifecycle.d.ts.map +1 -1
  20. package/dist/plugin/lifecycle.js +118 -7
  21. package/dist/plugin/lifecycle.js.map +1 -1
  22. package/dist/plugin/loader.d.ts.map +1 -1
  23. package/dist/plugin/loader.js +24 -1
  24. package/dist/plugin/loader.js.map +1 -1
  25. package/dist/plugin/session-hook-env.d.ts +140 -0
  26. package/dist/plugin/session-hook-env.d.ts.map +1 -0
  27. package/dist/plugin/session-hook-env.js +458 -0
  28. package/dist/plugin/session-hook-env.js.map +1 -0
  29. package/dist/plugin/shell-hook-env.d.ts +107 -0
  30. package/dist/plugin/shell-hook-env.d.ts.map +1 -0
  31. package/dist/plugin/shell-hook-env.js +272 -0
  32. package/dist/plugin/shell-hook-env.js.map +1 -0
  33. package/dist/plugin/shell-hook.d.ts +105 -2
  34. package/dist/plugin/shell-hook.d.ts.map +1 -1
  35. package/dist/plugin/shell-hook.js +282 -24
  36. package/dist/plugin/shell-hook.js.map +1 -1
  37. package/dist/plugin/worktree-hook-answer.d.ts +26 -0
  38. package/dist/plugin/worktree-hook-answer.d.ts.map +1 -0
  39. package/dist/plugin/worktree-hook-answer.js +132 -0
  40. package/dist/plugin/worktree-hook-answer.js.map +1 -0
  41. package/dist/public-runtime.d.ts +3 -3
  42. package/dist/public-runtime.d.ts.map +1 -1
  43. package/dist/public-runtime.js +3 -3
  44. package/dist/public-runtime.js.map +1 -1
  45. package/dist/public-types.d.ts +7 -1
  46. package/dist/public-types.d.ts.map +1 -1
  47. package/dist/runtime/query/executor.d.ts +5 -0
  48. package/dist/runtime/query/executor.d.ts.map +1 -1
  49. package/dist/runtime/query/executor.js +3 -0
  50. package/dist/runtime/query/executor.js.map +1 -1
  51. package/dist/runtime/query/index.d.ts +9 -0
  52. package/dist/runtime/query/index.d.ts.map +1 -1
  53. package/dist/runtime/query/index.js +3 -0
  54. package/dist/runtime/query/index.js.map +1 -1
  55. package/dist/runtime/query/tooling.d.ts +5 -0
  56. package/dist/runtime/query/tooling.d.ts.map +1 -1
  57. package/dist/runtime/query/tooling.js +3 -0
  58. package/dist/runtime/query/tooling.js.map +1 -1
  59. package/dist/session/workspace/hooked-driver.d.ts +72 -0
  60. package/dist/session/workspace/hooked-driver.d.ts.map +1 -0
  61. package/dist/session/workspace/hooked-driver.js +204 -0
  62. package/dist/session/workspace/hooked-driver.js.map +1 -0
  63. package/dist/session/workspace/index.d.ts +2 -0
  64. package/dist/session/workspace/index.d.ts.map +1 -1
  65. package/dist/session/workspace/index.js +1 -0
  66. package/dist/session/workspace/index.js.map +1 -1
  67. package/dist/skills/index.d.ts +1 -1
  68. package/dist/skills/index.d.ts.map +1 -1
  69. package/dist/skills/index.js +1 -1
  70. package/dist/skills/index.js.map +1 -1
  71. package/dist/skills/loader.d.ts +22 -0
  72. package/dist/skills/loader.d.ts.map +1 -1
  73. package/dist/skills/loader.js +41 -0
  74. package/dist/skills/loader.js.map +1 -1
  75. package/dist/tools/builtins/bash-cwd.d.ts +6 -1
  76. package/dist/tools/builtins/bash-cwd.d.ts.map +1 -1
  77. package/dist/tools/builtins/bash-cwd.js +27 -2
  78. package/dist/tools/builtins/bash-cwd.js.map +1 -1
  79. package/dist/tools/builtins/bash.js +1 -1
  80. package/dist/tools/builtins/bash.js.map +1 -1
  81. package/dist/tools/builtins/skill.d.ts.map +1 -1
  82. package/dist/tools/builtins/skill.js +93 -16
  83. package/dist/tools/builtins/skill.js.map +1 -1
  84. package/dist/types/plugin/index.d.ts +75 -2
  85. package/dist/types/plugin/index.d.ts.map +1 -1
  86. package/dist/types/plugin/index.js +5 -0
  87. package/dist/types/plugin/index.js.map +1 -1
  88. package/dist/types/skills/index.d.ts +8 -0
  89. package/dist/types/skills/index.d.ts.map +1 -1
  90. package/dist/types/skills/index.js.map +1 -1
  91. package/dist/types/tool/index.d.ts +68 -0
  92. package/dist/types/tool/index.d.ts.map +1 -1
  93. package/dist/types/tool/index.js.map +1 -1
  94. package/dist/types/workspace/ref.d.ts +6 -0
  95. package/dist/types/workspace/ref.d.ts.map +1 -1
  96. package/dist/utils/frontmatter.d.ts +16 -0
  97. package/dist/utils/frontmatter.d.ts.map +1 -1
  98. package/dist/utils/frontmatter.js +31 -1
  99. package/dist/utils/frontmatter.js.map +1 -1
  100. package/package.json +1 -1
  101. package/src/agents/file-definitions.ts +17 -2
  102. package/src/plugin/file-change-hooks.ts +131 -0
  103. package/src/plugin/file-change-watch.ts +367 -0
  104. package/src/plugin/index.ts +39 -1
  105. package/src/plugin/lifecycle.ts +162 -8
  106. package/src/plugin/loader.ts +22 -1
  107. package/src/plugin/session-hook-env.ts +475 -0
  108. package/src/plugin/shell-hook-env.ts +335 -0
  109. package/src/plugin/shell-hook.ts +416 -24
  110. package/src/plugin/worktree-hook-answer.ts +142 -0
  111. package/src/public-runtime.ts +32 -0
  112. package/src/public-types.ts +20 -1
  113. package/src/runtime/query/executor.ts +8 -0
  114. package/src/runtime/query/index.ts +12 -0
  115. package/src/runtime/query/tooling.ts +8 -0
  116. package/src/session/workspace/hooked-driver.ts +272 -0
  117. package/src/session/workspace/index.ts +7 -0
  118. package/src/skills/index.ts +3 -0
  119. package/src/skills/loader.ts +47 -0
  120. package/src/tools/builtins/bash-cwd.ts +25 -1
  121. package/src/tools/builtins/bash.ts +1 -1
  122. package/src/tools/builtins/skill.ts +115 -26
  123. package/src/types/plugin/index.ts +84 -1
  124. package/src/types/skills/index.ts +9 -0
  125. package/src/types/tool/index.ts +74 -0
  126. package/src/types/workspace/ref.ts +6 -0
  127. package/src/utils/frontmatter.ts +52 -1
@@ -0,0 +1,458 @@
1
+ /**
2
+ * Variables a hook sets for the commands that run after it.
3
+ *
4
+ * A person's shell picks up a project's environment when they enter its
5
+ * folder. A hook on `session_start`, `cwd_changed` or `file_changed` can do
6
+ * the same for the session's shell commands: it is started with
7
+ * `NAMZU_ENV_FILE`, the path of a file kept for the session outside the
8
+ * project, and appends lines of the form `export NAME=value` to it. The
9
+ * commands the shell tool runs afterwards start with those variables.
10
+ *
11
+ * ## What is read
12
+ *
13
+ * The file is read as data and never run. A line is, after leading white
14
+ * space, `export NAME=value` or `NAME=value`; blank lines and lines starting
15
+ * with `#` are skipped. A value is a run of text with no white space or shell
16
+ * punctuation, a single-quoted string (taken literally), or a double-quoted
17
+ * string (`\"`, `\\`, `\$` and a backquote escaped are read as the character
18
+ * itself); the three may be joined (`"a b"'c'd`). `$NAME` and `${NAME}` are
19
+ * replaced by the value the name has in the environment the session was
20
+ * started with or was set earlier (in this file or by an earlier kind of hook
21
+ * below), and by nothing when it has none. Nothing else is expanded: a line
22
+ * with `$(...)`, a backquote, `$((...))`, `${NAME:-x}` or any other
23
+ * parameter form is not read at all, and is reported instead, so a command is
24
+ * never run by us on a hook's behalf. A trailing ` # comment` is ignored.
25
+ *
26
+ * ## Which line wins
27
+ *
28
+ * A later line for the same name replaces an earlier one, and when two hooks
29
+ * of one kind set the same name the one that comes later in the configuration
30
+ * wins, however long each took (each hook has a file of its own, read in
31
+ * configuration order). There is no unset:
32
+ * to remove a variable, set it empty. The three kinds of hook have a file
33
+ * each, and a later kind outranks an earlier one: `session_start`, then
34
+ * `cwd_changed`, then `file_changed`. A change of the working folder clears
35
+ * what `cwd_changed` and `file_changed` hooks set (they describe the folder
36
+ * that was left) before the `cwd_changed` hooks run again, and keeps what
37
+ * `session_start` hooks set.
38
+ *
39
+ * ## Bounds
40
+ *
41
+ * Only the first 64 KiB of a file is read. A value longer than 8 KiB, a name
42
+ * longer than 128 characters and a variable beyond the 128th are left out
43
+ * and reported.
44
+ *
45
+ * ## Which names are refused
46
+ *
47
+ * `PATH` is allowed: it is the point of the feature. Refused, and reported
48
+ * once, are the names that decide what program a command runs without being
49
+ * a program named in it, and the ones that say where Namzu keeps its state:
50
+ * every `NAMZU_*` name, the dynamic loader's (`LD_*`, `DYLD_*`), the shell's
51
+ * start-up and prompt hooks (`BASH_ENV`, `ENV`, `SHELLOPTS`, `BASHOPTS`,
52
+ * `PROMPT_COMMAND`, `PS4`, `IFS`, `CDPATH`, `BASH_FUNC_*`, `SHELL`), and the
53
+ * interpreters' own injection variables (`NODE_OPTIONS`, `PYTHONSTARTUP`,
54
+ * `PYTHONPATH`, `PYTHONHOME`, `PERL5OPT`, `PERL5LIB`, `RUBYOPT`, `RUBYLIB`,
55
+ * `JAVA_TOOL_OPTIONS`), the programs Git runs on its own (`GIT_EXEC_PATH`,
56
+ * `GIT_SSH`, `GIT_SSH_COMMAND`, `GIT_ASKPASS`, `GIT_PROXY_COMMAND`,
57
+ * `GIT_EXTERNAL_DIFF`), the name `__proto__`, plus the places the
58
+ * home of the account and the user-level application folders live
59
+ * (`HOME`, `USERPROFILE`, `XDG_CONFIG_HOME`, `XDG_DATA_HOME`, `XDG_STATE_HOME`,
60
+ * `XDG_CACHE_HOME`).
61
+ */
62
+ import { closeSync, mkdirSync, openSync, readSync, rmSync, writeFileSync } from 'node:fs';
63
+ import { join } from 'node:path';
64
+ import { createTempDirSync } from '../utils/temp.js';
65
+ /** The events whose hooks may set variables, in the order a later one outranks an earlier one. */
66
+ export const HOOK_ENV_EVENTS = ['session_start', 'cwd_changed', 'file_changed'];
67
+ /** The most bytes of a hook's file that are read. */
68
+ export const MAX_HOOK_ENV_FILE_BYTES = 64 * 1024;
69
+ /** The longest value kept, in characters. */
70
+ export const MAX_HOOK_ENV_VALUE_CHARS = 8 * 1024;
71
+ /** The most variables hooks may have set at once. */
72
+ export const MAX_HOOK_ENV_VARIABLES = 128;
73
+ const MAX_NAME_CHARS = 128;
74
+ const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
75
+ const REFUSED_NAMES = new Set([
76
+ 'BASH_ENV',
77
+ 'ENV',
78
+ 'SHELLOPTS',
79
+ 'BASHOPTS',
80
+ 'PROMPT_COMMAND',
81
+ 'PS4',
82
+ 'IFS',
83
+ 'CDPATH',
84
+ 'NODE_OPTIONS',
85
+ 'PYTHONSTARTUP',
86
+ 'PERL5OPT',
87
+ 'PERL5LIB',
88
+ 'RUBYOPT',
89
+ 'JAVA_TOOL_OPTIONS',
90
+ 'PYTHONPATH',
91
+ 'PYTHONHOME',
92
+ 'RUBYLIB',
93
+ 'SHELL',
94
+ 'GIT_EXEC_PATH',
95
+ 'GIT_SSH',
96
+ 'GIT_SSH_COMMAND',
97
+ 'GIT_ASKPASS',
98
+ 'GIT_PROXY_COMMAND',
99
+ 'GIT_EXTERNAL_DIFF',
100
+ 'GCONV_PATH',
101
+ 'LOCPATH',
102
+ 'NLSPATH',
103
+ 'HOSTALIASES',
104
+ 'HOME',
105
+ 'USERPROFILE',
106
+ 'XDG_CONFIG_HOME',
107
+ 'XDG_DATA_HOME',
108
+ 'XDG_STATE_HOME',
109
+ 'XDG_CACHE_HOME',
110
+ ]);
111
+ /** Why a hook may not set `name`, in a sentence, or `undefined` when it may. */
112
+ export function refusedHookEnvName(name) {
113
+ if (name === '__proto__') {
114
+ return `${name} is not a name an environment can hold`;
115
+ }
116
+ if (name.startsWith('NAMZU_')) {
117
+ return `${name} is one of Namzu's own settings, and a hook may not change those`;
118
+ }
119
+ if (name.startsWith('LD_') || name.startsWith('DYLD_') || name === 'GCONV_PATH') {
120
+ return `${name} is a dynamic loader variable: it changes what code every command loads`;
121
+ }
122
+ if (name.startsWith('BASH_FUNC_')) {
123
+ return `${name} defines a shell function that every command would run`;
124
+ }
125
+ if (REFUSED_NAMES.has(name)) {
126
+ return `${name} decides what program runs, or where Namzu and the account keep their state`;
127
+ }
128
+ return undefined;
129
+ }
130
+ const envFileName = (event, slot) => slot === 0 ? `${event}.env` : `${event}.${slot}.env`;
131
+ /**
132
+ * One value: quoted and unquoted pieces joined, then only white space or a
133
+ * ` #` comment. Returns the text, or why it cannot be read.
134
+ */
135
+ function readValue(text, lookup) {
136
+ let out = '';
137
+ let i = 0;
138
+ const expand = (source, at) => {
139
+ // `source[at]` is `$`.
140
+ const after = source[at + 1];
141
+ if (after === '(' || after === '`') {
142
+ return { problem: 'it contains a command substitution, which is never run' };
143
+ }
144
+ if (after === '{') {
145
+ const close = source.indexOf('}', at + 2);
146
+ const inner = close < 0 ? '' : source.slice(at + 2, close);
147
+ if (!NAME.test(inner)) {
148
+ return { problem: 'it uses a form of `${...}` other than `${NAME}`, which is not read' };
149
+ }
150
+ out += lookup(inner);
151
+ return { next: close + 1 };
152
+ }
153
+ if (after !== undefined && /[A-Za-z_]/.test(after)) {
154
+ let end = at + 2;
155
+ while (end < source.length && /[A-Za-z0-9_]/.test(source[end]))
156
+ end += 1;
157
+ out += lookup(source.slice(at + 1, end));
158
+ return { next: end };
159
+ }
160
+ out += '$';
161
+ return { next: at + 1 };
162
+ };
163
+ while (i < text.length) {
164
+ const ch = text[i];
165
+ if (ch === "'") {
166
+ const close = text.indexOf("'", i + 1);
167
+ if (close < 0)
168
+ return { problem: 'a single quote is never closed' };
169
+ out += text.slice(i + 1, close);
170
+ i = close + 1;
171
+ }
172
+ else if (ch === '"') {
173
+ i += 1;
174
+ let closed = false;
175
+ while (i < text.length) {
176
+ const c = text[i];
177
+ if (c === '"') {
178
+ closed = true;
179
+ i += 1;
180
+ break;
181
+ }
182
+ if (c === '\\' && i + 1 < text.length) {
183
+ const nextChar = text[i + 1];
184
+ if (nextChar === '"' || nextChar === '\\' || nextChar === '$' || nextChar === '`') {
185
+ out += nextChar;
186
+ i += 2;
187
+ continue;
188
+ }
189
+ out += c;
190
+ i += 1;
191
+ continue;
192
+ }
193
+ if (c === '`')
194
+ return { problem: 'it contains a command substitution, which is never run' };
195
+ if (c === '$') {
196
+ const step = expand(text, i);
197
+ if ('problem' in step)
198
+ return step;
199
+ i = step.next;
200
+ continue;
201
+ }
202
+ out += c;
203
+ i += 1;
204
+ }
205
+ if (!closed)
206
+ return { problem: 'a double quote is never closed' };
207
+ }
208
+ else if (/\s/.test(ch)) {
209
+ const rest = text.slice(i).trim();
210
+ if (rest === '' || rest.startsWith('#'))
211
+ break;
212
+ return { problem: 'it has text after the value that a shell would read as another word' };
213
+ }
214
+ else if (ch === '$') {
215
+ const step = expand(text, i);
216
+ if ('problem' in step)
217
+ return step;
218
+ i = step.next;
219
+ }
220
+ else if (ch === '`' || ';&|<>()'.includes(ch)) {
221
+ return { problem: `it has "${ch}" outside quotes, which a shell would act on` };
222
+ }
223
+ else if (ch === '\\' && i + 1 < text.length) {
224
+ out += text[i + 1];
225
+ i += 2;
226
+ }
227
+ else {
228
+ out += ch;
229
+ i += 1;
230
+ }
231
+ }
232
+ return { value: out };
233
+ }
234
+ /** One line of a hook's file. */
235
+ export function readHookEnvLine(raw, lookup) {
236
+ const line = raw.trim();
237
+ if (line === '' || line.startsWith('#'))
238
+ return { skip: true };
239
+ const body = /^export\s+/.test(line) ? line.replace(/^export\s+/, '') : line;
240
+ const eq = body.indexOf('=');
241
+ if (eq <= 0) {
242
+ // `export NAME` re-exports what is already there: nothing to carry.
243
+ return NAME.test(body) && body !== line ? { skip: true } : { problem: 'it is not NAME=value' };
244
+ }
245
+ const name = body.slice(0, eq);
246
+ if (!NAME.test(name) || name.length > MAX_NAME_CHARS) {
247
+ return { problem: `"${name.slice(0, 40)}" is not a variable name` };
248
+ }
249
+ const refusal = refusedHookEnvName(name);
250
+ if (refusal !== undefined)
251
+ return { problem: refusal };
252
+ const value = readValue(body.slice(eq + 1), lookup);
253
+ if ('problem' in value)
254
+ return { problem: value.problem };
255
+ if (value.value.length > MAX_HOOK_ENV_VALUE_CHARS) {
256
+ return { problem: `its value is longer than ${MAX_HOOK_ENV_VALUE_CHARS} characters` };
257
+ }
258
+ if (value.value.includes('\0'))
259
+ return { problem: 'its value has a NUL character' };
260
+ return { ok: { name, value: value.value } };
261
+ }
262
+ /**
263
+ * The variables hooks have set in one session. `variables` is one live
264
+ * object, changed in place: hand it to the shell tool as its environment and
265
+ * every command starts with what hooks have set so far.
266
+ */
267
+ export class SessionHookEnv {
268
+ options;
269
+ /** What hooks have set, live. Do not write to it. */
270
+ // No prototype, so a variable called `__proto__` is a variable like any other.
271
+ variables = Object.create(null);
272
+ /** Per kind of hook, one layer for each hook that has a file, by the hook's place in the configuration. */
273
+ layers = new Map();
274
+ slots = new Map();
275
+ reported = new Set();
276
+ directory;
277
+ disposed = false;
278
+ constructor(options = {}) {
279
+ this.options = options;
280
+ }
281
+ /**
282
+ * The file a hook of `event` is told to append to; made on first use. Each
283
+ * hook has a file of its own (`slot` is its place in the configuration), so
284
+ * hooks that run side by side never interleave their lines and a variable
285
+ * two of them set takes the value of the one that comes later in the file.
286
+ */
287
+ fileFor(event, slot = 0) {
288
+ if (this.disposed)
289
+ return undefined;
290
+ try {
291
+ if (this.directory === undefined) {
292
+ this.directory = this.options.directory?.() ?? createTempDirSync('hook-env');
293
+ mkdirSync(this.directory, { recursive: true, mode: 0o700 });
294
+ }
295
+ const file = join(this.directory, envFileName(event, slot));
296
+ const known = this.slots.get(event) ?? new Set();
297
+ known.add(slot);
298
+ this.slots.set(event, known);
299
+ // Created empty, owner-only, so a hook that appends finds it there.
300
+ writeFileSync(file, '', { flag: 'a', mode: 0o600 });
301
+ return file;
302
+ }
303
+ catch (error) {
304
+ this.report(`Namzu could not make the file hooks set variables in (${error instanceof Error ? error.message : String(error)}), so no hook can set a variable for this session.`);
305
+ return undefined;
306
+ }
307
+ }
308
+ /**
309
+ * Read `event`'s file again and recompute what is set. Call after a hook of
310
+ * that event ends: with its `slot` for that hook's file, without one for
311
+ * every file the event has.
312
+ */
313
+ absorb(event, slot) {
314
+ if (this.disposed || this.directory === undefined)
315
+ return;
316
+ const wanted = slot === undefined ? [...(this.slots.get(event) ?? [])].sort((a, b) => a - b) : [slot];
317
+ for (const one of wanted) {
318
+ const text = this.readFile(join(this.directory, envFileName(event, one)));
319
+ if (text === undefined)
320
+ continue;
321
+ const bySlot = this.layers.get(event) ?? new Map();
322
+ this.layers.set(event, bySlot);
323
+ bySlot.set(one, this.parse(event, one, text));
324
+ }
325
+ this.merge();
326
+ }
327
+ /** Forget what `cwd_changed` and `file_changed` hooks set: it described the folder that was left. */
328
+ clearFolderLayers() {
329
+ if (this.disposed)
330
+ return;
331
+ for (const event of ['cwd_changed', 'file_changed']) {
332
+ this.layers.delete(event);
333
+ if (this.directory !== undefined) {
334
+ for (const slot of this.slots.get(event) ?? []) {
335
+ try {
336
+ writeFileSync(join(this.directory, envFileName(event, slot)), '', { mode: 0o600 });
337
+ }
338
+ catch {
339
+ // A file that cannot be emptied is read again at the next absorb.
340
+ }
341
+ }
342
+ }
343
+ }
344
+ this.merge();
345
+ }
346
+ /** The names set now and the kind of hook that set each, never the values. */
347
+ describe() {
348
+ const out = new Map();
349
+ for (const event of HOOK_ENV_EVENTS) {
350
+ for (const layer of this.orderedLayers(event)) {
351
+ for (const name of layer.keys())
352
+ out.set(name, event);
353
+ }
354
+ }
355
+ return [...out].map(([name, event]) => ({ name, event }));
356
+ }
357
+ /** Remove the files. The live object is emptied too. */
358
+ dispose() {
359
+ this.disposed = true;
360
+ this.layers.clear();
361
+ this.merge();
362
+ if (this.directory !== undefined) {
363
+ try {
364
+ rmSync(this.directory, { recursive: true, force: true });
365
+ }
366
+ catch {
367
+ // Scratch space is cleaned up by the operating system in the end.
368
+ }
369
+ this.directory = undefined;
370
+ }
371
+ }
372
+ report(message) {
373
+ if (this.reported.has(message))
374
+ return;
375
+ this.reported.add(message);
376
+ this.options.onProblem?.(message);
377
+ }
378
+ readFile(path) {
379
+ let fd;
380
+ try {
381
+ fd = openSync(path, 'r');
382
+ const buffer = Buffer.alloc(MAX_HOOK_ENV_FILE_BYTES + 1);
383
+ const read = readSync(fd, buffer, 0, buffer.length, 0);
384
+ if (read > MAX_HOOK_ENV_FILE_BYTES) {
385
+ this.report(`A hook's variables file is longer than ${MAX_HOOK_ENV_FILE_BYTES / 1024} KiB; only the first ${MAX_HOOK_ENV_FILE_BYTES / 1024} KiB is read.`);
386
+ }
387
+ return buffer.subarray(0, Math.min(read, MAX_HOOK_ENV_FILE_BYTES)).toString('utf8');
388
+ }
389
+ catch {
390
+ return undefined;
391
+ }
392
+ finally {
393
+ if (fd !== undefined) {
394
+ try {
395
+ closeSync(fd);
396
+ }
397
+ catch {
398
+ // Nothing more to do with a descriptor that will not close.
399
+ }
400
+ }
401
+ }
402
+ }
403
+ orderedLayers(event) {
404
+ const bySlot = this.layers.get(event);
405
+ if (bySlot === undefined)
406
+ return [];
407
+ return [...bySlot].sort(([a], [b]) => a - b).map(([, layer]) => layer);
408
+ }
409
+ parse(event, slot, text) {
410
+ const layer = new Map();
411
+ const inherited = this.options.inherited?.() ?? process.env;
412
+ // An earlier kind of hook's values are visible to `$NAME`, then this file's own so far.
413
+ const lower = new Map();
414
+ for (const earlier of HOOK_ENV_EVENTS) {
415
+ for (const [earlierSlot, entries] of [...(this.layers.get(earlier) ?? [])].sort(([a], [b]) => a - b)) {
416
+ if (earlier === event && earlierSlot >= slot)
417
+ break;
418
+ for (const [name, value] of entries)
419
+ lower.set(name, value);
420
+ }
421
+ if (earlier === event)
422
+ break;
423
+ }
424
+ const lookup = (name) => layer.get(name) ?? lower.get(name) ?? inherited[name] ?? '';
425
+ const lines = text.split(/\r?\n/);
426
+ for (const [index, raw] of lines.entries()) {
427
+ const read = readHookEnvLine(raw, lookup);
428
+ if ('skip' in read)
429
+ continue;
430
+ if ('problem' in read) {
431
+ this.report(`A ${event} hook set a variable that was not read (line ${index + 1}): ${read.problem}.`);
432
+ continue;
433
+ }
434
+ layer.set(read.ok.name, read.ok.value);
435
+ }
436
+ return layer;
437
+ }
438
+ merge() {
439
+ for (const key of Object.keys(this.variables))
440
+ delete this.variables[key];
441
+ let count = 0;
442
+ for (const event of HOOK_ENV_EVENTS) {
443
+ for (const layer of this.orderedLayers(event)) {
444
+ for (const [name, value] of layer) {
445
+ if (!(name in this.variables)) {
446
+ if (count >= MAX_HOOK_ENV_VARIABLES) {
447
+ this.report(`Hooks have set more than ${MAX_HOOK_ENV_VARIABLES} variables; the ones past that are left out.`);
448
+ continue;
449
+ }
450
+ count += 1;
451
+ }
452
+ this.variables[name] = value;
453
+ }
454
+ }
455
+ }
456
+ }
457
+ }
458
+ //# sourceMappingURL=session-hook-env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-hook-env.js","sourceRoot":"","sources":["../../src/plugin/session-hook-env.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAEH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACzF,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEpD,kGAAkG;AAClG,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,eAAe,EAAE,aAAa,EAAE,cAAc,CAAU,CAAA;AAGxF,qDAAqD;AACrD,MAAM,CAAC,MAAM,uBAAuB,GAAG,E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KAAK,CAAC,CAAA;QACvC,CAAC;QACD,OAAO,KAAK,CAAA;IACb,CAAC;IAEO,KAAK;QACZ,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;YAAE,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;QACzE,IAAI,KAAK,GAAG,CAAC,CAAA;QACb,KAAK,MAAM,KAAK,IAAI,eAAe,EAAE,CAAC;YACrC,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC/C,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,KAAK,EAAE,CAAC;oBACnC,IAAI,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;wBAC/B,IAAI,KAAK,IAAI,sBAAsB,EAAE,CAAC;4BACrC,IAAI,CAAC,MAAM,CACV,4BAA4B,sBAAsB,8CAA8C,CAChG,CAAA;4BACD,SAAQ;wBACT,CAAC;wBACD,KAAK,IAAI,CAAC,CAAA;oBACX,CAAC;oBACD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;gBAC7B,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;CACD"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * The environment a shell hook command is started with, beyond what the
3
+ * kernel always sets: the entry's own `env` map, and variables a moved
4
+ * command expects.
5
+ *
6
+ * A hooks block brought from elsewhere often starts a command with a variable
7
+ * for the project folder, named for the tool it was written for. That tool's
8
+ * name is not ours to know, so the rule here reads the shape of the name: a
9
+ * variable the command or its arguments refer to, that is not set, and whose
10
+ * name ends in one of {@link MOVED_FOLDER_ENDINGS}, is set to that folder.
11
+ * Nothing already set is ever replaced.
12
+ */
13
+ /** A name a hook is started with that `env` may not replace: the kernel sets these itself. */
14
+ export declare const SHELL_HOOK_RESERVED_ENV: readonly string[];
15
+ /** The folders a name's ending can stand for. */
16
+ export type MovedFolder = 'project' | 'plugin' | 'pluginData' | 'skill' | 'envFile';
17
+ /** Endings read by shape; the name looked for is the whole variable name. */
18
+ export declare const MOVED_FOLDER_ENDINGS: readonly {
19
+ readonly ending: string;
20
+ readonly folder: MovedFolder;
21
+ }[];
22
+ /**
23
+ * The folder a variable name stands for, or `undefined` when it has none here.
24
+ * A skill's folder answers `_SKILL_ROOT` and also `_PLUGIN_ROOT`, unless the
25
+ * entry belongs to a plugin (then `_PLUGIN_ROOT` is the plugin's folder).
26
+ */
27
+ export declare function movedFolderFor(name: string, folders: Readonly<Partial<Record<MovedFolder, string>>>): string | undefined;
28
+ /** The folders a plugin-shipped entry is given. */
29
+ export interface PluginFolders {
30
+ /** The plugin's own folder. */
31
+ readonly root: string;
32
+ /** The folder kept for the plugin's data, under the Namzu home and never inside `root`. */
33
+ readonly dataDir: string;
34
+ }
35
+ /** Whether a variable name stands for a plugin folder by its ending. */
36
+ export declare function isPluginFolderVariable(name: string): boolean;
37
+ /**
38
+ * `${NAME}` and `$NAME` in a command or argument, for a name that ends in
39
+ * `_PLUGIN_ROOT` or `_PLUGIN_DATA`, become the folder's path, so an entry in
40
+ * the exec form (which no shell reads) finds its own folder too. A reference
41
+ * with a modifier (`${NAME:-x}`) is left to the shell, which has the variable.
42
+ */
43
+ export declare function substitutePluginFolders(text: string, folders: PluginFolders | undefined): string;
44
+ /**
45
+ * For the exec form, which no shell reads: `${NAME}` and `$NAME` for a name
46
+ * that ends in one of {@link MOVED_FOLDER_ENDINGS} become the value the hook is
47
+ * started with (`env` is the environment {@link movedEnvironment} built, so a
48
+ * value the entry or the host already set wins). A reference with a modifier is
49
+ * left as written, as is a name with no value.
50
+ */
51
+ export declare function substituteMovedFolders(text: string, env: Readonly<Record<string, string | undefined>>): string;
52
+ /**
53
+ * The program that reads a shell hook's command, found on the host's own
54
+ * `PATH`. An entry may set `PATH` in its `env` to something that does not hold
55
+ * `sh`; the command is still read by it, so the shell is looked up before that
56
+ * `env` is applied. `undefined` when no change is needed or none is found.
57
+ */
58
+ export declare function shellPathOutsideEnv(name: string, hostPath: string | undefined, startedWith: string | undefined, isExecutable: (path: string) => boolean, separator: string): string | undefined;
59
+ /** What is wrong with an `env` name, or `undefined` when it can be used. */
60
+ export declare function shellHookEnvNameProblem(name: string): string | undefined;
61
+ /** The problems in an `env` value as a whole, each naming the variable. Empty when it is usable. */
62
+ export declare function shellHookEnvProblems(env: unknown): string[];
63
+ /**
64
+ * The variables the texts refer to, in order of first appearance. A reference
65
+ * with a default (`${NAME:-x}`, `${NAME-x}`, `${NAME:+x}`, `${NAME=x}`) is
66
+ * left out unless `withDefaults` is set: it works without the variable.
67
+ */
68
+ export declare function referencedVariables(texts: readonly string[], options?: {
69
+ readonly withDefaults?: boolean;
70
+ }): string[];
71
+ /**
72
+ * Whether the command text itself gives the variable a value (`NAME=...`,
73
+ * `for NAME in`, `select NAME in`, `read NAME`, `getopts opts NAME`).
74
+ */
75
+ export declare function assignedWithin(texts: readonly string[], name: string): boolean;
76
+ /** Whether the name is one a shell keeps for itself (`RANDOM`, `PPID`, `PWD`, ...). */
77
+ export declare function isShellOwnVariable(name: string): boolean;
78
+ /** `$NAME` and `${NAME}` in an `env` value become the value the hook already has; any other text stays as written. */
79
+ export declare function expandHookEnvValue(value: string, have: Readonly<Record<string, string | undefined>>): string;
80
+ export interface MovedEnvironment {
81
+ /** The environment to start the command with. */
82
+ readonly env: Record<string, string | undefined>;
83
+ /** Variables the command refers to that are set nowhere (and not by the command itself). */
84
+ readonly unset: readonly string[];
85
+ }
86
+ /**
87
+ * Layer the entry's `env` map and the by-shape variables over `base` (the
88
+ * inherited environment with the kernel's own variables). `kernel` holds the
89
+ * variables the kernel always sets; they are applied last and win.
90
+ */
91
+ export declare function movedEnvironment(entry: {
92
+ readonly command: string;
93
+ readonly args?: readonly string[];
94
+ readonly env?: unknown;
95
+ }, base: Readonly<Record<string, string | undefined>>, kernel: Readonly<Record<string, string | undefined>>, folders: Readonly<Partial<Record<MovedFolder, string>>>): MovedEnvironment;
96
+ /**
97
+ * The sentence added to a hook's failure when it could not find what it ran
98
+ * and its command refers to variables that are set nowhere; `undefined` when
99
+ * the failure does not look like a missing program or file, or nothing is unset.
100
+ */
101
+ export declare function hookUnsetVariableHint(outcome: {
102
+ readonly exitCode: number | null;
103
+ readonly stderr: string;
104
+ readonly spawnError?: string;
105
+ readonly unsetVariables?: readonly string[];
106
+ }): string | undefined;
107
+ //# sourceMappingURL=shell-hook-env.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell-hook-env.d.ts","sourceRoot":"","sources":["../../src/plugin/shell-hook-env.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,8FAA8F;AAC9F,eAAO,MAAM,uBAAuB,EAAE,SAAS,MAAM,EASpD,CAAA;AAED,iDAAiD;AACjD,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,QAAQ,GAAG,YAAY,GAAG,OAAO,GAAG,SAAS,CAAA;AAEnF,6EAA6E;AAC7E,eAAO,MAAM,oBAAoB,EAAE,SAAS;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAA;CAC5B,EAgBA,CAAA;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC7B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,CAAC,GACrD,MAAM,GAAG,SAAS,CAKpB;AAED,mDAAmD;AACnD,MAAM,WAAW,aAAa;IAC7B,+BAA+B;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,2FAA2F;IAC3F,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CACxB;AAED,wEAAwE;AACxE,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAK5D;AAED;;;;;GAKG;AACH,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