@namzu/sdk 63.1.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 (200) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/dist/agents/QueryAgent.d.ts.map +1 -1
  3. package/dist/agents/QueryAgent.js +2 -1
  4. package/dist/agents/QueryAgent.js.map +1 -1
  5. package/dist/agents/file-definitions.d.ts +10 -0
  6. package/dist/agents/file-definitions.d.ts.map +1 -1
  7. package/dist/agents/file-definitions.js +10 -2
  8. package/dist/agents/file-definitions.js.map +1 -1
  9. package/dist/compaction/hooks.d.ts +57 -0
  10. package/dist/compaction/hooks.d.ts.map +1 -0
  11. package/dist/compaction/hooks.js +44 -0
  12. package/dist/compaction/hooks.js.map +1 -0
  13. package/dist/compaction/manual.d.ts +9 -0
  14. package/dist/compaction/manual.d.ts.map +1 -1
  15. package/dist/compaction/manual.js +74 -5
  16. package/dist/compaction/manual.js.map +1 -1
  17. package/dist/compaction/model-summary.d.ts +6 -0
  18. package/dist/compaction/model-summary.d.ts.map +1 -1
  19. package/dist/compaction/model-summary.js +13 -3
  20. package/dist/compaction/model-summary.js.map +1 -1
  21. package/dist/compaction/verifier.d.ts +8 -2
  22. package/dist/compaction/verifier.d.ts.map +1 -1
  23. package/dist/compaction/verifier.js +23 -7
  24. package/dist/compaction/verifier.js.map +1 -1
  25. package/dist/plugin/file-change-hooks.d.ts +53 -0
  26. package/dist/plugin/file-change-hooks.d.ts.map +1 -0
  27. package/dist/plugin/file-change-hooks.js +111 -0
  28. package/dist/plugin/file-change-hooks.js.map +1 -0
  29. package/dist/plugin/file-change-watch.d.ts +111 -0
  30. package/dist/plugin/file-change-watch.d.ts.map +1 -0
  31. package/dist/plugin/file-change-watch.js +324 -0
  32. package/dist/plugin/file-change-watch.js.map +1 -0
  33. package/dist/plugin/hook-context.d.ts +16 -0
  34. package/dist/plugin/hook-context.d.ts.map +1 -1
  35. package/dist/plugin/hook-context.js +40 -0
  36. package/dist/plugin/hook-context.js.map +1 -1
  37. package/dist/plugin/index.d.ts +14 -4
  38. package/dist/plugin/index.d.ts.map +1 -1
  39. package/dist/plugin/index.js +10 -3
  40. package/dist/plugin/index.js.map +1 -1
  41. package/dist/plugin/lifecycle.d.ts +60 -1
  42. package/dist/plugin/lifecycle.d.ts.map +1 -1
  43. package/dist/plugin/lifecycle.js +118 -7
  44. package/dist/plugin/lifecycle.js.map +1 -1
  45. package/dist/plugin/loader.d.ts.map +1 -1
  46. package/dist/plugin/loader.js +24 -1
  47. package/dist/plugin/loader.js.map +1 -1
  48. package/dist/plugin/session-hook-env.d.ts +140 -0
  49. package/dist/plugin/session-hook-env.d.ts.map +1 -0
  50. package/dist/plugin/session-hook-env.js +458 -0
  51. package/dist/plugin/session-hook-env.js.map +1 -0
  52. package/dist/plugin/shell-hook-command.d.ts +25 -0
  53. package/dist/plugin/shell-hook-command.d.ts.map +1 -0
  54. package/dist/plugin/shell-hook-command.js +218 -0
  55. package/dist/plugin/shell-hook-command.js.map +1 -0
  56. package/dist/plugin/shell-hook-env.d.ts +107 -0
  57. package/dist/plugin/shell-hook-env.d.ts.map +1 -0
  58. package/dist/plugin/shell-hook-env.js +272 -0
  59. package/dist/plugin/shell-hook-env.js.map +1 -0
  60. package/dist/plugin/shell-hook-tool-names.d.ts +20 -0
  61. package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -0
  62. package/dist/plugin/shell-hook-tool-names.js +43 -0
  63. package/dist/plugin/shell-hook-tool-names.js.map +1 -0
  64. package/dist/plugin/shell-hook.d.ts +167 -22
  65. package/dist/plugin/shell-hook.d.ts.map +1 -1
  66. package/dist/plugin/shell-hook.js +651 -106
  67. package/dist/plugin/shell-hook.js.map +1 -1
  68. package/dist/plugin/worktree-hook-answer.d.ts +26 -0
  69. package/dist/plugin/worktree-hook-answer.d.ts.map +1 -0
  70. package/dist/plugin/worktree-hook-answer.js +132 -0
  71. package/dist/plugin/worktree-hook-answer.js.map +1 -0
  72. package/dist/public-runtime.d.ts +5 -3
  73. package/dist/public-runtime.d.ts.map +1 -1
  74. package/dist/public-runtime.js +4 -3
  75. package/dist/public-runtime.js.map +1 -1
  76. package/dist/public-types.d.ts +9 -2
  77. package/dist/public-types.d.ts.map +1 -1
  78. package/dist/runtime/query/compact-session.d.ts +7 -1
  79. package/dist/runtime/query/compact-session.d.ts.map +1 -1
  80. package/dist/runtime/query/compact-session.js +8 -3
  81. package/dist/runtime/query/compact-session.js.map +1 -1
  82. package/dist/runtime/query/executor/tool-call-admission.d.ts +1 -1
  83. package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
  84. package/dist/runtime/query/executor/tool-call-admission.js +4 -3
  85. package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
  86. package/dist/runtime/query/executor.d.ts +5 -0
  87. package/dist/runtime/query/executor.d.ts.map +1 -1
  88. package/dist/runtime/query/executor.js +8 -3
  89. package/dist/runtime/query/executor.js.map +1 -1
  90. package/dist/runtime/query/finalize-turn.d.ts.map +1 -1
  91. package/dist/runtime/query/finalize-turn.js +9 -2
  92. package/dist/runtime/query/finalize-turn.js.map +1 -1
  93. package/dist/runtime/query/index.d.ts +9 -0
  94. package/dist/runtime/query/index.d.ts.map +1 -1
  95. package/dist/runtime/query/index.js +3 -0
  96. package/dist/runtime/query/index.js.map +1 -1
  97. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  98. package/dist/runtime/query/iteration/index.js +2 -1
  99. package/dist/runtime/query/iteration/index.js.map +1 -1
  100. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  101. package/dist/runtime/query/iteration/phases/compaction.js +24 -12
  102. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  103. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  104. package/dist/runtime/query/iteration/phases/tool-review.js +2 -0
  105. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  106. package/dist/runtime/query/plugin-hooks.d.ts +13 -0
  107. package/dist/runtime/query/plugin-hooks.d.ts.map +1 -1
  108. package/dist/runtime/query/plugin-hooks.js +29 -2
  109. package/dist/runtime/query/plugin-hooks.js.map +1 -1
  110. package/dist/runtime/query/tooling.d.ts +5 -0
  111. package/dist/runtime/query/tooling.d.ts.map +1 -1
  112. package/dist/runtime/query/tooling.js +3 -0
  113. package/dist/runtime/query/tooling.js.map +1 -1
  114. package/dist/session/workspace/hooked-driver.d.ts +72 -0
  115. package/dist/session/workspace/hooked-driver.d.ts.map +1 -0
  116. package/dist/session/workspace/hooked-driver.js +204 -0
  117. package/dist/session/workspace/hooked-driver.js.map +1 -0
  118. package/dist/session/workspace/index.d.ts +2 -0
  119. package/dist/session/workspace/index.d.ts.map +1 -1
  120. package/dist/session/workspace/index.js +1 -0
  121. package/dist/session/workspace/index.js.map +1 -1
  122. package/dist/skills/index.d.ts +1 -1
  123. package/dist/skills/index.d.ts.map +1 -1
  124. package/dist/skills/index.js +1 -1
  125. package/dist/skills/index.js.map +1 -1
  126. package/dist/skills/loader.d.ts +22 -0
  127. package/dist/skills/loader.d.ts.map +1 -1
  128. package/dist/skills/loader.js +41 -0
  129. package/dist/skills/loader.js.map +1 -1
  130. package/dist/tools/builtins/bash-cwd.d.ts +6 -1
  131. package/dist/tools/builtins/bash-cwd.d.ts.map +1 -1
  132. package/dist/tools/builtins/bash-cwd.js +27 -2
  133. package/dist/tools/builtins/bash-cwd.js.map +1 -1
  134. package/dist/tools/builtins/bash.js +1 -1
  135. package/dist/tools/builtins/bash.js.map +1 -1
  136. package/dist/tools/builtins/skill.d.ts.map +1 -1
  137. package/dist/tools/builtins/skill.js +93 -16
  138. package/dist/tools/builtins/skill.js.map +1 -1
  139. package/dist/types/agent/query.d.ts +8 -0
  140. package/dist/types/agent/query.d.ts.map +1 -1
  141. package/dist/types/plugin/index.d.ts +130 -13
  142. package/dist/types/plugin/index.d.ts.map +1 -1
  143. package/dist/types/plugin/index.js +5 -0
  144. package/dist/types/plugin/index.js.map +1 -1
  145. package/dist/types/skills/index.d.ts +8 -0
  146. package/dist/types/skills/index.d.ts.map +1 -1
  147. package/dist/types/skills/index.js.map +1 -1
  148. package/dist/types/tool/index.d.ts +68 -0
  149. package/dist/types/tool/index.d.ts.map +1 -1
  150. package/dist/types/tool/index.js.map +1 -1
  151. package/dist/types/workspace/ref.d.ts +6 -0
  152. package/dist/types/workspace/ref.d.ts.map +1 -1
  153. package/dist/utils/frontmatter.d.ts +16 -0
  154. package/dist/utils/frontmatter.d.ts.map +1 -1
  155. package/dist/utils/frontmatter.js +31 -1
  156. package/dist/utils/frontmatter.js.map +1 -1
  157. package/package.json +1 -1
  158. package/src/agents/QueryAgent.ts +2 -1
  159. package/src/agents/file-definitions.ts +17 -2
  160. package/src/compaction/hooks.ts +104 -0
  161. package/src/compaction/manual.ts +117 -4
  162. package/src/compaction/model-summary.ts +19 -3
  163. package/src/compaction/verifier.ts +33 -3
  164. package/src/plugin/file-change-hooks.ts +131 -0
  165. package/src/plugin/file-change-watch.ts +367 -0
  166. package/src/plugin/hook-context.ts +46 -0
  167. package/src/plugin/index.ts +53 -1
  168. package/src/plugin/lifecycle.ts +162 -8
  169. package/src/plugin/loader.ts +22 -1
  170. package/src/plugin/session-hook-env.ts +475 -0
  171. package/src/plugin/shell-hook-command.ts +215 -0
  172. package/src/plugin/shell-hook-env.ts +335 -0
  173. package/src/plugin/shell-hook-tool-names.ts +46 -0
  174. package/src/plugin/shell-hook.ts +818 -100
  175. package/src/plugin/worktree-hook-answer.ts +142 -0
  176. package/src/public-runtime.ts +44 -0
  177. package/src/public-types.ts +22 -1
  178. package/src/runtime/query/compact-session.ts +16 -4
  179. package/src/runtime/query/executor/tool-call-admission.ts +10 -2
  180. package/src/runtime/query/executor.ts +13 -2
  181. package/src/runtime/query/finalize-turn.ts +9 -2
  182. package/src/runtime/query/index.ts +12 -0
  183. package/src/runtime/query/iteration/index.ts +2 -1
  184. package/src/runtime/query/iteration/phases/compaction.ts +31 -12
  185. package/src/runtime/query/iteration/phases/tool-review.ts +2 -0
  186. package/src/runtime/query/plugin-hooks.ts +32 -2
  187. package/src/runtime/query/tooling.ts +8 -0
  188. package/src/session/workspace/hooked-driver.ts +272 -0
  189. package/src/session/workspace/index.ts +7 -0
  190. package/src/skills/index.ts +3 -0
  191. package/src/skills/loader.ts +47 -0
  192. package/src/tools/builtins/bash-cwd.ts +25 -1
  193. package/src/tools/builtins/bash.ts +1 -1
  194. package/src/tools/builtins/skill.ts +115 -26
  195. package/src/types/agent/query.ts +9 -0
  196. package/src/types/plugin/index.ts +150 -23
  197. package/src/types/skills/index.ts +9 -0
  198. package/src/types/tool/index.ts +74 -0
  199. package/src/types/workspace/ref.ts +6 -0
  200. package/src/utils/frontmatter.ts +52 -1
@@ -5,7 +5,7 @@ import { parseAllowedTools } from '../../authorization/skill-grant.js'
5
5
  import { forkedSkillReportNote } from '../../compaction/loaded-skills.js'
6
6
  import type { Sandbox } from '../../types/sandbox/index.js'
7
7
  import { isInvocableBy, skillInvocation } from '../../types/skills/index.js'
8
- import type { ToolContext } from '../../types/tool/index.js'
8
+ import type { SkillHooksPlan, ToolContext } from '../../types/tool/index.js'
9
9
  import { defineTool } from '../defineTool.js'
10
10
 
11
11
  export { parseAllowedTools }
@@ -337,6 +337,11 @@ export function createSkillTool(options: SkillToolOptions = {}) {
337
337
  readOnly: true,
338
338
  destructive: false,
339
339
  concurrencySafe: true,
340
+ // A skill that carries hooks attaches them when it is used. A call the
341
+ // model wrote after it in the same message must run under them, so the
342
+ // calls before settle first and the calls after wait: message order is
343
+ // the order they run in.
344
+ executionBarrier: true,
340
345
 
341
346
  // The body is instructions for the model, often a hundred lines; the
342
347
  // person needs the row that says which skill was read, not the text.
@@ -496,28 +501,43 @@ export function createSkillTool(options: SkillToolOptions = {}) {
496
501
  let forkUnavailable = ''
497
502
  if (skill.metadata.context === 'fork' && input.cursor === undefined) {
498
503
  if (context.dispatchTool && context.toolRegistry?.has(DELEGATION_TOOL_NAME)) {
499
- return runForkedSkill({
504
+ const forkHeader = resolveModelDirectory
505
+ ? directoryHeader(
506
+ nonEmpty(
507
+ await resolveModelDirectory(
508
+ { name: input.name, directory: skill.dirPath },
509
+ directoryContextOf(context.sandbox),
510
+ ),
511
+ ),
512
+ )
513
+ : ''
514
+ // The delegated prompt is known before the agent starts: the host
515
+ // recognises the agent that receives it, among any others that
516
+ // start at the same time, and binds the skill's hooks to that one.
517
+ const forkPrompt = forkedPrompt(forkHeader, argsLine, skill.body)
518
+ const forkHooks = hooksPlanFor(skill, context, true, forkPrompt)
519
+ const forkResult = await runForkedSkill({
500
520
  name: input.name,
501
521
  skill,
502
- argsLine,
522
+ prompt: forkPrompt,
503
523
  dispatch: context.dispatchTool,
504
- header: resolveModelDirectory
505
- ? directoryHeader(
506
- nonEmpty(
507
- await resolveModelDirectory(
508
- { name: input.name, directory: skill.dirPath },
509
- directoryContextOf(context.sandbox),
510
- ),
511
- ),
512
- )
513
- : '',
524
+ hooks: forkHooks,
514
525
  })
526
+ return forkHooks?.notice
527
+ ? { ...forkResult, output: `[${forkHooks.notice}]\n\n${forkResult.output}` }
528
+ : forkResult
515
529
  }
516
530
  forkUnavailable =
517
531
  '[This skill is written to run as a delegated agent, but this turn cannot delegate. Follow its instructions yourself.]\n\n'
518
532
  }
519
533
 
520
- const allowed = parseAllowedTools(skill.metadata.allowedTools)
534
+ // A skill that carries hooks is read by the host first. When a hook
535
+ // that guards its work cannot be loaded, the skill's pre-approval is
536
+ // withheld, whatever else it says.
537
+ const hooksPlan = hooksPlanFor(skill, context, false)
538
+ const allowed = hooksPlan?.withholdGrants
539
+ ? undefined
540
+ : parseAllowedTools(skill.metadata.allowedTools)
521
541
  // Asked on every call, first page or continuation: the digest binds
522
542
  // the answer, so a mount that changed between pages is a stale cursor
523
543
  // rather than a second half written for a different directory.
@@ -572,7 +592,9 @@ export function createSkillTool(options: SkillToolOptions = {}) {
572
592
  ...(snapshot.skillDirectory ? { skillDirectory: snapshot.skillDirectory } : {}),
573
593
  })
574
594
  }
575
- const notice = grantNotice(allowed, grant, context.grantSkillTools !== undefined)
595
+ const notice =
596
+ (hooksPlan?.notice ? `\n\n[${hooksPlan.notice}]` : '') +
597
+ grantNotice(allowed, grant, context.grantSkillTools !== undefined)
576
598
  const page = pageSkillBody({
577
599
  snapshot:
578
600
  start === 0 && argsLine ? { ...snapshot, header: argsLine + snapshot.header } : snapshot,
@@ -589,6 +611,8 @@ export function createSkillTool(options: SkillToolOptions = {}) {
589
611
  }
590
612
  }
591
613
  grant?.commit()
614
+ // Live only now that the instructions were delivered, like the grant.
615
+ hooksPlan?.attach()
592
616
 
593
617
  return {
594
618
  success: true,
@@ -605,6 +629,48 @@ export function createSkillTool(options: SkillToolOptions = {}) {
605
629
  })
606
630
  }
607
631
 
632
+ /**
633
+ * What the host says of the hooks a skill carries, or `undefined` for a skill
634
+ * that carries none. A registry that cannot read hooks answers with a plan
635
+ * that attaches nothing and withholds the pre-approval: the skill still
636
+ * loads, and the model is told in one line why its commands are not
637
+ * pre-approved.
638
+ */
639
+ function hooksPlanFor(
640
+ skill: {
641
+ readonly metadata: { readonly name: string; readonly hooksSource?: string }
642
+ readonly dirPath?: string
643
+ },
644
+ context: ToolContext,
645
+ forked: boolean,
646
+ /** For a forked skill: the prompt the delegated agent is given. */
647
+ prompt?: string,
648
+ ): SkillHooksPlan | undefined {
649
+ const hooksSource = skill.metadata.hooksSource
650
+ if (hooksSource === undefined) return undefined
651
+ const prepare = context.skills?.prepareHooks
652
+ if (!prepare || !context.skills) {
653
+ return {
654
+ notice: `Skill ${skill.metadata.name} carries hooks, but this session cannot run a skill's hooks. They are not attached and its allowed-tools are not pre-approved.`,
655
+ withholdGrants: true,
656
+ attach: () => ({ remove: () => undefined }),
657
+ }
658
+ }
659
+ return prepare.call(
660
+ context.skills,
661
+ {
662
+ name: skill.metadata.name,
663
+ ...(skill.dirPath !== undefined ? { dirPath: skill.dirPath } : {}),
664
+ hooksSource,
665
+ },
666
+ {
667
+ forked,
668
+ ...(forked && prompt !== undefined ? { prompt } : {}),
669
+ ...(context.sessionId !== undefined ? { sessionId: String(context.sessionId) } : {}),
670
+ },
671
+ )
672
+ }
673
+
608
674
  /** The `skill` tool with no host options: the listing carries the registry's `location`. */
609
675
  export const SkillTool = createSkillTool()
610
676
 
@@ -667,6 +733,11 @@ function grantNotice(
667
733
  return `\n\n[${lines.join(' ')}]`
668
734
  }
669
735
 
736
+ /** The prompt a forked skill's delegated agent is given. */
737
+ function forkedPrompt(header: string, argsLine: string, body: string | undefined): string {
738
+ return `${header}${argsLine}${body ?? '(this skill has no body)'}`
739
+ }
740
+
670
741
  /**
671
742
  * Run a `context: fork` skill through the turn's delegation tool.
672
743
  *
@@ -687,19 +758,37 @@ async function runForkedSkill(input: {
687
758
  }
688
759
  readonly body?: string
689
760
  }
690
- readonly argsLine: string
691
- readonly header: string
761
+ /** The prompt the delegated agent is given; see {@link forkedPrompt}. */
762
+ readonly prompt: string
692
763
  readonly dispatch: NonNullable<ToolContext['dispatchTool']>
764
+ /** The skill's hooks, made live for the delegated agent's run only. */
765
+ readonly hooks?: SkillHooksPlan
693
766
  }) {
694
- const { metadata, body } = input.skill
695
- const result = await input.dispatch(DELEGATION_TOOL_NAME, {
696
- description: `Skill ${input.name}`,
697
- prompt: `${input.header}${input.argsLine}${body ?? '(this skill has no body)'}`,
698
- ...(metadata.agent === undefined ? {} : { subagent_type: metadata.agent }),
699
- ...(metadata.model === undefined ? {} : { model: metadata.model }),
700
- ...(metadata.effort === undefined ? {} : { effort: metadata.effort }),
701
- ...(metadata.background === undefined ? {} : { run_in_background: metadata.background }),
702
- })
767
+ const { metadata } = input.skill
768
+ const attached = input.hooks?.attach()
769
+ let ended = false
770
+ const end = (): void => {
771
+ if (ended) return
772
+ ended = true
773
+ attached?.remove()
774
+ }
775
+ let result: Awaited<ReturnType<typeof input.dispatch>>
776
+ try {
777
+ result = await input.dispatch(DELEGATION_TOOL_NAME, {
778
+ description: `Skill ${input.name}`,
779
+ prompt: input.prompt,
780
+ ...(metadata.agent === undefined ? {} : { subagent_type: metadata.agent }),
781
+ ...(metadata.model === undefined ? {} : { model: metadata.model }),
782
+ ...(metadata.effort === undefined ? {} : { effort: metadata.effort }),
783
+ ...(metadata.background === undefined ? {} : { run_in_background: metadata.background }),
784
+ })
785
+ } catch (error) {
786
+ end()
787
+ throw error
788
+ }
789
+ // A foreground agent is over when the dispatch returns. A background one is
790
+ // not: the host that bound the hooks to it ends them when it ends.
791
+ if (metadata.background !== true || !result.success) end()
703
792
  return {
704
793
  ...result,
705
794
  // Says what the text is, so a later pass never carries a report as the skill's steps.
@@ -25,6 +25,15 @@ export interface QueryAgentConfig extends BaseAgentConfig {
25
25
 
26
26
  persona?: AgentPersona
27
27
 
28
+ /**
29
+ * The name lifecycle hooks see for this agent (`agentType` on
30
+ * `subagent_start` and `subagent_stop`, `agent_type` for a shell hook).
31
+ * Absent, it is the agent's `type`, which for a host-defined class is the
32
+ * class's marker rather than the agent's name: a host that names its
33
+ * agents passes the name here.
34
+ */
35
+ agentType?: string
36
+
28
37
  /**
29
38
  * Channel a host uses to hand guidance to this turn's current turn.
30
39
  *
@@ -158,6 +158,30 @@ export type PluginHookEvent =
158
158
  * settled. Carries `notification` (`type` and `message`). Observational.
159
159
  */
160
160
  | 'notification'
161
+ /**
162
+ * A file a hook asked to watch changed on disk. Carries `filePath` and
163
+ * `change`. Fired by the host once the file has been still for a moment, so a
164
+ * burst of writes is one event. A hook cannot refuse anything here; it may
165
+ * hand back a new list of paths to watch (`watchPaths`).
166
+ */
167
+ | 'file_changed'
168
+ /**
169
+ * The folder the session's shell commands run in changed. Carries `oldCwd`
170
+ * and `newCwd`. Fired by the host, never when the two are the same folder.
171
+ */
172
+ | 'cwd_changed'
173
+ /**
174
+ * A worktree is about to be created. Carries `worktreeName` (the suggested
175
+ * name) and `worktreeRoot` (where a relative answer is placed). A hook that
176
+ * answers with a path (`worktreePath`) makes the worktree itself; the host
177
+ * then creates none.
178
+ */
179
+ | 'worktree_create'
180
+ /**
181
+ * A worktree is about to be removed. Carries `worktreePath`. A hook on it
182
+ * does the removing; the host then removes nothing.
183
+ */
184
+ | 'worktree_remove'
161
185
  | 'pre_llm_call'
162
186
  | 'post_llm_call'
163
187
  | 'iteration_start'
@@ -197,6 +221,10 @@ export function assertPluginHookEvent(event: PluginHookEvent): void {
197
221
  case 'permission_denied':
198
222
  case 'permission_request':
199
223
  case 'notification':
224
+ case 'file_changed':
225
+ case 'cwd_changed':
226
+ case 'worktree_create':
227
+ case 'worktree_remove':
200
228
  case 'pre_llm_call':
201
229
  case 'post_llm_call':
202
230
  case 'iteration_start':
@@ -246,12 +274,36 @@ export interface PluginModelResponse {
246
274
 
247
275
  /** What a compaction hook is shown about the pass. */
248
276
  export interface PluginCompactionInfo {
249
- /** `threshold`: the estimate crossed the line. `overflow`: the provider rejected the prompt. */
250
- readonly reason: 'threshold' | 'overflow'
277
+ /**
278
+ * `threshold`: the estimate crossed the line. `overflow`: the provider
279
+ * rejected the prompt. `manual`: a host asked for the pass outside a turn
280
+ * (a person's `/compact`, a summarised span, an idle pass); test `trigger`
281
+ * to tell a person's request from the kernel's own.
282
+ */
283
+ readonly reason: 'threshold' | 'overflow' | 'manual'
251
284
  readonly tokensBefore: number
252
285
  /** Present on `post_compact`. */
253
286
  readonly tokensAfter?: number
254
287
  readonly contextWindowTokens: number
288
+ /**
289
+ * Who started the pass: `manual` for one a person asked for (`/compact`,
290
+ * summarising a span), `auto` for every pass the kernel or a host started
291
+ * on its own (the threshold, a provider overflow, an idle pass). The
292
+ * kernel always sets it; it is optional only so a host that builds this
293
+ * context by hand keeps compiling. A matcher on these two events tests it.
294
+ */
295
+ readonly trigger?: 'manual' | 'auto'
296
+ /**
297
+ * On `pre_compact`: what the person typed with the command, or `null` when
298
+ * they typed nothing or the pass is not one a person asked for.
299
+ */
300
+ readonly instructions?: string | null
301
+ /**
302
+ * On `post_compact`: the summary the pass wrote, cut to
303
+ * `MAX_COMPACTION_SUMMARY_HOOK_CHARS`. Absent when the pass wrote none (a
304
+ * context reducer shed the history, or the pass shed nothing).
305
+ */
306
+ readonly summary?: string
255
307
  }
256
308
 
257
309
  /** One call in a `post_tool_batch` hook's context. */
@@ -264,6 +316,9 @@ export interface PluginToolBatchCall {
264
316
  readonly isError: boolean
265
317
  }
266
318
 
319
+ /** What happened to a watched file, on `file_changed`. */
320
+ export type PluginFileChange = 'change' | 'add' | 'unlink'
321
+
267
322
  /** What a `notification` hook is shown. */
268
323
  export interface PluginNotificationInfo {
269
324
  /**
@@ -275,19 +330,22 @@ export interface PluginNotificationInfo {
275
330
  }
276
331
 
277
332
  /**
278
- * Why a session started, on `session_start`: `startup` for a conversation
333
+ * Why a conversation began, on `session_start`: `startup` for a conversation
279
334
  * with no earlier turn, `resume` for one that already had turns (a resumed
280
- * conversation, a `--continue`, or a session rebuilt under a running one).
335
+ * conversation, a `--continue`, a fork, or a session rebuilt under a running
336
+ * one), `clear` for the empty conversation a host opens when the person
337
+ * clears the old one (`/clear`, `/new`).
281
338
  */
282
- export type PluginSessionSource = 'startup' | 'resume'
339
+ export type PluginSessionSource = 'startup' | 'resume' | 'clear'
283
340
 
284
341
  /**
285
- * Why a session closed, on `session_end`: `exit` when the host is finishing
286
- * with it (the process ends, the person quits), `replaced` when the host
287
- * builds another session in its place (a model or account switch, another
288
- * conversation opened).
342
+ * Why a conversation ended, on `session_end`: `exit` when the host is
343
+ * finishing with it (the process ends, the person quits), `replaced` when the
344
+ * host moves to another session or conversation in its place (a model or
345
+ * account switch, another conversation opened or forked), `clear` when the
346
+ * person cleared it and a new, empty conversation begins (`/clear`, `/new`).
289
347
  */
290
- export type PluginSessionEndReason = 'exit' | 'replaced'
348
+ export type PluginSessionEndReason = 'exit' | 'replaced' | 'clear'
291
349
 
292
350
  export interface PluginHookContext {
293
351
  /** The turn the hook fired in. Absent on `session_start` and `session_end`. */
@@ -308,9 +366,33 @@ export interface PluginHookContext {
308
366
  readonly endReason?: PluginSessionEndReason
309
367
  /** The operator's prompt, on `user_prompt_submit`. */
310
368
  readonly prompt?: string
369
+ /** The file that changed, absolute, on `file_changed`. */
370
+ readonly filePath?: string
371
+ /** What happened to `filePath`, on `file_changed`. */
372
+ readonly change?: PluginFileChange
373
+ /** The folder commands ran in before, on `cwd_changed`. */
374
+ readonly oldCwd?: string
375
+ /** The folder commands run in now, on `cwd_changed`. */
376
+ readonly newCwd?: string
377
+ /** The suggested name of the worktree, on `worktree_create`. */
378
+ readonly worktreeName?: string
379
+ /** The folder a relative worktree path is placed under, on `worktree_create`. */
380
+ readonly worktreeRoot?: string
381
+ /** The commit or branch the worktree should start from, on `worktree_create`, when one was asked for. */
382
+ readonly worktreeBaseRef?: string
383
+ /** The worktree to remove, on `worktree_remove`. */
384
+ readonly worktreePath?: string
311
385
  /** The pass, on `pre_compact` and `post_compact`. */
312
386
  readonly compaction?: PluginCompactionInfo
313
387
  readonly toolName?: string
388
+ /**
389
+ * The id of the call, on `pre_tool_use`, `post_tool_use`,
390
+ * `post_tool_use_failure`, `permission_request` and `permission_denied`: the
391
+ * same id `post_tool_batch` carries as `toolCallId`, so a hook can pair a
392
+ * before with an after. Absent on a call a tool makes through
393
+ * `dispatchTool` (it has no model-issued id).
394
+ */
395
+ readonly toolCallId?: string
314
396
  readonly toolInput?: unknown
315
397
  readonly toolResult?: ToolResult
316
398
  /** The calls of one model step with their outcomes, on `post_tool_batch`. */
@@ -328,6 +410,13 @@ export interface PluginHookContext {
328
410
  * keep the model working until the turn's own limits stop it.
329
411
  */
330
412
  readonly stopHookActive?: boolean
413
+ /**
414
+ * On `turn_end` and `subagent_stop`: the text of the model's last message
415
+ * of the turn, cut to `MAX_LAST_MESSAGE_HOOK_CHARS`. Absent when the turn
416
+ * produced no text (it ended on a tool call, was interrupted before the
417
+ * model wrote anything, or failed).
418
+ */
419
+ readonly lastAssistantMessage?: string
331
420
  readonly iteration?: number
332
421
  /**
333
422
  * Why the turn was stopped, on `turn_interrupt`.
@@ -409,6 +498,17 @@ export interface PluginHookNotes {
409
498
  readonly halt?: { readonly reason: string }
410
499
  /** A warning for the operator, reported through the logger. */
411
500
  readonly notice?: string
501
+ /**
502
+ * On `file_changed`, `cwd_changed` and `session_start`: the files the host
503
+ * should watch from now on, replacing the list hooks gave before. Other
504
+ * events ignore it.
505
+ */
506
+ readonly watchPaths?: readonly string[]
507
+ /**
508
+ * On `worktree_create`: the folder the hook made. The host uses it as the
509
+ * worktree and creates none. Other events ignore it.
510
+ */
511
+ readonly worktreePath?: string
412
512
  }
413
513
 
414
514
  export type PluginHookResult =
@@ -424,7 +524,12 @@ export type PluginHookResult =
424
524
  * on the prompt. Accepted on that event only: a tool hook that returns
425
525
  * it is rejected, because a tool result is not a place for context.
426
526
  */
427
- | { action: 'annotate'; text: string }
527
+ | {
528
+ action: 'annotate'
529
+ text: string
530
+ watchPaths?: readonly string[]
531
+ notice?: string
532
+ }
428
533
  | ({ action: 'skip'; reason: string } & PluginHookNotes)
429
534
  | ({
430
535
  action: 'modify'
@@ -501,18 +606,22 @@ export type PluginHookResultFor<E extends PluginHookEvent> = E extends 'user_pro
501
606
  ? Extract<PluginHookResult, { action: 'continue' | 'error' }>
502
607
  : E extends 'permission_request'
503
608
  ? Extract<PluginHookResult, { action: 'continue' | 'skip' }>
504
- : E extends 'turn_end' | 'subagent_stop' | 'pre_compact'
505
- ? Extract<PluginHookResult, { action: 'continue' | 'skip' | 'error' }>
506
- : E extends 'session_start'
507
- ? Extract<PluginHookResult, { action: 'continue' | 'annotate' }>
508
- : E extends
509
- | 'turn_interrupt'
510
- | 'session_end'
511
- | 'notification'
512
- | 'turn_failure'
513
- | 'permission_denied'
514
- ? Extract<PluginHookResult, { action: 'continue' }>
515
- : Extract<PluginHookResult, { action: 'continue' | 'error' }>
609
+ : E extends 'pre_compact'
610
+ ? Extract<PluginHookResult, { action: 'continue' | 'annotate' | 'skip' | 'error' }>
611
+ : E extends 'turn_end' | 'subagent_stop'
612
+ ? Extract<PluginHookResult, { action: 'continue' | 'skip' | 'error' }>
613
+ : E extends 'session_start'
614
+ ? Extract<PluginHookResult, { action: 'continue' | 'annotate' }>
615
+ : E extends
616
+ | 'turn_interrupt'
617
+ | 'session_end'
618
+ | 'notification'
619
+ | 'turn_failure'
620
+ | 'permission_denied'
621
+ | 'file_changed'
622
+ | 'cwd_changed'
623
+ ? Extract<PluginHookResult, { action: 'continue' }>
624
+ : Extract<PluginHookResult, { action: 'continue' | 'error' }>
516
625
 
517
626
  /** The event and its handler verdict stay correlated, including in hook arrays. */
518
627
  export type PluginHookDefinition<E extends PluginHookEvent = PluginHookEvent> =
@@ -536,6 +645,12 @@ export type PluginHookDefinition<E extends PluginHookEvent = PluginHookEvent> =
536
645
  * ignores it.
537
646
  */
538
647
  readonly statusMessage?: string
648
+ /**
649
+ * On a `file_changed` hook: the paths its matcher names, which the host
650
+ * watches for as long as the hook is attached. Listed by
651
+ * `PluginLifecycleManager.listHooks`.
652
+ */
653
+ readonly watches?: readonly string[]
539
654
  }
540
655
  : never
541
656
 
@@ -565,7 +680,18 @@ export interface PluginManifest {
565
680
  readonly instructions?: string
566
681
  readonly tools?: readonly string[]
567
682
  readonly skills?: readonly string[]
683
+ /**
684
+ * Paths inside the plugin folder. A path that ends in `.json` names a file of
685
+ * shell, http or prompt hook entries (read by the host, see `hookEntries`);
686
+ * any other path is a JavaScript module exporting a `hooks` array.
687
+ */
568
688
  readonly hooks?: readonly string[]
689
+ /**
690
+ * Shell, http or prompt hook entries written inline: the same events object a
691
+ * hooks file holds (event name to a list of entries or groups). The runtime
692
+ * keeps it as written; the host that owns shell hooks reads it.
693
+ */
694
+ readonly hookEntries?: Readonly<Record<string, unknown>>
569
695
  readonly mcpServers?: readonly PluginMCPServerConfig[]
570
696
  readonly connectors?: readonly string[]
571
697
  readonly personas?: readonly string[]
@@ -620,6 +746,7 @@ export const PluginManifestSchema = z.object({
620
746
  tools: z.array(z.string()).max(MAX_TOOLS_PER_PLUGIN).optional(),
621
747
  skills: z.array(z.string()).max(MAX_SKILLS_PER_PLUGIN).optional(),
622
748
  hooks: z.array(z.string()).max(MAX_HOOKS_PER_PLUGIN).optional(),
749
+ hookEntries: z.record(z.string(), z.unknown()).optional(),
623
750
  mcpServers: z.array(PluginMCPServerConfigSchema).max(MAX_MCP_SERVERS_PER_PLUGIN).optional(),
624
751
  connectors: z.array(z.string()).max(MAX_CONNECTORS_PER_PLUGIN).optional(),
625
752
  personas: z.array(z.string()).max(MAX_PERSONAS_PER_PLUGIN).optional(),
@@ -72,6 +72,15 @@ export interface SkillMetadata {
72
72
 
73
73
  /** Whether a `fork` skill's agent runs in the background (`background`). */
74
74
  background?: boolean
75
+
76
+ /**
77
+ * The `hooks` block of the front matter, as the file wrote it (the key's
78
+ * line and the lines under it). The kernel does not read it: a host that
79
+ * runs hooks parses it with its own YAML reader, because the block is a
80
+ * list of mappings in a mapping, which the front matter reader holds only
81
+ * as text. Absent when the file has no `hooks` key or it is empty.
82
+ */
83
+ hooksSource?: string
75
84
  }
76
85
 
77
86
  /**
@@ -99,6 +99,8 @@ export interface SkillRegistryRef {
99
99
  model?: string
100
100
  effort?: import('../provider/chat.js').ReasoningEffort
101
101
  background?: boolean
102
+ /** The `hooks` block as the file wrote it; see `SkillMetadata.hooksSource`. */
103
+ hooksSource?: string
102
104
  }
103
105
  body?: string
104
106
  /** The skill's directory, which `${CLAUDE_SKILL_DIR}` in `allowed-tools` names. */
@@ -109,6 +111,65 @@ export interface SkillRegistryRef {
109
111
  >
110
112
  /** Every registered name, for a "did you mean" that names real options. */
111
113
  names(): readonly string[]
114
+ /**
115
+ * Read the hooks a skill carries, without attaching anything.
116
+ *
117
+ * Called by the `skill` tool for a skill whose front matter has a `hooks`
118
+ * block. Hooks are not a thing the kernel reads: the host that runs them
119
+ * answers. The answer says what to tell the model, whether the skill's
120
+ * `allowed-tools` may still be pre-approved, and, as `attach`, the one
121
+ * call that makes the hooks live. `attach` is called only once the skill's
122
+ * instructions have been delivered, like the tool grant's `commit`, and
123
+ * returns a handle that removes exactly what it attached.
124
+ *
125
+ * Absent, a skill that carries hooks loads without them, and without its
126
+ * `allowed-tools` pre-approval: a skill written to run its commands under
127
+ * its own guard must not run them unguarded and pre-approved.
128
+ */
129
+ prepareHooks?(
130
+ skill: {
131
+ readonly name: string
132
+ readonly dirPath?: string
133
+ readonly hooksSource: string
134
+ },
135
+ options: SkillHooksOptions,
136
+ ): SkillHooksPlan
137
+ }
138
+
139
+ /** What the `skill` tool tells a host's hook reader about the use it is making of a skill. */
140
+ export interface SkillHooksOptions {
141
+ /** The skill runs in a delegated agent: its hooks belong to that agent's run only. */
142
+ readonly forked: boolean
143
+ /**
144
+ * For a forked skill: the prompt the delegated agent is given, so the host
145
+ * can tell that agent from any other that starts at the same time.
146
+ */
147
+ readonly prompt?: string
148
+ /** The session the `skill` tool was called in. */
149
+ readonly sessionId?: string
150
+ }
151
+
152
+ /** What a host decided about the hooks a skill carries; see {@link SkillRegistryRef.prepareHooks}. */
153
+ export interface SkillHooksPlan {
154
+ /** One line for the model, and for the person, about the hooks. Empty when there is nothing to say. */
155
+ readonly notice?: string
156
+ /**
157
+ * The skill's `allowed-tools` must not be pre-approved: some hook that
158
+ * guards its work could not be loaded, or hooks cannot be attached here.
159
+ */
160
+ readonly withholdGrants: boolean
161
+ /** Make the hooks live. Idempotent for a skill already attached to the conversation. */
162
+ attach(): SkillHooksHandle
163
+ }
164
+
165
+ /** What {@link SkillHooksPlan.attach} gives back. */
166
+ export interface SkillHooksHandle {
167
+ /**
168
+ * For a forked skill: bind the hooks to the delegated agent that is about
169
+ * to start, and end them when it ends. Absent for an inline skill, whose
170
+ * hooks last as long as the conversation.
171
+ */
172
+ readonly remove: () => void
112
173
  }
113
174
 
114
175
  /**
@@ -547,6 +608,19 @@ export interface ToolContext {
547
608
  /** The turn this call belongs to. */
548
609
  turnId: TurnId
549
610
  workingDirectory: string
611
+ /**
612
+ * Called by the shell tool, after a command has finished, when the folder the
613
+ * session's next shell command starts in is a different folder from the one
614
+ * this command started in (it ran `cd`, or was put back at the working
615
+ * directory for leaving the allowed folders). Awaited before the result is
616
+ * returned, so what a host does here is done before the next command. Never
617
+ * called with the same folder twice in a row, and not at all for a command
618
+ * that did not move.
619
+ */
620
+ workingDirectoryChanged?: (change: {
621
+ readonly from: string
622
+ readonly to: string
623
+ }) => Promise<void> | void
550
624
  /**
551
625
  * Directories besides the working directory the file tools may reach,
552
626
  * absolute. A host adds one for a session (`/add-dir`); a sandboxed turn
@@ -31,6 +31,12 @@ export interface GitWorktreeBackendMeta {
31
31
  worktreePath: string
32
32
  /** The commit the checkout started on; absent on refs made before it was recorded. */
33
33
  baseCommit?: string
34
+ /**
35
+ * Set when a `worktree_create` hook made the folder. It is the hook's: the
36
+ * kernel never runs its own Git removal on it, it asks the `worktree_remove`
37
+ * hooks and, with none, leaves the folder where it is.
38
+ */
39
+ hookOwned?: true
34
40
  }
35
41
 
36
42
  /**