@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
|
@@ -5,7 +5,20 @@ import {
|
|
|
5
5
|
compactNow,
|
|
6
6
|
compactRegion,
|
|
7
7
|
} from '../../compaction/manual.js'
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
type PrecomputeStore,
|
|
10
|
+
type PreparedSpend,
|
|
11
|
+
precomputeStoreFor,
|
|
12
|
+
} from '../../compaction/precompute.js'
|
|
13
|
+
import { estimateMessagesTokens } from '../../compaction/token-estimate.js'
|
|
14
|
+
import {
|
|
15
|
+
measuredDraft,
|
|
16
|
+
readCumulativeDropped,
|
|
17
|
+
readFoldedHistory,
|
|
18
|
+
} from '../../manager/session/turn-recorder.js'
|
|
19
|
+
import type { SessionLog } from '../../store/session-log/index.js'
|
|
20
|
+
import { DiskSessionLog } from '../../store/session-log/index.js'
|
|
21
|
+
import { accumulateTokenUsage } from '../../types/common/index.js'
|
|
9
22
|
import { NamzuError } from '../../types/errors/index.js'
|
|
10
23
|
import type { MessageId, SessionId } from '../../types/ids/index.js'
|
|
11
24
|
import type { Message } from '../../types/message/index.js'
|
|
@@ -28,6 +41,15 @@ export interface CompactSessionParams
|
|
|
28
41
|
* lapses. Recorded on the `compaction` record.
|
|
29
42
|
*/
|
|
30
43
|
readonly trigger?: 'manual' | 'idle'
|
|
44
|
+
/**
|
|
45
|
+
* What an `'idle'` pass does with a summary prepared ahead (see
|
|
46
|
+
* `CompactionConfig.precompute`). Absent: it is used when it is still valid
|
|
47
|
+
* (waiting for one still being written) and a fresh summary is written when
|
|
48
|
+
* it is not. `'only'`: used or nothing, a refusal ends the pass as a no-op
|
|
49
|
+
* (`null`). `false`: never used, and stopped first. Ignored for `'manual'`,
|
|
50
|
+
* which always stops it.
|
|
51
|
+
*/
|
|
52
|
+
readonly preparedSummary?: 'only' | false
|
|
31
53
|
/** Where to find the session's log, and the lease to append under. */
|
|
32
54
|
readonly locator?: SessionLocatorOptions
|
|
33
55
|
}
|
|
@@ -44,43 +66,57 @@ export interface CompactSessionParams
|
|
|
44
66
|
export async function compactSession(
|
|
45
67
|
params: CompactSessionParams,
|
|
46
68
|
): Promise<CompactionResult | null> {
|
|
47
|
-
const { sessionId, locator = {}, trigger = 'manual', hooks, ...input } = params
|
|
69
|
+
const { sessionId, locator = {}, trigger = 'manual', hooks, preparedSummary, ...input } = params
|
|
48
70
|
const log = await locateSessionLog(sessionId, locator)
|
|
49
71
|
return withSessionLease(log, locator.lease, async (lease) => {
|
|
50
72
|
const active = await log.activeTurn({ lease })
|
|
51
73
|
if (active) {
|
|
52
74
|
throw new TurnInProgressError({ sessionId, activeTurnId: active.turnId, state: active.state })
|
|
53
75
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
76
|
+
// One summary at a time. A host compacting an idle conversation takes the
|
|
77
|
+
// summary prepared ahead when it is still valid (or waits for the one being
|
|
78
|
+
// written); any other pass stops it first.
|
|
79
|
+
const usePrepared = trigger === 'idle' && preparedSummary !== false
|
|
80
|
+
const store = precomputeStoreFor(sessionId)
|
|
81
|
+
const owed = [...store.drainUnbooked(), ...(usePrepared ? [] : await store.cancel())]
|
|
82
|
+
return carrying(store, owed, async () => {
|
|
83
|
+
const history = await readFoldedHistory(log)
|
|
84
|
+
const ids = new Map<Message, MessageId>()
|
|
85
|
+
for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
|
|
86
|
+
const startedAt = performance.now()
|
|
87
|
+
const result = await compactNow({
|
|
88
|
+
// The session's own file, so the summary says where exact text is stored.
|
|
89
|
+
...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
|
|
90
|
+
...input,
|
|
91
|
+
messages: history.map((entry) => entry.message),
|
|
92
|
+
...(hooks ? { hooks: { ...hooks, sessionId, trigger } } : {}),
|
|
93
|
+
...(usePrepared
|
|
94
|
+
? { prepared: { sessionId, ...(preparedSummary === 'only' ? { only: true } : {}) } }
|
|
95
|
+
: {}),
|
|
69
96
|
})
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
keptMessageIds,
|
|
80
|
-
|
|
81
|
-
|
|
97
|
+
if (!result) return null
|
|
98
|
+
const head = await log.head()
|
|
99
|
+
if (!head) {
|
|
100
|
+
throw new NamzuError({
|
|
101
|
+
code: 'not_found',
|
|
102
|
+
message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
|
|
103
|
+
details: { sessionId },
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
const { summary, keptMessageIds } = compactionShape(result.messages, ids)
|
|
107
|
+
await appendCompaction(log, lease, {
|
|
108
|
+
strategy: input.config.strategy,
|
|
109
|
+
trigger,
|
|
110
|
+
replacesSeqRange: [1, head.pointer.seq],
|
|
111
|
+
summary,
|
|
112
|
+
keptMessageIds,
|
|
113
|
+
before: history.map((entry) => entry.message),
|
|
114
|
+
after: result.messages,
|
|
115
|
+
startedAt,
|
|
116
|
+
precomputed: result.precomputed === true,
|
|
117
|
+
})
|
|
118
|
+
return result
|
|
82
119
|
})
|
|
83
|
-
return result
|
|
84
120
|
})
|
|
85
121
|
}
|
|
86
122
|
|
|
@@ -123,55 +159,144 @@ export async function compactSessionRange(
|
|
|
123
159
|
if (active) {
|
|
124
160
|
throw new TurnInProgressError({ sessionId, activeTurnId: active.turnId, state: active.state })
|
|
125
161
|
}
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
162
|
+
// One summary at a time: a pass asked for between turns stops one being prepared.
|
|
163
|
+
const store = precomputeStoreFor(sessionId)
|
|
164
|
+
const owed = [...store.drainUnbooked(), ...(await store.cancel())]
|
|
165
|
+
return carrying(store, owed, async () => {
|
|
166
|
+
const history = await readFoldedHistory(log)
|
|
167
|
+
const position = (id: MessageId | undefined, fallback: number): number => {
|
|
168
|
+
if (id === undefined) return fallback
|
|
169
|
+
const at = history.findIndex((entry) => entry.messageId === id)
|
|
170
|
+
if (at < 0) {
|
|
171
|
+
throw new NamzuError({
|
|
172
|
+
code: 'not_found',
|
|
173
|
+
message: `Message ${id} is not in session ${sessionId}'s current context; it may already have been summarised.`,
|
|
174
|
+
details: { sessionId, messageId: id },
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
return at
|
|
178
|
+
}
|
|
179
|
+
const start = position(from, 0)
|
|
180
|
+
const end = position(until, history.length)
|
|
181
|
+
const ids = new Map<Message, MessageId>()
|
|
182
|
+
for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
|
|
183
|
+
const startedAt = performance.now()
|
|
184
|
+
const result = await compactRegion({
|
|
185
|
+
// The session's own file, so the summary says where exact text is stored.
|
|
186
|
+
...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
|
|
187
|
+
...input,
|
|
188
|
+
messages: history.map((entry) => entry.message),
|
|
189
|
+
start,
|
|
190
|
+
end,
|
|
191
|
+
...(hooks ? { hooks: { ...hooks, sessionId, trigger: 'manual' as const } } : {}),
|
|
192
|
+
})
|
|
193
|
+
if (!result) return null
|
|
194
|
+
const head = await log.head()
|
|
195
|
+
if (!head) {
|
|
131
196
|
throw new NamzuError({
|
|
132
197
|
code: 'not_found',
|
|
133
|
-
message: `
|
|
134
|
-
details: { sessionId
|
|
198
|
+
message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
|
|
199
|
+
details: { sessionId },
|
|
135
200
|
})
|
|
136
201
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
...(hooks ? { hooks: { ...hooks, sessionId, trigger: 'manual' as const } } : {}),
|
|
149
|
-
})
|
|
150
|
-
if (!result) return null
|
|
151
|
-
const head = await log.head()
|
|
152
|
-
if (!head) {
|
|
153
|
-
throw new NamzuError({
|
|
154
|
-
code: 'not_found',
|
|
155
|
-
message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
|
|
156
|
-
details: { sessionId },
|
|
202
|
+
const { summary, keptMessageIds } = compactionShape(result.messages, ids)
|
|
203
|
+
await appendCompaction(log, lease, {
|
|
204
|
+
strategy: input.config.strategy,
|
|
205
|
+
trigger: 'manual',
|
|
206
|
+
replacesSeqRange: [1, head.pointer.seq],
|
|
207
|
+
summary,
|
|
208
|
+
keptMessageIds,
|
|
209
|
+
before: history.map((entry) => entry.message),
|
|
210
|
+
after: result.messages,
|
|
211
|
+
startedAt,
|
|
212
|
+
precomputed: false,
|
|
157
213
|
})
|
|
158
|
-
|
|
159
|
-
const { summary, keptMessageIds } = compactionShape(result.messages, ids)
|
|
160
|
-
await log.append(lease, {
|
|
161
|
-
type: 'compaction',
|
|
162
|
-
compactionId: generateMessageId(),
|
|
163
|
-
strategy: input.config.strategy,
|
|
164
|
-
trigger: 'manual',
|
|
165
|
-
replacesSeqRange: [1, head.pointer.seq],
|
|
166
|
-
summary,
|
|
167
|
-
keptMessageIds,
|
|
168
|
-
tokensBefore: 0,
|
|
169
|
-
tokensAfter: 0,
|
|
214
|
+
return result
|
|
170
215
|
})
|
|
171
|
-
return result
|
|
172
216
|
})
|
|
173
217
|
}
|
|
174
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Run a between-turns pass that owes a ledger what prepared summaries spent
|
|
221
|
+
* (one this pass stopped, one that outlived its turn): the cost joins the
|
|
222
|
+
* result's `usage`, and a pass that ends without a result, or fails, leaves it
|
|
223
|
+
* with the session for the next pass or turn to book. Every prepared summary
|
|
224
|
+
* is booked once.
|
|
225
|
+
*/
|
|
226
|
+
async function carrying(
|
|
227
|
+
store: PrecomputeStore,
|
|
228
|
+
owed: readonly PreparedSpend[],
|
|
229
|
+
run: () => Promise<CompactionResult | null>,
|
|
230
|
+
): Promise<CompactionResult | null> {
|
|
231
|
+
try {
|
|
232
|
+
const result = await run()
|
|
233
|
+
if (!result) {
|
|
234
|
+
store.addUnbooked(owed)
|
|
235
|
+
return null
|
|
236
|
+
}
|
|
237
|
+
let usage = result.usage
|
|
238
|
+
for (const spent of owed) usage = accumulateTokenUsage(usage, spent.usage)
|
|
239
|
+
return { ...result, usage }
|
|
240
|
+
} catch (error) {
|
|
241
|
+
store.addUnbooked(owed)
|
|
242
|
+
throw error
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Append a between-turns pass: the `compaction` record, in the shape an
|
|
248
|
+
* earlier release reads (real sizes, a trigger it knows), then the
|
|
249
|
+
* `compaction_measured` record with what only this release records. A refused
|
|
250
|
+
* second write loses only the measures.
|
|
251
|
+
*/
|
|
252
|
+
async function appendCompaction(
|
|
253
|
+
log: SessionLog,
|
|
254
|
+
lease: Parameters<SessionLog['append']>[0],
|
|
255
|
+
pass: {
|
|
256
|
+
readonly strategy: string
|
|
257
|
+
readonly trigger: 'manual' | 'idle'
|
|
258
|
+
readonly replacesSeqRange: [number, number]
|
|
259
|
+
readonly summary: Message[]
|
|
260
|
+
readonly keptMessageIds: MessageId[]
|
|
261
|
+
readonly before: readonly Message[]
|
|
262
|
+
readonly after: readonly Message[]
|
|
263
|
+
readonly startedAt: number
|
|
264
|
+
readonly precomputed: boolean
|
|
265
|
+
},
|
|
266
|
+
): Promise<void> {
|
|
267
|
+
const tokensBefore = estimateMessagesTokens(pass.before)
|
|
268
|
+
const tokensAfter = estimateMessagesTokens(pass.after)
|
|
269
|
+
const compactionId = generateMessageId()
|
|
270
|
+
const priorDropped = await readCumulativeDropped(log)
|
|
271
|
+
await log.append(lease, {
|
|
272
|
+
type: 'compaction',
|
|
273
|
+
compactionId,
|
|
274
|
+
strategy: pass.strategy,
|
|
275
|
+
trigger: pass.trigger,
|
|
276
|
+
replacesSeqRange: pass.replacesSeqRange,
|
|
277
|
+
summary: pass.summary,
|
|
278
|
+
keptMessageIds: pass.keptMessageIds,
|
|
279
|
+
tokensBefore,
|
|
280
|
+
tokensAfter,
|
|
281
|
+
})
|
|
282
|
+
await log
|
|
283
|
+
.append(
|
|
284
|
+
lease,
|
|
285
|
+
measuredDraft(
|
|
286
|
+
compactionId,
|
|
287
|
+
{
|
|
288
|
+
trigger: pass.trigger,
|
|
289
|
+
tokensBefore,
|
|
290
|
+
tokensAfter,
|
|
291
|
+
durationMs: Math.max(0, Math.round(performance.now() - pass.startedAt)),
|
|
292
|
+
precomputed: pass.precomputed,
|
|
293
|
+
},
|
|
294
|
+
priorDropped + Math.max(0, tokensBefore - tokensAfter),
|
|
295
|
+
),
|
|
296
|
+
)
|
|
297
|
+
.catch(() => undefined)
|
|
298
|
+
}
|
|
299
|
+
|
|
175
300
|
/**
|
|
176
301
|
* Express a compacted context as a `compaction` record: the new messages
|
|
177
302
|
* before the first recorded one are its summary and the recorded ones are
|
|
@@ -13,6 +13,7 @@ import { type SessionEvent, isEphemeralEvent } from '../../types/session/events.
|
|
|
13
13
|
import type { SessionRecord } from '../../types/session/records.js'
|
|
14
14
|
import type { TaskEvent, TaskStore } from '../../types/task/index.js'
|
|
15
15
|
import { awaitWithAbort } from '../../utils/await-with-abort.js'
|
|
16
|
+
import { toErrorMessage } from '../../utils/error.js'
|
|
16
17
|
import { SCOPE_ATTRIBUTE } from '../../utils/log/types.js'
|
|
17
18
|
import { type Logger, resolveLogger } from '../../utils/logger.js'
|
|
18
19
|
|
|
@@ -82,7 +83,7 @@ export class EventTranslator {
|
|
|
82
83
|
recorder.onLogWriteHealth((change) => {
|
|
83
84
|
// Live only: the log is what cannot be written. The listener runs inside
|
|
84
85
|
// a log write, so it must not wait on one.
|
|
85
|
-
void this.
|
|
86
|
+
void this.emitFromStore(
|
|
86
87
|
change.state === 'degraded'
|
|
87
88
|
? {
|
|
88
89
|
type: 'session_log_degraded',
|
|
@@ -262,18 +263,35 @@ export class EventTranslator {
|
|
|
262
263
|
}
|
|
263
264
|
}
|
|
264
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Emit an event a store's listener raised. The stores call their listeners
|
|
268
|
+
* and do not wait on them, so a rejection here would reach no one and stop
|
|
269
|
+
* the process as an unhandled rejection. A failed log write has already been
|
|
270
|
+
* delivered live (see `emitEvent`); the failure itself is reported on the log.
|
|
271
|
+
*/
|
|
272
|
+
private async emitFromStore(draft: SessionEventDraft): Promise<void> {
|
|
273
|
+
try {
|
|
274
|
+
await this.emitEvent(draft)
|
|
275
|
+
} catch (error) {
|
|
276
|
+
this.log.warn('Could not record a session event raised by a store', {
|
|
277
|
+
'namzu.event.type': draft.type,
|
|
278
|
+
'exception.message': toErrorMessage(error),
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
265
283
|
wireActivityStore(activityStore: ActivityStore): void {
|
|
266
284
|
activityStore.on(async (event: ActivityEvent) => {
|
|
267
285
|
const activity = event.activity
|
|
268
286
|
if (event.type === 'activity.created') {
|
|
269
|
-
await this.
|
|
287
|
+
await this.emitFromStore({
|
|
270
288
|
type: 'activity_created',
|
|
271
289
|
activityId: activity.id,
|
|
272
290
|
activityType: activity.type,
|
|
273
291
|
description: activity.description,
|
|
274
292
|
})
|
|
275
293
|
} else {
|
|
276
|
-
await this.
|
|
294
|
+
await this.emitFromStore({
|
|
277
295
|
type: 'activity_updated',
|
|
278
296
|
activityId: activity.id,
|
|
279
297
|
status: activity.status,
|
|
@@ -296,7 +314,7 @@ export class EventTranslator {
|
|
|
296
314
|
if (task.sessionId !== sessionId) return
|
|
297
315
|
switch (event.type) {
|
|
298
316
|
case 'task.created':
|
|
299
|
-
await this.
|
|
317
|
+
await this.emitFromStore({
|
|
300
318
|
type: 'task_created',
|
|
301
319
|
taskId: task.id,
|
|
302
320
|
subject: task.subject,
|
|
@@ -311,7 +329,7 @@ export class EventTranslator {
|
|
|
311
329
|
case 'task.updated':
|
|
312
330
|
case 'task.claimed':
|
|
313
331
|
case 'task.deleted':
|
|
314
|
-
await this.
|
|
332
|
+
await this.emitFromStore({
|
|
315
333
|
type: 'task_updated',
|
|
316
334
|
taskId: task.id,
|
|
317
335
|
subject: task.subject,
|
|
@@ -336,7 +354,7 @@ export class EventTranslator {
|
|
|
336
354
|
const plan = event.plan
|
|
337
355
|
switch (event.type) {
|
|
338
356
|
case 'plan.ready':
|
|
339
|
-
await this.
|
|
357
|
+
await this.emitFromStore({
|
|
340
358
|
type: 'plan_ready',
|
|
341
359
|
planId: plan.id,
|
|
342
360
|
title: plan.title,
|
|
@@ -345,10 +363,10 @@ export class EventTranslator {
|
|
|
345
363
|
})
|
|
346
364
|
break
|
|
347
365
|
case 'plan.approved':
|
|
348
|
-
await this.
|
|
366
|
+
await this.emitFromStore({ type: 'plan_approved', planId: plan.id })
|
|
349
367
|
break
|
|
350
368
|
case 'plan.rejected':
|
|
351
|
-
await this.
|
|
369
|
+
await this.emitFromStore({
|
|
352
370
|
type: 'plan_rejected',
|
|
353
371
|
planId: plan.id,
|
|
354
372
|
reason: plan.rejectionReason,
|
|
@@ -356,7 +374,7 @@ export class EventTranslator {
|
|
|
356
374
|
break
|
|
357
375
|
case 'plan.step_updated':
|
|
358
376
|
if (event.step) {
|
|
359
|
-
await this.
|
|
377
|
+
await this.emitFromStore({
|
|
360
378
|
type: 'plan_step_updated',
|
|
361
379
|
planId: plan.id,
|
|
362
380
|
stepId: event.step.id,
|
|
@@ -365,10 +383,10 @@ export class EventTranslator {
|
|
|
365
383
|
}
|
|
366
384
|
break
|
|
367
385
|
case 'plan.completed':
|
|
368
|
-
await this.
|
|
386
|
+
await this.emitFromStore({ type: 'plan_completed', planId: plan.id })
|
|
369
387
|
break
|
|
370
388
|
case 'plan.failed':
|
|
371
|
-
await this.
|
|
389
|
+
await this.emitFromStore({
|
|
372
390
|
type: 'plan_failed',
|
|
373
391
|
planId: plan.id,
|
|
374
392
|
...(plan.failureReason ? { reason: plan.failureReason } : {}),
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util'
|
|
1
2
|
import { GENAI, NAMZU } from '../../../constants/telemetry/index.js'
|
|
2
3
|
import { boundHookContext } from '../../../plugin/hook-context.js'
|
|
3
4
|
import { callableToolNames, formatToolNames } from '../../../registry/tool/callable.js'
|
|
@@ -83,30 +84,74 @@ export async function runPreToolHook(
|
|
|
83
84
|
signal.throwIfAborted()
|
|
84
85
|
const manager = host.config.pluginManager
|
|
85
86
|
if (!manager) return { kind: 'continue', input, modified: false }
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
87
|
+
const runHooks = async (toolInput: unknown): Promise<readonly PluginHookResult[]> => {
|
|
88
|
+
const results = await awaitWithAbort(
|
|
89
|
+
manager.executeHooks(
|
|
90
|
+
'pre_tool_use',
|
|
91
|
+
{
|
|
92
|
+
sessionId: host.config.sessionId,
|
|
93
|
+
turnId: host.config.turnId,
|
|
94
|
+
toolName,
|
|
95
|
+
...(toolCallId !== undefined ? { toolCallId } : {}),
|
|
96
|
+
toolInput,
|
|
97
|
+
signal,
|
|
98
|
+
},
|
|
99
|
+
host.emitEvent,
|
|
100
|
+
),
|
|
101
|
+
signal,
|
|
102
|
+
)
|
|
103
|
+
signal.throwIfAborted()
|
|
104
|
+
return results
|
|
105
|
+
}
|
|
106
|
+
let outcome = interpretPreToolResults(toolName, input, await runHooks(input), host.requestHalt)
|
|
103
107
|
if (outcome.kind !== 'continue') return outcome
|
|
108
|
+
if (outcome.modified) {
|
|
109
|
+
// The hooks answered about the call the model made. The call that will
|
|
110
|
+
// run is the rewritten one, so every hook answers again about that one:
|
|
111
|
+
// a refusal or a question from any of them stands, and an `allow` counts
|
|
112
|
+
// only when it was given to the input that runs. A second rewrite is not
|
|
113
|
+
// followed (a hook that rewrites every input would never settle), but it
|
|
114
|
+
// is not taken as a yes either.
|
|
115
|
+
const rewritten = outcome.input
|
|
116
|
+
const first = outcome
|
|
117
|
+
const again = interpretPreToolResults(
|
|
118
|
+
toolName,
|
|
119
|
+
rewritten,
|
|
120
|
+
(await runHooks(rewritten)).map((result) => settleRewrite(result, rewritten)),
|
|
121
|
+
host.requestHalt,
|
|
122
|
+
)
|
|
123
|
+
if (again.kind !== 'continue') return again
|
|
124
|
+
const contexts = [
|
|
125
|
+
...new Set([...(first.context?.split('\n') ?? []), ...(again.context?.split('\n') ?? [])]),
|
|
126
|
+
]
|
|
127
|
+
outcome = {
|
|
128
|
+
kind: 'continue',
|
|
129
|
+
input: rewritten,
|
|
130
|
+
modified: true,
|
|
131
|
+
...(again.permission ? { permission: again.permission } : {}),
|
|
132
|
+
...(contexts.length > 0 ? { context: contexts.join('\n') } : {}),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
104
135
|
// A task being made or completed is announced once the call itself is allowed.
|
|
105
136
|
const refusal = await runTaskHook(host, manager, toolName, outcome.input, signal)
|
|
106
137
|
if (refusal === undefined) return outcome
|
|
107
138
|
return { kind: 'skip', input: outcome.input, output: refusal }
|
|
108
139
|
}
|
|
109
140
|
|
|
141
|
+
/**
|
|
142
|
+
* A hook's answer to the rewritten input: a `modify` to the same input is the
|
|
143
|
+
* hook agreeing, and a `modify` to anything else is dropped to a plain
|
|
144
|
+
* `continue` (its verdict was about an input that will not run).
|
|
145
|
+
*/
|
|
146
|
+
function settleRewrite(result: PluginHookResult, rewritten: unknown): PluginHookResult {
|
|
147
|
+
if (result.action !== 'modify') return result
|
|
148
|
+
const { input: _input, permission, ...rest } = result
|
|
149
|
+
if (isDeepStrictEqual(result.input, rewritten)) {
|
|
150
|
+
return { ...rest, action: 'continue', ...(permission ? { permission } : {}) }
|
|
151
|
+
}
|
|
152
|
+
return { ...rest, action: 'continue' }
|
|
153
|
+
}
|
|
154
|
+
|
|
110
155
|
/**
|
|
111
156
|
* `task_created` before `task_create` runs and `task_completed` before a
|
|
112
157
|
* `task_update` that completes a task. The text that refuses the call, or
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { TurnRecorder } from '../../manager/session/turn-recorder.js'
|
|
2
2
|
import { buildLimitConfig, checkLimitsDetailed } from '../../turn/LimitChecker.js'
|
|
3
3
|
import type { LimitCheckerConfig, StopReason } from '../../types/session/index.js'
|
|
4
|
+
import type { BudgetLeft } from './budget-nudge.js'
|
|
4
5
|
|
|
5
6
|
export interface GuardConfig {
|
|
6
7
|
tokenBudget: number
|
|
@@ -101,6 +102,27 @@ export class GuardCoordinator {
|
|
|
101
102
|
return Math.max(0, this.limitConfig.timeoutMs - (Date.now() - this.startTime))
|
|
102
103
|
}
|
|
103
104
|
|
|
105
|
+
/**
|
|
106
|
+
* What is left of each limit this turn has, for the request note that
|
|
107
|
+
* asks the model to leave its work tidy. `completedIterations` is the
|
|
108
|
+
* count before the call being prepared, so the call itself is counted.
|
|
109
|
+
* A limit that is off (zero) is left out.
|
|
110
|
+
*/
|
|
111
|
+
budgetLeft(completedIterations: number): BudgetLeft {
|
|
112
|
+
const left: BudgetLeft = {}
|
|
113
|
+
const { maxIterations, timeoutMs } = this.limitConfig
|
|
114
|
+
if (maxIterations > 0) {
|
|
115
|
+
left.iterations = {
|
|
116
|
+
left: Math.max(0, maxIterations - completedIterations),
|
|
117
|
+
total: maxIterations,
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (timeoutMs > 0) {
|
|
121
|
+
left.timeMs = { left: this.remainingUntilTimeoutMs(), total: timeoutMs }
|
|
122
|
+
}
|
|
123
|
+
return left
|
|
124
|
+
}
|
|
125
|
+
|
|
104
126
|
beforeIteration(recorder: TurnRecorder, abortSignal: AbortSignal): GuardCheckResult {
|
|
105
127
|
const limitState = {
|
|
106
128
|
aborted: abortSignal.aborted,
|
|
@@ -29,6 +29,7 @@ import { withTokenBudget } from '../../provider/token-budget.js'
|
|
|
29
29
|
import type { SessionPaths } from '../../session/paths.js'
|
|
30
30
|
import type { SessionTokenBudget, SessionTokenBudgetStore } from '../../store/budget/index.js'
|
|
31
31
|
import type { SessionCheckpointStore } from '../../store/checkpoint/index.js'
|
|
32
|
+
import { DiskSessionLog } from '../../store/session-log/index.js'
|
|
32
33
|
import type { SessionLease, SessionLog } from '../../store/session-log/index.js'
|
|
33
34
|
import {
|
|
34
35
|
GENAI,
|
|
@@ -923,6 +924,13 @@ export interface QueryParams {
|
|
|
923
924
|
*/
|
|
924
925
|
compactionSummaryRoute?: CompactionSummaryRouter
|
|
925
926
|
|
|
927
|
+
/**
|
|
928
|
+
* Close a compaction summary with the wording that lets the model ask when
|
|
929
|
+
* something is unclear, instead of telling it to continue directly. Absent
|
|
930
|
+
* or false, it continues directly.
|
|
931
|
+
*/
|
|
932
|
+
compactionAskWhenUnclear?: boolean
|
|
933
|
+
|
|
926
934
|
/**
|
|
927
935
|
* Where this conversation's durable state lives.
|
|
928
936
|
*
|
|
@@ -2104,12 +2112,14 @@ export async function* query(params: QueryParams): AsyncGenerator<SessionEvent,
|
|
|
2104
2112
|
...(params.projectInstructionContext
|
|
2105
2113
|
? { projectInstructionContext: params.projectInstructionContext }
|
|
2106
2114
|
: {}),
|
|
2115
|
+
...(ctx.storage.log instanceof DiskSessionLog ? { sessionFile: ctx.storage.log.file } : {}),
|
|
2107
2116
|
...(params.postCompactionContext
|
|
2108
2117
|
? { postCompactionContext: params.postCompactionContext }
|
|
2109
2118
|
: {}),
|
|
2110
2119
|
...(params.compactionSummaryRoute
|
|
2111
2120
|
? { compactionSummaryRoute: params.compactionSummaryRoute }
|
|
2112
2121
|
: {}),
|
|
2122
|
+
...(params.compactionAskWhenUnclear ? { compactionAskWhenUnclear: true } : {}),
|
|
2113
2123
|
...(providerContextWindow !== undefined ? { providerContextWindow } : {}),
|
|
2114
2124
|
...(providerAutoCompactLimit !== undefined ? { providerAutoCompactLimit } : {}),
|
|
2115
2125
|
resolveModelAutoCompactLimit: async (model) => {
|
|
@@ -67,6 +67,7 @@ import { toErrorMessage } from '../../../utils/error.js'
|
|
|
67
67
|
import { stableDigest } from '../../../utils/hash.js'
|
|
68
68
|
import { generateMessageId } from '../../../utils/id.js'
|
|
69
69
|
import { parseStructuredResultJson } from '../../../utils/structured-result-json.js'
|
|
70
|
+
import { type BudgetNudgeStage, budgetNudgeStage, budgetNudgeText } from '../budget-nudge.js'
|
|
70
71
|
import { createCallbackInference } from '../callback-inference.js'
|
|
71
72
|
import { SandboxAcquisitionStopped } from '../deferred-sandbox.js'
|
|
72
73
|
import { deliverDurableInbound } from '../durable-inbound.js'
|
|
@@ -345,6 +346,8 @@ export class IterationOrchestrator {
|
|
|
345
346
|
* sets it.
|
|
346
347
|
*/
|
|
347
348
|
private stopDeferredForOutstandingWork = false
|
|
349
|
+
/** The latest budget line this turn has recorded, so each stage is said once. */
|
|
350
|
+
private budgetNudgeStage: BudgetNudgeStage = 0
|
|
348
351
|
/** Nudges sent since the model last finished a call without hitting its output cap. */
|
|
349
352
|
private outputContinuations = 0
|
|
350
353
|
/** Watches the turn's steps for reasoning that piles up while nothing changes. */
|
|
@@ -888,6 +891,12 @@ export class IterationOrchestrator {
|
|
|
888
891
|
// edge BEFORE compaction runs (so the refreshed slot is what
|
|
889
892
|
// compaction preserves). No-op when no provider is configured.
|
|
890
893
|
await refreshWorkingMemory(this.ctx)
|
|
894
|
+
// A turn that starts after the prompt cache lapsed installs a summary
|
|
895
|
+
// written earlier, when one is ready and still valid, before the old
|
|
896
|
+
// tool results are trimmed.
|
|
897
|
+
if (iterationNum === 1 && this.ctx.compactionConfig?.clearStaleToolResultsAtStart) {
|
|
898
|
+
await runCompactionCheck(this.ctx, { preparedOnly: true })
|
|
899
|
+
}
|
|
891
900
|
if (iterationNum === 1) await runStaleResultClear(this.ctx)
|
|
892
901
|
await runCompactionCheck(this.ctx)
|
|
893
902
|
yield* this.ctx.drainPending()
|
|
@@ -922,6 +931,16 @@ export class IterationOrchestrator {
|
|
|
922
931
|
this.ctx.log,
|
|
923
932
|
)
|
|
924
933
|
}
|
|
934
|
+
// Before the limit ends the run, say so once per stage. Recorded, so
|
|
935
|
+
// the model keeps reading it, and only after the guard has passed
|
|
936
|
+
// this call: a run that is about to be stopped has no call to spend.
|
|
937
|
+
const budgetLeft = this.ctx.guard.budgetLeft(iterationNum - 1)
|
|
938
|
+
const budgetStage = budgetNudgeStage(budgetLeft)
|
|
939
|
+
if (budgetStage > this.budgetNudgeStage) {
|
|
940
|
+
this.budgetNudgeStage = budgetStage
|
|
941
|
+
const text = budgetNudgeText(budgetLeft, budgetStage)
|
|
942
|
+
if (text) appendRecordedContext(recorder, [{ key: 'budget-nudge', text }], this.ctx.log)
|
|
943
|
+
}
|
|
925
944
|
const contextSections =
|
|
926
945
|
this.ctx.promptContributions?.render('context', { iteration: iterationNum }) ?? []
|
|
927
946
|
const deferredMcpHints =
|
|
@@ -3627,7 +3646,13 @@ export class IterationOrchestrator {
|
|
|
3627
3646
|
this.hydrateOutcome()
|
|
3628
3647
|
return await this.outcomeLedger.preview()
|
|
3629
3648
|
},
|
|
3630
|
-
{
|
|
3649
|
+
{
|
|
3650
|
+
finalMessage,
|
|
3651
|
+
recordCheck: async (check) => {
|
|
3652
|
+
this.hydrateOutcome()
|
|
3653
|
+
await this.outcomeLedger.recordHostCheck(check)
|
|
3654
|
+
},
|
|
3655
|
+
},
|
|
3631
3656
|
)
|
|
3632
3657
|
if (typeof note !== 'string' || note.trim() === '') return undefined
|
|
3633
3658
|
this.beforeEndNotes += 1
|