@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.
- package/CHANGELOG.md +43 -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 +3 -3
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +3 -3
- 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 +5 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +3 -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 +3 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +5 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +3 -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 +93 -16
- 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/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 +68 -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 +32 -0
- package/src/public-types.ts +20 -1
- package/src/runtime/query/executor.ts +8 -0
- package/src/runtime/query/index.ts +12 -0
- package/src/runtime/query/tooling.ts +8 -0
- 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 +115 -26
- package/src/types/plugin/index.ts +84 -1
- package/src/types/skills/index.ts +9 -0
- package/src/types/tool/index.ts +74 -0
- package/src/types/workspace/ref.ts +6 -0
- package/src/utils/frontmatter.ts +52 -1
|
@@ -105,6 +105,8 @@
|
|
|
105
105
|
* and does not block: one that could not answer in time has not answered no.
|
|
106
106
|
*/
|
|
107
107
|
import { spawn } from 'node:child_process';
|
|
108
|
+
import { constants, accessSync, mkdirSync, statSync } from 'node:fs';
|
|
109
|
+
import { basename, delimiter } from 'node:path';
|
|
108
110
|
import { decodedCommands, decomposeCommandLine } from '../authorization/command-line.js';
|
|
109
111
|
import { SKILL_TOOL_NAME_ALIASES, permissionPatternToRegExpSource, } from '../authorization/skill-grant.js';
|
|
110
112
|
import { withSessionChildMarker } from '../process/session-child.js';
|
|
@@ -114,8 +116,10 @@ import { asPluginId } from '../utils/id.js';
|
|
|
114
116
|
import { NOOP_LOGGER } from '../utils/log/create-logger.js';
|
|
115
117
|
import { boundHookReplacement } from './hook-context.js';
|
|
116
118
|
import { readHookCommand } from './shell-hook-command.js';
|
|
119
|
+
import { hookUnsetVariableHint, movedEnvironment, shellPathOutsideEnv, substituteMovedFolders, } from './shell-hook-env.js';
|
|
117
120
|
import { postHookJson } from './shell-hook-http.js';
|
|
118
121
|
import { shellHookToolName } from './shell-hook-tool-names.js';
|
|
122
|
+
import { worktreeHookVerdict } from './worktree-hook-answer.js';
|
|
119
123
|
export const SHELL_HOOK_EVENTS = [
|
|
120
124
|
'user_prompt_submit',
|
|
121
125
|
'session_start',
|
|
@@ -134,6 +138,10 @@ export const SHELL_HOOK_EVENTS = [
|
|
|
134
138
|
'turn_failure',
|
|
135
139
|
'turn_start',
|
|
136
140
|
'turn_end',
|
|
141
|
+
'file_changed',
|
|
142
|
+
'cwd_changed',
|
|
143
|
+
'worktree_create',
|
|
144
|
+
'worktree_remove',
|
|
137
145
|
];
|
|
138
146
|
/** The plugin id host shell hooks register under when the host names none. */
|
|
139
147
|
export const SHELL_HOOKS_PLUGIN_ID = asPluginId('6d6f6bec-9d54-485b-90ca-52edbac1009d');
|
|
@@ -327,6 +335,49 @@ export function shellHookMatcherProblem(matcher) {
|
|
|
327
335
|
}
|
|
328
336
|
}
|
|
329
337
|
const IF_SHAPE = /^([A-Za-z0-9_.:*|, -]+)(?:\(([\s\S]*)\))?$/;
|
|
338
|
+
/** A part of a `file_changed` matcher that names a path: letters, digits and the characters of a path. */
|
|
339
|
+
const FILE_LIST_PART = /^[A-Za-z0-9_.:*\\/-]+$/;
|
|
340
|
+
/** The parts of a `file_changed` matcher when every part is written as a path, else `undefined`. */
|
|
341
|
+
function fileMatcherParts(matcher) {
|
|
342
|
+
if (matcher === undefined || matcher.trim() === '' || matcher.trim() === '*')
|
|
343
|
+
return undefined;
|
|
344
|
+
const parts = matcher.split(/[|,]/).map((part) => part.trim());
|
|
345
|
+
return parts.every((part) => FILE_LIST_PART.test(part)) ? parts : undefined;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* The files a `file_changed` matcher names: each part of a `|` list that is a
|
|
349
|
+
* path with no `*` in it (`.envrc`, `config/app.json`, `/etc/hosts`). A part
|
|
350
|
+
* with a `*`, a pattern, or no matcher names no file; the hook then runs only
|
|
351
|
+
* for the files hooks hand back with `watchPaths`. A relative path is under
|
|
352
|
+
* the working folder, which the host knows.
|
|
353
|
+
*/
|
|
354
|
+
export function fileChangedMatcherPaths(matcher) {
|
|
355
|
+
return (fileMatcherParts(matcher) ?? []).filter((part) => !part.includes('*'));
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Whether a `file_changed` hook with this matcher runs for `filePath`. The
|
|
359
|
+
* matcher is read as every matcher is, but against the file's base name: a
|
|
360
|
+
* part written as a path (`config/app.json`) stands for its base name
|
|
361
|
+
* (`app.json`). An unreadable pattern runs the hook.
|
|
362
|
+
*/
|
|
363
|
+
export function fileChangedMatches(matcher, filePath) {
|
|
364
|
+
if (filePath === undefined)
|
|
365
|
+
return true;
|
|
366
|
+
const name = basename(filePath.replace(/[\\/]+$/, ''));
|
|
367
|
+
const parts = fileMatcherParts(matcher);
|
|
368
|
+
if (parts === undefined)
|
|
369
|
+
return shellHookMatches(matcher, name);
|
|
370
|
+
return shellHookMatches(parts.map((part) => basename(part.replace(/[\\/]+$/, '')) || part).join('|'), name);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* What a `file_changed` entry's matcher does not do, or `undefined`: a matcher
|
|
374
|
+
* that names no file watches nothing by itself.
|
|
375
|
+
*/
|
|
376
|
+
export function fileChangedMatcherNote(matcher) {
|
|
377
|
+
if (fileChangedMatcherPaths(matcher).length > 0)
|
|
378
|
+
return undefined;
|
|
379
|
+
return 'its matcher names no file, so it watches nothing itself and runs only for files other hooks hand back with watchPaths';
|
|
380
|
+
}
|
|
330
381
|
/**
|
|
331
382
|
* Why an `if` cannot be read, or `undefined` when it can. A filter that
|
|
332
383
|
* cannot be read runs the hook.
|
|
@@ -435,6 +486,14 @@ export function shellHookPayload(input) {
|
|
|
435
486
|
? { agent_id: input.sessionId, agent_type: input.agentType }
|
|
436
487
|
: {}),
|
|
437
488
|
...(input.prompt !== undefined ? { prompt: input.prompt } : {}),
|
|
489
|
+
...(input.filePath !== undefined ? { file_path: input.filePath } : {}),
|
|
490
|
+
...(input.change !== undefined ? { change: input.change } : {}),
|
|
491
|
+
...(input.oldCwd !== undefined ? { old_cwd: input.oldCwd } : {}),
|
|
492
|
+
...(input.newCwd !== undefined ? { new_cwd: input.newCwd } : {}),
|
|
493
|
+
...(input.worktreeName !== undefined ? { name: input.worktreeName } : {}),
|
|
494
|
+
...(input.worktreeRoot !== undefined ? { worktree_root: input.worktreeRoot } : {}),
|
|
495
|
+
...(input.worktreeBaseRef !== undefined ? { base_ref: input.worktreeBaseRef } : {}),
|
|
496
|
+
...(input.worktreePath !== undefined ? { worktree_path: input.worktreePath } : {}),
|
|
438
497
|
...(input.compaction !== undefined ? compactionFields(input.event, input.compaction) : {}),
|
|
439
498
|
...(input.toolName !== undefined
|
|
440
499
|
? { tool_name: shellHookToolName(input.toolName, input.toolNames) }
|
|
@@ -493,6 +552,17 @@ function compactionFields(event, compaction) {
|
|
|
493
552
|
...(event === 'post_compact' && summary !== undefined ? { compact_summary: summary } : {}),
|
|
494
553
|
};
|
|
495
554
|
}
|
|
555
|
+
function isExecutableFile(path) {
|
|
556
|
+
try {
|
|
557
|
+
if (!statSync(path).isFile())
|
|
558
|
+
return false;
|
|
559
|
+
accessSync(path, constants.X_OK);
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
catch {
|
|
563
|
+
return false;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
496
566
|
/** Run one hook command to completion, bounded by its deadline and capture. */
|
|
497
567
|
export function runShellHook(entry, input, signal) {
|
|
498
568
|
const timeoutMs = Math.min(MAX_SHELL_HOOK_TIMEOUT_MS, Math.max(1, entry.timeoutMs ?? DEFAULT_SHELL_HOOK_TIMEOUT_MS));
|
|
@@ -500,21 +570,63 @@ export function runShellHook(entry, input, signal) {
|
|
|
500
570
|
const toolPath = pathOf(input.toolInput);
|
|
501
571
|
// A hook outside a turn must not see a turn id, including one the host
|
|
502
572
|
// inherited from an enclosing hook's environment.
|
|
503
|
-
|
|
573
|
+
// Nor the variables file of an enclosing hook: only the three events that own one get it.
|
|
574
|
+
const { NAMZU_TURN_ID: _inheritedTurnId, NAMZU_ENV_FILE: _inheritedEnvFile, ...inherited } = process.env;
|
|
575
|
+
const projectDir = input.projectDir ?? input.cwd;
|
|
576
|
+
const kernel = {
|
|
577
|
+
NAMZU_HOOK_EVENT: input.event,
|
|
578
|
+
NAMZU_SESSION_ID: input.sessionId,
|
|
579
|
+
NAMZU_PROJECT_DIR: projectDir,
|
|
580
|
+
...(input.plugin !== undefined
|
|
581
|
+
? { NAMZU_PLUGIN_ROOT: input.plugin.root, NAMZU_PLUGIN_DATA: input.plugin.dataDir }
|
|
582
|
+
: {}),
|
|
583
|
+
...(input.turnId !== undefined ? { NAMZU_TURN_ID: input.turnId } : {}),
|
|
584
|
+
...(input.envFile !== undefined ? { NAMZU_ENV_FILE: input.envFile } : {}),
|
|
585
|
+
...(input.toolName !== undefined
|
|
586
|
+
? { NAMZU_TOOL_NAME: shellHookToolName(input.toolName, input.toolNames) }
|
|
587
|
+
: {}),
|
|
588
|
+
...(toolPath !== undefined ? { NAMZU_TOOL_PATH: toolPath } : {}),
|
|
589
|
+
};
|
|
590
|
+
// The entry's `env`, and any `..._PROJECT_DIR`, `..._PLUGIN_ROOT` or
|
|
591
|
+
// `..._PLUGIN_DATA` its command refers to without it being set, over the
|
|
592
|
+
// inherited environment; the kernel's own names last. A shell expands the
|
|
593
|
+
// variables itself, so a folder name is never read as shell text.
|
|
594
|
+
const pluginFolders = input.plugin;
|
|
595
|
+
const { env, unset } = movedEnvironment(entry, { ...inherited, ...input.extraEnv, ...kernel }, kernel, {
|
|
596
|
+
project: projectDir,
|
|
597
|
+
...(input.skillRoot !== undefined ? { skill: input.skillRoot } : {}),
|
|
598
|
+
...(input.envFile !== undefined ? { envFile: input.envFile } : {}),
|
|
599
|
+
...(pluginFolders !== undefined
|
|
600
|
+
? { plugin: pluginFolders.root, pluginData: pluginFolders.dataDir }
|
|
601
|
+
: {}),
|
|
602
|
+
});
|
|
603
|
+
// The exec form has no shell to expand them, so the values are put in.
|
|
604
|
+
const placed = entry.args === undefined
|
|
605
|
+
? entry
|
|
606
|
+
: {
|
|
607
|
+
...entry,
|
|
608
|
+
command: substituteMovedFolders(entry.command, env),
|
|
609
|
+
args: entry.args.map((arg) => substituteMovedFolders(arg, env)),
|
|
610
|
+
};
|
|
611
|
+
const unsetVariables = unset.length > 0 ? { unsetVariables: unset } : {};
|
|
612
|
+
if (pluginFolders !== undefined) {
|
|
613
|
+
try {
|
|
614
|
+
// Created when first needed; never inside the plugin's own folder.
|
|
615
|
+
mkdirSync(pluginFolders.dataDir, { recursive: true, mode: 0o700 });
|
|
616
|
+
}
|
|
617
|
+
catch (error) {
|
|
618
|
+
return Promise.resolve({
|
|
619
|
+
exitCode: null,
|
|
620
|
+
timedOut: false,
|
|
621
|
+
stdout: '',
|
|
622
|
+
stderr: '',
|
|
623
|
+
spawnError: `the plugin's data folder could not be created (${error instanceof Error ? error.message : String(error)})`,
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
}
|
|
504
627
|
return new Promise((resolve) => {
|
|
505
628
|
let child;
|
|
506
629
|
try {
|
|
507
|
-
const env = {
|
|
508
|
-
...inherited,
|
|
509
|
-
NAMZU_HOOK_EVENT: input.event,
|
|
510
|
-
NAMZU_SESSION_ID: input.sessionId,
|
|
511
|
-
NAMZU_PROJECT_DIR: input.projectDir ?? input.cwd,
|
|
512
|
-
...(input.turnId !== undefined ? { NAMZU_TURN_ID: input.turnId } : {}),
|
|
513
|
-
...(input.toolName !== undefined
|
|
514
|
-
? { NAMZU_TOOL_NAME: shellHookToolName(input.toolName, input.toolNames) }
|
|
515
|
-
: {}),
|
|
516
|
-
...(toolPath !== undefined ? { NAMZU_TOOL_PATH: toolPath } : {}),
|
|
517
|
-
};
|
|
518
630
|
// Its own process group, so the deadline can kill everything the
|
|
519
631
|
// command started. `sh -c 'sleep 5'` is one process under a shell
|
|
520
632
|
// that execs its last command and two under one that forks it;
|
|
@@ -523,15 +635,15 @@ export function runShellHook(entry, input, signal) {
|
|
|
523
635
|
// its own — the hook's deadline becomes the grandchild's.
|
|
524
636
|
const detached = process.platform !== 'win32';
|
|
525
637
|
child =
|
|
526
|
-
|
|
638
|
+
placed.args !== undefined
|
|
527
639
|
? // The exec form: no shell reads `command` or any argument.
|
|
528
|
-
spawn(
|
|
640
|
+
spawn(placed.command, [...placed.args], {
|
|
529
641
|
cwd: input.cwd,
|
|
530
642
|
env: withSessionChildMarker(env),
|
|
531
643
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
532
644
|
detached,
|
|
533
645
|
})
|
|
534
|
-
: spawnHostShell(
|
|
646
|
+
: spawnHostShell(placed.command, {
|
|
535
647
|
cwd: input.cwd,
|
|
536
648
|
env,
|
|
537
649
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
@@ -541,7 +653,13 @@ export function runShellHook(entry, input, signal) {
|
|
|
541
653
|
// when installed, cmd.exe when not.
|
|
542
654
|
...(process.platform === 'win32'
|
|
543
655
|
? {}
|
|
544
|
-
: {
|
|
656
|
+
: {
|
|
657
|
+
shell: {
|
|
658
|
+
path: shellPathOutsideEnv('sh', inherited.PATH, env.PATH, isExecutableFile, delimiter) ?? 'sh',
|
|
659
|
+
dialect: 'sh',
|
|
660
|
+
source: 'sh',
|
|
661
|
+
},
|
|
662
|
+
}),
|
|
545
663
|
});
|
|
546
664
|
}
|
|
547
665
|
catch (error) {
|
|
@@ -551,6 +669,7 @@ export function runShellHook(entry, input, signal) {
|
|
|
551
669
|
stdout: '',
|
|
552
670
|
stderr: '',
|
|
553
671
|
spawnError: error instanceof Error ? error.message : String(error),
|
|
672
|
+
...unsetVariables,
|
|
554
673
|
});
|
|
555
674
|
return;
|
|
556
675
|
}
|
|
@@ -612,6 +731,7 @@ export function runShellHook(entry, input, signal) {
|
|
|
612
731
|
stderr,
|
|
613
732
|
...(stdoutTruncated ? { stdoutTruncated } : {}),
|
|
614
733
|
spawnError: error.message,
|
|
734
|
+
...unsetVariables,
|
|
615
735
|
});
|
|
616
736
|
});
|
|
617
737
|
child.on('close', (code) => {
|
|
@@ -621,6 +741,7 @@ export function runShellHook(entry, input, signal) {
|
|
|
621
741
|
stdout,
|
|
622
742
|
stderr,
|
|
623
743
|
...(stdoutTruncated ? { stdoutTruncated } : {}),
|
|
744
|
+
...unsetVariables,
|
|
624
745
|
});
|
|
625
746
|
});
|
|
626
747
|
child.on('exit', () => {
|
|
@@ -716,6 +837,10 @@ export const SHELL_HOOK_PASCAL_EVENT_NAMES = {
|
|
|
716
837
|
permission_denied: 'PermissionDenied',
|
|
717
838
|
turn_failure: 'StopFailure',
|
|
718
839
|
turn_end: 'Stop',
|
|
840
|
+
file_changed: 'FileChanged',
|
|
841
|
+
cwd_changed: 'CwdChanged',
|
|
842
|
+
worktree_create: 'WorktreeCreate',
|
|
843
|
+
worktree_remove: 'WorktreeRemove',
|
|
719
844
|
};
|
|
720
845
|
/** The keys that carry a replacement for a tool's result. */
|
|
721
846
|
const REPLACEMENT_KEYS = ['updatedToolOutput', 'updatedMCPToolOutput'];
|
|
@@ -804,6 +929,14 @@ function readShellHookJson(event, stdout, call = {}) {
|
|
|
804
929
|
};
|
|
805
930
|
}
|
|
806
931
|
}
|
|
932
|
+
// The same for a list of files to watch written at the top level.
|
|
933
|
+
if ((event === 'session_start' || event === 'file_changed' || event === 'cwd_changed') &&
|
|
934
|
+
parsed.watchPaths !== undefined &&
|
|
935
|
+
!(isRecord(parsed.hookSpecificOutput) && parsed.hookSpecificOutput.watchPaths !== undefined)) {
|
|
936
|
+
return {
|
|
937
|
+
invalid: '`watchPaths` is read under `hookSpecificOutput`, not at the top level of the answer',
|
|
938
|
+
};
|
|
939
|
+
}
|
|
807
940
|
const reading = {};
|
|
808
941
|
if (parsed.suppressOutput === true)
|
|
809
942
|
reading.suppressOutput = true;
|
|
@@ -842,6 +975,14 @@ function readShellHookJson(event, stdout, call = {}) {
|
|
|
842
975
|
if (problem)
|
|
843
976
|
return { invalid: `hookSpecificOutput: ${problem}` };
|
|
844
977
|
}
|
|
978
|
+
if ((event === 'session_start' || event === 'file_changed' || event === 'cwd_changed') &&
|
|
979
|
+
specific.watchPaths !== undefined) {
|
|
980
|
+
const list = specific.watchPaths;
|
|
981
|
+
if (!Array.isArray(list) || list.some((item) => typeof item !== 'string')) {
|
|
982
|
+
return { invalid: 'hookSpecificOutput: `watchPaths` must be a list of paths (strings)' };
|
|
983
|
+
}
|
|
984
|
+
reading.watchPaths = list.filter((item) => item.trim() !== '');
|
|
985
|
+
}
|
|
845
986
|
const additional = optionalString('additionalContext', specific);
|
|
846
987
|
if (additional && additional.trim().length > 0)
|
|
847
988
|
reading.context = additional.trim();
|
|
@@ -918,11 +1059,16 @@ export function shellHookVerdict(event, entry, outcome, log = NOOP_LOGGER, call
|
|
|
918
1059
|
'namzu.hook.event': event,
|
|
919
1060
|
'namzu.hook.command': label,
|
|
920
1061
|
};
|
|
1062
|
+
// A program or file that was not found, with a variable in the command that
|
|
1063
|
+
// is set nowhere: the line says which, and what to do about it.
|
|
1064
|
+
const hint = hookUnsetVariableHint(outcome);
|
|
921
1065
|
// A failure of the hook itself is "carry on" unless the entry asked for
|
|
922
1066
|
// it to count as a refusal, which is exit 2 with the failure as the reason.
|
|
923
1067
|
const failed = (reason, shown = reason) => {
|
|
924
1068
|
if (entry.onFailure !== 'block' || entry.type === 'prompt' || isAsyncShellHook(entry)) {
|
|
925
|
-
return
|
|
1069
|
+
return hint !== undefined
|
|
1070
|
+
? { action: 'continue', notice: `${label}: ${hint}` }
|
|
1071
|
+
: { action: 'continue' };
|
|
926
1072
|
}
|
|
927
1073
|
// The call has run and a refusal cannot undo it, but the hook may have
|
|
928
1074
|
// been the one thing keeping something out of the result (a redaction),
|
|
@@ -943,8 +1089,9 @@ export function shellHookVerdict(event, entry, outcome, log = NOOP_LOGGER, call
|
|
|
943
1089
|
log.warn('hook could not start', {
|
|
944
1090
|
...attrs,
|
|
945
1091
|
'namzu.hook.error': outcome.spawnError,
|
|
1092
|
+
...(hint !== undefined ? { 'namzu.hook.hint': hint } : {}),
|
|
946
1093
|
});
|
|
947
|
-
return failed(`could not start (${outcome.spawnError})`);
|
|
1094
|
+
return failed(`could not start (${outcome.spawnError})${hint !== undefined ? `; ${hint}` : ''}`);
|
|
948
1095
|
}
|
|
949
1096
|
if (outcome.timedOut) {
|
|
950
1097
|
log.warn('hook timed out', attrs);
|
|
@@ -981,8 +1128,9 @@ export function shellHookVerdict(event, entry, outcome, log = NOOP_LOGGER, call
|
|
|
981
1128
|
...attrs,
|
|
982
1129
|
'namzu.hook.exit': outcome.exitCode ?? -1,
|
|
983
1130
|
'namzu.hook.stderr': outcome.stderr.trim().slice(0, 500),
|
|
1131
|
+
...(hint !== undefined ? { 'namzu.hook.hint': hint } : {}),
|
|
984
1132
|
});
|
|
985
|
-
return failed(`exited with ${outcome.exitCode ?? 'no code'}`);
|
|
1133
|
+
return failed(`exited with ${outcome.exitCode ?? 'no code'}${hint !== undefined ? `; ${hint}` : ''}`);
|
|
986
1134
|
}
|
|
987
1135
|
// Exit 0.
|
|
988
1136
|
const text = outcome.stdout.trim();
|
|
@@ -1077,13 +1225,33 @@ export function shellHookVerdict(event, entry, outcome, log = NOOP_LOGGER, call
|
|
|
1077
1225
|
// Nothing here can be refused. What the hook said for the model becomes
|
|
1078
1226
|
// the session's context; a stop request and a warning still count.
|
|
1079
1227
|
return reading.context !== undefined
|
|
1080
|
-
? {
|
|
1228
|
+
? {
|
|
1229
|
+
action: 'annotate',
|
|
1230
|
+
text: reading.context,
|
|
1231
|
+
...(reading.watchPaths !== undefined ? { watchPaths: reading.watchPaths } : {}),
|
|
1232
|
+
...(reading.notice ? { notice: reading.notice } : {}),
|
|
1233
|
+
}
|
|
1081
1234
|
: {
|
|
1082
1235
|
action: 'continue',
|
|
1083
1236
|
...(reading.halt ? { halt: reading.halt } : {}),
|
|
1084
1237
|
...(reading.notice ? { notice: reading.notice } : {}),
|
|
1238
|
+
...(reading.watchPaths !== undefined ? { watchPaths: reading.watchPaths } : {}),
|
|
1085
1239
|
};
|
|
1086
1240
|
}
|
|
1241
|
+
case 'worktree_create':
|
|
1242
|
+
case 'worktree_remove':
|
|
1243
|
+
// Read by `worktreeHookVerdict`, which the handler calls for these two
|
|
1244
|
+
// events; a caller that gets here has no run to read.
|
|
1245
|
+
return { action: 'continue' };
|
|
1246
|
+
case 'file_changed':
|
|
1247
|
+
case 'cwd_changed':
|
|
1248
|
+
// Nothing here can be refused and nothing reaches the model: what the hook
|
|
1249
|
+
// can say is a line for the person and the files to watch from now on.
|
|
1250
|
+
return {
|
|
1251
|
+
action: 'continue',
|
|
1252
|
+
...(reading.notice ? { notice: reading.notice } : {}),
|
|
1253
|
+
...(reading.watchPaths !== undefined ? { watchPaths: reading.watchPaths } : {}),
|
|
1254
|
+
};
|
|
1087
1255
|
case 'post_tool_use':
|
|
1088
1256
|
case 'post_tool_use_failure': {
|
|
1089
1257
|
// The call has run, so a "block" cannot undo it: its reason is what the
|
|
@@ -1247,9 +1415,52 @@ function readJudgement(reply) {
|
|
|
1247
1415
|
return undefined;
|
|
1248
1416
|
}
|
|
1249
1417
|
}
|
|
1418
|
+
/**
|
|
1419
|
+
* One run of a `worktree_create` or `worktree_remove` hook, to its answer. It
|
|
1420
|
+
* is always awaited (an `async` flag cannot make a hook that does the work run
|
|
1421
|
+
* out of sight), and a `prompt` hook cannot do it at all, which is said as a
|
|
1422
|
+
* failure rather than skipped.
|
|
1423
|
+
*/
|
|
1424
|
+
async function runWorktreeHook(event, entry, input, signal, reportTimeout) {
|
|
1425
|
+
const label = describeShellHook(entry);
|
|
1426
|
+
if (entry.type === 'prompt') {
|
|
1427
|
+
return {
|
|
1428
|
+
action: 'error',
|
|
1429
|
+
message: `The ${event} hook \`${label}\` is a prompt, and a prompt cannot make or remove a worktree. Use a command or an http hook.`,
|
|
1430
|
+
};
|
|
1431
|
+
}
|
|
1432
|
+
if (entry.type === 'http') {
|
|
1433
|
+
const response = await postHookJson({
|
|
1434
|
+
url: entry.url,
|
|
1435
|
+
body: shellHookPayload(input),
|
|
1436
|
+
...(entry.headers !== undefined ? { headers: entry.headers } : {}),
|
|
1437
|
+
...(entry.allowedEnvVars !== undefined ? { allowedEnvVars: entry.allowedEnvVars } : {}),
|
|
1438
|
+
timeoutMs: hookTimeout(entry),
|
|
1439
|
+
...(signal !== undefined ? { signal } : {}),
|
|
1440
|
+
});
|
|
1441
|
+
reportTimeout(response.timedOut);
|
|
1442
|
+
const ok = response.status !== null && response.status >= 200 && response.status < 300;
|
|
1443
|
+
return worktreeHookVerdict(event, label, {
|
|
1444
|
+
exitCode: ok ? 0 : (response.status ?? null),
|
|
1445
|
+
timedOut: response.timedOut,
|
|
1446
|
+
stdout: ok ? response.body : '',
|
|
1447
|
+
stderr: ok ? '' : response.body.slice(0, 500),
|
|
1448
|
+
...(response.error !== undefined ? { spawnError: response.error } : {}),
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
const outcome = await runShellHook(entry, input, signal);
|
|
1452
|
+
reportTimeout(outcome.timedOut);
|
|
1453
|
+
return worktreeHookVerdict(event, label, outcome);
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* The place a hook has among the ones that set variables: entries are made in
|
|
1457
|
+
* configuration order, so a later number is a later line in the file.
|
|
1458
|
+
*/
|
|
1459
|
+
let nextHookEnvSlot = 0;
|
|
1250
1460
|
/** One entry as a hook definition the lifecycle manager accepts. */
|
|
1251
1461
|
export function createShellHook(event, configured, options) {
|
|
1252
1462
|
const log = options.log ?? NOOP_LOGGER;
|
|
1463
|
+
const envSlot = nextHookEnvSlot++;
|
|
1253
1464
|
const eventDefault = options.defaultTimeoutMs?.[event];
|
|
1254
1465
|
const entry = configured.timeoutMs === undefined && eventDefault !== undefined
|
|
1255
1466
|
? { ...configured, timeoutMs: eventDefault }
|
|
@@ -1267,9 +1478,14 @@ export function createShellHook(event, configured, options) {
|
|
|
1267
1478
|
return {
|
|
1268
1479
|
event,
|
|
1269
1480
|
...(entry.statusMessage !== undefined ? { statusMessage: entry.statusMessage } : {}),
|
|
1481
|
+
// A file named by the matcher is watched for as long as the hook is attached.
|
|
1482
|
+
...(event === 'file_changed' && fileChangedMatcherPaths(entry.matcher).length > 0
|
|
1483
|
+
? { watches: fileChangedMatcherPaths(entry.matcher) }
|
|
1484
|
+
: {}),
|
|
1270
1485
|
handler: async (context) => {
|
|
1271
|
-
if (spent)
|
|
1486
|
+
if (spent && event !== 'worktree_create' && event !== 'worktree_remove') {
|
|
1272
1487
|
return { action: 'continue' };
|
|
1488
|
+
}
|
|
1273
1489
|
// A notification is matched on its type; every other event on the tool.
|
|
1274
1490
|
const namesATool = !(event === 'pre_compact' ||
|
|
1275
1491
|
event === 'post_compact' ||
|
|
@@ -1296,17 +1512,48 @@ export function createShellHook(event, configured, options) {
|
|
|
1296
1512
|
// events are matched on `trigger`.
|
|
1297
1513
|
if (subject !== undefined && !shellHookMatches(entry.matcher, subject, { tool: namesATool }))
|
|
1298
1514
|
return { action: 'continue' };
|
|
1299
|
-
|
|
1515
|
+
// A changed file is matched on its base name, and a matcher written as a
|
|
1516
|
+
// path stands for the file's name: `config/app.json` is `app.json`.
|
|
1517
|
+
if (event === 'file_changed' && !fileChangedMatches(entry.matcher, context.filePath)) {
|
|
1300
1518
|
return { action: 'continue' };
|
|
1301
1519
|
}
|
|
1302
|
-
|
|
1520
|
+
// A worktree hook does the work itself, so a filter or a one-shot flag on
|
|
1521
|
+
// it could only make the kernel act where the person wanted the hook to:
|
|
1522
|
+
// both are ignored there, and the hook always runs.
|
|
1523
|
+
const doesTheWork = event === 'worktree_create' || event === 'worktree_remove';
|
|
1524
|
+
if (!doesTheWork &&
|
|
1525
|
+
!shellHookIfMatches(entry.if, context.toolName, context.toolInput, options.pathFilter)) {
|
|
1526
|
+
return { action: 'continue' };
|
|
1527
|
+
}
|
|
1528
|
+
if (entry.once === true && !doesTheWork)
|
|
1303
1529
|
spent = true;
|
|
1530
|
+
options.onRun?.({ event });
|
|
1304
1531
|
const transcriptPath = options.transcriptPath?.(String(context.sessionId));
|
|
1305
1532
|
const permissionMode = options.permissionMode?.();
|
|
1533
|
+
const envEvent = event === 'session_start' || event === 'cwd_changed' || event === 'file_changed'
|
|
1534
|
+
? event
|
|
1535
|
+
: undefined;
|
|
1536
|
+
const runsACommand = entry.type === undefined || entry.type === 'command';
|
|
1537
|
+
const envFile = envEvent !== undefined && runsACommand
|
|
1538
|
+
? options.hookEnv?.fileFor(envEvent, envSlot)
|
|
1539
|
+
: undefined;
|
|
1306
1540
|
const hookInput = {
|
|
1307
1541
|
event,
|
|
1308
|
-
|
|
1542
|
+
// A hook told the folder changed runs in the folder entered.
|
|
1543
|
+
cwd: event === 'cwd_changed' && context.newCwd !== undefined ? context.newCwd : options.cwd,
|
|
1544
|
+
...(envFile !== undefined ? { envFile } : {}),
|
|
1545
|
+
...(context.filePath !== undefined ? { filePath: context.filePath } : {}),
|
|
1546
|
+
...(context.change !== undefined ? { change: context.change } : {}),
|
|
1547
|
+
...(context.oldCwd !== undefined ? { oldCwd: context.oldCwd } : {}),
|
|
1548
|
+
...(context.newCwd !== undefined ? { newCwd: context.newCwd } : {}),
|
|
1549
|
+
...(context.worktreeName !== undefined ? { worktreeName: context.worktreeName } : {}),
|
|
1550
|
+
...(context.worktreeRoot !== undefined ? { worktreeRoot: context.worktreeRoot } : {}),
|
|
1551
|
+
...(context.worktreeBaseRef !== undefined
|
|
1552
|
+
? { worktreeBaseRef: context.worktreeBaseRef }
|
|
1553
|
+
: {}),
|
|
1554
|
+
...(context.worktreePath !== undefined ? { worktreePath: context.worktreePath } : {}),
|
|
1309
1555
|
...(options.projectDir !== undefined ? { projectDir: options.projectDir } : {}),
|
|
1556
|
+
...(options.plugin !== undefined ? { plugin: options.plugin } : {}),
|
|
1310
1557
|
sessionId: String(context.sessionId),
|
|
1311
1558
|
...(context.source !== undefined ? { sessionSource: context.source } : {}),
|
|
1312
1559
|
...(context.endReason !== undefined ? { endReason: context.endReason } : {}),
|
|
@@ -1345,11 +1592,16 @@ export function createShellHook(event, configured, options) {
|
|
|
1345
1592
|
: {}),
|
|
1346
1593
|
...(transcriptPath !== undefined ? { transcriptPath } : {}),
|
|
1347
1594
|
...(permissionMode !== undefined ? { permissionMode } : {}),
|
|
1595
|
+
...(options.extraEnv !== undefined ? { extraEnv: options.extraEnv } : {}),
|
|
1596
|
+
...(options.skillRoot !== undefined ? { skillRoot: options.skillRoot } : {}),
|
|
1348
1597
|
};
|
|
1349
1598
|
const verdictCall = {
|
|
1350
1599
|
...(context.toolName !== undefined ? { toolName: context.toolName } : {}),
|
|
1351
1600
|
...(context.toolInput !== undefined ? { toolInput: context.toolInput } : {}),
|
|
1352
1601
|
};
|
|
1602
|
+
if (event === 'worktree_create' || event === 'worktree_remove') {
|
|
1603
|
+
return runWorktreeHook(event, entry, hookInput, context.signal, reportTimeout);
|
|
1604
|
+
}
|
|
1353
1605
|
if (entry.type === 'http') {
|
|
1354
1606
|
const response = await postHookJson({
|
|
1355
1607
|
url: entry.url,
|
|
@@ -1377,6 +1629,8 @@ export function createShellHook(event, configured, options) {
|
|
|
1377
1629
|
// for it and its own failure is only written down.
|
|
1378
1630
|
void runShellHook(entry, hookInput)
|
|
1379
1631
|
.then((outcome) => {
|
|
1632
|
+
if (envEvent !== undefined)
|
|
1633
|
+
options.hookEnv?.absorb(envEvent, envSlot);
|
|
1380
1634
|
const verdict = shellHookVerdict(event, entry, outcome, log, verdictCall);
|
|
1381
1635
|
if (verdict.action !== 'continue') {
|
|
1382
1636
|
log.warn('an async hook answered, and its answer is not applied', {
|
|
@@ -1390,6 +1644,10 @@ export function createShellHook(event, configured, options) {
|
|
|
1390
1644
|
return { action: 'continue' };
|
|
1391
1645
|
}
|
|
1392
1646
|
const outcome = await runShellHook(entry, hookInput, context.signal);
|
|
1647
|
+
// What the hook appended counts even when it was ended or failed after
|
|
1648
|
+
// writing it: the lines are data it already gave.
|
|
1649
|
+
if (envEvent !== undefined)
|
|
1650
|
+
options.hookEnv?.absorb(envEvent, envSlot);
|
|
1393
1651
|
reportTimeout(outcome.timedOut);
|
|
1394
1652
|
return shellHookVerdict(event, entry, outcome, log, verdictCall);
|
|
1395
1653
|
},
|