@namzu/sdk 72.0.0 → 74.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 +61 -0
- package/README.md +34 -34
- 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 +7 -0
- package/dist/authorization/protected-paths.d.ts.map +1 -1
- package/dist/authorization/protected-paths.js +11 -0
- package/dist/authorization/protected-paths.js.map +1 -1
- package/dist/authorization/readonly-commands.d.ts +23 -0
- package/dist/authorization/readonly-commands.d.ts.map +1 -1
- package/dist/authorization/readonly-commands.js +89 -3
- package/dist/authorization/readonly-commands.js.map +1 -1
- package/dist/authorization/shell-paths.d.ts +8 -0
- package/dist/authorization/shell-paths.d.ts.map +1 -1
- package/dist/authorization/shell-paths.js +125 -2
- package/dist/authorization/shell-paths.js.map +1 -1
- package/dist/bridge/acp/permission.d.ts +6 -0
- package/dist/bridge/acp/permission.d.ts.map +1 -1
- package/dist/bridge/acp/permission.js.map +1 -1
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +21 -3
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/compaction/context-window.d.ts.map +1 -1
- package/dist/compaction/context-window.js +11 -0
- package/dist/compaction/context-window.js.map +1 -1
- package/dist/compaction/manual.d.ts +16 -1
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +75 -13
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/plan.d.ts +2 -1
- package/dist/compaction/plan.d.ts.map +1 -1
- package/dist/compaction/plan.js +6 -5
- package/dist/compaction/plan.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/retained-images.d.ts +16 -0
- package/dist/compaction/retained-images.d.ts.map +1 -0
- package/dist/compaction/retained-images.js +113 -0
- package/dist/compaction/retained-images.js.map +1 -0
- package/dist/compaction/token-estimate.d.ts +8 -3
- package/dist/compaction/token-estimate.d.ts.map +1 -1
- package/dist/compaction/token-estimate.js +76 -8
- package/dist/compaction/token-estimate.js.map +1 -1
- package/dist/compaction/tool-result-editing.d.ts.map +1 -1
- package/dist/compaction/tool-result-editing.js +15 -8
- package/dist/compaction/tool-result-editing.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 +17 -0
- package/dist/config/runtime.js.map +1 -1
- package/dist/constants/acp/index.d.ts +2 -0
- package/dist/constants/acp/index.d.ts.map +1 -1
- package/dist/constants/acp/index.js +2 -0
- package/dist/constants/acp/index.js.map +1 -1
- package/dist/manager/agent/lifecycle.d.ts.map +1 -1
- package/dist/manager/agent/lifecycle.js +8 -0
- 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/index.d.ts +2 -2
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +1 -1
- package/dist/plugin/index.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +30 -0
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +35 -0
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/pricing/catalogue.generated.d.ts.map +1 -1
- package/dist/pricing/catalogue.generated.js +178 -1
- package/dist/pricing/catalogue.generated.js.map +1 -1
- package/dist/provider/attribution.js +1 -1
- package/dist/provider/attribution.js.map +1 -1
- package/dist/public-runtime.d.ts +9 -5
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +12 -4
- 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 +3 -2
- package/dist/public-types.d.ts.map +1 -1
- package/dist/rag/embedding.d.ts +5 -1
- package/dist/rag/embedding.d.ts.map +1 -1
- package/dist/rag/embedding.js +22 -7
- package/dist/rag/embedding.js.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/query/abandon-turn.d.ts.map +1 -1
- package/dist/runtime/query/abandon-turn.js +9 -0
- package/dist/runtime/query/abandon-turn.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 +19 -3
- package/dist/runtime/query/compact-session.d.ts.map +1 -1
- package/dist/runtime/query/compact-session.js +180 -93
- package/dist/runtime/query/compact-session.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.map +1 -1
- package/dist/runtime/query/index.js +1 -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 +41 -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 +203 -45
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +2 -0
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js +1 -0
- 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/iteration/request-prefix.d.ts +21 -0
- package/dist/runtime/query/iteration/request-prefix.d.ts.map +1 -0
- package/dist/runtime/query/iteration/request-prefix.js +111 -0
- package/dist/runtime/query/iteration/request-prefix.js.map +1 -0
- package/dist/runtime/query/review-policy.d.ts +11 -3
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +54 -3
- package/dist/runtime/query/review-policy.js.map +1 -1
- 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/sandbox/exec-stdin.d.ts +21 -0
- package/dist/sandbox/exec-stdin.d.ts.map +1 -0
- package/dist/sandbox/exec-stdin.js +40 -0
- package/dist/sandbox/exec-stdin.js.map +1 -0
- package/dist/sandbox/file-walk.d.ts +12 -7
- package/dist/sandbox/file-walk.d.ts.map +1 -1
- package/dist/sandbox/file-walk.js +13 -4
- package/dist/sandbox/file-walk.js.map +1 -1
- package/dist/sandbox/ignore-matcher.d.ts +1 -0
- package/dist/sandbox/ignore-matcher.d.ts.map +1 -1
- package/dist/sandbox/ignore-matcher.js.map +1 -1
- package/dist/sandbox/ignore-source.js +1 -1
- package/dist/sandbox/index.d.ts +3 -2
- package/dist/sandbox/index.d.ts.map +1 -1
- package/dist/sandbox/index.js +2 -1
- package/dist/sandbox/index.js.map +1 -1
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +2 -0
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/session/paths.d.ts +6 -0
- package/dist/session/paths.d.ts.map +1 -1
- package/dist/session/paths.js +7 -0
- package/dist/session/paths.js.map +1 -1
- package/dist/session-query/index.d.ts +1 -0
- package/dist/session-query/index.d.ts.map +1 -1
- package/dist/session-query/index.js +26 -15
- package/dist/session-query/index.js.map +1 -1
- package/dist/store/evidence/disk.js +3 -1
- package/dist/store/evidence/disk.js.map +1 -1
- package/dist/store/evidence/index-page.d.ts +1 -0
- package/dist/store/evidence/index-page.d.ts.map +1 -1
- package/dist/store/evidence/index-page.js +8 -0
- package/dist/store/evidence/index-page.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/memory/operation-lock.d.ts.map +1 -1
- package/dist/store/memory/operation-lock.js +15 -1
- package/dist/store/memory/operation-lock.js.map +1 -1
- package/dist/store/session-log/core.d.ts.map +1 -1
- package/dist/store/session-log/core.js +23 -0
- package/dist/store/session-log/core.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/computer-use-image.d.ts +9 -0
- package/dist/tools/builtins/computer-use-image.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use-image.js +21 -0
- package/dist/tools/builtins/computer-use-image.js.map +1 -1
- package/dist/tools/builtins/computer-use.d.ts +1 -9
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +2 -22
- package/dist/tools/builtins/computer-use.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 +25 -0
- 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 +14 -0
- package/dist/toolsets/manager.d.ts.map +1 -1
- package/dist/toolsets/manager.js +60 -7
- package/dist/toolsets/manager.js.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 +6 -0
- package/dist/types/agent/task.d.ts.map +1 -1
- package/dist/types/common/index.d.ts +20 -0
- package/dist/types/common/index.d.ts.map +1 -1
- package/dist/types/common/index.js +4 -1
- package/dist/types/common/index.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 +11 -0
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/rag/embedding.d.ts +4 -0
- package/dist/types/rag/embedding.d.ts.map +1 -1
- package/dist/types/sandbox/index.d.ts +19 -0
- package/dist/types/sandbox/index.d.ts.map +1 -1
- package/dist/types/sandbox/index.js.map +1 -1
- package/dist/types/session/events.d.ts +55 -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 +81 -1
- package/dist/types/session/records.d.ts.map +1 -1
- package/dist/types/session/records.js +33 -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/utils/cost.d.ts +15 -0
- package/dist/utils/cost.d.ts.map +1 -1
- package/dist/utils/cost.js +24 -1
- package/dist/utils/cost.js.map +1 -1
- package/package.json +2 -2
- package/src/agents/examples/SupervisorAgent.ts +5 -0
- package/src/authorization/protected-paths.ts +11 -0
- package/src/authorization/readonly-commands.ts +85 -3
- package/src/authorization/shell-paths.ts +141 -3
- package/src/bridge/acp/permission.ts +6 -0
- package/src/bridge/acp/server.ts +20 -1
- package/src/compaction/context-window.ts +11 -0
- package/src/compaction/manual.ts +103 -17
- package/src/compaction/plan.ts +11 -5
- package/src/compaction/precompute.ts +390 -0
- package/src/compaction/retained-images.ts +130 -0
- package/src/compaction/token-estimate.ts +86 -8
- package/src/compaction/tool-result-editing.ts +20 -8
- package/src/config/runtime.ts +17 -0
- package/src/constants/acp/index.ts +2 -0
- package/src/manager/agent/lifecycle.ts +9 -0
- 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/index.ts +2 -0
- package/src/plugin/shell-hook.ts +60 -0
- package/src/pricing/catalogue.generated.ts +178 -1
- package/src/pricing/rates.source.json +155 -2
- package/src/provider/attribution.ts +1 -1
- package/src/public-runtime.ts +48 -0
- package/src/public-tools.ts +2 -0
- package/src/public-types.ts +8 -0
- package/src/rag/embedding.ts +23 -7
- package/src/registry/command/kernel-commands.ts +62 -3
- package/src/registry/tool/execute.ts +14 -0
- package/src/runtime/query/abandon-turn.ts +9 -0
- package/src/runtime/query/budget-nudge.ts +92 -0
- package/src/runtime/query/compact-session.ts +242 -87
- package/src/runtime/query/guard.ts +22 -0
- package/src/runtime/query/index.ts +1 -0
- package/src/runtime/query/iteration/index.ts +40 -1
- package/src/runtime/query/iteration/phases/compaction.ts +224 -46
- package/src/runtime/query/iteration/phases/context.ts +3 -0
- package/src/runtime/query/iteration/phases/precompute.ts +277 -0
- package/src/runtime/query/iteration/request-prefix.ts +161 -0
- package/src/runtime/query/review-policy.ts +79 -11
- package/src/runtime/query/turn/outcome-ledger.ts +34 -0
- package/src/sandbox/exec-stdin.ts +54 -0
- package/src/sandbox/file-walk.ts +25 -18
- package/src/sandbox/ignore-matcher.ts +1 -0
- package/src/sandbox/ignore-source.ts +1 -1
- package/src/sandbox/index.ts +8 -2
- package/src/sandbox/provider/local.ts +2 -0
- package/src/session/paths.ts +9 -0
- package/src/session-query/index.ts +23 -14
- package/src/store/evidence/disk.ts +3 -1
- package/src/store/evidence/index-page.ts +11 -0
- package/src/store/goal/gates.ts +738 -0
- package/src/store/goal/index.ts +109 -0
- package/src/store/memory/operation-lock.ts +19 -1
- package/src/store/session-log/core.ts +26 -0
- package/src/tools/builtins/bash.ts +1 -1
- package/src/tools/builtins/browser.ts +4 -0
- package/src/tools/builtins/computer-use-image.ts +20 -0
- package/src/tools/builtins/computer-use.ts +1 -20
- 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 +37 -10
- 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 +59 -8
- package/src/types/agent/supervisor.ts +11 -0
- package/src/types/agent/task.ts +7 -0
- package/src/types/common/index.ts +26 -1
- package/src/types/goal/index.ts +72 -0
- package/src/types/hitl/index.ts +11 -0
- package/src/types/rag/embedding.ts +7 -0
- package/src/types/sandbox/index.ts +19 -0
- package/src/types/session/events.ts +56 -0
- package/src/types/session/outcome.ts +16 -1
- package/src/types/session/records.ts +41 -0
- package/src/types/tool/index.ts +7 -0
- package/src/utils/cost.ts +44 -1
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The line a run reads shortly before one of its own limits ends it.
|
|
3
|
+
*
|
|
4
|
+
* Without it the iteration cap and the time limit cut a run off between two
|
|
5
|
+
* calls with no word to the model, which is the moment it most needs to leave
|
|
6
|
+
* its work in a state someone can pick up. The line is a request, never a
|
|
7
|
+
* change of limit: nothing here moves the cap or the clock.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Where the line is due: first when the remaining share is small, again when it is smaller. */
|
|
11
|
+
export type BudgetNudgeStage = 0 | 1 | 2
|
|
12
|
+
|
|
13
|
+
export interface BudgetLeft {
|
|
14
|
+
/** Calls left before the iteration cap, counting the one being prepared. */
|
|
15
|
+
iterations?: { left: number; total: number }
|
|
16
|
+
/** Milliseconds left before the time limit. */
|
|
17
|
+
timeMs?: { left: number; total: number }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const FIRST_ITERATION_FLOOR = 5
|
|
21
|
+
const SECOND_ITERATION_FLOOR = 2
|
|
22
|
+
const FIRST_TIME_FLOOR_MS = 2 * 60_000
|
|
23
|
+
const SECOND_TIME_FLOOR_MS = 30_000
|
|
24
|
+
const FIRST_SHARE = 0.1
|
|
25
|
+
const SECOND_SHARE = 0.03
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The larger of the fixed floor and the share, held to half of what the
|
|
29
|
+
* stage before it allowed so a short budget is not nagged from its first
|
|
30
|
+
* call: a ten-call run is told at four calls left, never at the start.
|
|
31
|
+
*/
|
|
32
|
+
function threshold(total: number, floor: number, share: number, ceiling: number): number {
|
|
33
|
+
return Math.min(Math.max(floor, total * share), ceiling)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function reached(left: number, total: number, floors: [number, number], shares: [number, number]) {
|
|
37
|
+
const first = threshold(total, floors[0], shares[0], total / 2)
|
|
38
|
+
const second = threshold(total, floors[1], shares[1], first / 2)
|
|
39
|
+
if (left <= second) return 2
|
|
40
|
+
if (left <= first) return 1
|
|
41
|
+
return 0
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The stage the remaining budget has reached; 0 when no limit is near or none is set. */
|
|
45
|
+
export function budgetNudgeStage(left: BudgetLeft): BudgetNudgeStage {
|
|
46
|
+
let stage: BudgetNudgeStage = 0
|
|
47
|
+
if (left.iterations && left.iterations.total > 0) {
|
|
48
|
+
stage = Math.max(
|
|
49
|
+
stage,
|
|
50
|
+
reached(
|
|
51
|
+
left.iterations.left,
|
|
52
|
+
left.iterations.total,
|
|
53
|
+
[FIRST_ITERATION_FLOOR, SECOND_ITERATION_FLOOR],
|
|
54
|
+
[FIRST_SHARE, SECOND_SHARE],
|
|
55
|
+
),
|
|
56
|
+
) as BudgetNudgeStage
|
|
57
|
+
}
|
|
58
|
+
if (left.timeMs && Number.isFinite(left.timeMs.total) && left.timeMs.total > 0) {
|
|
59
|
+
stage = Math.max(
|
|
60
|
+
stage,
|
|
61
|
+
reached(
|
|
62
|
+
left.timeMs.left,
|
|
63
|
+
left.timeMs.total,
|
|
64
|
+
[FIRST_TIME_FLOOR_MS, SECOND_TIME_FLOOR_MS],
|
|
65
|
+
[FIRST_SHARE, SECOND_SHARE],
|
|
66
|
+
),
|
|
67
|
+
) as BudgetNudgeStage
|
|
68
|
+
}
|
|
69
|
+
return stage
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function duration(ms: number): string {
|
|
73
|
+
const seconds = Math.max(0, Math.round(ms / 1000))
|
|
74
|
+
if (seconds < 90) return `${seconds} seconds`
|
|
75
|
+
return `${Math.round(seconds / 60)} minutes`
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The one line: how much is left, and what to do with it. */
|
|
79
|
+
export function budgetNudgeText(left: BudgetLeft, stage: BudgetNudgeStage): string | null {
|
|
80
|
+
if (stage === 0) return null
|
|
81
|
+
const parts: string[] = []
|
|
82
|
+
if (left.iterations && left.iterations.total > 0) {
|
|
83
|
+
const n = Math.max(0, left.iterations.left)
|
|
84
|
+
parts.push(`${n} more ${n === 1 ? 'step' : 'steps'}`)
|
|
85
|
+
}
|
|
86
|
+
if (left.timeMs && Number.isFinite(left.timeMs.total) && left.timeMs.total > 0) {
|
|
87
|
+
parts.push(`about ${duration(left.timeMs.left)}`)
|
|
88
|
+
}
|
|
89
|
+
if (parts.length === 0) return null
|
|
90
|
+
const urgency = stage === 2 ? 'Very little is left' : 'This run is nearing its limit'
|
|
91
|
+
return `${urgency}: ${parts.join(' and ')} before it is stopped. Leave the work in a working state, write down what is unfinished, then finish. The limit does not move.`
|
|
92
|
+
}
|
|
@@ -5,8 +5,23 @@ import {
|
|
|
5
5
|
compactNow,
|
|
6
6
|
compactRegion,
|
|
7
7
|
} from '../../compaction/manual.js'
|
|
8
|
-
import {
|
|
9
|
-
|
|
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 {
|
|
20
|
+
DiskSessionLog,
|
|
21
|
+
type SessionLease,
|
|
22
|
+
type SessionLog,
|
|
23
|
+
} from '../../store/session-log/index.js'
|
|
24
|
+
import { accumulateTokenUsage } from '../../types/common/index.js'
|
|
10
25
|
import { NamzuError } from '../../types/errors/index.js'
|
|
11
26
|
import type { MessageId, SessionId } from '../../types/ids/index.js'
|
|
12
27
|
import type { Message } from '../../types/message/index.js'
|
|
@@ -29,6 +44,21 @@ export interface CompactSessionParams
|
|
|
29
44
|
* lapses. Recorded on the `compaction` record.
|
|
30
45
|
*/
|
|
31
46
|
readonly trigger?: 'manual' | 'idle'
|
|
47
|
+
/**
|
|
48
|
+
* What an `'idle'` pass does with a summary prepared ahead (see
|
|
49
|
+
* `CompactionConfig.precompute`). Absent: it is used when it is still valid
|
|
50
|
+
* (waiting for one still being written) and a fresh summary is written when
|
|
51
|
+
* it is not. `'only'`: used or nothing, a refusal ends the pass as a no-op
|
|
52
|
+
* (`null`). `false`: never used, and stopped first. Ignored for `'manual'`,
|
|
53
|
+
* which always stops it.
|
|
54
|
+
*/
|
|
55
|
+
readonly preparedSummary?: 'only' | false
|
|
56
|
+
/**
|
|
57
|
+
* Close a turn whose writer is gone before a manual pass, keeping its
|
|
58
|
+
* recorded messages. Ignored for idle passes. Running and paused turns
|
|
59
|
+
* are always refused.
|
|
60
|
+
*/
|
|
61
|
+
readonly abandonInterrupted?: boolean
|
|
32
62
|
/** Where to find the session's log, and the lease to append under. */
|
|
33
63
|
readonly locator?: SessionLocatorOptions
|
|
34
64
|
}
|
|
@@ -39,51 +69,69 @@ export interface CompactSessionParams
|
|
|
39
69
|
* with no `turnId`: the messages the pass kept are named by id, the summary
|
|
40
70
|
* is recorded in full, and the shed messages stay in the log for audit.
|
|
41
71
|
*
|
|
42
|
-
* Refused while a turn is active (`TurnInProgressError`)
|
|
43
|
-
*
|
|
72
|
+
* Refused while a turn is active (`TurnInProgressError`), unless a manual
|
|
73
|
+
* pass opts into closing an interrupted turn. `null` when there was nothing
|
|
74
|
+
* to compact.
|
|
44
75
|
*/
|
|
45
76
|
export async function compactSession(
|
|
46
77
|
params: CompactSessionParams,
|
|
47
78
|
): Promise<CompactionResult | null> {
|
|
48
|
-
const {
|
|
79
|
+
const {
|
|
80
|
+
sessionId,
|
|
81
|
+
locator = {},
|
|
82
|
+
trigger = 'manual',
|
|
83
|
+
hooks,
|
|
84
|
+
preparedSummary,
|
|
85
|
+
abandonInterrupted,
|
|
86
|
+
...input
|
|
87
|
+
} = params
|
|
49
88
|
const log = await locateSessionLog(sessionId, locator)
|
|
50
89
|
return withSessionLease(log, locator.lease, async (lease) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
await prepareCompaction(log, lease, trigger === 'manual' && abandonInterrupted)
|
|
91
|
+
// One summary at a time. A host compacting an idle conversation takes the
|
|
92
|
+
// summary prepared ahead when it is still valid (or waits for the one being
|
|
93
|
+
// written); any other pass stops it first.
|
|
94
|
+
const usePrepared = trigger === 'idle' && preparedSummary !== false
|
|
95
|
+
const store = precomputeStoreFor(sessionId)
|
|
96
|
+
const owed = [...store.drainUnbooked(), ...(usePrepared ? [] : await store.cancel())]
|
|
97
|
+
return carrying(store, owed, async () => {
|
|
98
|
+
const history = await readFoldedHistory(log)
|
|
99
|
+
const ids = new Map<Message, MessageId>()
|
|
100
|
+
for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
|
|
101
|
+
const startedAt = performance.now()
|
|
102
|
+
const result = await compactNow({
|
|
103
|
+
// The session's own file, so the summary says where exact text is stored.
|
|
104
|
+
...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
|
|
105
|
+
...input,
|
|
106
|
+
messages: history.map((entry) => entry.message),
|
|
107
|
+
...(hooks ? { hooks: { ...hooks, sessionId, trigger } } : {}),
|
|
108
|
+
...(usePrepared
|
|
109
|
+
? { prepared: { sessionId, ...(preparedSummary === 'only' ? { only: true } : {}) } }
|
|
110
|
+
: {}),
|
|
72
111
|
})
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
keptMessageIds,
|
|
83
|
-
|
|
84
|
-
|
|
112
|
+
if (!result) return null
|
|
113
|
+
const head = await log.head()
|
|
114
|
+
if (!head) {
|
|
115
|
+
throw new NamzuError({
|
|
116
|
+
code: 'not_found',
|
|
117
|
+
message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
|
|
118
|
+
details: { sessionId },
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
const { summary, keptMessageIds } = compactionShape(result.messages, ids)
|
|
122
|
+
await appendCompaction(log, lease, {
|
|
123
|
+
strategy: input.config.strategy,
|
|
124
|
+
trigger,
|
|
125
|
+
replacesSeqRange: [1, head.pointer.seq],
|
|
126
|
+
summary,
|
|
127
|
+
keptMessageIds,
|
|
128
|
+
before: history.map((entry) => entry.message),
|
|
129
|
+
after: result.messages,
|
|
130
|
+
startedAt,
|
|
131
|
+
precomputed: result.precomputed === true,
|
|
132
|
+
})
|
|
133
|
+
return result
|
|
85
134
|
})
|
|
86
|
-
return result
|
|
87
135
|
})
|
|
88
136
|
}
|
|
89
137
|
|
|
@@ -114,69 +162,176 @@ export interface CompactSessionRangeParams extends CompactSessionParams {
|
|
|
114
162
|
* context (a compaction may already have folded it away), and
|
|
115
163
|
* `invalid_config` when a boundary would split a tool call from its result.
|
|
116
164
|
* `null` when the span holds nothing worth summarising; nothing is appended
|
|
117
|
-
* then
|
|
165
|
+
* then except to close an interrupted turn when explicitly requested.
|
|
118
166
|
*/
|
|
119
167
|
export async function compactSessionRange(
|
|
120
168
|
params: CompactSessionRangeParams,
|
|
121
169
|
): Promise<CompactionResult | null> {
|
|
122
|
-
const { sessionId, locator = {}, from, until, hooks, ...input } = params
|
|
170
|
+
const { sessionId, locator = {}, from, until, hooks, abandonInterrupted, ...input } = params
|
|
123
171
|
const log = await locateSessionLog(sessionId, locator)
|
|
124
172
|
return withSessionLease(log, locator.lease, async (lease) => {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
173
|
+
await prepareCompaction(log, lease, abandonInterrupted)
|
|
174
|
+
// One summary at a time: a pass asked for between turns stops one being prepared.
|
|
175
|
+
const store = precomputeStoreFor(sessionId)
|
|
176
|
+
const owed = [...store.drainUnbooked(), ...(await store.cancel())]
|
|
177
|
+
return carrying(store, owed, async () => {
|
|
178
|
+
const history = await readFoldedHistory(log)
|
|
179
|
+
const position = (id: MessageId | undefined, fallback: number): number => {
|
|
180
|
+
if (id === undefined) return fallback
|
|
181
|
+
const at = history.findIndex((entry) => entry.messageId === id)
|
|
182
|
+
if (at < 0) {
|
|
183
|
+
throw new NamzuError({
|
|
184
|
+
code: 'not_found',
|
|
185
|
+
message: `Message ${id} is not in session ${sessionId}'s current context; it may already have been summarised.`,
|
|
186
|
+
details: { sessionId, messageId: id },
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
return at
|
|
190
|
+
}
|
|
191
|
+
const start = position(from, 0)
|
|
192
|
+
const end = position(until, history.length)
|
|
193
|
+
const ids = new Map<Message, MessageId>()
|
|
194
|
+
for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
|
|
195
|
+
const startedAt = performance.now()
|
|
196
|
+
const result = await compactRegion({
|
|
197
|
+
// The session's own file, so the summary says where exact text is stored.
|
|
198
|
+
...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
|
|
199
|
+
...input,
|
|
200
|
+
messages: history.map((entry) => entry.message),
|
|
201
|
+
start,
|
|
202
|
+
end,
|
|
203
|
+
...(hooks ? { hooks: { ...hooks, sessionId, trigger: 'manual' as const } } : {}),
|
|
204
|
+
})
|
|
205
|
+
if (!result) return null
|
|
206
|
+
const head = await log.head()
|
|
207
|
+
if (!head) {
|
|
134
208
|
throw new NamzuError({
|
|
135
209
|
code: 'not_found',
|
|
136
|
-
message: `
|
|
137
|
-
details: { sessionId
|
|
210
|
+
message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
|
|
211
|
+
details: { sessionId },
|
|
138
212
|
})
|
|
139
213
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
start,
|
|
152
|
-
end,
|
|
153
|
-
...(hooks ? { hooks: { ...hooks, sessionId, trigger: 'manual' as const } } : {}),
|
|
154
|
-
})
|
|
155
|
-
if (!result) return null
|
|
156
|
-
const head = await log.head()
|
|
157
|
-
if (!head) {
|
|
158
|
-
throw new NamzuError({
|
|
159
|
-
code: 'not_found',
|
|
160
|
-
message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
|
|
161
|
-
details: { sessionId },
|
|
214
|
+
const { summary, keptMessageIds } = compactionShape(result.messages, ids)
|
|
215
|
+
await appendCompaction(log, lease, {
|
|
216
|
+
strategy: input.config.strategy,
|
|
217
|
+
trigger: 'manual',
|
|
218
|
+
replacesSeqRange: [1, head.pointer.seq],
|
|
219
|
+
summary,
|
|
220
|
+
keptMessageIds,
|
|
221
|
+
before: history.map((entry) => entry.message),
|
|
222
|
+
after: result.messages,
|
|
223
|
+
startedAt,
|
|
224
|
+
precomputed: false,
|
|
162
225
|
})
|
|
163
|
-
|
|
164
|
-
const { summary, keptMessageIds } = compactionShape(result.messages, ids)
|
|
165
|
-
await log.append(lease, {
|
|
166
|
-
type: 'compaction',
|
|
167
|
-
compactionId: generateMessageId(),
|
|
168
|
-
strategy: input.config.strategy,
|
|
169
|
-
trigger: 'manual',
|
|
170
|
-
replacesSeqRange: [1, head.pointer.seq],
|
|
171
|
-
summary,
|
|
172
|
-
keptMessageIds,
|
|
173
|
-
tokensBefore: 0,
|
|
174
|
-
tokensAfter: 0,
|
|
226
|
+
return result
|
|
175
227
|
})
|
|
176
|
-
return result
|
|
177
228
|
})
|
|
178
229
|
}
|
|
179
230
|
|
|
231
|
+
/** Check and close an interrupted turn under the same lease used for compaction. */
|
|
232
|
+
async function prepareCompaction(
|
|
233
|
+
log: SessionLog,
|
|
234
|
+
lease: SessionLease,
|
|
235
|
+
abandonInterrupted?: boolean,
|
|
236
|
+
): Promise<void> {
|
|
237
|
+
const active = await log.activeTurn({ lease })
|
|
238
|
+
if (!active) return
|
|
239
|
+
if (active.state === 'interrupted' && abandonInterrupted) {
|
|
240
|
+
await log.abandonTurn(
|
|
241
|
+
lease,
|
|
242
|
+
active.turnId,
|
|
243
|
+
'The reply was interrupted before manual compaction.',
|
|
244
|
+
)
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
throw new TurnInProgressError({
|
|
248
|
+
sessionId: log.sessionId,
|
|
249
|
+
activeTurnId: active.turnId,
|
|
250
|
+
state: active.state,
|
|
251
|
+
})
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Run a between-turns pass that owes a ledger what prepared summaries spent
|
|
256
|
+
* (one this pass stopped, one that outlived its turn): the cost joins the
|
|
257
|
+
* result's `usage`, and a pass that ends without a result, or fails, leaves it
|
|
258
|
+
* with the session for the next pass or turn to book. Every prepared summary
|
|
259
|
+
* is booked once.
|
|
260
|
+
*/
|
|
261
|
+
async function carrying(
|
|
262
|
+
store: PrecomputeStore,
|
|
263
|
+
owed: readonly PreparedSpend[],
|
|
264
|
+
run: () => Promise<CompactionResult | null>,
|
|
265
|
+
): Promise<CompactionResult | null> {
|
|
266
|
+
try {
|
|
267
|
+
const result = await run()
|
|
268
|
+
if (!result) {
|
|
269
|
+
store.addUnbooked(owed)
|
|
270
|
+
return null
|
|
271
|
+
}
|
|
272
|
+
let usage = result.usage
|
|
273
|
+
for (const spent of owed) usage = accumulateTokenUsage(usage, spent.usage)
|
|
274
|
+
return { ...result, usage }
|
|
275
|
+
} catch (error) {
|
|
276
|
+
store.addUnbooked(owed)
|
|
277
|
+
throw error
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Append a between-turns pass: the `compaction` record, in the shape an
|
|
283
|
+
* earlier release reads (real sizes, a trigger it knows), then the
|
|
284
|
+
* `compaction_measured` record with what only this release records. A refused
|
|
285
|
+
* second write loses only the measures.
|
|
286
|
+
*/
|
|
287
|
+
async function appendCompaction(
|
|
288
|
+
log: SessionLog,
|
|
289
|
+
lease: Parameters<SessionLog['append']>[0],
|
|
290
|
+
pass: {
|
|
291
|
+
readonly strategy: string
|
|
292
|
+
readonly trigger: 'manual' | 'idle'
|
|
293
|
+
readonly replacesSeqRange: [number, number]
|
|
294
|
+
readonly summary: Message[]
|
|
295
|
+
readonly keptMessageIds: MessageId[]
|
|
296
|
+
readonly before: readonly Message[]
|
|
297
|
+
readonly after: readonly Message[]
|
|
298
|
+
readonly startedAt: number
|
|
299
|
+
readonly precomputed: boolean
|
|
300
|
+
},
|
|
301
|
+
): Promise<void> {
|
|
302
|
+
const tokensBefore = estimateMessagesTokens(pass.before)
|
|
303
|
+
const tokensAfter = estimateMessagesTokens(pass.after)
|
|
304
|
+
const compactionId = generateMessageId()
|
|
305
|
+
const priorDropped = await readCumulativeDropped(log)
|
|
306
|
+
await log.append(lease, {
|
|
307
|
+
type: 'compaction',
|
|
308
|
+
compactionId,
|
|
309
|
+
strategy: pass.strategy,
|
|
310
|
+
trigger: pass.trigger,
|
|
311
|
+
replacesSeqRange: pass.replacesSeqRange,
|
|
312
|
+
summary: pass.summary,
|
|
313
|
+
keptMessageIds: pass.keptMessageIds,
|
|
314
|
+
tokensBefore,
|
|
315
|
+
tokensAfter,
|
|
316
|
+
})
|
|
317
|
+
await log
|
|
318
|
+
.append(
|
|
319
|
+
lease,
|
|
320
|
+
measuredDraft(
|
|
321
|
+
compactionId,
|
|
322
|
+
{
|
|
323
|
+
trigger: pass.trigger,
|
|
324
|
+
tokensBefore,
|
|
325
|
+
tokensAfter,
|
|
326
|
+
durationMs: Math.max(0, Math.round(performance.now() - pass.startedAt)),
|
|
327
|
+
precomputed: pass.precomputed,
|
|
328
|
+
},
|
|
329
|
+
priorDropped + Math.max(0, tokensBefore - tokensAfter),
|
|
330
|
+
),
|
|
331
|
+
)
|
|
332
|
+
.catch(() => undefined)
|
|
333
|
+
}
|
|
334
|
+
|
|
180
335
|
/**
|
|
181
336
|
* Express a compacted context as a `compaction` record: the new messages
|
|
182
337
|
* before the first recorded one are its summary and the recorded ones are
|
|
@@ -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,
|
|
@@ -2109,6 +2109,7 @@ export async function* query(params: QueryParams): AsyncGenerator<SessionEvent,
|
|
|
2109
2109
|
...(params.durableInbound ? { durableInbound: params.durableInbound } : {}),
|
|
2110
2110
|
...(params.resumeFromCheckpoint ? { resumedInput: queuedForThisRun } : {}),
|
|
2111
2111
|
...(params.waitForInbound ? { waitForInbound: params.waitForInbound } : {}),
|
|
2112
|
+
...(params.waitForOperator ? { waitForOperator: params.waitForOperator } : {}),
|
|
2112
2113
|
...(params.projectInstructionContext
|
|
2113
2114
|
? { projectInstructionContext: params.projectInstructionContext }
|
|
2114
2115
|
: {}),
|
|
@@ -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'
|
|
@@ -135,6 +136,7 @@ import {
|
|
|
135
136
|
streamWithProviderRejectedImageRecovery,
|
|
136
137
|
} from './provider-rejected-image.js'
|
|
137
138
|
import { appendRecordedContext } from './recorded-context.js'
|
|
139
|
+
import { digestRequestPrefix } from './request-prefix.js'
|
|
138
140
|
import { type MediaRemoval, RequestRepair } from './request-repair.js'
|
|
139
141
|
import { placeRequestContext } from './step-context.js'
|
|
140
142
|
import {
|
|
@@ -345,6 +347,8 @@ export class IterationOrchestrator {
|
|
|
345
347
|
* sets it.
|
|
346
348
|
*/
|
|
347
349
|
private stopDeferredForOutstandingWork = false
|
|
350
|
+
/** The latest budget line this turn has recorded, so each stage is said once. */
|
|
351
|
+
private budgetNudgeStage: BudgetNudgeStage = 0
|
|
348
352
|
/** Nudges sent since the model last finished a call without hitting its output cap. */
|
|
349
353
|
private outputContinuations = 0
|
|
350
354
|
/** Watches the turn's steps for reasoning that piles up while nothing changes. */
|
|
@@ -888,6 +892,12 @@ export class IterationOrchestrator {
|
|
|
888
892
|
// edge BEFORE compaction runs (so the refreshed slot is what
|
|
889
893
|
// compaction preserves). No-op when no provider is configured.
|
|
890
894
|
await refreshWorkingMemory(this.ctx)
|
|
895
|
+
// A turn that starts after the prompt cache lapsed installs a summary
|
|
896
|
+
// written earlier, when one is ready and still valid, before the old
|
|
897
|
+
// tool results are trimmed.
|
|
898
|
+
if (iterationNum === 1 && this.ctx.compactionConfig?.clearStaleToolResultsAtStart) {
|
|
899
|
+
await runCompactionCheck(this.ctx, { preparedOnly: true })
|
|
900
|
+
}
|
|
891
901
|
if (iterationNum === 1) await runStaleResultClear(this.ctx)
|
|
892
902
|
await runCompactionCheck(this.ctx)
|
|
893
903
|
yield* this.ctx.drainPending()
|
|
@@ -922,6 +932,16 @@ export class IterationOrchestrator {
|
|
|
922
932
|
this.ctx.log,
|
|
923
933
|
)
|
|
924
934
|
}
|
|
935
|
+
// Before the limit ends the run, say so once per stage. Recorded, so
|
|
936
|
+
// the model keeps reading it, and only after the guard has passed
|
|
937
|
+
// this call: a run that is about to be stopped has no call to spend.
|
|
938
|
+
const budgetLeft = this.ctx.guard.budgetLeft(iterationNum - 1)
|
|
939
|
+
const budgetStage = budgetNudgeStage(budgetLeft)
|
|
940
|
+
if (budgetStage > this.budgetNudgeStage) {
|
|
941
|
+
this.budgetNudgeStage = budgetStage
|
|
942
|
+
const text = budgetNudgeText(budgetLeft, budgetStage)
|
|
943
|
+
if (text) appendRecordedContext(recorder, [{ key: 'budget-nudge', text }], this.ctx.log)
|
|
944
|
+
}
|
|
925
945
|
const contextSections =
|
|
926
946
|
this.ctx.promptContributions?.render('context', { iteration: iterationNum }) ?? []
|
|
927
947
|
const deferredMcpHints =
|
|
@@ -1500,6 +1520,19 @@ export class IterationOrchestrator {
|
|
|
1500
1520
|
...(response.usage.cacheMiss !== undefined
|
|
1501
1521
|
? { cacheMiss: response.usage.cacheMiss }
|
|
1502
1522
|
: {}),
|
|
1523
|
+
requestPrefix: digestRequestPrefix({
|
|
1524
|
+
conversation: recorder.sessionId,
|
|
1525
|
+
model: stepModel,
|
|
1526
|
+
tools: llmTools,
|
|
1527
|
+
messages,
|
|
1528
|
+
wire: response.usage.requestShape,
|
|
1529
|
+
params: {
|
|
1530
|
+
effort: stepEffort,
|
|
1531
|
+
speed: turnConfig.speed,
|
|
1532
|
+
thinking: turnConfig.thinking,
|
|
1533
|
+
choice: step.toolChoice,
|
|
1534
|
+
},
|
|
1535
|
+
}),
|
|
1503
1536
|
...contextFigures,
|
|
1504
1537
|
})
|
|
1505
1538
|
|
|
@@ -3627,7 +3660,13 @@ export class IterationOrchestrator {
|
|
|
3627
3660
|
this.hydrateOutcome()
|
|
3628
3661
|
return await this.outcomeLedger.preview()
|
|
3629
3662
|
},
|
|
3630
|
-
{
|
|
3663
|
+
{
|
|
3664
|
+
finalMessage,
|
|
3665
|
+
recordCheck: async (check) => {
|
|
3666
|
+
this.hydrateOutcome()
|
|
3667
|
+
await this.outcomeLedger.recordHostCheck(check)
|
|
3668
|
+
},
|
|
3669
|
+
},
|
|
3631
3670
|
)
|
|
3632
3671
|
if (typeof note !== 'string' || note.trim() === '') return undefined
|
|
3633
3672
|
this.beforeEndNotes += 1
|