@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,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
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
import type { RequestWireShape } from '../../../types/common/index.js'
|
|
4
|
+
import type { Message } from '../../../types/message/index.js'
|
|
5
|
+
import type { RequestPrefixDigest } from '../../../types/session/events.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Where a conversation's prompt cache can break, readable from the log
|
|
9
|
+
* without its content.
|
|
10
|
+
*
|
|
11
|
+
* A provider cache reads the start of a request: the model, the tools, the
|
|
12
|
+
* system text, then each message in order. Each of those is reduced here to a
|
|
13
|
+
* short hash, and each request is compared with the one before it for the same
|
|
14
|
+
* conversation. When the cached share of a request falls, the record beside it
|
|
15
|
+
* says whether the model, the tools or the system text moved, or the first
|
|
16
|
+
* earlier message that was not repeated in place. Nothing of the content is
|
|
17
|
+
* kept: a hash of a message says only whether it changed.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** What the next request of a conversation is compared with. */
|
|
21
|
+
interface Earlier {
|
|
22
|
+
readonly model: string
|
|
23
|
+
readonly tools: string
|
|
24
|
+
readonly system: string
|
|
25
|
+
readonly params: string
|
|
26
|
+
/** Hash of the reported wire shape; empty when the driver reported none. */
|
|
27
|
+
readonly wire: string
|
|
28
|
+
readonly items: readonly string[]
|
|
29
|
+
/** How many of the last items were request-only: the next request does not repeat them. */
|
|
30
|
+
readonly tail: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Conversations kept, oldest dropped first. A long-running host holds many, a turn needs one. */
|
|
34
|
+
const MAX_CONVERSATIONS = 64
|
|
35
|
+
const earlier = new Map<string, Earlier>()
|
|
36
|
+
|
|
37
|
+
const hex = (text: string, length: number): string =>
|
|
38
|
+
createHash('sha256').update(text).digest('hex').slice(0, length)
|
|
39
|
+
|
|
40
|
+
/** The parts of a message the model is sent; bookkeeping (time, id, cache hints) is left out. */
|
|
41
|
+
function wireOf(message: Message): string {
|
|
42
|
+
const { role, content } = message
|
|
43
|
+
const extra = message as unknown as Record<string, unknown>
|
|
44
|
+
return JSON.stringify([
|
|
45
|
+
role,
|
|
46
|
+
content,
|
|
47
|
+
extra.toolCalls,
|
|
48
|
+
extra.toolCallId,
|
|
49
|
+
extra.textParts,
|
|
50
|
+
extra.attachments,
|
|
51
|
+
(extra.source as { replayState?: unknown } | undefined)?.replayState,
|
|
52
|
+
])
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A message the next request does not repeat: a request-only note, or the step's own
|
|
57
|
+
* guidance, a system message sent after the history (a system message that opens the
|
|
58
|
+
* conversation is part of the system text instead).
|
|
59
|
+
*/
|
|
60
|
+
const isRequestOnly = (message: Message): boolean => {
|
|
61
|
+
if (message.role === 'system') return true
|
|
62
|
+
const source = (message as { source?: { type?: string; kind?: string } }).source
|
|
63
|
+
return source?.type === 'runtime-context' && source.kind === 'step-context'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface RequestPrefixInput {
|
|
67
|
+
readonly conversation: string
|
|
68
|
+
readonly model: string
|
|
69
|
+
readonly tools: readonly unknown[] | undefined
|
|
70
|
+
readonly messages: readonly Message[]
|
|
71
|
+
/**
|
|
72
|
+
* What the driver reports it sent (see `TokenUsage.requestShape`); undefined when it does
|
|
73
|
+
* not say. The digest is computed from the SDK-level request, before the driver builds the
|
|
74
|
+
* wire request, so this is the only record of what the driver then kept or dropped.
|
|
75
|
+
*/
|
|
76
|
+
readonly wire?: RequestWireShape
|
|
77
|
+
/** The request's settings that are not part of its start but can still move a cache (effort, speed, choice). */
|
|
78
|
+
readonly params: unknown
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function digestRequestPrefix(input: RequestPrefixInput): RequestPrefixDigest {
|
|
82
|
+
let opening = 0
|
|
83
|
+
while (input.messages[opening]?.role === 'system') opening++
|
|
84
|
+
const system = hex(
|
|
85
|
+
input.messages
|
|
86
|
+
.slice(0, opening)
|
|
87
|
+
.map((message) => String(message.content))
|
|
88
|
+
.join('\n\n'),
|
|
89
|
+
12,
|
|
90
|
+
)
|
|
91
|
+
const rest = input.messages.slice(opening)
|
|
92
|
+
const items = rest.map((message) => hex(wireOf(message), 6))
|
|
93
|
+
let tail = 0
|
|
94
|
+
while (tail < rest.length && isRequestOnly(rest[rest.length - 1 - tail] as Message)) tail++
|
|
95
|
+
const model = hex(input.model, 12)
|
|
96
|
+
const tools = hex(JSON.stringify(input.tools ?? []), 12)
|
|
97
|
+
const params = hex(JSON.stringify(input.params ?? null), 12)
|
|
98
|
+
// Only these three move `wire`: a developer item appearing for the first time in a
|
|
99
|
+
// healthy conversation is not a change of how the driver talks to the backend.
|
|
100
|
+
const wire = input.wire
|
|
101
|
+
? hex(
|
|
102
|
+
JSON.stringify([
|
|
103
|
+
input.wire.cacheKey,
|
|
104
|
+
input.wire.sessionHeader,
|
|
105
|
+
input.wire.developerRefused === true,
|
|
106
|
+
]),
|
|
107
|
+
12,
|
|
108
|
+
)
|
|
109
|
+
: ''
|
|
110
|
+
|
|
111
|
+
const before = earlier.get(input.conversation)
|
|
112
|
+
let changed: RequestPrefixDigest['changed']
|
|
113
|
+
let firstChangedItem: number | undefined
|
|
114
|
+
if (before) {
|
|
115
|
+
const moved: NonNullable<RequestPrefixDigest['changed']> = []
|
|
116
|
+
if (before.model !== model) moved.push('model')
|
|
117
|
+
if (before.tools !== tools) moved.push('tools')
|
|
118
|
+
if (before.system !== system) moved.push('system')
|
|
119
|
+
if (before.params !== params) moved.push('params')
|
|
120
|
+
if (before.wire !== wire && before.wire !== '' && wire !== '') moved.push('wire')
|
|
121
|
+
changed = moved
|
|
122
|
+
// Only what the earlier request kept for good can be rewritten: its own request-only
|
|
123
|
+
// tail is gone from this one by design.
|
|
124
|
+
const stable = before.items.length - before.tail
|
|
125
|
+
for (let at = 0; at < stable; at++) {
|
|
126
|
+
if (items[at] !== before.items[at]) {
|
|
127
|
+
firstChangedItem = at
|
|
128
|
+
break
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
earlier.delete(input.conversation)
|
|
133
|
+
earlier.set(input.conversation, { model, tools, system, params, wire, items, tail })
|
|
134
|
+
while (earlier.size > MAX_CONVERSATIONS) {
|
|
135
|
+
const oldest = earlier.keys().next().value
|
|
136
|
+
if (oldest === undefined) break
|
|
137
|
+
earlier.delete(oldest)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
model,
|
|
142
|
+
tools,
|
|
143
|
+
system,
|
|
144
|
+
params,
|
|
145
|
+
items: items.join(''),
|
|
146
|
+
...(input.wire
|
|
147
|
+
? {
|
|
148
|
+
cacheKey: input.wire.cacheKey,
|
|
149
|
+
sessionHeader: input.wire.sessionHeader,
|
|
150
|
+
developerRefused: input.wire.developerRefused === true,
|
|
151
|
+
}
|
|
152
|
+
: {}),
|
|
153
|
+
...(changed ? { changed } : {}),
|
|
154
|
+
...(firstChangedItem !== undefined ? { firstChangedItem } : {}),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Test seam: forget what earlier requests held. */
|
|
159
|
+
export function resetRequestPrefixDigests(): void {
|
|
160
|
+
earlier.clear()
|
|
161
|
+
}
|
|
@@ -854,9 +854,11 @@ export interface ToolReviewRequest {
|
|
|
854
854
|
readonly perCall?: true
|
|
855
855
|
/**
|
|
856
856
|
* Aborts when the question stops being open: the turn was cancelled, a
|
|
857
|
-
* rule or the mode now covers the call,
|
|
858
|
-
* turn
|
|
859
|
-
* ignored. Present
|
|
857
|
+
* rule or the mode now covers the call, an earlier refusal ended the
|
|
858
|
+
* turn, or the person wrote a message instead of answering. A host that
|
|
859
|
+
* shows the question closes it then; its answer is ignored. Present with
|
|
860
|
+
* `perCall`, and on any request when the host gave the turn a live message
|
|
861
|
+
* box (`ResumeContext.waitForOperator`).
|
|
860
862
|
*/
|
|
861
863
|
readonly signal?: AbortSignal
|
|
862
864
|
}
|
|
@@ -1037,6 +1039,69 @@ function isHookGranted(tc: ToolCallSummary): boolean {
|
|
|
1037
1039
|
)
|
|
1038
1040
|
}
|
|
1039
1041
|
|
|
1042
|
+
/**
|
|
1043
|
+
* What the model is told when a message from the person closed a question that
|
|
1044
|
+
* was open, or about to be shown. Nobody refused the call, and the text says so:
|
|
1045
|
+
* the refusal path would otherwise read as the person's decision.
|
|
1046
|
+
*/
|
|
1047
|
+
export const CLOSED_BY_MESSAGE_FEEDBACK =
|
|
1048
|
+
"[Tool call not completed: a message from the user arrived while the approval request for this call was open or about to be shown, so the request was closed and the call did not run. Nobody refused it, so this is not the user's decision. The message follows; ask again if the action is still needed.]"
|
|
1049
|
+
|
|
1050
|
+
/**
|
|
1051
|
+
* Put one question to the person, and take it back if they write instead.
|
|
1052
|
+
*
|
|
1053
|
+
* The host's prompt is handed a signal that closes it (the turn's own, joined
|
|
1054
|
+
* with this one), so a card is taken off the screen; the answer is then the
|
|
1055
|
+
* message's closing text rather than a refusal by the person. With no live
|
|
1056
|
+
* message box (`ResumeContext.waitForOperator`) the question waits as before.
|
|
1057
|
+
*/
|
|
1058
|
+
async function askUntilMessage(
|
|
1059
|
+
prompt: ToolReviewPrompt,
|
|
1060
|
+
request: Omit<ToolReviewRequest, 'signal'>,
|
|
1061
|
+
context: ResumeContext | undefined,
|
|
1062
|
+
): Promise<ToolReviewAnswer> {
|
|
1063
|
+
const wake = context?.waitForOperator
|
|
1064
|
+
if (!wake) {
|
|
1065
|
+
return prompt({
|
|
1066
|
+
...request,
|
|
1067
|
+
...(request.perCall && context?.signal ? { signal: context.signal } : {}),
|
|
1068
|
+
})
|
|
1069
|
+
}
|
|
1070
|
+
const closing = new AbortController()
|
|
1071
|
+
const turn = context?.signal
|
|
1072
|
+
const signal = turn ? AbortSignal.any([turn, closing.signal]) : closing.signal
|
|
1073
|
+
const watching = new AbortController()
|
|
1074
|
+
return new Promise<ToolReviewAnswer>((resolve, reject) => {
|
|
1075
|
+
let done = false
|
|
1076
|
+
const finish = (settle: () => void): void => {
|
|
1077
|
+
if (done) return
|
|
1078
|
+
done = true
|
|
1079
|
+
watching.abort()
|
|
1080
|
+
settle()
|
|
1081
|
+
}
|
|
1082
|
+
turn?.addEventListener('abort', () => watching.abort(), { once: true })
|
|
1083
|
+
Promise.resolve(wake(watching.signal)).then(
|
|
1084
|
+
() =>
|
|
1085
|
+
finish(() => {
|
|
1086
|
+
// Settled first, so the prompt's own rejection on the close below is ignored.
|
|
1087
|
+
resolve({ kind: 'reject', feedback: CLOSED_BY_MESSAGE_FEEDBACK })
|
|
1088
|
+
closing.abort(new Error('A message from the user closed the approval request.'))
|
|
1089
|
+
}),
|
|
1090
|
+
// An aborted or closed wait is the turn ending, which is not a message.
|
|
1091
|
+
() => {},
|
|
1092
|
+
)
|
|
1093
|
+
// One turn of the microtask queue later, so a message that was already waiting has
|
|
1094
|
+
// settled the question before the person is shown it, not after.
|
|
1095
|
+
queueMicrotask(() => {
|
|
1096
|
+
if (done) return
|
|
1097
|
+
Promise.resolve(prompt({ ...request, signal })).then(
|
|
1098
|
+
(answer) => finish(() => resolve(answer)),
|
|
1099
|
+
(error: unknown) => finish(() => reject(error)),
|
|
1100
|
+
)
|
|
1101
|
+
})
|
|
1102
|
+
})
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1040
1105
|
/** The handler behind `createReviewPolicy`, for a host that wants only the function. */
|
|
1041
1106
|
export function createReviewHandler(options: ReviewPolicyOptions = {}): ResumeHandler {
|
|
1042
1107
|
const { prompt, registry } = options
|
|
@@ -1254,14 +1319,17 @@ export function createReviewHandler(options: ReviewPolicyOptions = {}): ResumeHa
|
|
|
1254
1319
|
shown?: readonly ToolCallSummary[],
|
|
1255
1320
|
): Promise<ToolReviewAnswer> =>
|
|
1256
1321
|
answered ??
|
|
1257
|
-
(
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1322
|
+
askUntilMessage(
|
|
1323
|
+
prompt as ToolReviewPrompt,
|
|
1324
|
+
{
|
|
1325
|
+
sessionId: request.sessionId,
|
|
1326
|
+
turnId: request.turnId,
|
|
1327
|
+
toolCalls: shown ?? request.toolCalls,
|
|
1328
|
+
...(screenConsent ? { screenConsent } : {}),
|
|
1329
|
+
...(request.perCall ? { perCall: true as const } : {}),
|
|
1330
|
+
},
|
|
1331
|
+
context,
|
|
1332
|
+
)
|
|
1265
1333
|
// The first look at the screen in a session is the operator's to allow:
|
|
1266
1334
|
// what is on it goes to the model provider, and a screenshot reads as
|
|
1267
1335
|
// harmlessly as a file read to every rule below. Asked once, in the
|
|
@@ -379,6 +379,8 @@ export class OutcomeLedger {
|
|
|
379
379
|
/** A call ran that could change the workspace and is not a write tool: only the detector could say whether it did. */
|
|
380
380
|
private couldHaveChangedUnseen = false
|
|
381
381
|
private writes = 0
|
|
382
|
+
/** The loop step the last observed calls finished in. */
|
|
383
|
+
private lastStep = 0
|
|
382
384
|
private readonly checks = new Map<string, CheckState>()
|
|
383
385
|
/** Background jobs that run a recognised check, by job id: the command they run. */
|
|
384
386
|
private readonly jobChecks = new Map<string, string>()
|
|
@@ -500,6 +502,7 @@ export class OutcomeLedger {
|
|
|
500
502
|
|
|
501
503
|
/** Feed the calls of one finished step, in order. */
|
|
502
504
|
async observe(step: number, calls: readonly OutcomeCall[]): Promise<void> {
|
|
505
|
+
this.lastStep = Math.max(this.lastStep, step)
|
|
503
506
|
// Checks that finished in this step with nothing after them that could
|
|
504
507
|
// change the workspace: the detector, asked once at the end of the step,
|
|
505
508
|
// answers for exactly the state they left.
|
|
@@ -591,6 +594,33 @@ export class OutcomeLedger {
|
|
|
591
594
|
this.paths.add(clipped)
|
|
592
595
|
}
|
|
593
596
|
|
|
597
|
+
/**
|
|
598
|
+
* A check the host ran itself, such as a gate the person set for a goal. It is
|
|
599
|
+
* recorded as the latest run of its command on the files as they are now: the
|
|
600
|
+
* fingerprint is taken here, so a write or an unseen change after it makes it
|
|
601
|
+
* stale as for any other check.
|
|
602
|
+
*/
|
|
603
|
+
async recordHostCheck(check: {
|
|
604
|
+
readonly command: string
|
|
605
|
+
readonly result: 'passed' | 'failed'
|
|
606
|
+
readonly exitCode?: number
|
|
607
|
+
}): Promise<void> {
|
|
608
|
+
if (this.unrecorded) return
|
|
609
|
+
const key = normalise(check.command)
|
|
610
|
+
const fingerprint =
|
|
611
|
+
this.started && this.startFingerprint !== null ? await this.detect() : undefined
|
|
612
|
+
this.storeCheck(key, {
|
|
613
|
+
key,
|
|
614
|
+
command: clip(key, OUTCOME_MAX_COMMAND_CHARS),
|
|
615
|
+
step: this.lastStep,
|
|
616
|
+
result: check.result,
|
|
617
|
+
...(check.exitCode !== undefined ? { exitCode: check.exitCode } : {}),
|
|
618
|
+
afterWrite: false,
|
|
619
|
+
afterUnknown: false,
|
|
620
|
+
fingerprint,
|
|
621
|
+
})
|
|
622
|
+
}
|
|
623
|
+
|
|
594
624
|
private recordCheck(step: number, command: string, call: OutcomeCall): CheckState {
|
|
595
625
|
const key = normalise(command)
|
|
596
626
|
const { result, exitCode } = this.judge(command, call)
|
|
@@ -605,6 +635,10 @@ export class OutcomeLedger {
|
|
|
605
635
|
// Not asked for yet: `undefined` until the step's detector call answers.
|
|
606
636
|
fingerprint: undefined,
|
|
607
637
|
}
|
|
638
|
+
return this.storeCheck(key, state)
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
private storeCheck(key: string, state: CheckState): CheckState {
|
|
608
642
|
// A failure stands until the same command passes. A later run that never
|
|
609
643
|
// settled (background, timed out) says nothing about the failure.
|
|
610
644
|
const earlier = this.checks.get(key)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { SandboxExecOptions } from '../types/sandbox/index.js'
|
|
2
|
+
|
|
3
|
+
/** The most standard input, in UTF-8 bytes, one `exec` may carry. */
|
|
4
|
+
export const SANDBOX_EXEC_STDIN_MAX_BYTES = 1024 * 1024
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `SandboxExecOptions.stdin` was refused: it is not text, it is over
|
|
8
|
+
* {@link SANDBOX_EXEC_STDIN_MAX_BYTES}, or the backend has no guest agent to
|
|
9
|
+
* carry it. Thrown before anything is sent or started.
|
|
10
|
+
*/
|
|
11
|
+
export class SandboxExecStdinError extends Error {
|
|
12
|
+
constructor(
|
|
13
|
+
message: string,
|
|
14
|
+
readonly reason: 'not-text' | 'too-large' | 'unsupported',
|
|
15
|
+
) {
|
|
16
|
+
super(message)
|
|
17
|
+
this.name = 'SandboxExecStdinError'
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The stdin an agent-backed backend sends, validated: `undefined` when the
|
|
23
|
+
* caller gave none, the text when it is within the bound, a
|
|
24
|
+
* {@link SandboxExecStdinError} otherwise.
|
|
25
|
+
*/
|
|
26
|
+
export function execStdinOf(
|
|
27
|
+
opts: Pick<SandboxExecOptions, 'stdin'> | undefined,
|
|
28
|
+
): string | undefined {
|
|
29
|
+
const stdin = opts?.stdin
|
|
30
|
+
if (stdin === undefined) return undefined
|
|
31
|
+
if (typeof stdin !== 'string') {
|
|
32
|
+
throw new SandboxExecStdinError('exec stdin must be a string.', 'not-text')
|
|
33
|
+
}
|
|
34
|
+
const bytes = Buffer.byteLength(stdin, 'utf8')
|
|
35
|
+
if (bytes > SANDBOX_EXEC_STDIN_MAX_BYTES) {
|
|
36
|
+
throw new SandboxExecStdinError(
|
|
37
|
+
`exec stdin is ${bytes} bytes, over the ${SANDBOX_EXEC_STDIN_MAX_BYTES}-byte limit. Nothing was sent. Stage a larger document with writeFile and redirect it.`,
|
|
38
|
+
'too-large',
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
return stdin
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** For a backend without a guest agent: refuse a call that carries stdin. */
|
|
45
|
+
export function refuseExecStdin(
|
|
46
|
+
opts: Pick<SandboxExecOptions, 'stdin'> | undefined,
|
|
47
|
+
backend: string,
|
|
48
|
+
): void {
|
|
49
|
+
if (opts?.stdin === undefined) return
|
|
50
|
+
throw new SandboxExecStdinError(
|
|
51
|
+
`The ${backend} sandbox cannot give a command standard input, so nothing was run. Use an agent-backed sandbox (Firecracker or Kubernetes), or stage the text with writeFile and redirect it.`,
|
|
52
|
+
'unsupported',
|
|
53
|
+
)
|
|
54
|
+
}
|