@namzu/sdk 71.1.0 → 73.0.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 +55 -0
- package/dist/agents/QueryAgent.d.ts.map +1 -1
- package/dist/agents/QueryAgent.js +1 -0
- package/dist/agents/QueryAgent.js.map +1 -1
- package/dist/agents/examples/SupervisorAgent.d.ts.map +1 -1
- package/dist/agents/examples/SupervisorAgent.js +5 -0
- package/dist/agents/examples/SupervisorAgent.js.map +1 -1
- package/dist/authorization/protected-paths.d.ts.map +1 -1
- package/dist/authorization/protected-paths.js +4 -0
- package/dist/authorization/protected-paths.js.map +1 -1
- package/dist/compaction/index.d.ts +1 -1
- package/dist/compaction/index.d.ts.map +1 -1
- package/dist/compaction/index.js +1 -1
- package/dist/compaction/index.js.map +1 -1
- package/dist/compaction/manual.d.ts +24 -1
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +80 -21
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/model-summary.d.ts +24 -6
- package/dist/compaction/model-summary.d.ts.map +1 -1
- package/dist/compaction/model-summary.js +108 -23
- package/dist/compaction/model-summary.js.map +1 -1
- package/dist/compaction/precompute.d.ts +179 -0
- package/dist/compaction/precompute.d.ts.map +1 -0
- package/dist/compaction/precompute.js +274 -0
- package/dist/compaction/precompute.js.map +1 -0
- package/dist/compaction/restore.d.ts +23 -0
- package/dist/compaction/restore.d.ts.map +1 -1
- package/dist/compaction/restore.js +59 -1
- package/dist/compaction/restore.js.map +1 -1
- package/dist/compaction/summary.d.ts +37 -2
- package/dist/compaction/summary.d.ts.map +1 -1
- package/dist/compaction/summary.js +46 -6
- package/dist/compaction/summary.js.map +1 -1
- package/dist/config/runtime.d.ts +4 -0
- package/dist/config/runtime.d.ts.map +1 -1
- package/dist/config/runtime.js +20 -3
- package/dist/config/runtime.js.map +1 -1
- package/dist/connector/index.d.ts +2 -2
- package/dist/connector/index.d.ts.map +1 -1
- package/dist/connector/index.js +1 -1
- package/dist/connector/index.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +26 -6
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +49 -7
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/connector/mcp/index.d.ts +2 -0
- package/dist/connector/mcp/index.d.ts.map +1 -1
- package/dist/connector/mcp/index.js +1 -0
- package/dist/connector/mcp/index.js.map +1 -1
- package/dist/connector/mcp/sandbox-stdio.d.ts +67 -0
- package/dist/connector/mcp/sandbox-stdio.d.ts.map +1 -0
- package/dist/connector/mcp/sandbox-stdio.js +178 -0
- package/dist/connector/mcp/sandbox-stdio.js.map +1 -0
- package/dist/connector/mcp/stdio-program.d.ts +34 -0
- package/dist/connector/mcp/stdio-program.d.ts.map +1 -0
- package/dist/connector/mcp/stdio-program.js +47 -0
- package/dist/connector/mcp/stdio-program.js.map +1 -0
- package/dist/connector/mcp/stdio.d.ts.map +1 -1
- package/dist/connector/mcp/stdio.js +5 -2
- package/dist/connector/mcp/stdio.js.map +1 -1
- package/dist/connector/web/guarded-fetch.d.ts +51 -2
- package/dist/connector/web/guarded-fetch.d.ts.map +1 -1
- package/dist/connector/web/guarded-fetch.js +140 -19
- package/dist/connector/web/guarded-fetch.js.map +1 -1
- package/dist/connector/web/index.d.ts +2 -2
- package/dist/connector/web/index.d.ts.map +1 -1
- package/dist/connector/web/index.js +1 -1
- package/dist/connector/web/index.js.map +1 -1
- package/dist/manager/agent/lifecycle.d.ts.map +1 -1
- package/dist/manager/agent/lifecycle.js +9 -1
- package/dist/manager/agent/lifecycle.js.map +1 -1
- package/dist/manager/session/turn-recorder.d.ts +26 -0
- package/dist/manager/session/turn-recorder.d.ts.map +1 -1
- package/dist/manager/session/turn-recorder.js +72 -5
- package/dist/manager/session/turn-recorder.js.map +1 -1
- package/dist/peers/protocol.d.ts +9 -0
- package/dist/peers/protocol.d.ts.map +1 -1
- package/dist/peers/protocol.js +4 -0
- package/dist/peers/protocol.js.map +1 -1
- package/dist/peers/record.d.ts +6 -0
- package/dist/peers/record.d.ts.map +1 -1
- package/dist/peers/record.js +4 -0
- package/dist/peers/record.js.map +1 -1
- package/dist/plugin/hook-module/capabilities.d.ts.map +1 -1
- package/dist/plugin/hook-module/capabilities.js +24 -13
- package/dist/plugin/hook-module/capabilities.js.map +1 -1
- package/dist/plugin/hook-module/host.d.ts +31 -0
- package/dist/plugin/hook-module/host.d.ts.map +1 -1
- package/dist/plugin/hook-module/host.js +56 -13
- package/dist/plugin/hook-module/host.js.map +1 -1
- package/dist/plugin/hook-module/index.d.ts +3 -2
- package/dist/plugin/hook-module/index.d.ts.map +1 -1
- package/dist/plugin/hook-module/index.js +2 -1
- package/dist/plugin/hook-module/index.js.map +1 -1
- package/dist/plugin/index.d.ts +3 -2
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +2 -1
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/shell-hook-execute.d.ts +55 -0
- package/dist/plugin/shell-hook-execute.d.ts.map +1 -0
- package/dist/plugin/shell-hook-execute.js +73 -0
- package/dist/plugin/shell-hook-execute.js.map +1 -0
- package/dist/plugin/shell-hook-restrict.d.ts +31 -0
- package/dist/plugin/shell-hook-restrict.d.ts.map +1 -0
- package/dist/plugin/shell-hook-restrict.js +69 -0
- package/dist/plugin/shell-hook-restrict.js.map +1 -0
- package/dist/plugin/shell-hook.d.ts +59 -2
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +79 -12
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/public-runtime.d.ts +11 -7
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +9 -6
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +1 -1
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +1 -1
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +5 -2
- package/dist/public-types.d.ts.map +1 -1
- package/dist/registry/command/kernel-commands.d.ts +7 -1
- package/dist/registry/command/kernel-commands.d.ts.map +1 -1
- package/dist/registry/command/kernel-commands.js +40 -2
- package/dist/registry/command/kernel-commands.js.map +1 -1
- package/dist/registry/tool/execute.d.ts +8 -0
- package/dist/registry/tool/execute.d.ts.map +1 -1
- package/dist/registry/tool/execute.js +12 -0
- package/dist/registry/tool/execute.js.map +1 -1
- package/dist/runtime/jobs/websocket-source.d.ts +2 -22
- package/dist/runtime/jobs/websocket-source.d.ts.map +1 -1
- package/dist/runtime/jobs/websocket-source.js +2 -33
- package/dist/runtime/jobs/websocket-source.js.map +1 -1
- package/dist/runtime/query/budget-nudge.d.ts +27 -0
- package/dist/runtime/query/budget-nudge.d.ts.map +1 -0
- package/dist/runtime/query/budget-nudge.js +66 -0
- package/dist/runtime/query/budget-nudge.js.map +1 -0
- package/dist/runtime/query/compact-session.d.ts +9 -0
- package/dist/runtime/query/compact-session.d.ts.map +1 -1
- package/dist/runtime/query/compact-session.js +158 -76
- package/dist/runtime/query/compact-session.js.map +1 -1
- package/dist/runtime/query/events.d.ts +7 -0
- package/dist/runtime/query/events.d.ts.map +1 -1
- package/dist/runtime/query/events.js +29 -11
- package/dist/runtime/query/events.js.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.d.ts.map +1 -1
- package/dist/runtime/query/executor/tool-call-admission.js +51 -10
- package/dist/runtime/query/executor/tool-call-admission.js.map +1 -1
- package/dist/runtime/query/guard.d.ts +8 -0
- package/dist/runtime/query/guard.d.ts.map +1 -1
- package/dist/runtime/query/guard.js +20 -0
- package/dist/runtime/query/guard.js.map +1 -1
- package/dist/runtime/query/index.d.ts +6 -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/iteration/index.d.ts +2 -0
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +27 -1
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.d.ts +1 -0
- package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.js +164 -43
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +4 -0
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js.map +1 -1
- package/dist/runtime/query/iteration/phases/precompute.d.ts +60 -0
- package/dist/runtime/query/iteration/phases/precompute.d.ts.map +1 -0
- package/dist/runtime/query/iteration/phases/precompute.js +240 -0
- package/dist/runtime/query/iteration/phases/precompute.js.map +1 -0
- package/dist/runtime/query/turn/outcome-ledger.d.ts +14 -0
- package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.js +28 -0
- package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
- package/dist/scheduler/completion-inbox.d.ts.map +1 -1
- package/dist/scheduler/completion-inbox.js +15 -0
- package/dist/scheduler/completion-inbox.js.map +1 -1
- package/dist/store/goal/gates.d.ts +53 -0
- package/dist/store/goal/gates.d.ts.map +1 -0
- package/dist/store/goal/gates.js +664 -0
- package/dist/store/goal/gates.js.map +1 -0
- package/dist/store/goal/index.d.ts +16 -1
- package/dist/store/goal/index.d.ts.map +1 -1
- package/dist/store/goal/index.js +74 -0
- package/dist/store/goal/index.js.map +1 -1
- package/dist/store/session-index/index.d.ts +13 -0
- package/dist/store/session-index/index.d.ts.map +1 -1
- package/dist/store/session-index/index.js +1 -0
- package/dist/store/session-index/index.js.map +1 -1
- package/dist/store/session-index/rebuild.d.ts +12 -0
- package/dist/store/session-index/rebuild.d.ts.map +1 -1
- package/dist/store/session-index/rebuild.js +20 -4
- package/dist/store/session-index/rebuild.js.map +1 -1
- package/dist/store/session-index/sqlite.d.ts +20 -0
- package/dist/store/session-index/sqlite.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.js +50 -12
- package/dist/store/session-index/sqlite.js.map +1 -1
- package/dist/tools/admit-facts.d.ts.map +1 -1
- package/dist/tools/admit-facts.js +59 -0
- package/dist/tools/admit-facts.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/browser.d.ts.map +1 -1
- package/dist/tools/builtins/browser.js +2 -0
- package/dist/tools/builtins/browser.js.map +1 -1
- package/dist/tools/builtins/monitor.d.ts.map +1 -1
- package/dist/tools/builtins/monitor.js +1 -0
- package/dist/tools/builtins/monitor.js.map +1 -1
- package/dist/tools/builtins/search-tools.js +2 -2
- package/dist/tools/builtins/search-tools.js.map +1 -1
- package/dist/tools/builtins/sleep-guard.js +1 -1
- package/dist/tools/builtins/sleep-guard.js.map +1 -1
- package/dist/tools/builtins/wait-for-job.js +1 -1
- package/dist/tools/builtins/wait-for-job.js.map +1 -1
- package/dist/tools/coordinator/ask-user-question.d.ts.map +1 -1
- package/dist/tools/coordinator/ask-user-question.js +92 -3
- package/dist/tools/coordinator/ask-user-question.js.map +1 -1
- package/dist/tools/coordinator/index.d.ts.map +1 -1
- package/dist/tools/coordinator/index.js +3 -0
- package/dist/tools/coordinator/index.js.map +1 -1
- package/dist/tools/defineTool.d.ts +2 -0
- package/dist/tools/defineTool.d.ts.map +1 -1
- package/dist/tools/defineTool.js +3 -0
- package/dist/tools/defineTool.js.map +1 -1
- package/dist/tools/goal/confirmation.d.ts +40 -2
- package/dist/tools/goal/confirmation.d.ts.map +1 -1
- package/dist/tools/goal/confirmation.js +117 -0
- package/dist/tools/goal/confirmation.js.map +1 -1
- package/dist/tools/goal/index.d.ts.map +1 -1
- package/dist/tools/goal/index.js +14 -2
- package/dist/tools/goal/index.js.map +1 -1
- package/dist/tools/memory/save.d.ts.map +1 -1
- package/dist/tools/memory/save.js +1 -0
- package/dist/tools/memory/save.js.map +1 -1
- package/dist/tools/schedules/index.d.ts +1 -0
- package/dist/tools/schedules/index.d.ts.map +1 -1
- package/dist/tools/schedules/index.js +1 -0
- package/dist/tools/schedules/index.js.map +1 -1
- package/dist/tools/schedules/loop-tool.d.ts.map +1 -1
- package/dist/tools/schedules/loop-tool.js +1 -0
- package/dist/tools/schedules/loop-tool.js.map +1 -1
- package/dist/tools/schedules/present.d.ts.map +1 -1
- package/dist/tools/schedules/present.js +2 -0
- package/dist/tools/schedules/present.js.map +1 -1
- package/dist/tools/schedules/schedule-tool.d.ts.map +1 -1
- package/dist/tools/schedules/schedule-tool.js +79 -9
- package/dist/tools/schedules/schedule-tool.js.map +1 -1
- package/dist/tools/schedules/self-tool.d.ts +16 -0
- package/dist/tools/schedules/self-tool.d.ts.map +1 -0
- package/dist/tools/schedules/self-tool.js +140 -0
- package/dist/tools/schedules/self-tool.js.map +1 -0
- package/dist/tools/schedules/timer-tools.d.ts.map +1 -1
- package/dist/tools/schedules/timer-tools.js +1 -0
- package/dist/tools/schedules/timer-tools.js.map +1 -1
- package/dist/tools/schedules/types.d.ts +63 -0
- package/dist/tools/schedules/types.d.ts.map +1 -1
- package/dist/toolsets/manager.d.ts +10 -0
- package/dist/toolsets/manager.d.ts.map +1 -1
- package/dist/toolsets/manager.js +56 -7
- package/dist/toolsets/manager.js.map +1 -1
- package/dist/turn/memory-promoter.d.ts.map +1 -1
- package/dist/turn/memory-promoter.js +20 -4
- package/dist/turn/memory-promoter.js.map +1 -1
- package/dist/types/agent/base.d.ts +6 -0
- package/dist/types/agent/base.d.ts.map +1 -1
- package/dist/types/agent/supervisor.d.ts +10 -0
- package/dist/types/agent/supervisor.d.ts.map +1 -1
- package/dist/types/agent/task.d.ts +11 -0
- package/dist/types/agent/task.d.ts.map +1 -1
- package/dist/types/agent/task.js.map +1 -1
- package/dist/types/goal/index.d.ts +66 -0
- package/dist/types/goal/index.d.ts.map +1 -1
- package/dist/types/hitl/index.d.ts +8 -0
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/message/index.d.ts +7 -0
- package/dist/types/message/index.d.ts.map +1 -1
- package/dist/types/message/index.js.map +1 -1
- package/dist/types/session/events.d.ts +7 -0
- package/dist/types/session/events.d.ts.map +1 -1
- package/dist/types/session/events.js.map +1 -1
- package/dist/types/session/outcome.d.ts +12 -0
- package/dist/types/session/outcome.d.ts.map +1 -1
- package/dist/types/session/outcome.js.map +1 -1
- package/dist/types/session/records.d.ts +79 -1
- package/dist/types/session/records.d.ts.map +1 -1
- package/dist/types/session/records.js +24 -0
- package/dist/types/session/records.js.map +1 -1
- package/dist/types/tool/index.d.ts +7 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/tool/presentation.d.ts +21 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/agents/QueryAgent.ts +1 -0
- package/src/agents/examples/SupervisorAgent.ts +5 -0
- package/src/authorization/protected-paths.ts +4 -0
- package/src/compaction/index.ts +1 -0
- package/src/compaction/manual.ts +130 -23
- package/src/compaction/model-summary.ts +118 -23
- package/src/compaction/precompute.ts +390 -0
- package/src/compaction/restore.ts +74 -1
- package/src/compaction/summary.ts +69 -3
- package/src/config/runtime.ts +20 -3
- package/src/connector/index.ts +3 -0
- package/src/connector/mcp/client.ts +49 -7
- package/src/connector/mcp/index.ts +5 -0
- package/src/connector/mcp/sandbox-stdio.ts +214 -0
- package/src/connector/mcp/stdio-program.ts +68 -0
- package/src/connector/mcp/stdio.ts +5 -2
- package/src/connector/web/guarded-fetch.ts +197 -30
- package/src/connector/web/index.ts +4 -1
- package/src/manager/agent/lifecycle.ts +10 -1
- package/src/manager/session/turn-recorder.ts +92 -5
- package/src/peers/protocol.ts +4 -0
- package/src/peers/record.ts +4 -0
- package/src/plugin/hook-module/capabilities.ts +36 -14
- package/src/plugin/hook-module/host.ts +86 -13
- package/src/plugin/hook-module/index.ts +3 -0
- package/src/plugin/index.ts +3 -0
- package/src/plugin/shell-hook-execute.ts +126 -0
- package/src/plugin/shell-hook-restrict.ts +66 -0
- package/src/plugin/shell-hook.ts +138 -10
- package/src/public-runtime.ts +41 -0
- package/src/public-tools.ts +2 -0
- package/src/public-types.ts +21 -0
- package/src/registry/command/kernel-commands.ts +62 -3
- package/src/registry/tool/execute.ts +14 -0
- package/src/runtime/jobs/websocket-source.ts +2 -46
- package/src/runtime/query/budget-nudge.ts +92 -0
- package/src/runtime/query/compact-session.ts +195 -70
- package/src/runtime/query/events.ts +29 -11
- package/src/runtime/query/executor/tool-call-admission.ts +62 -17
- package/src/runtime/query/guard.ts +22 -0
- package/src/runtime/query/index.ts +10 -0
- package/src/runtime/query/iteration/index.ts +26 -1
- package/src/runtime/query/iteration/phases/compaction.ts +173 -43
- package/src/runtime/query/iteration/phases/context.ts +6 -0
- package/src/runtime/query/iteration/phases/precompute.ts +277 -0
- package/src/runtime/query/turn/outcome-ledger.ts +34 -0
- package/src/scheduler/completion-inbox.ts +14 -0
- package/src/store/goal/gates.ts +738 -0
- package/src/store/goal/index.ts +109 -0
- package/src/store/session-index/index.ts +14 -0
- package/src/store/session-index/rebuild.ts +22 -4
- package/src/store/session-index/sqlite.ts +63 -14
- package/src/tools/admit-facts.ts +64 -0
- package/src/tools/builtins/bash.ts +1 -1
- package/src/tools/builtins/browser.ts +4 -0
- package/src/tools/builtins/monitor.ts +2 -0
- package/src/tools/builtins/search-tools.ts +2 -2
- package/src/tools/builtins/sleep-guard.ts +1 -1
- package/src/tools/builtins/wait-for-job.ts +1 -1
- package/src/tools/coordinator/ask-user-question.ts +124 -13
- package/src/tools/coordinator/index.ts +3 -0
- package/src/tools/defineTool.ts +5 -0
- package/src/tools/goal/confirmation.ts +171 -3
- package/src/tools/goal/index.ts +15 -3
- package/src/tools/memory/save.ts +2 -0
- package/src/tools/schedules/index.ts +1 -0
- package/src/tools/schedules/loop-tool.ts +2 -0
- package/src/tools/schedules/present.ts +2 -0
- package/src/tools/schedules/schedule-tool.ts +84 -9
- package/src/tools/schedules/self-tool.ts +152 -0
- package/src/tools/schedules/timer-tools.ts +2 -0
- package/src/tools/schedules/types.ts +62 -0
- package/src/toolsets/manager.ts +55 -8
- package/src/turn/memory-promoter.ts +24 -3
- package/src/types/agent/base.ts +7 -0
- package/src/types/agent/supervisor.ts +11 -0
- package/src/types/agent/task.ts +12 -0
- package/src/types/goal/index.ts +72 -0
- package/src/types/hitl/index.ts +8 -0
- package/src/types/message/index.ts +7 -0
- package/src/types/session/events.ts +7 -0
- package/src/types/session/outcome.ts +16 -1
- package/src/types/session/records.ts +27 -0
- package/src/types/tool/index.ts +7 -0
- package/src/types/tool/presentation.ts +22 -0
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
} from '../../types/plugin/index.js'
|
|
8
8
|
import { toErrorMessage } from '../../utils/error.js'
|
|
9
9
|
import type { Logger } from '../../utils/logger.js'
|
|
10
|
+
import { restrictHookVerdict } from '../shell-hook-restrict.js'
|
|
10
11
|
import {
|
|
11
12
|
type ShellHookCall,
|
|
12
13
|
type ShellHookEvent,
|
|
@@ -97,6 +98,27 @@ export const workerTransport: HookModuleTransportFactory = () => {
|
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
/**
|
|
102
|
+
* The nouns a module reaches the host through on `$`. A host that does not
|
|
103
|
+
* want a module to touch its files, processes or network names them in
|
|
104
|
+
* {@link StartHookModuleOptions.refuseCapabilities}.
|
|
105
|
+
*/
|
|
106
|
+
export type HookModuleCapability = 'process' | 'fs' | 'http' | 'store' | 'env'
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Every capability that reaches the machine the host runs on: programs
|
|
110
|
+
* (`process`), files (`fs`, and `store`, which is a file in the plugin's data
|
|
111
|
+
* folder), the network (`http`) and the host's variables (`env`). Leaves a
|
|
112
|
+
* module that computes and logs.
|
|
113
|
+
*/
|
|
114
|
+
export const HOST_REACHING_CAPABILITIES: readonly HookModuleCapability[] = Object.freeze([
|
|
115
|
+
'process',
|
|
116
|
+
'fs',
|
|
117
|
+
'http',
|
|
118
|
+
'store',
|
|
119
|
+
'env',
|
|
120
|
+
])
|
|
121
|
+
|
|
100
122
|
export interface StartHookModuleOptions {
|
|
101
123
|
readonly plugin: {
|
|
102
124
|
readonly name: string
|
|
@@ -119,6 +141,23 @@ export interface StartHookModuleOptions {
|
|
|
119
141
|
readonly catchMs?: number
|
|
120
142
|
readonly clock?: HookModuleClock
|
|
121
143
|
readonly transport?: HookModuleTransportFactory
|
|
144
|
+
/**
|
|
145
|
+
* Refuse a module whose scan declares a call on any of these nouns of `$`
|
|
146
|
+
* (`$.process.run`, `$.fs.read`, `$.http.fetch`, ...). The module is not
|
|
147
|
+
* loaded, its guards answer as a failed guard does, and `message` says which
|
|
148
|
+
* call and where. The bridge also refuses a call on these nouns whatever a
|
|
149
|
+
* worker sends, so a call the scan did not see still goes nowhere. Absent,
|
|
150
|
+
* nothing is refused. When `shell.execute` is set and no `bridge` is given,
|
|
151
|
+
* `process` is refused too, since the bridge would start the program on the host.
|
|
152
|
+
*/
|
|
153
|
+
readonly refuseCapabilities?: readonly HookModuleCapability[]
|
|
154
|
+
/**
|
|
155
|
+
* Answers the calls on `$` instead of the bridge made on this host, so a
|
|
156
|
+
* host that runs the worker (see {@link HOOK_WORKER_SOURCE} and
|
|
157
|
+
* `transport`) and its calls elsewhere owns both. Only the calls the
|
|
158
|
+
* module's scan declared, and `refuseCapabilities` does not name, reach it.
|
|
159
|
+
*/
|
|
160
|
+
readonly bridge?: CapabilityBridge
|
|
122
161
|
readonly environment?: Readonly<Record<string, string | undefined>>
|
|
123
162
|
readonly fetch?: typeof fetch
|
|
124
163
|
/** Called once, with the reason, when the module stops running for any cause but being disposed. */
|
|
@@ -358,18 +397,49 @@ export async function startHookModule(options: StartHookModuleOptions): Promise<
|
|
|
358
397
|
)
|
|
359
398
|
}
|
|
360
399
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
400
|
+
// What the host will not let a module reach: named by the host, and a program on the host when
|
|
401
|
+
// the host's own hooks are run elsewhere and no bridge of its own was given.
|
|
402
|
+
const refused = new Set<string>(options.refuseCapabilities ?? [])
|
|
403
|
+
if (options.shell.execute !== undefined && options.bridge === undefined) refused.add('process')
|
|
404
|
+
const refusedCalls = scan.calls.filter((call) => refused.has(call.call.split('.')[1] ?? ''))
|
|
405
|
+
if (refusedCalls.length > 0) {
|
|
406
|
+
const first = refusedCalls[0]
|
|
407
|
+
const more = refusedCalls.length > 1 ? ` (and ${refusedCalls.length - 1} more)` : ''
|
|
408
|
+
return unloaded(
|
|
409
|
+
withRecovery(
|
|
410
|
+
`hooks module ${label} not loaded: ${first?.file}:${first?.line}: ${first?.call} is a call this host does not allow a module${more}`,
|
|
411
|
+
),
|
|
412
|
+
guards,
|
|
413
|
+
)
|
|
414
|
+
}
|
|
415
|
+
const declaredCalls = scan.calls
|
|
416
|
+
.filter((call) => call.supported && !refused.has(call.call.split('.')[1] ?? ''))
|
|
417
|
+
.map((call) => call.call)
|
|
418
|
+
const own: CapabilityBridge | undefined = options.bridge
|
|
419
|
+
const bridge: CapabilityBridge =
|
|
420
|
+
own !== undefined
|
|
421
|
+
? {
|
|
422
|
+
call: async (noun, method, args) => {
|
|
423
|
+
// What the module's own scan declared is all its calls reach, whatever the worker sends.
|
|
424
|
+
if (!declaredCalls.includes(`$.${noun}.${method}`)) {
|
|
425
|
+
throw new Error(`$.${noun}.${method} is not among the calls this module declares`)
|
|
426
|
+
}
|
|
427
|
+
return own.call(noun, method, args)
|
|
428
|
+
},
|
|
429
|
+
endProcesses: () => own.endProcesses(),
|
|
430
|
+
}
|
|
431
|
+
: createCapabilityBridge({
|
|
432
|
+
root: plugin.root,
|
|
433
|
+
...(plugin.dataDir !== undefined ? { dataDir: plugin.dataDir } : {}),
|
|
434
|
+
projectDir: options.projectDir,
|
|
435
|
+
envNames: scan.envNames,
|
|
436
|
+
// What the module's own scan declared is all its calls reach, whatever the worker sends.
|
|
437
|
+
allowedCalls: declaredCalls,
|
|
438
|
+
...(options.environment !== undefined ? { environment: options.environment } : {}),
|
|
439
|
+
...(options.fetch !== undefined ? { fetch: options.fetch } : {}),
|
|
440
|
+
log,
|
|
441
|
+
pluginName: plugin.name,
|
|
442
|
+
})
|
|
373
443
|
|
|
374
444
|
// --- the worker --------------------------------------------------------
|
|
375
445
|
let transport: HookModuleTransport | undefined
|
|
@@ -671,13 +741,16 @@ export async function startHookModule(options: StartHookModuleOptions): Promise<
|
|
|
671
741
|
...(context.toolName !== undefined ? { toolName: context.toolName } : {}),
|
|
672
742
|
...(context.toolInput !== undefined ? { toolInput: context.toolInput } : {}),
|
|
673
743
|
}
|
|
674
|
-
|
|
744
|
+
const verdict = shellHookVerdict(
|
|
675
745
|
event,
|
|
676
746
|
{ type: 'command', command: commandLabel },
|
|
677
747
|
{ exitCode: 0, timedOut: false, stdout: JSON.stringify(json.json), stderr: '' },
|
|
678
748
|
log,
|
|
679
749
|
call,
|
|
680
750
|
)
|
|
751
|
+
return options.shell.restrictVerdicts === true
|
|
752
|
+
? restrictHookVerdict(verdict, log, { event, command: commandLabel })
|
|
753
|
+
: verdict
|
|
681
754
|
},
|
|
682
755
|
} as PluginHookDefinition)
|
|
683
756
|
}
|
|
@@ -27,10 +27,12 @@ export type {
|
|
|
27
27
|
export {
|
|
28
28
|
HOOK_MODULE_BUDGET_MS,
|
|
29
29
|
HOOK_MODULE_CATCH_MS,
|
|
30
|
+
HOST_REACHING_CAPABILITIES,
|
|
30
31
|
startHookModule,
|
|
31
32
|
workerTransport,
|
|
32
33
|
} from './host.js'
|
|
33
34
|
export type {
|
|
35
|
+
HookModuleCapability,
|
|
34
36
|
HookModuleClock,
|
|
35
37
|
HookModuleHandle,
|
|
36
38
|
HookModuleTransport,
|
|
@@ -52,3 +54,4 @@ export {
|
|
|
52
54
|
withHookModuleRecovery,
|
|
53
55
|
} from './consent.js'
|
|
54
56
|
export type { HookModuleConsentEntry, HookModuleSummary } from './consent.js'
|
|
57
|
+
export { HOOK_WORKER_SOURCE } from './worker-source.js'
|
package/src/plugin/index.ts
CHANGED
|
@@ -45,11 +45,13 @@ export {
|
|
|
45
45
|
SHELL_HOOK_PASCAL_EVENT_NAMES,
|
|
46
46
|
SHELL_HOOKS_PLUGIN_ID,
|
|
47
47
|
shellHookIfMatches,
|
|
48
|
+
shellHookEntryLimitProblem,
|
|
48
49
|
shellHookIfProblem,
|
|
49
50
|
shellHookMatcherProblem,
|
|
50
51
|
shellHookMatches,
|
|
51
52
|
shellHookVerdict,
|
|
52
53
|
} from './shell-hook.js'
|
|
54
|
+
export { restrictHookVerdict } from './shell-hook-restrict.js'
|
|
53
55
|
export {
|
|
54
56
|
assignedWithin,
|
|
55
57
|
isPluginFolderVariable,
|
|
@@ -79,6 +81,7 @@ export type {
|
|
|
79
81
|
ShellHookOnFailure,
|
|
80
82
|
ShellHookCall,
|
|
81
83
|
ShellHookInput,
|
|
84
|
+
ShellHookHandlerType,
|
|
82
85
|
ShellHookOptions,
|
|
83
86
|
ShellHookPathFilter,
|
|
84
87
|
ShellHookOutcome,
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running a hook command somewhere other than the host.
|
|
3
|
+
*
|
|
4
|
+
* A host that runs an agent's tools inside a sandbox cannot attach hooks that
|
|
5
|
+
* start as children of its own process: the hook would run outside the
|
|
6
|
+
* boundary the tools are held to. `ShellHookOptions.execute` is the seam. With
|
|
7
|
+
* it set, a command hook is handed to the caller's executor as a request, the
|
|
8
|
+
* executor runs it wherever the host keeps its tools, and what comes back
|
|
9
|
+
* becomes the hook's outcome. Nothing on this path starts a process in the host.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ShellHookOutcome } from './shell-hook.js'
|
|
13
|
+
|
|
14
|
+
/** One command hook, as an executor receives it. */
|
|
15
|
+
export interface ShellHookExecRequest {
|
|
16
|
+
/**
|
|
17
|
+
* The exec form (`args` present): the program. The shell form (`args`
|
|
18
|
+
* absent): shell text to run with `sh -c`, which is the executor's to run.
|
|
19
|
+
*/
|
|
20
|
+
readonly command: string
|
|
21
|
+
readonly args?: readonly string[]
|
|
22
|
+
/** The JSON object the hook reads on stdin. */
|
|
23
|
+
readonly stdin: string
|
|
24
|
+
/**
|
|
25
|
+
* The whole environment of the command. It is built from the hook's own
|
|
26
|
+
* variables and the host's `extraEnv`; the host process's environment is
|
|
27
|
+
* not in it.
|
|
28
|
+
*/
|
|
29
|
+
readonly env: Readonly<Record<string, string>>
|
|
30
|
+
/** The folder the hook is run for, as the host names it; the executor maps it if its world differs. */
|
|
31
|
+
readonly cwd: string
|
|
32
|
+
/** The hook's deadline. */
|
|
33
|
+
readonly timeoutMs: number
|
|
34
|
+
/** Aborted at the deadline or when the turn is cancelled; the executor must end the command. */
|
|
35
|
+
readonly signal: AbortSignal
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What an executor answers with once the command has ended. */
|
|
39
|
+
export interface ShellHookExecResult {
|
|
40
|
+
/** `null` when the command was ended by a signal. */
|
|
41
|
+
readonly exitCode: number | null
|
|
42
|
+
readonly stdout: string
|
|
43
|
+
readonly stderr: string
|
|
44
|
+
/** Set when the executor itself ended the command at the deadline. */
|
|
45
|
+
readonly timedOut?: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Runs one command hook for a host. A rejection is the hook failing to start
|
|
50
|
+
* (reported as such), never a reason to run it on the host instead.
|
|
51
|
+
*/
|
|
52
|
+
export type ShellHookExecutor = (request: ShellHookExecRequest) => Promise<ShellHookExecResult>
|
|
53
|
+
|
|
54
|
+
const messageOf = (error: unknown): string =>
|
|
55
|
+
error instanceof Error ? error.message : String(error)
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Run `request` through `execute`, bounded by the request's deadline and the
|
|
59
|
+
* capture cap. The deadline is enforced here too: an executor that ignores the
|
|
60
|
+
* signal still does not hold the hook open past its time.
|
|
61
|
+
*/
|
|
62
|
+
export function runThroughExecutor(
|
|
63
|
+
execute: ShellHookExecutor,
|
|
64
|
+
request: Omit<ShellHookExecRequest, 'signal'>,
|
|
65
|
+
cap: number,
|
|
66
|
+
signal: AbortSignal | undefined,
|
|
67
|
+
extra: Pick<ShellHookOutcome, 'unsetVariables'>,
|
|
68
|
+
): Promise<ShellHookOutcome> {
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
const controller = new AbortController()
|
|
71
|
+
let settled = false
|
|
72
|
+
const finish = (outcome: ShellHookOutcome): void => {
|
|
73
|
+
if (settled) return
|
|
74
|
+
settled = true
|
|
75
|
+
clearTimeout(timer)
|
|
76
|
+
signal?.removeEventListener('abort', onAbort)
|
|
77
|
+
controller.abort()
|
|
78
|
+
resolve({
|
|
79
|
+
...outcome,
|
|
80
|
+
...(extra.unsetVariables ? { unsetVariables: extra.unsetVariables } : {}),
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
const timer = setTimeout(
|
|
84
|
+
() => finish({ exitCode: null, timedOut: true, stdout: '', stderr: '' }),
|
|
85
|
+
request.timeoutMs,
|
|
86
|
+
)
|
|
87
|
+
const onAbort = (): void => controller.abort()
|
|
88
|
+
if (signal?.aborted) controller.abort()
|
|
89
|
+
else signal?.addEventListener('abort', onAbort, { once: true })
|
|
90
|
+
let running: Promise<ShellHookExecResult>
|
|
91
|
+
try {
|
|
92
|
+
running = execute({ ...request, signal: controller.signal })
|
|
93
|
+
} catch (error) {
|
|
94
|
+
finish({
|
|
95
|
+
exitCode: null,
|
|
96
|
+
timedOut: false,
|
|
97
|
+
stdout: '',
|
|
98
|
+
stderr: '',
|
|
99
|
+
spawnError: messageOf(error),
|
|
100
|
+
})
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
running.then(
|
|
104
|
+
(result) => {
|
|
105
|
+
const out = typeof result?.stdout === 'string' ? result.stdout : ''
|
|
106
|
+
const err = typeof result?.stderr === 'string' ? result.stderr : ''
|
|
107
|
+
const code = result?.exitCode
|
|
108
|
+
finish({
|
|
109
|
+
exitCode: typeof code === 'number' && Number.isInteger(code) ? code : null,
|
|
110
|
+
timedOut: result?.timedOut === true,
|
|
111
|
+
stdout: out.slice(0, cap),
|
|
112
|
+
stderr: err.slice(0, cap),
|
|
113
|
+
...(out.length > cap ? { stdoutTruncated: true } : {}),
|
|
114
|
+
})
|
|
115
|
+
},
|
|
116
|
+
(error) =>
|
|
117
|
+
finish({
|
|
118
|
+
exitCode: null,
|
|
119
|
+
timedOut: false,
|
|
120
|
+
stdout: '',
|
|
121
|
+
stderr: '',
|
|
122
|
+
spawnError: messageOf(error),
|
|
123
|
+
}),
|
|
124
|
+
)
|
|
125
|
+
})
|
|
126
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Limiting what a hook's answer may do.
|
|
3
|
+
*
|
|
4
|
+
* A host that cannot vouch for the code behind a hook may still want its
|
|
5
|
+
* refusals and its notes to the model. `ShellHookOptions.restrictVerdicts`
|
|
6
|
+
* keeps those and drops every answer that widens what the session may do:
|
|
7
|
+
*
|
|
8
|
+
* - an `allow` (a hook's `permissionDecision: allow`, `decision: approve`, a
|
|
9
|
+
* module's `allow: true`) stands in for no one's yes; `ask` is kept, since it
|
|
10
|
+
* only sends the call to a person;
|
|
11
|
+
* - `updatedInput` does not rewrite the call;
|
|
12
|
+
* - `retry` does not run a call again;
|
|
13
|
+
* - an elicitation answer does not stand in for the person's;
|
|
14
|
+
* - a worktree folder, files to watch and a retryable mark are not taken.
|
|
15
|
+
*
|
|
16
|
+
* Kept: `deny`, `block`, `skip`, `error`, `halt`, `additionalContext`
|
|
17
|
+
* (`context`, `annotate`), notices, and a replacement of a tool's result
|
|
18
|
+
* (which only narrows what the model reads; a guard that failed closed
|
|
19
|
+
* answers with one).
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { PluginHookResult } from '../types/plugin/index.js'
|
|
23
|
+
import type { Logger } from '../utils/logger.js'
|
|
24
|
+
|
|
25
|
+
/** What a restricted answer was stripped of, named for the log. */
|
|
26
|
+
function dropped(result: PluginHookResult): string[] {
|
|
27
|
+
const names: string[] = []
|
|
28
|
+
if (result.action === 'modify') names.push('updatedInput')
|
|
29
|
+
if (result.action === 'retry') names.push('retry')
|
|
30
|
+
if ('permission' in result && result.permission?.decision === 'allow') names.push('allow')
|
|
31
|
+
if ('elicitation' in result && result.elicitation !== undefined) names.push('elicitation')
|
|
32
|
+
if ('worktreePath' in result && result.worktreePath !== undefined) names.push('worktreePath')
|
|
33
|
+
if ('watchPaths' in result && result.watchPaths !== undefined) names.push('watchPaths')
|
|
34
|
+
if ('retryable' in result && result.retryable !== undefined) names.push('retryable')
|
|
35
|
+
return names
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* `result` as a restricted host accepts it. A verdict that only narrows is
|
|
40
|
+
* returned as it came; one that widens is returned without the widening part.
|
|
41
|
+
*/
|
|
42
|
+
export function restrictHookVerdict(
|
|
43
|
+
result: PluginHookResult,
|
|
44
|
+
log: Logger,
|
|
45
|
+
where: { readonly event: string; readonly command: string },
|
|
46
|
+
): PluginHookResult {
|
|
47
|
+
const names = dropped(result)
|
|
48
|
+
if (names.length === 0) return result
|
|
49
|
+
log.warn('a hook answer was narrowed by the host', {
|
|
50
|
+
'namzu.hook.event': where.event,
|
|
51
|
+
'namzu.hook.command': where.command,
|
|
52
|
+
'namzu.hook.dropped': names.join(','),
|
|
53
|
+
})
|
|
54
|
+
if (result.action === 'retry') return { action: 'continue' }
|
|
55
|
+
const { elicitation, worktreePath, watchPaths, retryable, ...rest } = result as PluginHookResult &
|
|
56
|
+
Record<string, unknown>
|
|
57
|
+
const kept = rest as Record<string, unknown>
|
|
58
|
+
if (kept.action === 'modify') {
|
|
59
|
+
kept.action = 'continue'
|
|
60
|
+
kept.input = undefined
|
|
61
|
+
}
|
|
62
|
+
const permission = kept.permission as { decision: string } | undefined
|
|
63
|
+
if (permission?.decision === 'allow') kept.permission = undefined
|
|
64
|
+
for (const key of Object.keys(kept)) if (kept[key] === undefined) delete kept[key]
|
|
65
|
+
return kept as PluginHookResult
|
|
66
|
+
}
|
package/src/plugin/shell-hook.ts
CHANGED
|
@@ -157,7 +157,9 @@ import {
|
|
|
157
157
|
shellPathOutsideEnv,
|
|
158
158
|
substituteMovedFolders,
|
|
159
159
|
} from './shell-hook-env.js'
|
|
160
|
+
import { type ShellHookExecutor, runThroughExecutor } from './shell-hook-execute.js'
|
|
160
161
|
import { postHookJson } from './shell-hook-http.js'
|
|
162
|
+
import { restrictHookVerdict } from './shell-hook-restrict.js'
|
|
161
163
|
import { type ShellHookToolNames, shellHookToolName } from './shell-hook-tool-names.js'
|
|
162
164
|
import { worktreeHookVerdict } from './worktree-hook-answer.js'
|
|
163
165
|
|
|
@@ -1004,11 +1006,18 @@ function isExecutableFile(path: string): boolean {
|
|
|
1004
1006
|
}
|
|
1005
1007
|
}
|
|
1006
1008
|
|
|
1007
|
-
/**
|
|
1009
|
+
/**
|
|
1010
|
+
* Run one hook command to completion, bounded by its deadline and capture.
|
|
1011
|
+
*
|
|
1012
|
+
* With `execute` the command goes to that executor and nothing is started in
|
|
1013
|
+
* this process: no fallback exists, and the host's own environment is not
|
|
1014
|
+
* handed over (the command gets the hook's variables and `extraEnv` only).
|
|
1015
|
+
*/
|
|
1008
1016
|
export function runShellHook(
|
|
1009
1017
|
entry: ShellHookEntry,
|
|
1010
1018
|
input: ShellHookInput,
|
|
1011
1019
|
signal?: AbortSignal,
|
|
1020
|
+
execute?: ShellHookExecutor,
|
|
1012
1021
|
): Promise<ShellHookOutcome> {
|
|
1013
1022
|
const timeoutMs = Math.min(
|
|
1014
1023
|
MAX_SHELL_HOOK_TIMEOUT_MS,
|
|
@@ -1023,7 +1032,7 @@ export function runShellHook(
|
|
|
1023
1032
|
NAMZU_TURN_ID: _inheritedTurnId,
|
|
1024
1033
|
NAMZU_ENV_FILE: _inheritedEnvFile,
|
|
1025
1034
|
...inherited
|
|
1026
|
-
} = process.env
|
|
1035
|
+
} = execute !== undefined ? ({} as NodeJS.ProcessEnv) : process.env
|
|
1027
1036
|
const projectDir = input.projectDir ?? input.cwd
|
|
1028
1037
|
const kernel = {
|
|
1029
1038
|
NAMZU_HOOK_EVENT: input.event,
|
|
@@ -1074,6 +1083,27 @@ export function runShellHook(
|
|
|
1074
1083
|
args: entry.args.map((arg) => substituteMovedFolders(arg, env)),
|
|
1075
1084
|
}
|
|
1076
1085
|
const unsetVariables = unset.length > 0 ? { unsetVariables: unset } : {}
|
|
1086
|
+
if (execute !== undefined) {
|
|
1087
|
+
// Nothing below this block runs: no host folder is made, no host process is started.
|
|
1088
|
+
const sent: Record<string, string> = {}
|
|
1089
|
+
for (const [name, value] of Object.entries(withSessionChildMarker(withSafeGitConfig(env)))) {
|
|
1090
|
+
if (typeof value === 'string') sent[name] = value
|
|
1091
|
+
}
|
|
1092
|
+
return runThroughExecutor(
|
|
1093
|
+
execute,
|
|
1094
|
+
{
|
|
1095
|
+
command: placed.command,
|
|
1096
|
+
...(placed.args !== undefined ? { args: placed.args } : {}),
|
|
1097
|
+
stdin: payload,
|
|
1098
|
+
env: sent,
|
|
1099
|
+
cwd: input.cwd,
|
|
1100
|
+
timeoutMs,
|
|
1101
|
+
},
|
|
1102
|
+
MAX_CAPTURE_BYTES,
|
|
1103
|
+
signal,
|
|
1104
|
+
unsetVariables,
|
|
1105
|
+
)
|
|
1106
|
+
}
|
|
1077
1107
|
if (pluginFolders !== undefined) {
|
|
1078
1108
|
try {
|
|
1079
1109
|
// Created when first needed; never inside the plugin's own folder.
|
|
@@ -2093,6 +2123,37 @@ export type ShellHookMcpToolCaller = (request: {
|
|
|
2093
2123
|
readonly signal: AbortSignal
|
|
2094
2124
|
}) => Promise<ShellHookMcpToolAnswer | undefined>
|
|
2095
2125
|
|
|
2126
|
+
/** A part `matcherForms: 'names'` accepts: a name, or a prefix with one `*` at the end only. */
|
|
2127
|
+
const NAME_ONLY_PART = /^[A-Za-z0-9_.:-]+\*?$|^\*$/
|
|
2128
|
+
|
|
2129
|
+
/** The types an entry can have (`type` absent reads as `command`). */
|
|
2130
|
+
export type ShellHookHandlerType = 'command' | 'http' | 'prompt' | 'mcp_tool'
|
|
2131
|
+
|
|
2132
|
+
/**
|
|
2133
|
+
* Why `entry` may not be registered under `options.handlerTypes` and
|
|
2134
|
+
* `options.matcherForms`, or `undefined` when it may. The wording names what
|
|
2135
|
+
* was refused and which option refused it.
|
|
2136
|
+
*/
|
|
2137
|
+
export function shellHookEntryLimitProblem(
|
|
2138
|
+
entry: Pick<ShellHookHandler, 'matcher'> & { readonly type?: ShellHookHandlerType },
|
|
2139
|
+
options: Pick<ShellHookOptions, 'handlerTypes' | 'matcherForms'>,
|
|
2140
|
+
): string | undefined {
|
|
2141
|
+
const type: ShellHookHandlerType = entry.type ?? 'command'
|
|
2142
|
+
if (options.handlerTypes !== undefined && !options.handlerTypes.includes(type)) {
|
|
2143
|
+
const allowed = options.handlerTypes.length > 0 ? options.handlerTypes.join(', ') : 'none'
|
|
2144
|
+
return `a ${type} hook is refused: this host accepts only ${allowed} hooks (handlerTypes), so the entry was not attached`
|
|
2145
|
+
}
|
|
2146
|
+
if (options.matcherForms === 'names') {
|
|
2147
|
+
const matcher = entry.matcher
|
|
2148
|
+
if (matcher === undefined || matcher.trim() === '' || matcher.trim() === '*') return undefined
|
|
2149
|
+
const parts = matcher.split(/[|,]/).map((part) => part.trim())
|
|
2150
|
+
if (!parts.every((part) => NAME_ONLY_PART.test(part)) || matcher.includes('.*')) {
|
|
2151
|
+
return `the matcher "${matcher}" is refused: this host accepts only a name, names joined by |, a prefix ending in * (mcp__server__*), empty or * (matcherForms: names), so the entry was not attached`
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
return undefined
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2096
2157
|
export interface ShellHookOptions {
|
|
2097
2158
|
/** Answers `prompt` hooks. Without it a `prompt` hook is logged and lets the action go ahead. */
|
|
2098
2159
|
readonly judge?: ShellHookJudge
|
|
@@ -2159,6 +2220,46 @@ export interface ShellHookOptions {
|
|
|
2159
2220
|
* Without it no hook gets the variable and nothing is carried.
|
|
2160
2221
|
*/
|
|
2161
2222
|
readonly hookEnv?: SessionHookEnv
|
|
2223
|
+
/**
|
|
2224
|
+
* Runs every command hook of this adapter somewhere other than this process
|
|
2225
|
+
* (a sandbox the host keeps its tools in). With it set, no hook command is
|
|
2226
|
+
* ever started as a child of the host, whatever the event: a request the
|
|
2227
|
+
* executor cannot serve fails the hook, it does not run on the host. The
|
|
2228
|
+
* command gets the hook's own variables and `extraEnv`, not the host's
|
|
2229
|
+
* environment, and the plugin's data folder is not made here. A hook that
|
|
2230
|
+
* writes variables for later commands (`NAMZU_ENV_FILE`) is not given the
|
|
2231
|
+
* file, which is a host path. `http`, `prompt` and `mcp_tool` hooks start no
|
|
2232
|
+
* process and are not routed through it.
|
|
2233
|
+
*/
|
|
2234
|
+
readonly execute?: ShellHookExecutor
|
|
2235
|
+
/**
|
|
2236
|
+
* Accept only the answers that narrow: `deny`, `block`, `skip`, errors,
|
|
2237
|
+
* `additionalContext`, notices and `halt`. An `allow`, `permissionDecision:
|
|
2238
|
+
* allow`, `updatedInput`, `retry` and the other answers that widen what the
|
|
2239
|
+
* session may do are dropped, whichever hook gave them. Absent, every answer
|
|
2240
|
+
* is read as before.
|
|
2241
|
+
*/
|
|
2242
|
+
readonly restrictVerdicts?: boolean
|
|
2243
|
+
/**
|
|
2244
|
+
* The entry types this adapter accepts: an allow-list of `command`, `http`,
|
|
2245
|
+
* `prompt` and `mcp_tool`. An entry of any other type is refused when it is
|
|
2246
|
+
* registered, before anything is attached, with a refusal that names the
|
|
2247
|
+
* entry's type and the list (`createShellHook` and `attachShellHooks` throw
|
|
2248
|
+
* it). For a host that runs untrusted entries and wants no web request, no
|
|
2249
|
+
* judge call and no tool call to leave from its own process. Absent, every
|
|
2250
|
+
* type is accepted. An entry with no `type` is a `command`.
|
|
2251
|
+
*/
|
|
2252
|
+
readonly handlerTypes?: readonly ShellHookHandlerType[]
|
|
2253
|
+
/**
|
|
2254
|
+
* How an entry's `matcher` may be written. `'names'` refuses, when the entry
|
|
2255
|
+
* is registered, any matcher that is not a plain name list: a name, names
|
|
2256
|
+
* joined by `|` (or `,`), a prefix ending in `*` (`mcp__server__*`), empty or
|
|
2257
|
+
* `*`. A regular expression is never compiled or run then, so a pattern that
|
|
2258
|
+
* backtracks without bound cannot reach the host. The refusal names the
|
|
2259
|
+
* matcher. Absent, a matcher is read as before (a name list, or a regular
|
|
2260
|
+
* expression).
|
|
2261
|
+
*/
|
|
2262
|
+
readonly matcherForms?: 'names'
|
|
2162
2263
|
/**
|
|
2163
2264
|
* Called when a hook of this adapter ran out its deadline and was ended.
|
|
2164
2265
|
* `hook` is the command, URL or prompt text the entry names.
|
|
@@ -2346,6 +2447,7 @@ async function runWorktreeHook(
|
|
|
2346
2447
|
input: ShellHookInput,
|
|
2347
2448
|
signal: AbortSignal | undefined,
|
|
2348
2449
|
reportTimeout: (timedOut: boolean) => void,
|
|
2450
|
+
execute: ShellHookExecutor | undefined,
|
|
2349
2451
|
): Promise<PluginHookResult> {
|
|
2350
2452
|
const label = describeShellHook(entry)
|
|
2351
2453
|
if (entry.type === 'prompt' || entry.type === 'mcp_tool') {
|
|
@@ -2373,7 +2475,7 @@ async function runWorktreeHook(
|
|
|
2373
2475
|
...(response.error !== undefined ? { spawnError: response.error } : {}),
|
|
2374
2476
|
})
|
|
2375
2477
|
}
|
|
2376
|
-
const outcome = await runShellHook(entry, input, signal)
|
|
2478
|
+
const outcome = await runShellHook(entry, input, signal, execute)
|
|
2377
2479
|
reportTimeout(outcome.timedOut)
|
|
2378
2480
|
return worktreeHookVerdict(event, label, outcome)
|
|
2379
2481
|
}
|
|
@@ -2467,6 +2569,8 @@ export function createShellHook(
|
|
|
2467
2569
|
configured: ShellHookHandler,
|
|
2468
2570
|
options: ShellHookOptions,
|
|
2469
2571
|
): PluginHookDefinition {
|
|
2572
|
+
const refusal = shellHookEntryLimitProblem(configured, options)
|
|
2573
|
+
if (refusal !== undefined) throw new Error(`Shell hook ${event}: ${refusal}`)
|
|
2470
2574
|
const log = options.log ?? NOOP_LOGGER
|
|
2471
2575
|
const envSlot = nextHookEnvSlot++
|
|
2472
2576
|
const eventDefault = options.defaultTimeoutMs?.[event]
|
|
@@ -2483,7 +2587,7 @@ export function createShellHook(
|
|
|
2483
2587
|
let spent = false
|
|
2484
2588
|
// The shell verdict branches on `event`, but TypeScript cannot retain that
|
|
2485
2589
|
// correlation across the async handler closure and the union of events.
|
|
2486
|
-
|
|
2590
|
+
const definition = {
|
|
2487
2591
|
event,
|
|
2488
2592
|
...(entry.statusMessage !== undefined ? { statusMessage: entry.statusMessage } : {}),
|
|
2489
2593
|
// A file named by the matcher is watched for as long as the hook is attached.
|
|
@@ -2570,8 +2674,9 @@ export function createShellHook(
|
|
|
2570
2674
|
? event
|
|
2571
2675
|
: undefined
|
|
2572
2676
|
const runsACommand = entry.type === undefined || entry.type === 'command'
|
|
2677
|
+
// The variables file is a host path; a command run elsewhere is not given it.
|
|
2573
2678
|
const envFile =
|
|
2574
|
-
envEvent !== undefined && runsACommand
|
|
2679
|
+
envEvent !== undefined && runsACommand && options.execute === undefined
|
|
2575
2680
|
? options.hookEnv?.fileFor(envEvent, envSlot)
|
|
2576
2681
|
: undefined
|
|
2577
2682
|
const hookInput = shellHookInputFor(event, context, options, {
|
|
@@ -2583,7 +2688,14 @@ export function createShellHook(
|
|
|
2583
2688
|
...(context.toolInput !== undefined ? { toolInput: context.toolInput } : {}),
|
|
2584
2689
|
}
|
|
2585
2690
|
if (event === 'worktree_create' || event === 'worktree_remove') {
|
|
2586
|
-
return runWorktreeHook(
|
|
2691
|
+
return runWorktreeHook(
|
|
2692
|
+
event,
|
|
2693
|
+
entry,
|
|
2694
|
+
hookInput,
|
|
2695
|
+
context.signal,
|
|
2696
|
+
reportTimeout,
|
|
2697
|
+
options.execute,
|
|
2698
|
+
)
|
|
2587
2699
|
}
|
|
2588
2700
|
if (entry.type === 'http') {
|
|
2589
2701
|
const response = await postHookJson({
|
|
@@ -2627,9 +2739,10 @@ export function createShellHook(
|
|
|
2627
2739
|
if (entry.async === true) {
|
|
2628
2740
|
// Started and left running: it decides nothing, so nothing waits
|
|
2629
2741
|
// for it and its own failure is only written down.
|
|
2630
|
-
void runShellHook(entry, hookInput)
|
|
2742
|
+
void runShellHook(entry, hookInput, undefined, options.execute)
|
|
2631
2743
|
.then((outcome) => {
|
|
2632
|
-
if (envEvent !== undefined
|
|
2744
|
+
if (envEvent !== undefined && options.execute === undefined)
|
|
2745
|
+
options.hookEnv?.absorb(envEvent, envSlot)
|
|
2633
2746
|
const verdict = shellHookVerdict(event, entry, outcome, log, verdictCall)
|
|
2634
2747
|
if (verdict.action !== 'continue') {
|
|
2635
2748
|
log.warn('an async hook answered, and its answer is not applied', {
|
|
@@ -2642,14 +2755,22 @@ export function createShellHook(
|
|
|
2642
2755
|
.catch(() => undefined)
|
|
2643
2756
|
return { action: 'continue' }
|
|
2644
2757
|
}
|
|
2645
|
-
const outcome = await runShellHook(entry, hookInput, context.signal)
|
|
2758
|
+
const outcome = await runShellHook(entry, hookInput, context.signal, options.execute)
|
|
2646
2759
|
// What the hook appended counts even when it was ended or failed after
|
|
2647
2760
|
// writing it: the lines are data it already gave.
|
|
2648
|
-
if (envEvent !== undefined
|
|
2761
|
+
if (envEvent !== undefined && options.execute === undefined)
|
|
2762
|
+
options.hookEnv?.absorb(envEvent, envSlot)
|
|
2649
2763
|
reportTimeout(outcome.timedOut)
|
|
2650
2764
|
return shellHookVerdict(event, entry, outcome, log, verdictCall)
|
|
2651
2765
|
},
|
|
2652
2766
|
} as PluginHookDefinition
|
|
2767
|
+
if (options.restrictVerdicts !== true) return definition
|
|
2768
|
+
const where = { event, command: describeShellHook(entry) }
|
|
2769
|
+
return {
|
|
2770
|
+
...definition,
|
|
2771
|
+
handler: async (context: PluginHookContext) =>
|
|
2772
|
+
restrictHookVerdict(await definition.handler(context as never), log, where),
|
|
2773
|
+
} as PluginHookDefinition
|
|
2653
2774
|
}
|
|
2654
2775
|
|
|
2655
2776
|
/**
|
|
@@ -2673,6 +2794,13 @@ export function attachShellHooks(
|
|
|
2673
2794
|
)
|
|
2674
2795
|
}
|
|
2675
2796
|
}
|
|
2797
|
+
// Every entry is checked against the limits first, so a refusal leaves nothing attached.
|
|
2798
|
+
for (const event of SHELL_HOOK_EVENTS) {
|
|
2799
|
+
for (const entry of hooks[event] ?? []) {
|
|
2800
|
+
const refusal = shellHookEntryLimitProblem(entry, options)
|
|
2801
|
+
if (refusal !== undefined) throw new Error(`Shell hook ${event}: ${refusal}`)
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2676
2804
|
const pluginId = options.pluginId ?? SHELL_HOOKS_PLUGIN_ID
|
|
2677
2805
|
let count = 0
|
|
2678
2806
|
for (const event of SHELL_HOOK_EVENTS) {
|