@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
|
@@ -24,9 +24,15 @@ import {
|
|
|
24
24
|
planCompaction,
|
|
25
25
|
planSalienceWorkingSet,
|
|
26
26
|
} from '../../../../compaction/plan.js'
|
|
27
|
+
import { precomputeStoreFor } from '../../../../compaction/precompute.js'
|
|
27
28
|
import type { ContextReducer, ContextReduction } from '../../../../compaction/reducer.js'
|
|
28
29
|
import { createSlidingWindowReducer } from '../../../../compaction/reducer.js'
|
|
29
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
carriedWorkingCommands,
|
|
32
|
+
collectToolsUsed,
|
|
33
|
+
keptInstructionFiles,
|
|
34
|
+
renderRestoredContext,
|
|
35
|
+
} from '../../../../compaction/restore.js'
|
|
30
36
|
import { findRetainedIndices } from '../../../../compaction/retention.js'
|
|
31
37
|
import { serializeState } from '../../../../compaction/serializer.js'
|
|
32
38
|
import {
|
|
@@ -55,6 +61,7 @@ import {
|
|
|
55
61
|
estimateRefillTokens,
|
|
56
62
|
} from './compaction-breaker.js'
|
|
57
63
|
import type { IterationContext } from './context.js'
|
|
64
|
+
import { armPrecompute, bookPreparedSpend, takePrepared } from './precompute.js'
|
|
58
65
|
import { isWorkingMemoryMessage } from './working-memory.js'
|
|
59
66
|
|
|
60
67
|
// `isCompactionMessage` and the header moved to `compaction/summary.ts` —
|
|
@@ -647,6 +654,7 @@ async function applyReducer(
|
|
|
647
654
|
windowSource: 'config' | 'provider' | 'model-table' | 'default'
|
|
648
655
|
},
|
|
649
656
|
): Promise<void> {
|
|
657
|
+
const startedAt = performance.now()
|
|
650
658
|
const messages = ctx.recorder.messages
|
|
651
659
|
const before = messages.length
|
|
652
660
|
const beforeChars = totalChars(messages)
|
|
@@ -719,6 +727,13 @@ async function applyReducer(
|
|
|
719
727
|
// Found by a test written for the decline paths asserting that success does
|
|
720
728
|
// NOT report a failure, which is the only reason anybody looked here.
|
|
721
729
|
const tokensAfter = estimateTokens(ctx)
|
|
730
|
+
const durationMs = Math.max(0, Math.round(performance.now() - startedAt))
|
|
731
|
+
ctx.recorder.noteCompaction?.({
|
|
732
|
+
trigger: 'auto',
|
|
733
|
+
tokensBefore: reduction.estimatedTokens,
|
|
734
|
+
tokensAfter,
|
|
735
|
+
durationMs,
|
|
736
|
+
})
|
|
722
737
|
await ctx.emitEvent?.({
|
|
723
738
|
type: 'compaction_completed',
|
|
724
739
|
turnId: ctx.recorder.turnId,
|
|
@@ -730,6 +745,8 @@ async function applyReducer(
|
|
|
730
745
|
measuredBy: measurement.measuredBy,
|
|
731
746
|
contextWindowTokens: reduction.contextWindowTokens,
|
|
732
747
|
windowSource: measurement.windowSource,
|
|
748
|
+
trigger: 'auto',
|
|
749
|
+
durationMs,
|
|
733
750
|
})
|
|
734
751
|
await emitContextUsageSnapshot(
|
|
735
752
|
ctx,
|
|
@@ -796,17 +813,22 @@ interface CompactionPass {
|
|
|
796
813
|
hookInstructions?: string
|
|
797
814
|
/** The summary the pass installed, for `post_compact`. */
|
|
798
815
|
summary?: string
|
|
816
|
+
/** The summary was written ahead of time and installed at once. */
|
|
817
|
+
precomputed?: boolean
|
|
818
|
+
/** A turn that started after the prompt cache lapsed asked for it. */
|
|
819
|
+
cause?: 'cache_expired'
|
|
799
820
|
tokensBefore: number
|
|
800
821
|
contextWindowTokens: number
|
|
801
822
|
}
|
|
802
823
|
|
|
803
824
|
export async function runCompactionCheck(
|
|
804
825
|
ctx: IterationContext,
|
|
805
|
-
options?: { force?: boolean; deeper?: boolean },
|
|
826
|
+
options?: { force?: boolean; deeper?: boolean; preparedOnly?: boolean },
|
|
806
827
|
): Promise<void> {
|
|
807
828
|
const pass: CompactionPass = {
|
|
808
829
|
attempted: false,
|
|
809
830
|
reason: options?.force ? 'overflow' : 'threshold',
|
|
831
|
+
...(options?.preparedOnly ? { cause: 'cache_expired' as const } : {}),
|
|
810
832
|
tokensBefore: 0,
|
|
811
833
|
contextWindowTokens: 0,
|
|
812
834
|
}
|
|
@@ -847,13 +869,23 @@ export async function runCompactionCheck(
|
|
|
847
869
|
async function runCompactionCheckInner(
|
|
848
870
|
ctx: IterationContext,
|
|
849
871
|
pass: CompactionPass,
|
|
850
|
-
options?: { force?: boolean; deeper?: boolean },
|
|
872
|
+
options?: { force?: boolean; deeper?: boolean; preparedOnly?: boolean },
|
|
851
873
|
): Promise<void> {
|
|
874
|
+
const startedAt = performance.now()
|
|
875
|
+
// What summaries prepared earlier cost, before any early return below.
|
|
876
|
+
bookPreparedSpend(ctx)
|
|
852
877
|
const configured = ctx.compactionConfig
|
|
853
878
|
if (!configured) return
|
|
854
879
|
if (configured.strategy === 'disabled') return
|
|
855
880
|
// Automatic compaction off: only a pass the provider forced (an overflow) runs.
|
|
856
881
|
if (configured.autoCompact === false && !options?.force) return
|
|
882
|
+
if (options?.preparedOnly) {
|
|
883
|
+
// Only a summary already written, and only where one could be used: a
|
|
884
|
+
// reducer owns reduction, and nothing prepared means nothing to install.
|
|
885
|
+
if (configured.precompute === false || !ctx.compactionSummaryRoute) return
|
|
886
|
+
if (ctx.contextReducer || configured.strategy === 'sliding-window') return
|
|
887
|
+
if (precomputeStoreFor(ctx.recorder.sessionId).entry?.status !== 'ready') return
|
|
888
|
+
}
|
|
857
889
|
const deepened = options?.deeper ? deepenConfig(configured, ctx.recorder.messages) : configured
|
|
858
890
|
|
|
859
891
|
const measured = measureContext(ctx)
|
|
@@ -902,7 +934,20 @@ async function runCompactionCheckInner(
|
|
|
902
934
|
config.autoCompactWindowTokens,
|
|
903
935
|
)
|
|
904
936
|
const startAt = salience ? Math.min(config.softTarget ?? DEFAULT_SOFT_TARGET, trigger) : trigger
|
|
905
|
-
if (!options?.force && usage < startAt)
|
|
937
|
+
if (!options?.force && !options?.preparedOnly && usage < startAt) {
|
|
938
|
+
// Short of the trigger. Near it, a summary starts in the background so it
|
|
939
|
+
// is ready by the time the trigger is reached; far from it, nothing runs.
|
|
940
|
+
armPrecompute(ctx, {
|
|
941
|
+
config,
|
|
942
|
+
usage,
|
|
943
|
+
trigger,
|
|
944
|
+
budget,
|
|
945
|
+
estimatedTokens,
|
|
946
|
+
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
|
|
947
|
+
source: 'estimate',
|
|
948
|
+
})
|
|
949
|
+
return
|
|
950
|
+
}
|
|
906
951
|
|
|
907
952
|
// A pass that can shed nothing and has not reached the summary trigger
|
|
908
953
|
// does nothing but announce itself: two hooks, a started event and a log
|
|
@@ -914,10 +959,21 @@ async function runCompactionCheckInner(
|
|
|
914
959
|
if (
|
|
915
960
|
salience &&
|
|
916
961
|
!options?.force &&
|
|
962
|
+
!options?.preparedOnly &&
|
|
917
963
|
!ctx.contextReducer &&
|
|
918
964
|
usage < trigger &&
|
|
919
965
|
!canShedSalienceWorkingSet(ctx.recorder.messages, config)
|
|
920
966
|
) {
|
|
967
|
+
// Nothing to shed yet, but the summary can still start near the trigger.
|
|
968
|
+
armPrecompute(ctx, {
|
|
969
|
+
config,
|
|
970
|
+
usage,
|
|
971
|
+
trigger,
|
|
972
|
+
budget,
|
|
973
|
+
estimatedTokens,
|
|
974
|
+
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
|
|
975
|
+
source: 'estimate',
|
|
976
|
+
})
|
|
921
977
|
return
|
|
922
978
|
}
|
|
923
979
|
|
|
@@ -1051,7 +1107,7 @@ async function runCompactionCheckInner(
|
|
|
1051
1107
|
// The decision is `planCompaction`'s; installing it is this file's. The
|
|
1052
1108
|
// planner touches no `ctx` at all, which is what lets the boundary
|
|
1053
1109
|
// arithmetic be tested without a turn.
|
|
1054
|
-
if (salience) {
|
|
1110
|
+
if (salience && !options?.preparedOnly) {
|
|
1055
1111
|
let openTasks: string[] | undefined
|
|
1056
1112
|
if (ctx.taskStore) {
|
|
1057
1113
|
try {
|
|
@@ -1079,6 +1135,15 @@ async function runCompactionCheckInner(
|
|
|
1079
1135
|
// Under the trigger, the working set was the whole pass: the summary
|
|
1080
1136
|
// path is for the trigger, not for the soft target.
|
|
1081
1137
|
if (!options?.force && (estimatedTokens - working.reclaimedTokens) / budget < trigger) {
|
|
1138
|
+
armPrecompute(ctx, {
|
|
1139
|
+
config,
|
|
1140
|
+
usage: (estimatedTokens - working.reclaimedTokens) / budget,
|
|
1141
|
+
trigger,
|
|
1142
|
+
budget,
|
|
1143
|
+
estimatedTokens: estimatedTokens - working.reclaimedTokens,
|
|
1144
|
+
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
|
|
1145
|
+
source: 'estimate',
|
|
1146
|
+
})
|
|
1082
1147
|
return
|
|
1083
1148
|
}
|
|
1084
1149
|
}
|
|
@@ -1089,6 +1154,8 @@ async function runCompactionCheckInner(
|
|
|
1089
1154
|
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
|
|
1090
1155
|
estimatedTokens,
|
|
1091
1156
|
...(options?.force ? { force: true } : {}),
|
|
1157
|
+
// Installing a prepared summary leaves the tool results to the stale clear.
|
|
1158
|
+
...(options?.preparedOnly ? { skipToolResultClear: true } : {}),
|
|
1092
1159
|
})
|
|
1093
1160
|
|
|
1094
1161
|
if (clearPlan.kind === 'cleared') {
|
|
@@ -1163,52 +1230,82 @@ async function runCompactionCheckInner(
|
|
|
1163
1230
|
return
|
|
1164
1231
|
}
|
|
1165
1232
|
|
|
1166
|
-
|
|
1233
|
+
let { olderMessages, recentMessages, keepStart } = plan
|
|
1234
|
+
const { systemMessages } = plan
|
|
1167
1235
|
|
|
1168
1236
|
let compactedContent: string
|
|
1169
1237
|
|
|
1238
|
+
// A summary written ahead of time stands in for the one this pass would
|
|
1239
|
+
// write, when it still describes the start of the conversation. What was
|
|
1240
|
+
// said after it was armed stays verbatim after the summary.
|
|
1241
|
+
const prepared =
|
|
1242
|
+
ctx.compactionSummaryRoute && config.precompute !== false && !ctx.contextReducer
|
|
1243
|
+
? await takePrepared(ctx, {
|
|
1244
|
+
messages,
|
|
1245
|
+
floorLength: systemMessages.length,
|
|
1246
|
+
hookInstructions: pass.hookInstructions,
|
|
1247
|
+
overheadTokens: estimateToolCatalogTokens(ctx),
|
|
1248
|
+
budget,
|
|
1249
|
+
triggerTokens: trigger * budget,
|
|
1250
|
+
...(options?.preparedOnly ? { readyOnly: true } : {}),
|
|
1251
|
+
})
|
|
1252
|
+
: undefined
|
|
1253
|
+
// A turn that starts after the cache lapsed installs a summary that is
|
|
1254
|
+
// ready, and writes none: a refusal is a skip.
|
|
1255
|
+
if (options?.preparedOnly && !prepared) return
|
|
1256
|
+
if (prepared) {
|
|
1257
|
+
keepStart = prepared.keepStart
|
|
1258
|
+
olderMessages = messages.slice(systemMessages.length, keepStart)
|
|
1259
|
+
recentMessages = messages.slice(keepStart)
|
|
1260
|
+
pass.precomputed = true
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1170
1263
|
// A model the host set aside writes the summary when it can. Its own cost
|
|
1171
1264
|
// goes on the turn's ledger at the price of the model that answered. When
|
|
1172
1265
|
// there is no such model, or its call does not give a summary, the
|
|
1173
1266
|
// extracted state below is used, with its verifier step where that applies.
|
|
1174
1267
|
let modelRoute: { providerId: string; model: string } | undefined
|
|
1175
|
-
const modelWritten =
|
|
1176
|
-
?
|
|
1177
|
-
:
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1268
|
+
const modelWritten = prepared
|
|
1269
|
+
? prepared.summary
|
|
1270
|
+
: !ctx.compactionSummaryRoute
|
|
1271
|
+
? undefined
|
|
1272
|
+
: await summarizeWithModel({
|
|
1273
|
+
manager,
|
|
1274
|
+
olderMessages,
|
|
1275
|
+
previousSummaries: replaceableSummaries(systemMessages),
|
|
1276
|
+
config,
|
|
1277
|
+
...(pass.hookInstructions !== undefined
|
|
1278
|
+
? { hookInstructions: pass.hookInstructions }
|
|
1279
|
+
: {}),
|
|
1280
|
+
resolveRoute: async (signal) => {
|
|
1281
|
+
const route = await ctx.compactionSummaryRoute?.(signal)
|
|
1282
|
+
if (route) {
|
|
1283
|
+
modelRoute = {
|
|
1284
|
+
providerId: route.providerId ?? ctx.recorder.servingProviderId,
|
|
1285
|
+
model: route.model,
|
|
1286
|
+
}
|
|
1189
1287
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
}
|
|
1211
|
-
})
|
|
1288
|
+
return route
|
|
1289
|
+
},
|
|
1290
|
+
onUsage: (usage) => {
|
|
1291
|
+
if (modelRoute) ctx.recorder.accumulateUsage(usage, modelRoute)
|
|
1292
|
+
},
|
|
1293
|
+
signal: ctx.abortController.signal,
|
|
1294
|
+
onFallback: (reason, error) => {
|
|
1295
|
+
if (reason === 'no_route') return
|
|
1296
|
+
if (error !== undefined) noteCompactionError(ctx, error)
|
|
1297
|
+
ctx.log.warn(
|
|
1298
|
+
'The model-written compaction summary was not used — using the extracted state',
|
|
1299
|
+
{
|
|
1300
|
+
[NAMZU.TURN_ID]: ctx.recorder.turnId,
|
|
1301
|
+
'namzu.runtime.reason': reason,
|
|
1302
|
+
...(error !== undefined
|
|
1303
|
+
? { 'exception.message': error instanceof Error ? error.message : String(error) }
|
|
1304
|
+
: {}),
|
|
1305
|
+
},
|
|
1306
|
+
)
|
|
1307
|
+
},
|
|
1308
|
+
})
|
|
1212
1309
|
|
|
1213
1310
|
if (modelWritten !== undefined) {
|
|
1214
1311
|
compactedContent = modelWritten
|
|
@@ -1302,18 +1399,30 @@ async function runCompactionCheckInner(
|
|
|
1302
1399
|
// shown by an earlier pass, is not put back a second time.
|
|
1303
1400
|
const wholeSkills = handledSkillNames([...preservedSystem, ...retainedOlder, ...recentMessages])
|
|
1304
1401
|
const putBack: { rendered?: RenderedSkills } = {}
|
|
1402
|
+
const mounted = new Set(ctx.tools?.listNames?.() ?? [])
|
|
1305
1403
|
const restored = renderRestoredContext(manager.getState(), {
|
|
1306
1404
|
contextWindowTokens: budget,
|
|
1307
1405
|
wholeSkills,
|
|
1308
1406
|
onSkills: (rendered) => {
|
|
1309
1407
|
putBack.rendered = rendered
|
|
1310
1408
|
},
|
|
1409
|
+
// What the model was working with, named so it keeps using it: the tools it
|
|
1410
|
+
// called (this pass's span and what earlier summaries listed) and the
|
|
1411
|
+
// project instructions that stay whole after the summary.
|
|
1412
|
+
toolsUsed: collectToolsUsed(messages),
|
|
1413
|
+
instructionFiles: keptInstructionFiles([
|
|
1414
|
+
...preservedSystem,
|
|
1415
|
+
...retainedOlder,
|
|
1416
|
+
...recentMessages,
|
|
1417
|
+
]),
|
|
1418
|
+
// The roster of agents is in the description of the tool that hands work to
|
|
1419
|
+
// them, which every request carries; say so instead of copying it.
|
|
1420
|
+
...(mounted.has('create_task') ? { agentsTool: 'create_task' } : {}),
|
|
1311
1421
|
...(hostNote ? { hostNote } : {}),
|
|
1312
1422
|
})
|
|
1313
1423
|
if (restored) compactedContent = `${compactedContent}\n\n${restored}`
|
|
1314
1424
|
|
|
1315
1425
|
// The pointer is only worth a line when both tools it names are mounted.
|
|
1316
|
-
const mounted = new Set(ctx.tools?.listNames?.() ?? [])
|
|
1317
1426
|
const compactionMessage = buildCompactionMessage(
|
|
1318
1427
|
compactedContent,
|
|
1319
1428
|
mounted.has('search_conversation') && mounted.has('read_conversation')
|
|
@@ -1322,6 +1431,13 @@ async function runCompactionCheckInner(
|
|
|
1322
1431
|
// The tools the model had loaded stay loaded: the messages that loaded
|
|
1323
1432
|
// them are what this pass cuts.
|
|
1324
1433
|
collectRevealedTools(messages),
|
|
1434
|
+
{
|
|
1435
|
+
// The summary was written without the messages kept after it.
|
|
1436
|
+
keptTail: retainedOlder.length + recentMessages.length > 0,
|
|
1437
|
+
toolsUsed: collectToolsUsed(messages),
|
|
1438
|
+
...(ctx.sessionFile ? { sessionFile: ctx.sessionFile } : {}),
|
|
1439
|
+
...(ctx.compactionAskWhenUnclear ? { askWhenUnclear: true } : {}),
|
|
1440
|
+
},
|
|
1325
1441
|
)
|
|
1326
1442
|
|
|
1327
1443
|
const newMessages = [...preservedSystem, compactionMessage, ...retainedOlder, ...recentMessages]
|
|
@@ -1369,6 +1485,16 @@ async function runCompactionCheckInner(
|
|
|
1369
1485
|
pass.summary = compactedContent
|
|
1370
1486
|
|
|
1371
1487
|
const newEstimate = estimateTokens(ctx)
|
|
1488
|
+
// Said before anything below emits: an emit reconciles the log with the new
|
|
1489
|
+
// context, and the `compaction` record it writes carries what was said here.
|
|
1490
|
+
const durationMs = Math.max(0, Math.round(performance.now() - startedAt))
|
|
1491
|
+
ctx.recorder.noteCompaction?.({
|
|
1492
|
+
trigger: pass.precomputed ? 'prepared' : 'auto',
|
|
1493
|
+
tokensBefore: estimatedTokens,
|
|
1494
|
+
tokensAfter: newEstimate,
|
|
1495
|
+
durationMs,
|
|
1496
|
+
...(pass.precomputed ? { precomputed: true } : {}),
|
|
1497
|
+
})
|
|
1372
1498
|
|
|
1373
1499
|
// The provider's count described the PRE-compaction prompt; the window
|
|
1374
1500
|
// it just shrank to has not been sent yet, so the post number is
|
|
@@ -1422,6 +1548,10 @@ async function runCompactionCheckInner(
|
|
|
1422
1548
|
contextWindowTokens: budget,
|
|
1423
1549
|
windowSource: window.source,
|
|
1424
1550
|
reachedResetThreshold: reachedReset,
|
|
1551
|
+
trigger: pass.precomputed ? 'prepared' : 'auto',
|
|
1552
|
+
durationMs,
|
|
1553
|
+
...(pass.precomputed ? { precomputed: true } : {}),
|
|
1554
|
+
...(pass.cause ? { cause: pass.cause } : {}),
|
|
1425
1555
|
})
|
|
1426
1556
|
await emitContextUsageSnapshot(ctx, newEstimate, budget, window.source)
|
|
1427
1557
|
if (putBack.rendered && putBack.rendered.carried.length > 0) {
|
|
@@ -274,6 +274,12 @@ export interface IterationContext {
|
|
|
274
274
|
/** Live project policy; separate from human inbound continuation. */
|
|
275
275
|
readonly projectInstructionContext?: ProjectInstructionContext
|
|
276
276
|
|
|
277
|
+
/** The file the session's full record is stored in, named in a compaction summary so exact text can be found again. Absent for an in-memory session. */
|
|
278
|
+
readonly sessionFile?: string
|
|
279
|
+
|
|
280
|
+
/** Close a compaction summary with the wording that lets the model ask when something is unclear. */
|
|
281
|
+
readonly compactionAskWhenUnclear?: boolean
|
|
282
|
+
|
|
277
283
|
/** Host text appended to the restored context after a compaction. */
|
|
278
284
|
readonly postCompactionContext?: () => string | undefined | Promise<string | undefined>
|
|
279
285
|
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { askPreCompact } from '../../../../compaction/hooks.js'
|
|
2
|
+
import { replaceableSummaries, summarizeWithModel } from '../../../../compaction/model-summary.js'
|
|
3
|
+
import { planCompaction } from '../../../../compaction/plan.js'
|
|
4
|
+
import {
|
|
5
|
+
PRECOMPUTE_DEFAULT_ARM_FRACTION,
|
|
6
|
+
type PrecomputeArmSource,
|
|
7
|
+
type PrecomputeEntry,
|
|
8
|
+
armUsage,
|
|
9
|
+
claimPrepared,
|
|
10
|
+
messageDigest,
|
|
11
|
+
precomputeStoreFor,
|
|
12
|
+
} from '../../../../compaction/precompute.js'
|
|
13
|
+
import { estimateMessagesTokens } from '../../../../compaction/token-estimate.js'
|
|
14
|
+
import type { CompactionConfig } from '../../../../config/runtime.js'
|
|
15
|
+
import { CHARS_PER_TOKEN } from '../../../../constants/limits.js'
|
|
16
|
+
import { NAMZU } from '../../../../constants/telemetry/index.js'
|
|
17
|
+
import type { Message } from '../../../../types/message/index.js'
|
|
18
|
+
import type { IterationContext } from './context.js'
|
|
19
|
+
|
|
20
|
+
/** The message the conversation is on: the last one the person's side wrote. */
|
|
21
|
+
function currentPrompt(messages: readonly Message[]): Message | undefined {
|
|
22
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
23
|
+
if (messages[i]?.role === 'user') return messages[i]
|
|
24
|
+
}
|
|
25
|
+
return undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ArmInput {
|
|
29
|
+
readonly config: CompactionConfig
|
|
30
|
+
/** Context size and trigger, as shares of the window. */
|
|
31
|
+
readonly usage: number
|
|
32
|
+
readonly trigger: number
|
|
33
|
+
readonly budget: number
|
|
34
|
+
readonly estimatedTokens: number
|
|
35
|
+
readonly maxOutputTokens?: number
|
|
36
|
+
readonly source: PrecomputeArmSource
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Start a summary in the background when the context is near the trigger.
|
|
41
|
+
*
|
|
42
|
+
* Costs nothing until the context passes the arm point, so a short session
|
|
43
|
+
* never makes the request. Returns at once; the request runs on its own
|
|
44
|
+
* controller, tied to the session and not to this turn, so a turn that ends
|
|
45
|
+
* before the trigger leaves it running for the next one to use.
|
|
46
|
+
*/
|
|
47
|
+
export function armPrecompute(ctx: IterationContext, input: ArmInput): boolean {
|
|
48
|
+
const { config } = input
|
|
49
|
+
if (config.precompute === false) return false
|
|
50
|
+
if (config.strategy === 'disabled') return false
|
|
51
|
+
if (config.autoCompact === false) return false
|
|
52
|
+
// A reducer owns reduction, and without a model route the extracted state
|
|
53
|
+
// is free to build at the trigger: there is nothing to prepare.
|
|
54
|
+
if (ctx.contextReducer || config.strategy === 'sliding-window') return false
|
|
55
|
+
const route = ctx.compactionSummaryRoute
|
|
56
|
+
const manager = ctx.workingStateManager
|
|
57
|
+
if (!route || !manager) return false
|
|
58
|
+
if (
|
|
59
|
+
input.usage <
|
|
60
|
+
armUsage(input.trigger, config.precomputeArmFraction ?? PRECOMPUTE_DEFAULT_ARM_FRACTION)
|
|
61
|
+
)
|
|
62
|
+
return false
|
|
63
|
+
if (input.usage >= input.trigger) return false
|
|
64
|
+
|
|
65
|
+
const store = precomputeStoreFor(ctx.recorder.sessionId)
|
|
66
|
+
const live = [...ctx.recorder.messages]
|
|
67
|
+
const prompt = currentPrompt(live)
|
|
68
|
+
if (!store.canArm(prompt)) return false
|
|
69
|
+
|
|
70
|
+
const plan = planCompaction({
|
|
71
|
+
messages: live,
|
|
72
|
+
config,
|
|
73
|
+
contextWindowTokens: input.budget,
|
|
74
|
+
...(input.maxOutputTokens !== undefined ? { maxOutputTokens: input.maxOutputTokens } : {}),
|
|
75
|
+
estimatedTokens: input.estimatedTokens,
|
|
76
|
+
skipToolResultClear: true,
|
|
77
|
+
})
|
|
78
|
+
if (plan.kind !== 'plan') return false
|
|
79
|
+
|
|
80
|
+
const controller = new AbortController()
|
|
81
|
+
const entry: PrecomputeEntry = {
|
|
82
|
+
status: 'pending',
|
|
83
|
+
source: input.source,
|
|
84
|
+
covered: plan.olderMessages,
|
|
85
|
+
coveredDigests: plan.olderMessages.map(messageDigest),
|
|
86
|
+
windowTokens: input.budget,
|
|
87
|
+
prompt,
|
|
88
|
+
controller,
|
|
89
|
+
done: Promise.resolve(),
|
|
90
|
+
spend: [],
|
|
91
|
+
}
|
|
92
|
+
store.set(entry)
|
|
93
|
+
const providerFallback = ctx.recorder.servingProviderId
|
|
94
|
+
let modelRoute: { providerId: string; model: string } | undefined
|
|
95
|
+
const settle = (status: 'ready' | 'failed'): void => {
|
|
96
|
+
entry.status = status
|
|
97
|
+
if (status === 'failed') {
|
|
98
|
+
// A failed request may still have spent tokens. When the entry is still the
|
|
99
|
+
// store's (nobody has cancelled or taken it), its spend moves to the
|
|
100
|
+
// unbooked list now, because clearing the entry would drop it. A cancel or
|
|
101
|
+
// a claim returns the entry's spend itself.
|
|
102
|
+
if (store.entry === entry) store.addUnbooked(entry.spend.splice(0))
|
|
103
|
+
store.clear(entry)
|
|
104
|
+
// A stopped summary is not the summary's fault.
|
|
105
|
+
if (!controller.signal.aborted) store.countFailure(prompt)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
entry.done = (async () => {
|
|
109
|
+
try {
|
|
110
|
+
if (ctx.pluginManager) {
|
|
111
|
+
const answer = await askPreCompact(
|
|
112
|
+
ctx.pluginManager,
|
|
113
|
+
{
|
|
114
|
+
sessionId: ctx.recorder.sessionId,
|
|
115
|
+
turnId: ctx.recorder.turnId,
|
|
116
|
+
iteration: ctx.recorder.currentIteration,
|
|
117
|
+
signal: controller.signal,
|
|
118
|
+
emitEvent: ctx.emitEvent,
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
reason: 'threshold',
|
|
122
|
+
trigger: 'auto',
|
|
123
|
+
instructions: null,
|
|
124
|
+
tokensBefore: input.estimatedTokens,
|
|
125
|
+
contextWindowTokens: input.budget,
|
|
126
|
+
},
|
|
127
|
+
)
|
|
128
|
+
if (answer.refusal !== undefined) {
|
|
129
|
+
// A hook postponed this housekeeping: say so for this prompt only.
|
|
130
|
+
ctx.log.info('A pre_compact hook cancelled a summary prepared ahead', {
|
|
131
|
+
[NAMZU.TURN_ID]: ctx.recorder.turnId,
|
|
132
|
+
'namzu.hook.reason': answer.refusal,
|
|
133
|
+
})
|
|
134
|
+
settle('failed')
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
if (answer.instructions !== undefined) entry.hookInstructions = answer.instructions
|
|
138
|
+
}
|
|
139
|
+
const text = await summarizeWithModel({
|
|
140
|
+
manager,
|
|
141
|
+
olderMessages: plan.olderMessages,
|
|
142
|
+
previousSummaries: replaceableSummaries(plan.systemMessages),
|
|
143
|
+
config,
|
|
144
|
+
...(entry.hookInstructions !== undefined
|
|
145
|
+
? { hookInstructions: entry.hookInstructions }
|
|
146
|
+
: {}),
|
|
147
|
+
resolveRoute: async (signal) => {
|
|
148
|
+
const resolved = await route(signal)
|
|
149
|
+
if (resolved) {
|
|
150
|
+
modelRoute = {
|
|
151
|
+
providerId: resolved.providerId ?? providerFallback,
|
|
152
|
+
model: resolved.model,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return resolved
|
|
156
|
+
},
|
|
157
|
+
onUsage: (usage) => {
|
|
158
|
+
if (!modelRoute) return
|
|
159
|
+
// On the turn that armed it while that turn is open; otherwise kept
|
|
160
|
+
// on the entry for the pass that takes it, the one that stops it, or
|
|
161
|
+
// the next turn.
|
|
162
|
+
if (ctx.recorder.acceptsUsage) ctx.recorder.accumulateUsage(usage, modelRoute)
|
|
163
|
+
else entry.spend.push({ usage, route: modelRoute })
|
|
164
|
+
},
|
|
165
|
+
signal: controller.signal,
|
|
166
|
+
})
|
|
167
|
+
if (text === undefined) {
|
|
168
|
+
settle('failed')
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
entry.summary = text
|
|
172
|
+
settle('ready')
|
|
173
|
+
} catch {
|
|
174
|
+
settle('failed')
|
|
175
|
+
}
|
|
176
|
+
})()
|
|
177
|
+
ctx.log.info('Preparing a compaction summary ahead of the trigger', {
|
|
178
|
+
[NAMZU.TURN_ID]: ctx.recorder.turnId,
|
|
179
|
+
'namzu.runtime.arm_source': input.source,
|
|
180
|
+
'namzu.runtime.context_tokens': input.estimatedTokens,
|
|
181
|
+
'namzu.runtime.covered_messages': plan.olderMessages.length,
|
|
182
|
+
})
|
|
183
|
+
return true
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Put on this turn what prepared summaries spent that no turn has booked: a
|
|
188
|
+
* request that finished after the turn that started it had ended, one stopped
|
|
189
|
+
* by a pass a person asked for, one a pass refused or found nothing to do with.
|
|
190
|
+
* A summary still in flight is left alone (nothing it has spent is known yet),
|
|
191
|
+
* and one still waiting to be used keeps its summary and gives up only its cost.
|
|
192
|
+
*/
|
|
193
|
+
export function bookPreparedSpend(ctx: IterationContext): void {
|
|
194
|
+
if (!ctx.recorder.acceptsUsage) return
|
|
195
|
+
const store = precomputeStoreFor(ctx.recorder.sessionId)
|
|
196
|
+
if (!store.holdsSpend()) return
|
|
197
|
+
for (const { usage, route } of store.takeSpend()) ctx.recorder.accumulateUsage(usage, route)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface TakeInput {
|
|
201
|
+
/** The messages the pass is about to cut; a staged clear, when there is one. Same positions as the live context. */
|
|
202
|
+
readonly messages: readonly Message[]
|
|
203
|
+
/** The leading system messages. */
|
|
204
|
+
readonly floorLength: number
|
|
205
|
+
readonly hookInstructions: string | undefined
|
|
206
|
+
/** Tokens the request carries beyond its messages (the tool catalogue). */
|
|
207
|
+
readonly overheadTokens: number
|
|
208
|
+
readonly budget: number
|
|
209
|
+
/** The trigger as tokens: a swap that leaves the context at or above it is refused. */
|
|
210
|
+
readonly triggerTokens: number
|
|
211
|
+
/** Take only a summary that is already written; never wait for one in flight. */
|
|
212
|
+
readonly readyOnly?: boolean
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface TakenSummary {
|
|
216
|
+
readonly summary: string
|
|
217
|
+
/** The new cut: everything before it is summarised, everything from it on stays. */
|
|
218
|
+
readonly keepStart: number
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Take the prepared summary for the pass that is about to write one.
|
|
223
|
+
*
|
|
224
|
+
* Waits for one still in flight (it is the summary this pass would have
|
|
225
|
+
* written). A refusal consumes the entry, counts toward the failure cap and
|
|
226
|
+
* returns `undefined`, and the caller writes a fresh summary as before. What
|
|
227
|
+
* the prepared request spent goes onto the turn that takes it.
|
|
228
|
+
*/
|
|
229
|
+
export async function takePrepared(
|
|
230
|
+
ctx: IterationContext,
|
|
231
|
+
input: TakeInput,
|
|
232
|
+
): Promise<TakenSummary | undefined> {
|
|
233
|
+
const store = precomputeStoreFor(ctx.recorder.sessionId)
|
|
234
|
+
const pending = store.entry?.status === 'pending'
|
|
235
|
+
if (pending && !input.readyOnly) {
|
|
236
|
+
ctx.log.info('Waiting for the summary prepared ahead', {
|
|
237
|
+
[NAMZU.TURN_ID]: ctx.recorder.turnId,
|
|
238
|
+
})
|
|
239
|
+
}
|
|
240
|
+
const claimed = await claimPrepared(store, ctx.recorder.messages, {
|
|
241
|
+
floorLength: input.floorLength,
|
|
242
|
+
hookInstructions: input.hookInstructions,
|
|
243
|
+
limitTokens: input.triggerTokens,
|
|
244
|
+
windowTokens: input.budget,
|
|
245
|
+
awaitPending: !input.readyOnly,
|
|
246
|
+
waitFor: (promise) => raceWithAbort(promise, ctx.abortController.signal),
|
|
247
|
+
tokensAfterSwap: (keepStart, summary) =>
|
|
248
|
+
estimateMessagesTokens(input.messages.slice(0, input.floorLength)) +
|
|
249
|
+
estimateMessagesTokens(input.messages.slice(keepStart)) +
|
|
250
|
+
Math.ceil(summary.length / CHARS_PER_TOKEN) +
|
|
251
|
+
input.overheadTokens,
|
|
252
|
+
})
|
|
253
|
+
for (const { usage, route } of claimed.spend) ctx.recorder.accumulateUsage(usage, route)
|
|
254
|
+
if (claimed.ok) return { summary: claimed.summary, keepStart: claimed.keepStart }
|
|
255
|
+
if (claimed.reason !== 'none') {
|
|
256
|
+
ctx.log.info('The summary prepared ahead was not used', {
|
|
257
|
+
[NAMZU.TURN_ID]: ctx.recorder.turnId,
|
|
258
|
+
'namzu.runtime.reason': claimed.reason,
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
return undefined
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Wait for `promise`, or throw when `signal` aborts first. Neither outcome stops `promise`. */
|
|
265
|
+
async function raceWithAbort(promise: Promise<void>, signal: AbortSignal): Promise<void> {
|
|
266
|
+
signal.throwIfAborted()
|
|
267
|
+
let onAbort: (() => void) | undefined
|
|
268
|
+
const aborted = new Promise<never>((_, reject) => {
|
|
269
|
+
onAbort = () => reject(signal.reason ?? new Error('aborted'))
|
|
270
|
+
signal.addEventListener('abort', onAbort, { once: true })
|
|
271
|
+
})
|
|
272
|
+
try {
|
|
273
|
+
await Promise.race([promise, aborted])
|
|
274
|
+
} finally {
|
|
275
|
+
if (onAbort) signal.removeEventListener('abort', onAbort)
|
|
276
|
+
}
|
|
277
|
+
}
|