@orion-agents/orion-code 0.3.66 → 0.4.3
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 +81 -0
- package/README.md +78 -361
- package/README.zh-CN.md +71 -262
- package/bin/orion +4 -1
- package/dist/acp/session-updates.d.ts +34 -0
- package/dist/acp/session-updates.js +162 -0
- package/dist/acp/sessions.d.ts +32 -0
- package/dist/acp/sessions.js +315 -0
- package/dist/acp/stdio.d.ts +3 -0
- package/dist/acp/stdio.js +59 -0
- package/dist/cli.js +136 -185
- package/dist/commands/context-tool-command-handlers.d.ts +1 -1
- package/dist/commands/context-tool-command-handlers.js +24 -3
- package/dist/commands/core-command-handlers.js +5 -1
- package/dist/commands/diagnostic-command-handlers.js +20 -6
- package/dist/commands/diagnostic-commands.js +0 -12
- package/dist/commands/init-command.d.ts +3 -0
- package/dist/commands/init-command.js +131 -0
- package/dist/commands/model-command-handlers.d.ts +1 -2
- package/dist/commands/model-command-handlers.js +15 -100
- package/dist/commands/model-commands.js +0 -11
- package/dist/commands/registry.js +0 -18
- package/dist/commands/rsi-command.d.ts +2 -0
- package/dist/commands/rsi-command.js +180 -0
- package/dist/commands/session-command-handlers.js +28 -19
- package/dist/commands/system-commands.js +0 -1
- package/dist/commands/tool-commands.js +1 -1
- package/dist/commands/types.d.ts +4 -10
- package/dist/commands/types.js +1 -21
- package/dist/commands/workflow-command-handlers.js +13 -4
- package/dist/framework/loop-budget-contract.d.ts +7 -2
- package/dist/framework/loop-budget-contract.js +4 -1
- package/dist/framework/loop-progress.d.ts +22 -0
- package/dist/framework/loop-progress.js +79 -0
- package/dist/framework/prompt.d.ts +0 -2
- package/dist/framework/prompt.js +9 -27
- package/dist/framework/query.d.ts +27 -20
- package/dist/framework/query.js +453 -153
- package/dist/framework/store.d.ts +6 -10
- package/dist/framework/store.js +2 -21
- package/dist/framework/tool-call-orchestrator.d.ts +7 -0
- package/dist/framework/tool-call-orchestrator.js +8 -3
- package/dist/framework/tool-state.d.ts +2 -0
- package/dist/framework/tool.d.ts +0 -23
- package/dist/framework/verification-command.d.ts +15 -0
- package/dist/framework/verification-command.js +32 -0
- package/dist/harness/assembler.d.ts +1 -0
- package/dist/harness/assembler.js +24 -0
- package/dist/harness/capsule.d.ts +1 -1
- package/dist/harness/capsule.js +33 -14
- package/dist/harness/context-harness.d.ts +31 -12
- package/dist/harness/context-harness.js +491 -35
- package/dist/harness/contract.js +1 -1
- package/dist/harness/drift-guard.d.ts +4 -0
- package/dist/harness/drift-guard.js +68 -16
- package/dist/harness/intent.js +21 -0
- package/dist/harness/ledger.d.ts +6 -11
- package/dist/harness/ledger.js +39 -9
- package/dist/harness/progress-controller.d.ts +12 -7
- package/dist/harness/progress-controller.js +66 -21
- package/dist/harness/state.js +9 -1
- package/dist/harness/task-checklist.d.ts +16 -0
- package/dist/harness/task-checklist.js +95 -0
- package/dist/harness/types.d.ts +73 -1
- package/dist/harness/verification.d.ts +19 -1
- package/dist/harness/verification.js +116 -29
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -3
- package/dist/print-ui/event-stream.d.ts +38 -0
- package/dist/print-ui/event-stream.js +187 -0
- package/dist/print-ui/launch.d.ts +12 -1
- package/dist/print-ui/launch.js +173 -11
- package/dist/product/environment.d.ts +1 -1
- package/dist/product/environment.js +1 -1
- package/dist/product/identity.d.ts +1 -1
- package/dist/product/identity.js +1 -1
- package/dist/product/paths.d.ts +3 -2
- package/dist/product/paths.js +6 -2
- package/dist/runtime/agent-loop.d.ts +11 -2
- package/dist/runtime/agent-loop.js +165 -25
- package/dist/runtime/agent-runtime-controller.d.ts +13 -3
- package/dist/runtime/agent-runtime-controller.js +347 -183
- package/dist/runtime/agent-runtime-protocol.d.ts +10 -18
- package/dist/runtime/agent-runtime-runner.d.ts +10 -0
- package/dist/runtime/builtin-tool-provider.js +5 -1
- package/dist/runtime/capability-receipt-journal.js +1 -0
- package/dist/runtime/durable-tool-receipt-reader.js +2 -1
- package/dist/runtime/first-party-tool-services.d.ts +1 -0
- package/dist/runtime/first-party-tool-services.js +2 -12
- package/dist/runtime/first-party-tool-universe.d.ts +1 -0
- package/dist/runtime/first-party-tool-universe.js +1 -1
- package/dist/runtime/followup-queue-store.d.ts +16 -0
- package/dist/runtime/followup-queue-store.js +75 -0
- package/dist/runtime/goal-lifecycle-v2.d.ts +11 -1
- package/dist/runtime/goal-lifecycle-v2.js +85 -15
- package/dist/runtime/goal-runtime-coordinator.d.ts +10 -1
- package/dist/runtime/goal-runtime-coordinator.js +81 -24
- package/dist/runtime/loop-budget.js +7 -5
- package/dist/runtime/mcp/first-party-mcp-adapter.d.ts +2 -0
- package/dist/runtime/mcp/first-party-mcp-adapter.js +23 -19
- package/dist/runtime/model-coordinator.js +2 -5
- package/dist/runtime/orion-runtime-v1.d.ts +18 -2
- package/dist/runtime/orion-runtime-v1.js +135 -39
- package/dist/runtime/orion-session-runner.d.ts +7 -0
- package/dist/runtime/orion-session-runner.js +82 -0
- package/dist/runtime/plan-review.d.ts +1 -0
- package/dist/runtime/plan-review.js +8 -4
- package/dist/runtime/product-bootstrap.d.ts +6 -0
- package/dist/runtime/product-bootstrap.js +41 -51
- package/dist/runtime/product-orion-runtime.js +128 -52
- package/dist/runtime/release-receipts.d.ts +24 -133
- package/dist/runtime/release-receipts.js +129 -693
- package/dist/runtime/rich-text/layout.js +108 -65
- package/dist/runtime/rsi/contracts.d.ts +50 -0
- package/dist/runtime/rsi/contracts.js +17 -0
- package/dist/runtime/rsi/diagnosis.d.ts +46 -0
- package/dist/runtime/rsi/diagnosis.js +98 -0
- package/dist/runtime/rsi/evaluation-suite.d.ts +25 -0
- package/dist/runtime/rsi/evaluation-suite.js +141 -0
- package/dist/runtime/rsi/evaluation-worker.d.ts +19 -0
- package/dist/runtime/rsi/evaluation-worker.js +64 -0
- package/dist/runtime/rsi/evaluation.d.ts +49 -0
- package/dist/runtime/rsi/evaluation.js +351 -0
- package/dist/runtime/rsi/lifecycle.d.ts +54 -0
- package/dist/runtime/rsi/lifecycle.js +279 -0
- package/dist/runtime/rsi/proposal.d.ts +39 -0
- package/dist/runtime/rsi/proposal.js +312 -0
- package/dist/runtime/rsi/receipt.d.ts +28 -0
- package/dist/runtime/rsi/receipt.js +138 -0
- package/dist/runtime/rsi/skill-state.d.ts +7 -0
- package/dist/runtime/rsi/skill-state.js +50 -0
- package/dist/runtime/rsi/trajectory.d.ts +19 -0
- package/dist/runtime/rsi/trajectory.js +314 -0
- package/dist/runtime/rsi/triage.d.ts +274 -0
- package/dist/runtime/rsi/triage.js +58 -0
- package/dist/runtime/session-composer-control.d.ts +5 -24
- package/dist/runtime/session-composer-control.js +27 -189
- package/dist/runtime/step-snapshot.d.ts +1 -0
- package/dist/runtime/subagents/policy.js +0 -1
- package/dist/runtime/subagents/production.js +1 -1
- package/dist/runtime/subagents/runtime-integration.js +2 -4
- package/dist/runtime/subagents/types.d.ts +0 -5
- package/dist/runtime/subagents/types.js +0 -2
- package/dist/runtime/task-context-recovery.d.ts +55 -0
- package/dist/runtime/task-context-recovery.js +342 -0
- package/dist/runtime/task-context-service.d.ts +16 -10
- package/dist/runtime/task-context-service.js +35 -4
- package/dist/runtime/thread-admission.d.ts +1 -1
- package/dist/runtime/thread-admission.js +12 -6
- package/dist/runtime/thread-event-store.d.ts +2 -2
- package/dist/runtime/thread-event-store.js +7 -2
- package/dist/runtime/thread-projection.js +4 -2
- package/dist/runtime/thread-runtime.d.ts +7 -0
- package/dist/runtime/thread-runtime.js +43 -0
- package/dist/runtime/thread-session-index.d.ts +8 -0
- package/dist/runtime/thread-session-index.js +50 -1
- package/dist/runtime/thread-session-view.d.ts +2 -1
- package/dist/runtime/thread-session-view.js +4 -0
- package/dist/runtime/thread-tool-journal.d.ts +1 -0
- package/dist/runtime/thread-tool-journal.js +6 -2
- package/dist/runtime/thread-ui-adapter.d.ts +1 -0
- package/dist/runtime/thread-ui-adapter.js +88 -11
- package/dist/runtime/tool-gateway.d.ts +7 -0
- package/dist/runtime/tool-gateway.js +20 -0
- package/dist/runtime/tool-transcript-entry.d.ts +16 -0
- package/dist/runtime/tool-transcript-entry.js +73 -0
- package/dist/runtime/turn-commit.d.ts +4 -1
- package/dist/runtime/turn-commit.js +19 -15
- package/dist/runtime/ui-events.d.ts +12 -0
- package/dist/runtime/ui-view-model.d.ts +6 -5
- package/dist/runtime/ui-view-model.js +33 -19
- package/dist/runtime/workspace-runtime-kernel.js +0 -1
- package/dist/services/atomic-write.d.ts +2 -0
- package/dist/services/atomic-write.js +12 -1
- package/dist/services/code-review/format.d.ts +4 -1
- package/dist/services/code-review/format.js +5 -1
- package/dist/services/code-review/runner.d.ts +2 -2
- package/dist/services/code-review/runner.js +86 -64
- package/dist/services/code-review/snapshot.d.ts +9 -9
- package/dist/services/code-review/snapshot.js +182 -166
- package/dist/services/compact/auto-compact.d.ts +21 -6
- package/dist/services/compact/auto-compact.js +148 -22
- package/dist/services/compact/background-candidate.d.ts +33 -0
- package/dist/services/compact/background-candidate.js +184 -0
- package/dist/services/compact/compact.d.ts +4 -1
- package/dist/services/compact/compact.js +30 -3
- package/dist/services/compact/coordinator.d.ts +10 -4
- package/dist/services/compact/coordinator.js +17 -7
- package/dist/services/compact/summary-generator.d.ts +4 -2
- package/dist/services/compact/summary-generator.js +17 -3
- package/dist/services/config.d.ts +5 -5
- package/dist/services/config.js +30 -45
- package/dist/services/doctor.js +85 -13
- package/dist/services/effort.d.ts +5 -5
- package/dist/services/effort.js +5 -11
- package/dist/services/file-lock.d.ts +9 -3
- package/dist/services/file-lock.js +92 -26
- package/dist/services/global-config.d.ts +26 -56
- package/dist/services/global-config.js +213 -183
- package/dist/services/llm.d.ts +40 -4
- package/dist/services/llm.js +164 -45
- package/dist/services/model-client-pool.d.ts +15 -9
- package/dist/services/model-client-pool.js +62 -33
- package/dist/services/model-context.d.ts +2 -1
- package/dist/services/model-context.js +15 -9
- package/dist/services/model-registry.d.ts +4 -15
- package/dist/services/model-registry.js +32 -101
- package/dist/services/provider-diagnostics.d.ts +7 -0
- package/dist/services/provider-diagnostics.js +9 -10
- package/dist/services/provider-resilience/coordinator.d.ts +1 -1
- package/dist/services/provider-resilience/coordinator.js +10 -6
- package/dist/services/provider-resilience/request-gate.d.ts +3 -0
- package/dist/services/provider-resilience/request-gate.js +29 -18
- package/dist/services/provider-resilience/types.d.ts +2 -0
- package/dist/services/session-storage.d.ts +7 -10
- package/dist/services/session-storage.js +57 -30
- package/dist/services/settings-coordinator.d.ts +0 -6
- package/dist/services/settings-coordinator.js +19 -56
- package/dist/services/settings-document-repository.d.ts +3 -10
- package/dist/services/settings-document-repository.js +7 -85
- package/dist/services/usage-state.d.ts +3 -3
- package/dist/services/usage-state.js +11 -42
- package/dist/terminal-ui/answer-format.d.ts +14 -0
- package/dist/terminal-ui/answer-format.js +119 -0
- package/dist/terminal-ui/launch.d.ts +7 -6
- package/dist/terminal-ui/launch.js +160 -70
- package/dist/terminal-ui/output-queue.d.ts +2 -1
- package/dist/terminal-ui/output-queue.js +1 -1
- package/dist/terminal-ui/raw-editor.d.ts +2 -1
- package/dist/terminal-ui/raw-editor.js +56 -15
- package/dist/tools/core/edit-file.js +2 -0
- package/dist/tools/core/write-file.js +5 -0
- package/dist/tools/lsp.d.ts +16 -1
- package/dist/tools/lsp.js +60 -19
- package/dist/tools/sandbox.d.ts +1 -1
- package/dist/tools/sandbox.js +3 -16
- package/dist/tools/todo.d.ts +4 -2
- package/dist/tools/todo.js +76 -31
- package/dist/tui-core/input-parser.d.ts +1 -0
- package/dist/tui-core/input-parser.js +6 -0
- package/dist/tui-core/style.d.ts +1 -8
- package/dist/tui-core/style.js +3 -110
- package/dist/tui-ui/chrome-view-model.d.ts +1 -1
- package/dist/tui-ui/chrome-view-model.js +22 -27
- package/dist/tui-ui/launch.js +14 -14
- package/dist/tui-ui/layout.d.ts +5 -7
- package/dist/tui-ui/layout.js +129 -124
- package/dist/tui-ui/runner.d.ts +11 -12
- package/dist/tui-ui/runner.js +88 -67
- package/dist/tui-ui/state.d.ts +0 -11
- package/dist/tui-ui/state.js +22 -33
- package/dist/tui-ui/terminal-image.js +22 -4
- package/dist/tui-ui/transcript-inspector-layout.js +6 -3
- package/dist/tui-ui/transcript-inspector.d.ts +1 -0
- package/dist/tui-ui/transcript-inspector.js +7 -1
- package/dist/tui-ui/transcript-layout.js +42 -6
- package/dist/utils/terminal-text.d.ts +11 -0
- package/dist/utils/terminal-text.js +114 -0
- package/docs/cli-guide.md +562 -0
- package/docs/orion.example.yaml +42 -0
- package/docs/readme.md +12 -57
- package/npm-shrinkwrap.json +484 -1855
- package/package.json +13 -63
- package/dist/migration/command.d.ts +0 -9
- package/dist/migration/command.js +0 -100
- package/dist/migration/index.d.ts +0 -7
- package/dist/migration/index.js +0 -14
- package/dist/migration/migrate.d.ts +0 -19
- package/dist/migration/migrate.js +0 -406
- package/dist/migration/types.d.ts +0 -55
- package/dist/migration/types.js +0 -6
- package/dist/services/tool-allowlist.d.ts +0 -110
- package/dist/services/tool-allowlist.js +0 -299
- package/dist/services/yaml-config.d.ts +0 -57
- package/dist/services/yaml-config.js +0 -178
- package/dist/web/cli-options.d.ts +0 -17
- package/dist/web/cli-options.js +0 -51
- package/dist/web/code-review-run-service.d.ts +0 -68
- package/dist/web/code-review-run-service.js +0 -228
- package/dist/web/context-text.d.ts +0 -17
- package/dist/web/context-text.js +0 -40
- package/dist/web/errors.d.ts +0 -6
- package/dist/web/errors.js +0 -13
- package/dist/web/event-hub.d.ts +0 -45
- package/dist/web/event-hub.js +0 -370
- package/dist/web/file-read-service.d.ts +0 -101
- package/dist/web/file-read-service.js +0 -593
- package/dist/web/file-write-service.d.ts +0 -16
- package/dist/web/file-write-service.js +0 -103
- package/dist/web/git-compare-service.d.ts +0 -45
- package/dist/web/git-compare-service.js +0 -160
- package/dist/web/git-diff-document.d.ts +0 -102
- package/dist/web/git-diff-document.js +0 -258
- package/dist/web/git-file-source.d.ts +0 -25
- package/dist/web/git-file-source.js +0 -21
- package/dist/web/git-history-service.d.ts +0 -151
- package/dist/web/git-history-service.js +0 -619
- package/dist/web/git-index-transaction.d.ts +0 -45
- package/dist/web/git-index-transaction.js +0 -174
- package/dist/web/git-patch-builder.d.ts +0 -30
- package/dist/web/git-patch-builder.js +0 -138
- package/dist/web/git-read-model-service.d.ts +0 -452
- package/dist/web/git-read-model-service.js +0 -1833
- package/dist/web/git-version-service.d.ts +0 -79
- package/dist/web/git-version-service.js +0 -279
- package/dist/web/host-daemon.d.ts +0 -70
- package/dist/web/host-daemon.js +0 -237
- package/dist/web/index.d.ts +0 -6
- package/dist/web/index.js +0 -28
- package/dist/web/launch.d.ts +0 -12
- package/dist/web/launch.js +0 -89
- package/dist/web/native-directory-picker.d.ts +0 -63
- package/dist/web/native-directory-picker.js +0 -169
- package/dist/web/protocol.d.ts +0 -705
- package/dist/web/protocol.js +0 -639
- package/dist/web/review-service.d.ts +0 -22
- package/dist/web/review-service.js +0 -68
- package/dist/web/server.d.ts +0 -20
- package/dist/web/server.js +0 -1453
- package/dist/web/session-runtime-registry.d.ts +0 -138
- package/dist/web/session-runtime-registry.js +0 -471
- package/dist/web/terminal-manager.d.ts +0 -156
- package/dist/web/terminal-manager.js +0 -779
- package/dist/web/terminal-server.d.ts +0 -8
- package/dist/web/terminal-server.js +0 -304
- package/dist/web/workbench-controller.d.ts +0 -395
- package/dist/web/workbench-controller.js +0 -3077
- package/dist/web/workspace-discovery.d.ts +0 -92
- package/dist/web/workspace-discovery.js +0 -324
- package/dist/web/workspace-mutation-arbiter.d.ts +0 -54
- package/dist/web/workspace-mutation-arbiter.js +0 -267
- package/dist/web/workspace-open-telemetry.d.ts +0 -55
- package/dist/web/workspace-open-telemetry.js +0 -73
- package/dist/web-client/assets/FilesPanel-CPDovK_K.js +0 -1
- package/dist/web-client/assets/GitPanel-DtU_TD61.js +0 -4
- package/dist/web-client/assets/ReviewPanel-vMO7BEA3.js +0 -6
- package/dist/web-client/assets/TerminalPanel-C9vDTAlS.js +0 -23
- package/dist/web-client/assets/TerminalPanel-DLuoa74B.css +0 -1
- package/dist/web-client/assets/index-CABdwh4l.css +0 -1
- package/dist/web-client/assets/index-KvshfEbl.js +0 -20
- package/dist/web-client/assets/useAutoNotice-IE1g2wcb.js +0 -1
- package/dist/web-client/favicon.svg +0 -9
- package/dist/web-client/index.html +0 -17
- package/docs/architecture/agent-mode-permission-contract.md +0 -87
- package/docs/architecture/v0.3.0-web-api.yaml +0 -1340
- package/docs/architecture/v0.3.1-web-api.yaml +0 -2338
- package/docs/architecture/v0.3.2-web-api.yaml +0 -3029
- package/docs/architecture/v0.3.3-web-api.yaml +0 -3205
- package/docs/architecture/v0.3.4-stabilization-contract.md +0 -32
- package/docs/migration/v0.1.9-to-v0.2.0.md +0 -98
- package/docs/migration/v0.2.2-to-v0.3.0-settings.md +0 -114
- package/docs/migration/v0.2.2-to-v0.3.0.md +0 -55
- package/docs/migration/v0.3.0-to-v0.3.1.md +0 -88
- package/docs/migration/v0.3.1-to-v0.3.2.md +0 -94
- package/docs/migration/v0.3.2-to-v0.3.3.md +0 -62
- package/docs/migration/v0.3.3-to-v0.3.4.md +0 -36
- package/docs/orion.example.json +0 -86
- package/docs/plan/v0.2.0-dsh-harness-redesign-plan.md +0 -1234
- package/docs/plan/v0.2.0-release-checklist.md +0 -91
- package/docs/plan/v0.3.0-node-runtime-compatibility-plan.md +0 -57
- package/docs/plan/v0.3.0-settings-integration-plan.md +0 -740
- package/docs/plan/v0.3.0-web-workbench-plan.md +0 -376
- package/docs/plan/v0.3.1-web-workbench-professional-shell-plan.md +0 -848
- package/docs/plan/v0.3.2-web-workbench-layout-and-composer-plan.md +0 -869
- package/docs/plan/v0.3.3-plan.md +0 -1113
- package/docs/plan/v0.3.4-stabilization-plan.md +0 -89
- package/docs/test/v0.3.1-web-workbench-e2e-plan.md +0 -140
- package/docs/test/v0.3.2-web-workbench-e2e-plan.md +0 -100
- package/docs/test/v0.3.3-web-workbench-e2e-plan.md +0 -79
- package/docs/test/v0.3.4-stabilization-e2e-plan.md +0 -54
|
@@ -14,6 +14,7 @@ const model_context_1 = require("../model-context");
|
|
|
14
14
|
const token_estimate_1 = require("../../utils/token-estimate");
|
|
15
15
|
const fingerprint_1 = require("./fingerprint");
|
|
16
16
|
const planner_1 = require("./planner");
|
|
17
|
+
const background_candidate_1 = require("./background-candidate");
|
|
17
18
|
function normalizeTargetRatio(value) {
|
|
18
19
|
if (!Number.isFinite(value))
|
|
19
20
|
return planner_1.DEFAULT_COMPACT_TARGET_RATIO;
|
|
@@ -24,6 +25,7 @@ function normalizeTargetRatio(value) {
|
|
|
24
25
|
// ============================================================================
|
|
25
26
|
class AutoCompact {
|
|
26
27
|
constructor(config) {
|
|
28
|
+
this.background = new background_candidate_1.BackgroundCompactCandidate(status => this.config.onBackgroundStatus?.(status));
|
|
27
29
|
this.lastCompactTime = 0;
|
|
28
30
|
this.compactCount = 0;
|
|
29
31
|
/** 最后一次计算的 token 使用量 */
|
|
@@ -55,6 +57,8 @@ class AutoCompact {
|
|
|
55
57
|
getHarnessState: config?.getHarnessState,
|
|
56
58
|
compactInstructions: config?.compactInstructions,
|
|
57
59
|
llm: config?.llm ?? undefined,
|
|
60
|
+
backgroundSummaryTransport: config?.backgroundSummaryTransport,
|
|
61
|
+
onBackgroundStatus: config?.onBackgroundStatus,
|
|
58
62
|
};
|
|
59
63
|
}
|
|
60
64
|
/**
|
|
@@ -72,6 +76,9 @@ class AutoCompact {
|
|
|
72
76
|
normalizeTargetRatio(config.targetRatio) !== this.config.targetRatio) ||
|
|
73
77
|
('compactInstructions' in config &&
|
|
74
78
|
config.compactInstructions !== this.config.compactInstructions) ||
|
|
79
|
+
('backgroundSummaryTransport' in config &&
|
|
80
|
+
config.backgroundSummaryTransport !== this.config.backgroundSummaryTransport) ||
|
|
81
|
+
('llm' in config && (config.llm ?? undefined) !== this.config.llm) ||
|
|
75
82
|
(config.enabled !== undefined && config.enabled !== this.config.enabled);
|
|
76
83
|
if (config.threshold !== undefined)
|
|
77
84
|
this.config.threshold = config.threshold;
|
|
@@ -104,6 +111,11 @@ class AutoCompact {
|
|
|
104
111
|
}
|
|
105
112
|
if ('llm' in config)
|
|
106
113
|
this.config.llm = config.llm ?? undefined;
|
|
114
|
+
if ('backgroundSummaryTransport' in config) {
|
|
115
|
+
this.config.backgroundSummaryTransport = config.backgroundSummaryTransport;
|
|
116
|
+
}
|
|
117
|
+
if ('onBackgroundStatus' in config)
|
|
118
|
+
this.config.onBackgroundStatus = config.onBackgroundStatus;
|
|
107
119
|
if (policyChanged)
|
|
108
120
|
this.resetAttemptPolicyState();
|
|
109
121
|
}
|
|
@@ -148,16 +160,16 @@ class AutoCompact {
|
|
|
148
160
|
async checkAndCompact(messages, usedTokens) {
|
|
149
161
|
return (await this.checkAndCompactOutcome(messages, usedTokens)).messages;
|
|
150
162
|
}
|
|
151
|
-
async checkAndCompactOutcome(messages, usedTokens, postValidate) {
|
|
152
|
-
return this.compactIfNeeded(messages, usedTokens, this.config.threshold, 'threshold', postValidate);
|
|
163
|
+
async checkAndCompactOutcome(messages, usedTokens, postValidate, abortSignal) {
|
|
164
|
+
return this.compactIfNeeded(messages, usedTokens, this.config.threshold, 'threshold', postValidate, abortSignal);
|
|
153
165
|
}
|
|
154
166
|
/**
|
|
155
167
|
* Finalize an already-triggered compact before persistence. Once a request
|
|
156
168
|
* has a pending candidate, the final history must still meet the 65% target;
|
|
157
169
|
* waiting until the normal 95% trigger would produce an invalid checkpoint.
|
|
158
170
|
*/
|
|
159
|
-
async ensureHeadroomAndCompactOutcome(messages, usedTokens, postValidate) {
|
|
160
|
-
return this.compactIfNeeded(messages, usedTokens, this.config.targetRatio, 'threshold', postValidate);
|
|
171
|
+
async ensureHeadroomAndCompactOutcome(messages, usedTokens, postValidate, abortSignal) {
|
|
172
|
+
return this.compactIfNeeded(messages, usedTokens, this.config.targetRatio, 'threshold', postValidate, abortSignal);
|
|
161
173
|
}
|
|
162
174
|
/**
|
|
163
175
|
* LLM 调用前的预测压缩。用于避免下一次请求已经接近模型上下文上限。
|
|
@@ -165,12 +177,105 @@ class AutoCompact {
|
|
|
165
177
|
async checkPredictiveAndCompact(messages, predictedTokens) {
|
|
166
178
|
return (await this.checkPredictiveCompactOutcome(messages, predictedTokens)).messages;
|
|
167
179
|
}
|
|
168
|
-
async checkPredictiveCompactOutcome(messages, predictedTokens, postValidate) {
|
|
169
|
-
return this.compactIfNeeded(messages, predictedTokens, this.config.predictiveCompactThreshold, 'predictive', postValidate);
|
|
180
|
+
async checkPredictiveCompactOutcome(messages, predictedTokens, postValidate, abortSignal) {
|
|
181
|
+
return this.compactIfNeeded(messages, predictedTokens, this.config.predictiveCompactThreshold, 'predictive', postValidate, abortSignal);
|
|
182
|
+
}
|
|
183
|
+
/** Try optional local compaction without latching a failure into the
|
|
184
|
+
* token-pressure policy or spending an extra provider request. */
|
|
185
|
+
async compactForToolPressure(messages, predictedTokens, postValidate, abortSignal) {
|
|
186
|
+
const local = new AutoCompact({
|
|
187
|
+
...this.config,
|
|
188
|
+
llm: undefined,
|
|
189
|
+
onCompact: undefined,
|
|
190
|
+
backgroundSummaryTransport: undefined,
|
|
191
|
+
});
|
|
192
|
+
return local.compactIfNeeded(messages, predictedTokens, 0, 'predictive', postValidate, abortSignal);
|
|
193
|
+
}
|
|
194
|
+
cancelBackground(reason) {
|
|
195
|
+
this.background.cancel(reason);
|
|
170
196
|
}
|
|
171
|
-
|
|
197
|
+
compatibility(transport, options) {
|
|
198
|
+
return JSON.stringify([
|
|
199
|
+
transport.revision,
|
|
200
|
+
this.config.modelId,
|
|
201
|
+
this.getSafeInputBudget(),
|
|
202
|
+
this.config.targetRatio,
|
|
203
|
+
options.maxMessages,
|
|
204
|
+
options.summaryOptions?.instructions,
|
|
205
|
+
options.harnessState?.taskEpoch ?? options.contextCapsule?.contract?.taskEpoch,
|
|
206
|
+
options.contextCapsule?.contract?.objective,
|
|
207
|
+
]);
|
|
208
|
+
}
|
|
209
|
+
async compactAtCheckpoint(messages, fingerprint, options, postValidate, allowReuse = true) {
|
|
210
|
+
if (!this.config.backgroundSummaryTransport) {
|
|
211
|
+
const result = await (0, compact_1.compactMessages)(messages, options);
|
|
212
|
+
return { result, validation: postValidate?.(result) };
|
|
213
|
+
}
|
|
214
|
+
const transport = this.config.backgroundSummaryTransport();
|
|
215
|
+
const prepared = transport && allowReuse
|
|
216
|
+
? this.background.take(messages, fingerprint, this.compatibility(transport, options))
|
|
217
|
+
: undefined;
|
|
218
|
+
if (!prepared)
|
|
219
|
+
this.background.cancel('foreground_required');
|
|
220
|
+
if (prepared) {
|
|
221
|
+
try {
|
|
222
|
+
// Rebuild the current plan and its authoritative semantic data. The
|
|
223
|
+
// historical hint can only replace its own verified atomic prefix.
|
|
224
|
+
const result = await (0, compact_1.compactMessages)(messages, {
|
|
225
|
+
...options,
|
|
226
|
+
preparedSummary: prepared,
|
|
227
|
+
llm: undefined,
|
|
228
|
+
});
|
|
229
|
+
const validation = postValidate?.(result);
|
|
230
|
+
if (result.diagnostics.some(entry => entry.code === 'background_summary_reused') &&
|
|
231
|
+
(!validation || validation.valid)) {
|
|
232
|
+
this.background.recordRoute('adopted');
|
|
233
|
+
return { result, validation };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
if (!(error instanceof compact_1.CompactCandidateValidationError))
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Context is already due. A pending provider call must never delay it.
|
|
242
|
+
this.background.recordRoute('foreground', prepared ? 'candidate_revalidation' : undefined);
|
|
243
|
+
if (options.compactMode === 'manual') {
|
|
244
|
+
const result = transport
|
|
245
|
+
? await this.compactManualWithProvider(messages, options, transport)
|
|
246
|
+
: await (0, compact_1.compactMessages)(messages, options);
|
|
247
|
+
return { result, validation: postValidate?.(result) };
|
|
248
|
+
}
|
|
249
|
+
const result = await (0, compact_1.compactMessages)(messages, { ...options, llm: undefined });
|
|
250
|
+
return { result, validation: postValidate?.(result) };
|
|
251
|
+
}
|
|
252
|
+
async compactManualWithProvider(messages, options, transport) {
|
|
253
|
+
const timeout = AbortSignal.timeout(8000);
|
|
254
|
+
const abortSignal = options.abortSignal
|
|
255
|
+
? AbortSignal.any([timeout, options.abortSignal])
|
|
256
|
+
: timeout;
|
|
257
|
+
try {
|
|
258
|
+
return await (0, compact_1.compactMessages)(messages, {
|
|
259
|
+
...options,
|
|
260
|
+
llm: transport.llm,
|
|
261
|
+
summaryOptions: { ...options.summaryOptions, singleProviderRequest: true },
|
|
262
|
+
abortSignal,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
if (!timeout.aborted || options.abortSignal?.aborted)
|
|
267
|
+
throw error;
|
|
268
|
+
this.background.recordRoute('foreground', 'summary_timeout');
|
|
269
|
+
return (0, compact_1.compactMessages)(messages, { ...options, llm: undefined });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async compactIfNeeded(messages, usedTokens, threshold, mode, postValidate, abortSignal) {
|
|
172
273
|
const beforeTokens = (0, token_estimate_1.estimateMessagesTokens)(messages);
|
|
173
274
|
const fingerprint = (0, fingerprint_1.canonicalMessagesFingerprint)(messages);
|
|
275
|
+
if (abortSignal?.aborted) {
|
|
276
|
+
this.cancelBackground('cancelled');
|
|
277
|
+
return this.unchangedAttempt('not_needed', messages, mode, fingerprint, 'cancelled');
|
|
278
|
+
}
|
|
174
279
|
if (!this.config.enabled) {
|
|
175
280
|
return this.unchangedAttempt('not_needed', messages, mode, fingerprint, 'disabled');
|
|
176
281
|
}
|
|
@@ -184,8 +289,31 @@ class AutoCompact {
|
|
|
184
289
|
const harnessState = shouldCaptureSemanticAuthority
|
|
185
290
|
? (this.config.getHarnessState?.() ?? undefined)
|
|
186
291
|
: undefined;
|
|
292
|
+
const options = {
|
|
293
|
+
maxMessages: this.config.maxMessages,
|
|
294
|
+
summaryOptions: { instructions: this.config.compactInstructions },
|
|
295
|
+
contextCapsule,
|
|
296
|
+
harnessState,
|
|
297
|
+
llm: this.config.llm,
|
|
298
|
+
compactMode: 'auto_pre_turn',
|
|
299
|
+
safeInputBudget: this.getPlannerSafeInputBudget(),
|
|
300
|
+
targetRatio: this.config.targetRatio,
|
|
301
|
+
abortSignal,
|
|
302
|
+
};
|
|
187
303
|
// 达到阈值才触发
|
|
188
304
|
if (!shouldCompact) {
|
|
305
|
+
// An answer can cross the early threshold on its own. Prepare from
|
|
306
|
+
// that complete history before the next user turn reaches the hard limit.
|
|
307
|
+
const transport = this.preCompactArmed
|
|
308
|
+
? this.config.backgroundSummaryTransport?.()
|
|
309
|
+
: undefined;
|
|
310
|
+
if (transport) {
|
|
311
|
+
this.background.offer(messages, fingerprint, this.compatibility(transport, options), {
|
|
312
|
+
...options,
|
|
313
|
+
llm: transport.llm,
|
|
314
|
+
summaryOptions: { ...options.summaryOptions, singleProviderRequest: true },
|
|
315
|
+
}, abortSignal);
|
|
316
|
+
}
|
|
189
317
|
this.resetNoProgressAttempts();
|
|
190
318
|
return this.unchangedAttempt('not_needed', messages, mode, fingerprint, 'below_threshold');
|
|
191
319
|
}
|
|
@@ -205,19 +333,14 @@ class AutoCompact {
|
|
|
205
333
|
return this.recordNoProgress('duplicate', messages, mode, fingerprint, beforeTokens, 'The same compact history reached the trigger again without new durable context.');
|
|
206
334
|
}
|
|
207
335
|
let result;
|
|
336
|
+
let postValidation;
|
|
208
337
|
try {
|
|
209
|
-
result = await
|
|
210
|
-
maxMessages: this.config.maxMessages,
|
|
211
|
-
summaryOptions: { instructions: this.config.compactInstructions },
|
|
212
|
-
contextCapsule,
|
|
213
|
-
harnessState,
|
|
214
|
-
llm: this.config.llm,
|
|
215
|
-
compactMode: 'auto_pre_turn',
|
|
216
|
-
safeInputBudget: this.getPlannerSafeInputBudget(),
|
|
217
|
-
targetRatio: this.config.targetRatio,
|
|
218
|
-
});
|
|
338
|
+
({ result, validation: postValidation } = await this.compactAtCheckpoint(messages, fingerprint, options, postValidate));
|
|
219
339
|
}
|
|
220
340
|
catch (error) {
|
|
341
|
+
if (abortSignal?.aborted) {
|
|
342
|
+
return this.unchangedAttempt('not_needed', messages, mode, fingerprint, 'cancelled');
|
|
343
|
+
}
|
|
221
344
|
if (!(error instanceof compact_1.CompactCandidateValidationError))
|
|
222
345
|
throw error;
|
|
223
346
|
this.rejectedAttemptCount++;
|
|
@@ -235,7 +358,6 @@ class AutoCompact {
|
|
|
235
358
|
this.lastPauseFailure = failure;
|
|
236
359
|
return this.pausedAttempt(messages, mode, failure);
|
|
237
360
|
}
|
|
238
|
-
const postValidation = postValidate?.(result);
|
|
239
361
|
if (postValidation && !postValidation.valid) {
|
|
240
362
|
this.rejectedAttemptCount++;
|
|
241
363
|
this.consecutiveNoProgressAttempts++;
|
|
@@ -347,13 +469,13 @@ class AutoCompact {
|
|
|
347
469
|
this.lastPauseFailure = null;
|
|
348
470
|
}
|
|
349
471
|
resetAttemptPolicyState() {
|
|
472
|
+
this.cancelBackground('policy_changed');
|
|
350
473
|
this.lastCompactFingerprint = null;
|
|
351
474
|
this.lastRejectedFingerprint = null;
|
|
352
475
|
this.resetNoProgressAttempts();
|
|
353
476
|
}
|
|
354
477
|
getSafeInputBudget() {
|
|
355
|
-
return (0, model_context_1.resolveContextBudget)(this.config.modelId, this.config.outputReserveTokens)
|
|
356
|
-
.safeInputBudget;
|
|
478
|
+
return (0, model_context_1.resolveContextBudget)(this.config.modelId, this.config.outputReserveTokens, this.config.llm?.getContextWindow?.()).safeInputBudget;
|
|
357
479
|
}
|
|
358
480
|
/** Translate provider-observed correction into the estimator budget. */
|
|
359
481
|
getPlannerSafeInputBudget() {
|
|
@@ -416,6 +538,7 @@ class AutoCompact {
|
|
|
416
538
|
*/
|
|
417
539
|
getStats() {
|
|
418
540
|
const model = (0, model_context_1.resolveModelContext)(this.config.modelId);
|
|
541
|
+
const configuredWindow = this.config.llm?.getContextWindow?.();
|
|
419
542
|
return {
|
|
420
543
|
compactCount: this.compactCount,
|
|
421
544
|
lastCompactTime: this.lastCompactTime,
|
|
@@ -424,8 +547,8 @@ class AutoCompact {
|
|
|
424
547
|
predictiveCompactThreshold: this.config.predictiveCompactThreshold,
|
|
425
548
|
enabled: this.config.enabled,
|
|
426
549
|
modelId: this.config.modelId,
|
|
427
|
-
contextWindow: model.contextWindow,
|
|
428
|
-
maxOutputTokens: model.maxOutputTokens,
|
|
550
|
+
contextWindow: configuredWindow ?? model.contextWindow,
|
|
551
|
+
maxOutputTokens: configuredWindow === undefined ? model.maxOutputTokens : this.config.outputReserveTokens,
|
|
429
552
|
ctxPercent: this.lastCtxPercent,
|
|
430
553
|
lastTokenCount: this.lastTokenCount,
|
|
431
554
|
preCompactArmed: this.preCompactArmed,
|
|
@@ -437,6 +560,7 @@ class AutoCompact {
|
|
|
437
560
|
consecutiveNoProgressAttempts: this.consecutiveNoProgressAttempts,
|
|
438
561
|
duplicateAttemptCount: this.duplicateAttemptCount,
|
|
439
562
|
rejectedAttemptCount: this.rejectedAttemptCount,
|
|
563
|
+
backgroundCompact: this.background.getStatus(),
|
|
440
564
|
};
|
|
441
565
|
}
|
|
442
566
|
getLastCompactResult() {
|
|
@@ -447,6 +571,8 @@ class AutoCompact {
|
|
|
447
571
|
*/
|
|
448
572
|
setEnabled(enabled) {
|
|
449
573
|
this.config.enabled = enabled;
|
|
574
|
+
if (!enabled)
|
|
575
|
+
this.cancelBackground('disabled');
|
|
450
576
|
}
|
|
451
577
|
}
|
|
452
578
|
exports.AutoCompact = AutoCompact;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Message } from '../llm';
|
|
2
|
+
import { type CompactOptions, type CompactResult } from './compact';
|
|
3
|
+
type CandidateState = 'preparing' | 'ready' | 'adopted' | 'discarded' | 'failed' | 'foreground';
|
|
4
|
+
export interface BackgroundCompactStatus {
|
|
5
|
+
readonly state: CandidateState;
|
|
6
|
+
readonly reason?: string;
|
|
7
|
+
readonly elapsedMs: number;
|
|
8
|
+
readonly sourceMessages?: number;
|
|
9
|
+
readonly summaryAttempts?: number;
|
|
10
|
+
readonly summaryResponses?: number;
|
|
11
|
+
readonly summaryRoute?: CompactResult['summarySource'];
|
|
12
|
+
readonly promptTokens?: number;
|
|
13
|
+
readonly completionTokens?: number;
|
|
14
|
+
}
|
|
15
|
+
/** In-memory work owned by a session. This object cannot commit a checkpoint. */
|
|
16
|
+
export declare class BackgroundCompactCandidate {
|
|
17
|
+
private readonly onStatus?;
|
|
18
|
+
private attempt?;
|
|
19
|
+
private lastAttempt?;
|
|
20
|
+
private lastStatus?;
|
|
21
|
+
constructor(onStatus?: ((status: BackgroundCompactStatus) => void) | undefined);
|
|
22
|
+
getStatus(): BackgroundCompactStatus | undefined;
|
|
23
|
+
offer(messages: readonly Message[], fingerprint: string, compatibility: string, options: CompactOptions, abortSignal?: AbortSignal): void;
|
|
24
|
+
take(messages: readonly Message[], fingerprint: string, compatibility: string): CompactResult | undefined;
|
|
25
|
+
recordRoute(state: 'adopted' | 'foreground', reason?: string): void;
|
|
26
|
+
cancel(reason: string): void;
|
|
27
|
+
private matches;
|
|
28
|
+
private finish;
|
|
29
|
+
private record;
|
|
30
|
+
private publish;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=background-candidate.d.ts.map
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BackgroundCompactCandidate = void 0;
|
|
4
|
+
const observability_1 = require("../../utils/observability");
|
|
5
|
+
const compact_1 = require("./compact");
|
|
6
|
+
const fingerprint_1 = require("./fingerprint");
|
|
7
|
+
const TIMEOUT_MS = 8000;
|
|
8
|
+
const MAX_CANDIDATE_AGE_MS = 120000;
|
|
9
|
+
/** In-memory work owned by a session. This object cannot commit a checkpoint. */
|
|
10
|
+
class BackgroundCompactCandidate {
|
|
11
|
+
constructor(onStatus) {
|
|
12
|
+
this.onStatus = onStatus;
|
|
13
|
+
}
|
|
14
|
+
getStatus() {
|
|
15
|
+
if (this.attempt && this.lastStatus?.state === 'preparing') {
|
|
16
|
+
return { ...this.lastStatus, elapsedMs: Date.now() - this.attempt.startedAt };
|
|
17
|
+
}
|
|
18
|
+
return this.lastStatus;
|
|
19
|
+
}
|
|
20
|
+
offer(messages, fingerprint, compatibility, options, abortSignal) {
|
|
21
|
+
if (abortSignal?.aborted)
|
|
22
|
+
return;
|
|
23
|
+
if (this.attempt && this.matches(this.attempt, messages, fingerprint, compatibility))
|
|
24
|
+
return;
|
|
25
|
+
this.cancel('source_changed');
|
|
26
|
+
if (this.lastAttempt?.fingerprint === fingerprint &&
|
|
27
|
+
this.lastAttempt.compatibility === compatibility)
|
|
28
|
+
return;
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const onAbort = () => this.cancel('cancelled');
|
|
31
|
+
const timer = setTimeout(() => this.cancel('timeout'), TIMEOUT_MS);
|
|
32
|
+
timer.unref();
|
|
33
|
+
abortSignal?.addEventListener('abort', onAbort, { once: true });
|
|
34
|
+
const attempt = {
|
|
35
|
+
fingerprint,
|
|
36
|
+
compatibility,
|
|
37
|
+
messageCount: messages.length,
|
|
38
|
+
startedAt: Date.now(),
|
|
39
|
+
controller,
|
|
40
|
+
summaryAttempts: 0,
|
|
41
|
+
summaryResponses: 0,
|
|
42
|
+
clean: () => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
abortSignal?.removeEventListener('abort', onAbort);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
this.attempt = attempt;
|
|
48
|
+
this.lastAttempt = { fingerprint, compatibility };
|
|
49
|
+
this.record(attempt, 'preparing');
|
|
50
|
+
const snapshot = structuredClone([...messages]);
|
|
51
|
+
const contextCapsule = structuredClone(options.contextCapsule);
|
|
52
|
+
const harnessState = structuredClone(options.harnessState);
|
|
53
|
+
const llm = options.llm;
|
|
54
|
+
// Yield a turn of the event loop so starting speculative work does not add
|
|
55
|
+
// summary planning or provider round trips to the requesting checkpoint.
|
|
56
|
+
void new Promise(resolve => setImmediate(resolve)).then(async () => {
|
|
57
|
+
if (this.attempt !== attempt || controller.signal.aborted)
|
|
58
|
+
return;
|
|
59
|
+
try {
|
|
60
|
+
const result = await (0, compact_1.compactMessages)(snapshot, {
|
|
61
|
+
...options,
|
|
62
|
+
contextCapsule,
|
|
63
|
+
harnessState,
|
|
64
|
+
...(llm
|
|
65
|
+
? {
|
|
66
|
+
llm: {
|
|
67
|
+
chat: async (input, tools, request) => {
|
|
68
|
+
attempt.summaryAttempts++;
|
|
69
|
+
(0, observability_1.incrementDiagnosticMetric)('compact.candidate.summary_attempt');
|
|
70
|
+
this.record(attempt, 'preparing');
|
|
71
|
+
const response = await llm.chat(input, tools, request);
|
|
72
|
+
if (this.attempt !== attempt || controller.signal.aborted)
|
|
73
|
+
return response;
|
|
74
|
+
attempt.summaryResponses++;
|
|
75
|
+
if (response.usage) {
|
|
76
|
+
attempt.promptTokens =
|
|
77
|
+
(attempt.promptTokens ?? 0) + response.usage.promptTokens;
|
|
78
|
+
attempt.completionTokens =
|
|
79
|
+
(attempt.completionTokens ?? 0) + response.usage.completionTokens;
|
|
80
|
+
}
|
|
81
|
+
this.record(attempt, 'preparing');
|
|
82
|
+
return response;
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
: {}),
|
|
87
|
+
abortSignal: controller.signal,
|
|
88
|
+
});
|
|
89
|
+
if (this.attempt !== attempt)
|
|
90
|
+
return;
|
|
91
|
+
if (result.plan.evictedGroups.length === 0) {
|
|
92
|
+
this.finish(attempt, 'discarded', 'no_evictable_history');
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
attempt.result = result;
|
|
96
|
+
attempt.clean();
|
|
97
|
+
// Keep cancellation armed while a ready candidate waits for the next
|
|
98
|
+
// checkpoint, even though no provider work remains.
|
|
99
|
+
abortSignal?.addEventListener('abort', onAbort, { once: true });
|
|
100
|
+
this.record(attempt, 'ready', result.diagnostics.find(entry => entry.fallbackUsed)?.code);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
if (this.attempt === attempt)
|
|
104
|
+
this.finish(attempt, 'failed', 'summary_invalid');
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
take(messages, fingerprint, compatibility) {
|
|
109
|
+
const attempt = this.attempt;
|
|
110
|
+
if (!attempt)
|
|
111
|
+
return undefined;
|
|
112
|
+
if (!this.matches(attempt, messages, fingerprint, compatibility)) {
|
|
113
|
+
this.cancel('source_changed');
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
if (!attempt.result) {
|
|
117
|
+
this.cancel('foreground_required');
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
attempt.clean();
|
|
121
|
+
this.attempt = undefined;
|
|
122
|
+
return attempt.result;
|
|
123
|
+
}
|
|
124
|
+
recordRoute(state, reason) {
|
|
125
|
+
(0, observability_1.incrementDiagnosticMetric)(`compact.candidate.${state}`);
|
|
126
|
+
this.publish({
|
|
127
|
+
...this.lastStatus,
|
|
128
|
+
state,
|
|
129
|
+
reason: reason ?? this.lastStatus?.reason,
|
|
130
|
+
elapsedMs: this.lastStatus?.elapsedMs ?? 0,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
cancel(reason) {
|
|
134
|
+
const attempt = this.attempt;
|
|
135
|
+
if (!attempt)
|
|
136
|
+
return;
|
|
137
|
+
attempt.controller.abort();
|
|
138
|
+
this.finish(attempt, reason === 'timeout' ? 'failed' : 'discarded', reason);
|
|
139
|
+
}
|
|
140
|
+
matches(attempt, messages, fingerprint, compatibility) {
|
|
141
|
+
if (attempt.compatibility !== compatibility ||
|
|
142
|
+
messages.length < attempt.messageCount ||
|
|
143
|
+
Date.now() - attempt.startedAt > MAX_CANDIDATE_AGE_MS) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
const prefix = messages.length === attempt.messageCount
|
|
147
|
+
? fingerprint
|
|
148
|
+
: (0, fingerprint_1.canonicalMessagesFingerprint)(messages.slice(0, attempt.messageCount));
|
|
149
|
+
return attempt.fingerprint === prefix;
|
|
150
|
+
}
|
|
151
|
+
finish(attempt, state, reason) {
|
|
152
|
+
attempt.clean();
|
|
153
|
+
this.attempt = undefined;
|
|
154
|
+
this.record(attempt, state, reason);
|
|
155
|
+
}
|
|
156
|
+
record(attempt, state, reason) {
|
|
157
|
+
if (this.lastStatus?.state !== state)
|
|
158
|
+
(0, observability_1.incrementDiagnosticMetric)(`compact.candidate.${state}`);
|
|
159
|
+
this.publish({
|
|
160
|
+
state,
|
|
161
|
+
...(reason ? { reason } : {}),
|
|
162
|
+
elapsedMs: Date.now() - attempt.startedAt,
|
|
163
|
+
sourceMessages: attempt.messageCount,
|
|
164
|
+
summaryAttempts: attempt.summaryAttempts,
|
|
165
|
+
summaryResponses: attempt.summaryResponses,
|
|
166
|
+
...(attempt.result ? { summaryRoute: attempt.result.summarySource } : {}),
|
|
167
|
+
...(attempt.promptTokens === undefined ? {} : { promptTokens: attempt.promptTokens }),
|
|
168
|
+
...(attempt.completionTokens === undefined
|
|
169
|
+
? {}
|
|
170
|
+
: { completionTokens: attempt.completionTokens }),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
publish(status) {
|
|
174
|
+
this.lastStatus = status;
|
|
175
|
+
try {
|
|
176
|
+
this.onStatus?.(status);
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
// A projection must not prevent compaction from reaching its terminal state.
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
exports.BackgroundCompactCandidate = BackgroundCompactCandidate;
|
|
184
|
+
//# sourceMappingURL=background-candidate.js.map
|
|
@@ -16,7 +16,10 @@ export interface CompactOptions {
|
|
|
16
16
|
contextCapsule?: ContextCapsule;
|
|
17
17
|
harnessState?: HarnessState;
|
|
18
18
|
goalObjective?: string;
|
|
19
|
-
llm?: LLMService
|
|
19
|
+
llm?: Pick<LLMService, 'chat'>;
|
|
20
|
+
abortSignal?: AbortSignal;
|
|
21
|
+
/** An owner-validated prepared result may reuse exactly its evicted prefix. */
|
|
22
|
+
preparedSummary?: CompactResult;
|
|
20
23
|
compactMode?: 'manual' | 'auto_pre_turn' | 'mid_turn';
|
|
21
24
|
/** Safe provider input budget used by the semantic headroom validator. */
|
|
22
25
|
safeInputBudget?: number;
|
|
@@ -306,6 +306,20 @@ function resultFromCandidate(candidate) {
|
|
|
306
306
|
plan: candidate.plan,
|
|
307
307
|
};
|
|
308
308
|
}
|
|
309
|
+
function prepareReusePlan(plan, prepared) {
|
|
310
|
+
const evicted = prepared.plan.evictedGroups;
|
|
311
|
+
if (evicted.length === 0 ||
|
|
312
|
+
evicted.length >= plan.groups.length ||
|
|
313
|
+
evicted.some((group, index) => plan.groups[index].id !== group.id))
|
|
314
|
+
return undefined;
|
|
315
|
+
const recentGroups = plan.groups.slice(evicted.length);
|
|
316
|
+
return {
|
|
317
|
+
...plan,
|
|
318
|
+
evictedGroups: plan.groups.slice(0, evicted.length),
|
|
319
|
+
recentGroups,
|
|
320
|
+
recentStartIndex: recentGroups[0].startIndex,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
309
323
|
/** Prepare a model-context projection without mutating or persisting its source. */
|
|
310
324
|
async function prepareCompactCandidate(messages, options = {}) {
|
|
311
325
|
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
@@ -322,13 +336,15 @@ async function prepareCompactCandidate(messages, options = {}) {
|
|
|
322
336
|
{ role: 'user', content: CONTEXT_SUMMARY_PREFIX },
|
|
323
337
|
{ role: 'assistant', content: SUMMARY_ACK },
|
|
324
338
|
]);
|
|
325
|
-
const
|
|
339
|
+
const currentPlan = (0, planner_1.planCompactMessages)(conversation, {
|
|
326
340
|
maxMessages: opts.maxMessages,
|
|
327
341
|
safeInputBudget: opts.safeInputBudget,
|
|
328
342
|
targetRatio: opts.targetRatio ?? planner_1.DEFAULT_COMPACT_TARGET_RATIO,
|
|
329
343
|
fixedTokens: (0, token_estimate_1.estimateMessagesTokens)([...systemMessages, ...pinned]) + framingTokens,
|
|
330
344
|
summaryReserveTokens: opts.summaryReserveTokens,
|
|
331
345
|
});
|
|
346
|
+
const reused = opts.preparedSummary && prepareReusePlan(currentPlan, opts.preparedSummary);
|
|
347
|
+
const plan = reused ?? currentPlan;
|
|
332
348
|
const evictedMessages = (0, planner_1.flattenCompactGroups)(plan.evictedGroups);
|
|
333
349
|
const oldMessages = priorSummary ? [{ ...priorSummary }, ...evictedMessages] : evictedMessages;
|
|
334
350
|
let semanticSummary = (0, semantic_summary_1.extractCompactSummary)(plan.evictedGroups, {
|
|
@@ -344,8 +360,19 @@ async function prepareCompactCandidate(messages, options = {}) {
|
|
|
344
360
|
let summarySource = 'heuristic';
|
|
345
361
|
let diagnostics = [];
|
|
346
362
|
if (plan.evictedGroups.length > 0) {
|
|
347
|
-
if (opts.
|
|
348
|
-
|
|
363
|
+
if (reused && opts.preparedSummary) {
|
|
364
|
+
({ summary, summarySource, diagnostics } = opts.preparedSummary);
|
|
365
|
+
diagnostics = [
|
|
366
|
+
...diagnostics,
|
|
367
|
+
{
|
|
368
|
+
code: 'background_summary_reused',
|
|
369
|
+
message: 'Reused the verified background summary; subsequent messages were retained.',
|
|
370
|
+
fallbackUsed: false,
|
|
371
|
+
},
|
|
372
|
+
];
|
|
373
|
+
}
|
|
374
|
+
else if (opts.llm) {
|
|
375
|
+
const generated = await (0, summary_generator_1.generateSummaryWithSource)(oldMessages, opts.llm, opts.summaryOptions, opts.abortSignal);
|
|
349
376
|
summary = generated.text;
|
|
350
377
|
summarySource = generated.source;
|
|
351
378
|
diagnostics = generated.diagnostics;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ContextCapsule, HarnessState } from '../../harness';
|
|
2
|
-
import type { LLMService, Message } from '../llm';
|
|
3
|
-
import { AutoCompact } from './auto-compact';
|
|
4
|
-
import {
|
|
2
|
+
import type { CompactSummaryTransport, LLMService, Message } from '../llm';
|
|
3
|
+
import { AutoCompact, type AutoCompactConfig } from './auto-compact';
|
|
4
|
+
import type { CompactResult } from './compact';
|
|
5
5
|
export interface CompactCoordinatorConfig {
|
|
6
6
|
modelId: string;
|
|
7
7
|
llm?: LLMService | null;
|
|
@@ -12,6 +12,8 @@ export interface CompactCoordinatorConfig {
|
|
|
12
12
|
compactInstructions?: string;
|
|
13
13
|
getContextCapsule?: () => ContextCapsule | undefined | null;
|
|
14
14
|
getHarnessState?: () => HarnessState | undefined | null;
|
|
15
|
+
backgroundSummaryTransport?: () => CompactSummaryTransport | undefined;
|
|
16
|
+
onBackgroundStatus?: AutoCompactConfig['onBackgroundStatus'];
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* Owns compact policy and provider calibration for one runtime. Manual
|
|
@@ -24,7 +26,11 @@ export declare class CompactCoordinator {
|
|
|
24
26
|
constructor(config: CompactCoordinatorConfig);
|
|
25
27
|
configure(config: Partial<CompactCoordinatorConfig>): void;
|
|
26
28
|
getAutomatic(): AutoCompact;
|
|
27
|
-
|
|
29
|
+
cancelBackground(reason: string): void;
|
|
30
|
+
compactManual(messages: Message[], maxMessages: number, focus?: string, budget?: {
|
|
31
|
+
readonly safeInputBudget: number;
|
|
32
|
+
readonly targetTokens: number;
|
|
33
|
+
}, abortSignal?: AbortSignal): Promise<CompactResult>;
|
|
28
34
|
private toAutoConfig;
|
|
29
35
|
}
|
|
30
36
|
//# sourceMappingURL=coordinator.d.ts.map
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.CompactCoordinator = void 0;
|
|
4
4
|
const model_context_1 = require("../model-context");
|
|
5
5
|
const auto_compact_1 = require("./auto-compact");
|
|
6
|
-
const
|
|
6
|
+
const fingerprint_1 = require("./fingerprint");
|
|
7
7
|
const planner_1 = require("./planner");
|
|
8
8
|
/**
|
|
9
9
|
* Owns compact policy and provider calibration for one runtime. Manual
|
|
@@ -22,18 +22,25 @@ class CompactCoordinator {
|
|
|
22
22
|
getAutomatic() {
|
|
23
23
|
return this.automatic;
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
cancelBackground(reason) {
|
|
26
|
+
this.automatic.cancelBackground(reason);
|
|
27
|
+
}
|
|
28
|
+
async compactManual(messages, maxMessages, focus, budget, abortSignal) {
|
|
29
|
+
const { result } = await this.automatic.compactAtCheckpoint(messages, (0, fingerprint_1.canonicalMessagesFingerprint)(messages), {
|
|
27
30
|
maxMessages,
|
|
28
31
|
summaryOptions: { focus, instructions: this.config.compactInstructions },
|
|
29
32
|
contextCapsule: this.config.getContextCapsule?.() ?? undefined,
|
|
30
33
|
harnessState: this.config.getHarnessState?.() ?? undefined,
|
|
31
34
|
llm: this.config.llm ?? undefined,
|
|
32
35
|
compactMode: 'manual',
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
abortSignal,
|
|
37
|
+
safeInputBudget: budget?.safeInputBudget ??
|
|
38
|
+
(0, model_context_1.resolveContextBudget)(this.config.modelId, this.config.outputReserveTokens, this.config.llm?.getContextWindow?.()).safeInputBudget,
|
|
39
|
+
targetRatio: budget
|
|
40
|
+
? budget.targetTokens / budget.safeInputBudget
|
|
41
|
+
: (this.config.targetRatio ?? planner_1.DEFAULT_COMPACT_TARGET_RATIO),
|
|
42
|
+
}, undefined, !focus && !budget);
|
|
43
|
+
return result;
|
|
37
44
|
}
|
|
38
45
|
toAutoConfig(config) {
|
|
39
46
|
return {
|
|
@@ -44,6 +51,9 @@ class CompactCoordinator {
|
|
|
44
51
|
maxConsecutiveNoProgressAttempts: config.maxConsecutiveNoProgressAttempts,
|
|
45
52
|
compactInstructions: config.compactInstructions,
|
|
46
53
|
maxMessages: 20,
|
|
54
|
+
...(config.backgroundSummaryTransport ? { preCompactThreshold: 0.68 } : {}),
|
|
55
|
+
backgroundSummaryTransport: config.backgroundSummaryTransport,
|
|
56
|
+
onBackgroundStatus: config.onBackgroundStatus,
|
|
47
57
|
getContextCapsule: config.getContextCapsule,
|
|
48
58
|
getHarnessState: config.getHarnessState,
|
|
49
59
|
};
|