@bastani/atomic 0.9.28-alpha.1 → 0.9.28-alpha.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 +60 -0
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/native-client-licenses/NOTICE +2 -0
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +34 -0
- package/dist/builtin/workflows/builtin/{chunk-wz8v67bn.js → chunk-aqv8ny38.js} +257 -259
- package/dist/builtin/workflows/builtin/{chunk-f61w0hpm.js → chunk-br4ac9a3.js} +1 -1
- package/dist/builtin/workflows/builtin/{chunk-tecnpe8j.js → chunk-p3xmajmj.js} +438 -39
- package/dist/builtin/workflows/builtin/chunk-p6jagvyf.js +8 -0
- package/dist/builtin/workflows/builtin/goal.js +2 -2
- package/dist/builtin/workflows/builtin/index.js +4 -4
- package/dist/builtin/workflows/builtin/ralph.js +2 -2
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/authoring.d.ts +14 -0
- package/dist/builtin/workflows/src/extension/index.bundle.mjs +111102 -110218
- package/dist/builtin/workflows/src/index.js +42284 -41141
- package/dist/builtin/workflows/src/shared/authoring-contract-stage.d.ts +16 -11
- package/dist/builtin/workflows/src/shared/authoring-contract-ui.d.ts +16 -1
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +8 -1
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/credential-print.d.ts +1 -1
- package/dist/cli/credential-print.d.ts.map +1 -1
- package/dist/cli/credential-print.js +2 -56
- package/dist/cli/credential-print.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +4 -2
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session-accessors.d.ts.map +1 -1
- package/dist/core/agent-session-accessors.js +6 -1
- package/dist/core/agent-session-accessors.js.map +1 -1
- package/dist/core/agent-session-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-compaction.js +20 -3
- package/dist/core/agent-session-compaction.js.map +1 -1
- package/dist/core/agent-session-events.d.ts.map +1 -1
- package/dist/core/agent-session-events.js +2 -1
- package/dist/core/agent-session-events.js.map +1 -1
- package/dist/core/agent-session-extension-bindings.d.ts +6 -0
- package/dist/core/agent-session-extension-bindings.d.ts.map +1 -1
- package/dist/core/agent-session-extension-bindings.js +90 -9
- package/dist/core/agent-session-extension-bindings.js.map +1 -1
- package/dist/core/agent-session-methods.d.ts +3 -1
- package/dist/core/agent-session-methods.d.ts.map +1 -1
- package/dist/core/agent-session-methods.js.map +1 -1
- package/dist/core/agent-session-prompt.d.ts.map +1 -1
- package/dist/core/agent-session-prompt.js +14 -1
- package/dist/core/agent-session-prompt.js.map +1 -1
- package/dist/core/agent-session-tool-registry.d.ts.map +1 -1
- package/dist/core/agent-session-tool-registry.js +1 -0
- package/dist/core/agent-session-tool-registry.js.map +1 -1
- package/dist/core/agent-session-types.d.ts +2 -0
- package/dist/core/agent-session-types.d.ts.map +1 -1
- package/dist/core/agent-session-types.js.map +1 -1
- package/dist/core/agent-session.d.ts +1 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +6 -1
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +22 -4
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/child-session-options.d.ts.map +1 -1
- package/dist/core/child-session-options.js +17 -2
- package/dist/core/child-session-options.js.map +1 -1
- package/dist/core/compaction/classifier-compaction.d.ts +13 -0
- package/dist/core/compaction/classifier-compaction.d.ts.map +1 -0
- package/dist/core/compaction/classifier-compaction.js +110 -0
- package/dist/core/compaction/classifier-compaction.js.map +1 -0
- package/dist/core/compaction/compaction-boundary.d.ts.map +1 -1
- package/dist/core/compaction/compaction-boundary.js +2 -6
- package/dist/core/compaction/compaction-boundary.js.map +1 -1
- package/dist/core/compaction/compaction-runner.d.ts +16 -1
- package/dist/core/compaction/compaction-runner.d.ts.map +1 -1
- package/dist/core/compaction/compaction-runner.js +97 -12
- package/dist/core/compaction/compaction-runner.js.map +1 -1
- package/dist/core/compaction/compaction-types.d.ts +18 -1
- package/dist/core/compaction/compaction-types.d.ts.map +1 -1
- package/dist/core/compaction/compaction-types.js +1 -1
- package/dist/core/compaction/compaction-types.js.map +1 -1
- package/dist/core/compaction/fallback-planner.d.ts +1 -0
- package/dist/core/compaction/fallback-planner.d.ts.map +1 -1
- package/dist/core/compaction/fallback-planner.js +2 -0
- package/dist/core/compaction/fallback-planner.js.map +1 -1
- package/dist/core/compaction/index.d.ts +3 -0
- package/dist/core/compaction/index.d.ts.map +1 -1
- package/dist/core/compaction/index.js +3 -0
- package/dist/core/compaction/index.js.map +1 -1
- package/dist/core/compaction/model-resolver.d.ts +16 -0
- package/dist/core/compaction/model-resolver.d.ts.map +1 -0
- package/dist/core/compaction/model-resolver.js +18 -0
- package/dist/core/compaction/model-resolver.js.map +1 -0
- package/dist/core/compaction/morph-compaction.d.ts +14 -0
- package/dist/core/compaction/morph-compaction.d.ts.map +1 -0
- package/dist/core/compaction/morph-compaction.js +99 -0
- package/dist/core/compaction/morph-compaction.js.map +1 -0
- package/dist/core/compaction/pi-summary-fallback.d.ts +34 -0
- package/dist/core/compaction/pi-summary-fallback.d.ts.map +1 -0
- package/dist/core/compaction/pi-summary-fallback.js +357 -0
- package/dist/core/compaction/pi-summary-fallback.js.map +1 -0
- package/dist/core/compaction/planner-outcome.d.ts +10 -6
- package/dist/core/compaction/planner-outcome.d.ts.map +1 -1
- package/dist/core/compaction/planner-outcome.js +11 -4
- package/dist/core/compaction/planner-outcome.js.map +1 -1
- package/dist/core/compaction/range-planner-diagnostics.d.ts +3 -1
- package/dist/core/compaction/range-planner-diagnostics.d.ts.map +1 -1
- package/dist/core/compaction/range-planner-diagnostics.js +11 -0
- package/dist/core/compaction/range-planner-diagnostics.js.map +1 -1
- package/dist/core/compaction/range-planner.d.ts +3 -3
- package/dist/core/compaction/range-planner.d.ts.map +1 -1
- package/dist/core/compaction/range-planner.js +59 -28
- package/dist/core/compaction/range-planner.js.map +1 -1
- package/dist/core/compaction/region-trimming.d.ts.map +1 -1
- package/dist/core/compaction/region-trimming.js +7 -0
- package/dist/core/compaction/region-trimming.js.map +1 -1
- package/dist/core/compaction/structured-compaction-input.d.ts +19 -0
- package/dist/core/compaction/structured-compaction-input.d.ts.map +1 -0
- package/dist/core/compaction/structured-compaction-input.js +70 -0
- package/dist/core/compaction/structured-compaction-input.js.map +1 -0
- package/dist/core/compaction/transcript-serialization.d.ts +1 -0
- package/dist/core/compaction/transcript-serialization.d.ts.map +1 -1
- package/dist/core/compaction/transcript-serialization.js +26 -0
- package/dist/core/compaction/transcript-serialization.js.map +1 -1
- package/dist/core/extensions/api-types.d.ts +2 -2
- package/dist/core/extensions/api-types.d.ts.map +1 -1
- package/dist/core/extensions/api-types.js.map +1 -1
- package/dist/core/extensions/context-types.d.ts +1 -0
- package/dist/core/extensions/context-types.d.ts.map +1 -1
- package/dist/core/extensions/context-types.js.map +1 -1
- package/dist/core/extensions/loader-api.js.map +1 -1
- package/dist/core/extensions/loader-virtual-modules.d.ts +3 -4
- package/dist/core/extensions/loader-virtual-modules.d.ts.map +1 -1
- package/dist/core/extensions/loader-virtual-modules.js +14 -9
- package/dist/core/extensions/loader-virtual-modules.js.map +1 -1
- package/dist/core/extensions/runner-context.d.ts +10 -4
- package/dist/core/extensions/runner-context.d.ts.map +1 -1
- package/dist/core/extensions/runner-context.js +47 -3
- package/dist/core/extensions/runner-context.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +10 -3
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +31 -7
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/tool-events.d.ts +1 -1
- package/dist/core/extensions/tool-events.js.map +1 -1
- package/dist/core/extensions/ts-source-specifiers.d.ts +18 -0
- package/dist/core/extensions/ts-source-specifiers.d.ts.map +1 -0
- package/dist/core/extensions/ts-source-specifiers.js +142 -0
- package/dist/core/extensions/ts-source-specifiers.js.map +1 -0
- package/dist/core/extensions/workflow-events.d.ts +49 -0
- package/dist/core/extensions/workflow-events.d.ts.map +1 -1
- package/dist/core/extensions/workflow-events.js.map +1 -1
- package/dist/core/extensions/workflow-run-control-hub.d.ts +16 -4
- package/dist/core/extensions/workflow-run-control-hub.d.ts.map +1 -1
- package/dist/core/extensions/workflow-run-control-hub.js +72 -1
- package/dist/core/extensions/workflow-run-control-hub.js.map +1 -1
- package/dist/core/extensions/workflow-run-control.d.ts +11 -3
- package/dist/core/extensions/workflow-run-control.d.ts.map +1 -1
- package/dist/core/extensions/workflow-run-control.js.map +1 -1
- package/dist/core/messages.d.ts +1 -0
- package/dist/core/messages.d.ts.map +1 -1
- package/dist/core/messages.js +5 -1
- package/dist/core/messages.js.map +1 -1
- package/dist/core/remote-catalog-provider.d.ts.map +1 -1
- package/dist/core/remote-catalog-provider.js +3 -0
- package/dist/core/remote-catalog-provider.js.map +1 -1
- package/dist/core/sdk-exports.d.ts +2 -2
- package/dist/core/sdk-exports.js.map +1 -1
- package/dist/core/sdk-types.d.ts +3 -0
- package/dist/core/sdk-types.d.ts.map +1 -1
- package/dist/core/sdk-types.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +17 -8
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager-history.d.ts.map +1 -1
- package/dist/core/session-manager-history.js +4 -2
- package/dist/core/session-manager-history.js.map +1 -1
- package/dist/core/settings-manager-basic-accessors.d.ts +2 -0
- package/dist/core/settings-manager-basic-accessors.d.ts.map +1 -1
- package/dist/core/settings-manager-basic-accessors.js +29 -0
- package/dist/core/settings-manager-basic-accessors.js.map +1 -1
- package/dist/core/settings-manager-core.js.map +1 -1
- package/dist/core/settings-merge.d.ts +3 -0
- package/dist/core/settings-merge.d.ts.map +1 -1
- package/dist/core/settings-merge.js +18 -4
- package/dist/core/settings-merge.js.map +1 -1
- package/dist/core/settings-storage.js.map +1 -1
- package/dist/core/settings-types.d.ts +1 -0
- package/dist/core/settings-types.d.ts.map +1 -1
- package/dist/core/settings-types.js.map +1 -1
- package/dist/core/transcript.d.ts +58 -0
- package/dist/core/transcript.d.ts.map +1 -0
- package/dist/core/transcript.js +185 -0
- package/dist/core/transcript.js.map +1 -0
- package/dist/extensions/codemode/execute.d.ts.map +1 -1
- package/dist/extensions/codemode/execute.js +14 -1
- package/dist/extensions/codemode/execute.js.map +1 -1
- package/dist/extensions/codemode/tool.js +2 -2
- package/dist/extensions/codemode/tool.js.map +1 -1
- package/dist/extensions/herdr/index.d.ts.map +1 -1
- package/dist/extensions/herdr/index.js +15 -10
- package/dist/extensions/herdr/index.js.map +1 -1
- package/dist/extensions/herdr/pane-owner.d.ts +6 -2
- package/dist/extensions/herdr/pane-owner.d.ts.map +1 -1
- package/dist/extensions/herdr/pane-owner.js +19 -4
- package/dist/extensions/herdr/pane-owner.js.map +1 -1
- package/dist/extensions/llama/provider.d.ts.map +1 -1
- package/dist/extensions/llama/provider.js +19 -8
- package/dist/extensions/llama/provider.js.map +1 -1
- package/dist/extensions/mcp/cli.js +18 -24
- package/dist/extensions/mcp/cli.js.map +1 -1
- package/dist/extensions/mcp/client/auth-provider.d.ts +4 -0
- package/dist/extensions/mcp/client/auth-provider.d.ts.map +1 -1
- package/dist/extensions/mcp/client/auth-provider.js.map +1 -1
- package/dist/extensions/mcp/client/oauth/discovery.d.ts +3 -0
- package/dist/extensions/mcp/client/oauth/discovery.d.ts.map +1 -1
- package/dist/extensions/mcp/client/oauth/discovery.js +8 -5
- package/dist/extensions/mcp/client/oauth/discovery.js.map +1 -1
- package/dist/extensions/mcp/client/oauth/flow.d.ts +4 -0
- package/dist/extensions/mcp/client/oauth/flow.d.ts.map +1 -1
- package/dist/extensions/mcp/client/oauth/flow.js +12 -2
- package/dist/extensions/mcp/client/oauth/flow.js.map +1 -1
- package/dist/extensions/mcp/client/transports/streamable-http.d.ts +3 -0
- package/dist/extensions/mcp/client/transports/streamable-http.d.ts.map +1 -1
- package/dist/extensions/mcp/client/transports/streamable-http.js +17 -14
- package/dist/extensions/mcp/client/transports/streamable-http.js.map +1 -1
- package/dist/extensions/mcp/index.d.ts.map +1 -1
- package/dist/extensions/mcp/index.js +150 -72
- package/dist/extensions/mcp/index.js.map +1 -1
- package/dist/extensions/mcp/oauth.d.ts +5 -3
- package/dist/extensions/mcp/oauth.d.ts.map +1 -1
- package/dist/extensions/mcp/oauth.js +36 -10
- package/dist/extensions/mcp/oauth.js.map +1 -1
- package/dist/extensions/mcp/ui.d.ts +3 -3
- package/dist/extensions/mcp/ui.d.ts.map +1 -1
- package/dist/extensions/mcp/ui.js +10 -2
- package/dist/extensions/mcp/ui.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/main-deferred-startup.d.ts +1 -0
- package/dist/main-deferred-startup.d.ts.map +1 -1
- package/dist/main-deferred-startup.js +9 -21
- package/dist/main-deferred-startup.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +8 -6
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/compaction-boundary-message.d.ts +2 -0
- package/dist/modes/interactive/components/compaction-boundary-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/compaction-boundary-message.js +15 -3
- package/dist/modes/interactive/components/compaction-boundary-message.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector-items.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector-items.js +43 -0
- package/dist/modes/interactive/components/settings-selector-items.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector-types.d.ts +6 -0
- package/dist/modes/interactive/components/settings-selector-types.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector-types.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-input-handling.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-input-handling.js +1 -47
- package/dist/modes/interactive/interactive-input-handling.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-base.d.ts +0 -1
- package/dist/modes/interactive/interactive-mode-base.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-base.js +0 -1
- package/dist/modes/interactive/interactive-mode-base.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.d.ts +0 -1
- package/dist/modes/interactive/interactive-mode-surface.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.js.map +1 -1
- package/dist/modes/interactive/interactive-prompt-restore.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-prompt-restore.js +0 -5
- package/dist/modes/interactive/interactive-prompt-restore.js.map +1 -1
- package/dist/modes/interactive/interactive-prompt-turn.js +6 -4
- package/dist/modes/interactive/interactive-prompt-turn.js.map +1 -1
- package/dist/modes/interactive/interactive-render-chat.js +13 -6
- package/dist/modes/interactive/interactive-render-chat.js.map +1 -1
- package/dist/modes/interactive/interactive-selectors.js +8 -0
- package/dist/modes/interactive/interactive-selectors.js.map +1 -1
- package/dist/modes/interactive/interactive-session-runtime.js +2 -0
- package/dist/modes/interactive/interactive-session-runtime.js.map +1 -1
- package/dist/modes/interactive/interactive-startup.js +1 -1
- package/dist/modes/interactive/interactive-startup.js.map +1 -1
- package/dist/modes/rpc/rpc-client-process.d.ts +7 -0
- package/dist/modes/rpc/rpc-client-process.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client-process.js +20 -3
- package/dist/modes/rpc/rpc-client-process.js.map +1 -1
- package/dist/modes/rpc/rpc-command-timeouts.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-command-timeouts.js +1 -0
- package/dist/modes/rpc/rpc-command-timeouts.js.map +1 -1
- package/dist/utils/ansi.d.ts +8 -0
- package/dist/utils/ansi.d.ts.map +1 -1
- package/dist/utils/ansi.js +29 -10
- package/dist/utils/ansi.js.map +1 -1
- package/dist/utils/clipboard.d.ts.map +1 -1
- package/dist/utils/clipboard.js +13 -3
- package/dist/utils/clipboard.js.map +1 -1
- package/dist/utils/credential-redaction.d.ts +2 -0
- package/dist/utils/credential-redaction.d.ts.map +1 -0
- package/dist/utils/credential-redaction.js +20 -0
- package/dist/utils/credential-redaction.js.map +1 -0
- package/dist/utils/image-resize-core.d.ts +8 -0
- package/dist/utils/image-resize-core.d.ts.map +1 -1
- package/dist/utils/image-resize-core.js +1 -0
- package/dist/utils/image-resize-core.js.map +1 -1
- package/dist/utils/image-resize-worker.js +3 -2
- package/dist/utils/image-resize-worker.js.map +1 -1
- package/dist/utils/image-resize.d.ts +2 -2
- package/dist/utils/image-resize.d.ts.map +1 -1
- package/dist/utils/image-resize.js +7 -10
- package/dist/utils/image-resize.js.map +1 -1
- package/docs/codemode.md +21 -1
- package/docs/compaction/reference.md +10 -4
- package/docs/compaction.md +40 -8
- package/docs/docs.json +1 -0
- package/docs/extensions/api-reference.md +4 -0
- package/docs/extensions/events.md +2 -2
- package/docs/llama-cpp.md +9 -2
- package/docs/mcp-servers.md +3 -3
- package/docs/models.md +10 -0
- package/docs/providers.md +39 -5
- package/docs/reference/cli.md +2 -2
- package/docs/sdk/reference.md +19 -4
- package/docs/sdk/web-server.md +325 -0
- package/docs/sdk.md +83 -1
- package/docs/security.md +1 -1
- package/docs/settings.md +2 -2
- package/docs/tools.md +1 -1
- package/docs/workflows/api-reference.md +71 -3
- package/docs/workflows/builtins.md +4 -0
- package/docs/workflows/operations.md +13 -5
- package/npm-shrinkwrap.json +32 -32
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"child-session-options.d.ts","sourceRoot":"","sources":["../../src/core/child-session-options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"child-session-options.d.ts","sourceRoot":"","sources":["../../src/core/child-session-options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAKhE,2FAA2F;AAC3F,MAAM,MAAM,2BAA2B,GAAG,CAAC,OAAO,EAAE,yBAAyB,KAAK,yBAAyB,CAAC;AAO5G,wBAAgB,0BAA0B,CACzC,MAAM,EAAE,yBAAyB,EACjC,KAAK,EAAE,yBAAyB,EAChC,kBAAkB,GAAE,SAAS,MAAM,EAAO,GACxC,yBAAyB,CAoD3B"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
|
+
import { applyToolModifiers, getToolListError, isToolModifier } from "./settings-merge.js";
|
|
2
3
|
import { createToolNameMatcher, isMcpToolName } from "./tool-selection.js";
|
|
4
|
+
import { allToolNames, getDefaultToolNames } from "./tools/index.js";
|
|
3
5
|
/** Optional undefined values retain inheritance; null and other explicit values do not. */
|
|
4
6
|
function definedOptions(options) {
|
|
5
7
|
return Object.fromEntries(Object.entries(options ?? {}).filter(([, value]) => value !== undefined));
|
|
@@ -13,10 +15,23 @@ export function inheritChildSessionOptions(parent, child, availableToolNames = [
|
|
|
13
15
|
const ceiling = parent.noTools === "all" ? [] : parent.tools;
|
|
14
16
|
const parentAllows = createToolNameMatcher(ceiling ?? []);
|
|
15
17
|
const parentNamesMcp = createToolNameMatcher((ceiling ?? []).filter((entry) => entry.startsWith("mcp__")));
|
|
16
|
-
const
|
|
18
|
+
const toolListError = child.tools ? getToolListError(child.tools) : undefined;
|
|
19
|
+
if (toolListError)
|
|
20
|
+
throw new Error(`Invalid tools option: ${toolListError}`);
|
|
21
|
+
const codingToolNames = new Set(allToolNames);
|
|
22
|
+
const inheritedCustomToolNames = [...new Set([...availableToolNames, ...(ceiling ?? [])])].filter((name) => !name.includes("*") && !codingToolNames.has(name) && parentAllows(name));
|
|
23
|
+
const childToolNames = child.tools?.some(isToolModifier)
|
|
24
|
+
? applyToolModifiers([
|
|
25
|
+
...((child.noTools ?? parent.noTools)
|
|
26
|
+
? []
|
|
27
|
+
: ((child.settingsManager ?? parent.settingsManager)?.getDefaultTools() ?? getDefaultToolNames())),
|
|
28
|
+
...inheritedCustomToolNames,
|
|
29
|
+
], child.tools)
|
|
30
|
+
: child.tools;
|
|
31
|
+
const childAllows = childToolNames === undefined ? undefined : createToolNameMatcher(childToolNames);
|
|
17
32
|
const tools = ceiling === undefined
|
|
18
33
|
? child.tools
|
|
19
|
-
: [...new Set([...availableToolNames, ...ceiling, ...(
|
|
34
|
+
: [...new Set([...availableToolNames, ...ceiling, ...(childToolNames ?? [])])].filter((name) => !name.includes("*") &&
|
|
20
35
|
parentAllows(name) &&
|
|
21
36
|
(!childAllows || childAllows(name)) &&
|
|
22
37
|
(!isMcpToolName(name) || ceiling.includes(name) || parentNamesMcp(name)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"child-session-options.js","sourceRoot":"","sources":["../../src/core/child-session-options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"child-session-options.js","sourceRoot":"","sources":["../../src/core/child-session-options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC3F,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAC3E,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAKrE,2FAA2F;AAC3F,SAAS,cAAc,CAAmB,OAAsB;IAC/D,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,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AC,aAAa,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,cAAc,CAAC,IAAI,CAAC,CAAC,CACzE,CAAC;IACL,MAAM,UAAU,GAAG,MAAM,CAAC,sBAAsB,CAAC;IACjD,MAAM,SAAS,GAAG,KAAK,CAAC,sBAAsB,CAAC;IAC/C,OAAO;QACN,GAAG,MAAM;QACT,GAAG,cAAc,CAAC,KAAK,CAAC;QACxB,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,GAAI,EAAE,KAAK,CAAC,GAAG,IAAI,KAAK,CAAC,cAAc,EAAE,MAAM,EAAE,IAAI,GAAG,CAAC;QAC7E,QAAQ;QACR,KAAK;QACL,aAAa,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,IAAI,EAAE,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,aAAa,IAAI,EAAE,CAAC,CAAC;QAChF,OAAO,EAAE,MAAM,CAAC,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC;QAC7E,iBAAiB,EAAE,EAAE,GAAG,MAAM,CAAC,iBAAiB,EAAE,GAAG,cAAc,CAAC,KAAK,CAAC,iBAAiB,CAAC,EAAE;QAC9F,sBAAsB,EACrB,UAAU,IAAI,SAAS;YACtB,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,IAAI,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC;YAC1E,CAAC,CAAC,CAAC,UAAU,IAAI,SAAS,CAAC;KAC7B,CAAC;AACH,CAAC","sourcesContent":["import { resolve } from \"node:path\";\nimport type { CreateAgentSessionOptions } from \"./sdk-types.ts\";\nimport { applyToolModifiers, getToolListError, isToolModifier } from \"./settings-merge.js\";\nimport { createToolNameMatcher, isMcpToolName } from \"./tool-selection.ts\";\nimport { allToolNames, getDefaultToolNames } from \"./tools/index.js\";\n\n/** Internal owner-bound adapter seam. Does not admit work or transfer parent authority. */\nexport type ChildSessionOptionsResolver = (options: CreateAgentSessionOptions) => CreateAgentSessionOptions;\n\n/** Optional undefined values retain inheritance; null and other explicit values do not. */\nfunction definedOptions<T extends object>(options: T | undefined): Partial<T> {\n\treturn Object.fromEntries(Object.entries(options ?? {}).filter(([, value]) => value !== undefined)) as Partial<T>;\n}\n\nexport function inheritChildSessionOptions(\n\tparent: CreateAgentSessionOptions,\n\tchild: CreateAgentSessionOptions,\n\tavailableToolNames: readonly string[] = [],\n): CreateAgentSessionOptions {\n\tconst builtins = { ...parent.builtins, ...definedOptions(child.builtins) };\n\tfor (const name of [\"workflows\", \"subagents\", \"mcp\", \"web-access\", \"intercom\"] as const) {\n\t\tif (parent.builtins?.[name] === false) builtins[name] = false;\n\t}\n\tconst ceiling = parent.noTools === \"all\" ? [] : parent.tools;\n\tconst parentAllows = createToolNameMatcher(ceiling ?? []);\n\tconst parentNamesMcp = createToolNameMatcher((ceiling ?? []).filter((entry) => entry.startsWith(\"mcp__\")));\n\tconst toolListError = child.tools ? getToolListError(child.tools) : undefined;\n\tif (toolListError) throw new Error(`Invalid tools option: ${toolListError}`);\n\tconst codingToolNames = new Set<string>(allToolNames);\n\tconst inheritedCustomToolNames = [...new Set([...availableToolNames, ...(ceiling ?? [])])].filter(\n\t\t(name) => !name.includes(\"*\") && !codingToolNames.has(name) && parentAllows(name),\n\t);\n\tconst childToolNames = child.tools?.some(isToolModifier)\n\t\t? applyToolModifiers(\n\t\t\t\t[\n\t\t\t\t\t...((child.noTools ?? parent.noTools)\n\t\t\t\t\t\t? []\n\t\t\t\t\t\t: ((child.settingsManager ?? parent.settingsManager)?.getDefaultTools() ?? getDefaultToolNames())),\n\t\t\t\t\t...inheritedCustomToolNames,\n\t\t\t\t],\n\t\t\t\tchild.tools,\n\t\t\t)\n\t\t: child.tools;\n\tconst childAllows = childToolNames === undefined ? undefined : createToolNameMatcher(childToolNames);\n\tconst tools =\n\t\tceiling === undefined\n\t\t\t? child.tools\n\t\t\t: [...new Set([...availableToolNames, ...ceiling, ...(childToolNames ?? [])])].filter(\n\t\t\t\t\t(name) =>\n\t\t\t\t\t\t!name.includes(\"*\") &&\n\t\t\t\t\t\tparentAllows(name) &&\n\t\t\t\t\t\t(!childAllows || childAllows(name)) &&\n\t\t\t\t\t\t(!isMcpToolName(name) || ceiling.includes(name) || parentNamesMcp(name)),\n\t\t\t\t);\n\tconst parentGate = parent.isFallbackModelAllowed;\n\tconst childGate = child.isFallbackModelAllowed;\n\treturn {\n\t\t...parent,\n\t\t...definedOptions(child),\n\t\tcwd: resolve(parent.cwd!, child.cwd ?? child.sessionManager?.getCwd() ?? \".\"),\n\t\tbuiltins,\n\t\ttools,\n\t\texcludedTools: [...(parent.excludedTools ?? []), ...(child.excludedTools ?? [])],\n\t\tnoTools: parent.noTools === \"all\" ? \"all\" : (child.noTools ?? parent.noTools),\n\t\textensionBindings: { ...parent.extensionBindings, ...definedOptions(child.extensionBindings) },\n\t\tisFallbackModelAllowed:\n\t\t\tparentGate && childGate\n\t\t\t\t? (model, effort) => parentGate(model, effort) && childGate(model, effort)\n\t\t\t\t: (parentGate ?? childGate),\n\t};\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ClassifierContext, ClassifierResult, Usage } from "@bastani/pi-ai";
|
|
2
|
+
import type { LineRange, NumberedRegion, VerbatimCompactionParameters } from "./compaction-types.js";
|
|
3
|
+
import { type CompactionInputMessage, type StructuredCompactionInput } from "./structured-compaction-input.js";
|
|
4
|
+
export interface ClassifierUnit extends LineRange {
|
|
5
|
+
message: CompactionInputMessage;
|
|
6
|
+
position: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function buildClassifierUnits(input: StructuredCompactionInput): ClassifierUnit[];
|
|
9
|
+
export declare function planClassifierRanges(region: NumberedRegion, parameters: VerbatimCompactionParameters, classify: (context: ClassifierContext) => Promise<ClassifierResult>, options?: {
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
onUsage?: (usage: Usage) => void;
|
|
12
|
+
}): Promise<LineRange[]>;
|
|
13
|
+
//# sourceMappingURL=classifier-compaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classifier-compaction.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/classifier-compaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACjF,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,4BAA4B,EAAE,MAAM,uBAAuB,CAAC;AAErG,OAAO,EAEN,KAAK,sBAAsB,EAC3B,KAAK,yBAAyB,EAC9B,MAAM,kCAAkC,CAAC;AAM1C,MAAM,WAAW,cAAe,SAAQ,SAAS;IAChD,OAAO,EAAE,sBAAsB,CAAC;IAChC,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,GAAG,cAAc,EAAE,CAuCvF;AAED,wBAAsB,oBAAoB,CACzC,MAAM,EAAE,cAAc,EACtB,UAAU,EAAE,4BAA4B,EACxC,QAAQ,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,OAAO,CAAC,gBAAgB,CAAC,EACnE,OAAO,GAAE;IAAE,MAAM,CAAC,EAAE,WAAW,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;CAAO,GACtE,OAAO,CAAC,SAAS,EAAE,CAAC,CA6DtB"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { validateDeletedRanges } from "./deleted-ranges.js";
|
|
2
|
+
import { buildStructuredCompactionInput, } from "./structured-compaction-input.js";
|
|
3
|
+
const MAX_UNIT_LINES = 32;
|
|
4
|
+
const MAX_UNIT_TOKENS = 1024;
|
|
5
|
+
const CLASSIFIER_CONCURRENCY = 4;
|
|
6
|
+
export function buildClassifierUnits(input) {
|
|
7
|
+
const units = [];
|
|
8
|
+
let offset = 0;
|
|
9
|
+
for (const message of input.messages) {
|
|
10
|
+
let start = 0;
|
|
11
|
+
let lines = [];
|
|
12
|
+
let tokens = 0;
|
|
13
|
+
const flush = () => {
|
|
14
|
+
if (!lines.length)
|
|
15
|
+
return;
|
|
16
|
+
units.push({
|
|
17
|
+
start: offset + start + 1,
|
|
18
|
+
end: offset + start + lines.length,
|
|
19
|
+
message: { ...message, lines },
|
|
20
|
+
position: `lines ${start + 1}-${start + lines.length} of ${message.lines.length}`,
|
|
21
|
+
});
|
|
22
|
+
lines = [];
|
|
23
|
+
tokens = 0;
|
|
24
|
+
};
|
|
25
|
+
for (let index = 0; index < message.lines.length; index++) {
|
|
26
|
+
const line = message.lines[index];
|
|
27
|
+
const lineTokens = Math.ceil((JSON.stringify(line).length + 1) / 4);
|
|
28
|
+
if (input.protected.some((range) => range.id === message.id && range.start <= index + 1 && range.end >= index + 1)) {
|
|
29
|
+
flush();
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (lineTokens > MAX_UNIT_TOKENS)
|
|
33
|
+
throw new Error("Classifier compaction line exceeds the unit token limit");
|
|
34
|
+
if (lines.length >= MAX_UNIT_LINES || tokens + lineTokens > MAX_UNIT_TOKENS)
|
|
35
|
+
flush();
|
|
36
|
+
if (!lines.length)
|
|
37
|
+
start = index;
|
|
38
|
+
lines.push(line);
|
|
39
|
+
tokens += lineTokens;
|
|
40
|
+
}
|
|
41
|
+
flush();
|
|
42
|
+
offset += message.lines.length;
|
|
43
|
+
}
|
|
44
|
+
return units;
|
|
45
|
+
}
|
|
46
|
+
export async function planClassifierRanges(region, parameters, classify, options = {}) {
|
|
47
|
+
const input = buildStructuredCompactionInput(region, parameters);
|
|
48
|
+
const units = buildClassifierUnits(input);
|
|
49
|
+
const ranked = [];
|
|
50
|
+
let cursor = 0;
|
|
51
|
+
let failure;
|
|
52
|
+
await Promise.all(Array.from({ length: Math.min(CLASSIFIER_CONCURRENCY, units.length) }, async () => {
|
|
53
|
+
try {
|
|
54
|
+
while (cursor < units.length && !failure) {
|
|
55
|
+
if (options.signal?.aborted)
|
|
56
|
+
throw new Error("Compaction cancelled");
|
|
57
|
+
const unit = units[cursor++];
|
|
58
|
+
const result = await classify({
|
|
59
|
+
state: {
|
|
60
|
+
query: input.query,
|
|
61
|
+
message: { id: unit.message.id, role: unit.message.role, lines: unit.message.lines },
|
|
62
|
+
position: unit.position,
|
|
63
|
+
},
|
|
64
|
+
questions: {
|
|
65
|
+
score: {
|
|
66
|
+
type: "score",
|
|
67
|
+
instructions: "How much does the assistant still need these lines to continue the task? Retain current requirements, decisions, unresolved errors, and unique implementation facts; completed repetitive output is less useful.",
|
|
68
|
+
criteria: ["not needed", "slightly useful", "useful", "important", "essential"],
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
if (result.usage)
|
|
73
|
+
options.onUsage?.(result.usage);
|
|
74
|
+
const answer = result.answers.score;
|
|
75
|
+
if (result.stopReason !== "stop")
|
|
76
|
+
throw new Error(result.errorMessage || "Classifier compaction failed");
|
|
77
|
+
if (answer?.type !== "score" ||
|
|
78
|
+
!Number.isFinite(answer.score) ||
|
|
79
|
+
answer.score < 0 ||
|
|
80
|
+
answer.score > 4 ||
|
|
81
|
+
!Number.isFinite(answer.confidence) ||
|
|
82
|
+
answer.confidence < 0 ||
|
|
83
|
+
answer.confidence > 1)
|
|
84
|
+
throw new Error("Invalid classifier compaction score");
|
|
85
|
+
ranked.push({ unit, score: answer.score, confidence: answer.confidence });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
failure ??= error instanceof Error ? error : new Error(String(error));
|
|
90
|
+
}
|
|
91
|
+
}));
|
|
92
|
+
if (failure)
|
|
93
|
+
throw failure;
|
|
94
|
+
if (options.signal?.aborted)
|
|
95
|
+
throw new Error("Compaction cancelled");
|
|
96
|
+
ranked.sort((a, b) => a.score - b.score || b.confidence - a.confidence || a.unit.start - b.unit.start);
|
|
97
|
+
let kept = region.lines.length;
|
|
98
|
+
const target = Math.round(kept * parameters.compression_ratio);
|
|
99
|
+
const ranges = [];
|
|
100
|
+
for (const { unit } of ranked) {
|
|
101
|
+
if (kept <= target)
|
|
102
|
+
break;
|
|
103
|
+
ranges.push({ start: unit.start, end: unit.end });
|
|
104
|
+
kept -= unit.end - unit.start + 1;
|
|
105
|
+
}
|
|
106
|
+
if (!validateDeletedRanges(ranges, region).length)
|
|
107
|
+
throw new Error("Classifier compaction produced no usable ranges");
|
|
108
|
+
return ranges;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=classifier-compaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classifier-compaction.js","sourceRoot":"","sources":["../../../src/core/compaction/classifier-compaction.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EACN,8BAA8B,GAG9B,MAAM,kCAAkC,CAAC;AAE1C,MAAM,cAAc,GAAG,EAAE,CAAC;AAC1B,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,MAAM,sBAAsB,GAAG,CAAC,CAAC;AAOjC,MAAM,UAAU,oBAAoB,CAAC,KAAgC;IACpE,MAAM,KAAK,GAAqB,EAAE,CAAC;IACnC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,OAAO,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;QACtC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,IAAI,KAAK,GAAa,EAAE,CAAC;QACzB,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,MAAM,KAAK,GAAG,GAAG,EAAE;YAClB,IAAI,CAAC,KAAK,CAAC,MAAM;gBAAE,OAAO;YAC1B,KAAK,CAAC,IAAI,CAAC;gBACV,KAAK,EAA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C,CAAC,CACF,CAAC;IACF,IAAI,OAAO;QAAE,MAAM,OAAO,CAAC;IAC3B,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;IACrE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACvG,IAAI,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC;IAC/B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,UAAU,CAAC,iBAAiB,CAAC,CAAC;IAC/D,MAAM,MAAM,GAAgB,EAAE,CAAC;IAC/B,KAAK,MAAM,EAAE,IAAI,EAAE,IAAI,MAAM,EAAE,CAAC;QAC/B,IAAI,IAAI,IAAI,MAAM;YAAE,MAAM;QAC1B,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;QAClD,IAAI,IAAI,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACnC,CAAC;IACD,IAAI,CAAC,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,MAAM;QAChD,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACpE,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import type { ClassifierContext, ClassifierResult, Usage } from \"@bastani/pi-ai\";\nimport type { LineRange, NumberedRegion, VerbatimCompactionParameters } from \"./compaction-types.js\";\nimport { validateDeletedRanges } from \"./deleted-ranges.js\";\nimport {\n\tbuildStructuredCompactionInput,\n\ttype CompactionInputMessage,\n\ttype StructuredCompactionInput,\n} from \"./structured-compaction-input.js\";\n\nconst MAX_UNIT_LINES = 32;\nconst MAX_UNIT_TOKENS = 1024;\nconst CLASSIFIER_CONCURRENCY = 4;\n\nexport interface ClassifierUnit extends LineRange {\n\tmessage: CompactionInputMessage;\n\tposition: string;\n}\n\nexport function buildClassifierUnits(input: StructuredCompactionInput): ClassifierUnit[] {\n\tconst units: ClassifierUnit[] = [];\n\tlet offset = 0;\n\tfor (const message of input.messages) {\n\t\tlet start = 0;\n\t\tlet lines: string[] = [];\n\t\tlet tokens = 0;\n\t\tconst flush = () => {\n\t\t\tif (!lines.length) return;\n\t\t\tunits.push({\n\t\t\t\tstart: offset + start + 1,\n\t\t\t\tend: offset + start + lines.length,\n\t\t\t\tmessage: { ...message, lines },\n\t\t\t\tposition: `lines ${start + 1}-${start + lines.length} of ${message.lines.length}`,\n\t\t\t});\n\t\t\tlines = [];\n\t\t\ttokens = 0;\n\t\t};\n\t\tfor (let index = 0; index < message.lines.length; index++) {\n\t\t\tconst line = message.lines[index];\n\t\t\tconst lineTokens = Math.ceil((JSON.stringify(line).length + 1) / 4);\n\t\t\tif (\n\t\t\t\tinput.protected.some(\n\t\t\t\t\t(range) => range.id === message.id && range.start <= index + 1 && range.end >= index + 1,\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tflush();\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (lineTokens > MAX_UNIT_TOKENS) throw new Error(\"Classifier compaction line exceeds the unit token limit\");\n\t\t\tif (lines.length >= MAX_UNIT_LINES || tokens + lineTokens > MAX_UNIT_TOKENS) flush();\n\t\t\tif (!lines.length) start = index;\n\t\t\tlines.push(line);\n\t\t\ttokens += lineTokens;\n\t\t}\n\t\tflush();\n\t\toffset += message.lines.length;\n\t}\n\treturn units;\n}\n\nexport async function planClassifierRanges(\n\tregion: NumberedRegion,\n\tparameters: VerbatimCompactionParameters,\n\tclassify: (context: ClassifierContext) => Promise<ClassifierResult>,\n\toptions: { signal?: AbortSignal; onUsage?: (usage: Usage) => void } = {},\n): Promise<LineRange[]> {\n\tconst input = buildStructuredCompactionInput(region, parameters);\n\tconst units = buildClassifierUnits(input);\n\tconst ranked: { unit: ClassifierUnit; score: number; confidence: number }[] = [];\n\tlet cursor = 0;\n\tlet failure: Error | undefined;\n\tawait Promise.all(\n\t\tArray.from({ length: Math.min(CLASSIFIER_CONCURRENCY, units.length) }, async () => {\n\t\t\ttry {\n\t\t\t\twhile (cursor < units.length && !failure) {\n\t\t\t\t\tif (options.signal?.aborted) throw new Error(\"Compaction cancelled\");\n\t\t\t\t\tconst unit = units[cursor++];\n\t\t\t\t\tconst result = await classify({\n\t\t\t\t\t\tstate: {\n\t\t\t\t\t\t\tquery: input.query,\n\t\t\t\t\t\t\tmessage: { id: unit.message.id, role: unit.message.role, lines: unit.message.lines },\n\t\t\t\t\t\t\tposition: unit.position,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tquestions: {\n\t\t\t\t\t\t\tscore: {\n\t\t\t\t\t\t\t\ttype: \"score\",\n\t\t\t\t\t\t\t\tinstructions:\n\t\t\t\t\t\t\t\t\t\"How much does the assistant still need these lines to continue the task? Retain current requirements, decisions, unresolved errors, and unique implementation facts; completed repetitive output is less useful.\",\n\t\t\t\t\t\t\t\tcriteria: [\"not needed\", \"slightly useful\", \"useful\", \"important\", \"essential\"],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t\tif (result.usage) options.onUsage?.(result.usage);\n\t\t\t\t\tconst answer = result.answers.score;\n\t\t\t\t\tif (result.stopReason !== \"stop\") throw new Error(result.errorMessage || \"Classifier compaction failed\");\n\t\t\t\t\tif (\n\t\t\t\t\t\tanswer?.type !== \"score\" ||\n\t\t\t\t\t\t!Number.isFinite(answer.score) ||\n\t\t\t\t\t\tanswer.score < 0 ||\n\t\t\t\t\t\tanswer.score > 4 ||\n\t\t\t\t\t\t!Number.isFinite(answer.confidence) ||\n\t\t\t\t\t\tanswer.confidence < 0 ||\n\t\t\t\t\t\tanswer.confidence > 1\n\t\t\t\t\t)\n\t\t\t\t\t\tthrow new Error(\"Invalid classifier compaction score\");\n\t\t\t\t\tranked.push({ unit, score: answer.score, confidence: answer.confidence });\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tfailure ??= error instanceof Error ? error : new Error(String(error));\n\t\t\t}\n\t\t}),\n\t);\n\tif (failure) throw failure;\n\tif (options.signal?.aborted) throw new Error(\"Compaction cancelled\");\n\tranked.sort((a, b) => a.score - b.score || b.confidence - a.confidence || a.unit.start - b.unit.start);\n\tlet kept = region.lines.length;\n\tconst target = Math.round(kept * parameters.compression_ratio);\n\tconst ranges: LineRange[] = [];\n\tfor (const { unit } of ranked) {\n\t\tif (kept <= target) break;\n\t\tranges.push({ start: unit.start, end: unit.end });\n\t\tkept -= unit.end - unit.start + 1;\n\t}\n\tif (!validateDeletedRanges(ranges, region).length)\n\t\tthrow new Error(\"Classifier compaction produced no usable ranges\");\n\treturn ranges;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compaction-boundary.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/compaction-boundary.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAqC,YAAY,EAAE,MAAM,6BAA6B,CAAC;AACnG,OAAO,EAAE,KAAK,kBAAkB,EAAkD,MAAM,iBAAiB,CAAC;AAM1G,OAAO,EAIN,KAAK,4BAA4B,EACjC,KAAK,6BAA6B,EAClC,MAAM,uBAAuB,CAAC;AAW/B;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,6BAA6B,GAAG,MAAM,CAG3F;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,6BAA6B,GAAG,OAAO,CAE5F;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,6BAA6B,EAAE,QAA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AAE,YAAY,EAAE,EAC3B,QAAQ,EAAE,kBAAkB,EAC5B,OAAO,GAAE,OAAO,CAAC,4BAA4B,CAAM,EACnD,OAAO,GAAE,yBAA8B,GACrC,6BAA6B,GAAG,SAAS,CAsC3C"}
|
|
@@ -4,7 +4,7 @@ import { applyContextEdit, buildSessionProjection, collectContextEdits } from ".
|
|
|
4
4
|
import { estimateProjectedContextTokens, estimateTokens } from "./compaction.js";
|
|
5
5
|
import { COMPACTION_AUTO_QUERY, normalizeCompactionParameters, normalizeCompactionQuery, } from "./compaction-parameters.js";
|
|
6
6
|
import { MIN_COMPACTABLE_REGION_LINES, VERBATIM_COMPACTION_STRATEGY, } from "./compaction-types.js";
|
|
7
|
-
import {
|
|
7
|
+
import { createConversationRegion } from "./transcript-serialization.js";
|
|
8
8
|
const keptTailTokensByPreparation = new WeakMap();
|
|
9
9
|
/**
|
|
10
10
|
* Return the independently estimated cost of the protected tail.
|
|
@@ -122,11 +122,7 @@ export function prepareCompactionBoundary(pathEntries, settings, options = {}, c
|
|
|
122
122
|
const tailStart = Math.max(0, visible.length - parameters.preserve_recent);
|
|
123
123
|
const regionMessages = visible.slice(0, tailStart);
|
|
124
124
|
const tailMessages = visible.slice(tailStart);
|
|
125
|
-
const
|
|
126
|
-
const regionText = [previous?.entry.summary, serialized]
|
|
127
|
-
.filter((text) => typeof text === "string" && text.length > 0)
|
|
128
|
-
.join("\n");
|
|
129
|
-
const region = createNumberedRegion(regionText);
|
|
125
|
+
const region = createConversationRegion(convertToLlm(regionMessages.map((item) => item.message)), previous?.entry.summary);
|
|
130
126
|
if (!control.allowSmallRegion && region.lines.length < MIN_COMPACTABLE_REGION_LINES)
|
|
131
127
|
return undefined;
|
|
132
128
|
const preparation = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compaction-boundary.js","sourceRoot":"","sources":["../../../src/core/compaction/compaction-boundary.ts"],"names":[],"mappings":"AACA,OAAO,EACN,YAAY,EACZ,0BAA0B,EAC1B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EAAE,CAAC;QAC3D,oBAAoB,EAAE,YAAY,CAAC,MAAM;QACzC,YAAY,EAAE,8BAA8B,CAAC,sBAAsB,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC,MAAM;QAC7F,UAAU;QACV,QAAQ;KACR,CAAC;IACF,2BAA2B,CAAC,GAAG,CAC9B,WAAW,EACX,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CACzE,CAAC;IACF,OAAO,WAAW,CAAC;AACpB,CAAC","sourcesContent":["import type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport {\n\tconvertToLlm,\n\tcreateBranchSummaryMessage,\n\tcreateCustomMessage,\n\tmessageIsLlmVisible,\n\tmessageStartsLlmUserTurn,\n} from \"../messages.ts\";\nimport { normalizeDerivedSessionEntries } from \"../session-entry-normalization.ts\";\nimport { applyContextEdit, buildSessionProjection, collectContextEdits } from \"../session-manager-history.ts\";\nimport type { CompactionEntry, ContextEditEntry, SessionEntry } from \"../session-manager-types.ts\";\nimport { type CompactionSettings, estimateProjectedContextTokens, estimateTokens } from \"./compaction.ts\";\nimport {\n\tCOMPACTION_AUTO_QUERY,\n\tnormalizeCompactionParameters,\n\tnormalizeCompactionQuery,\n} from \"./compaction-parameters.js\";\nimport {\n\tMIN_COMPACTABLE_REGION_LINES,\n\tVERBATIM_COMPACTION_STRATEGY,\n\ttype VerbatimCompactionDetails,\n\ttype VerbatimCompactionParameters,\n\ttype VerbatimCompactionPreparation,\n} from \"./compaction-types.js\";\nimport { createNumberedRegion, serializeConversationForCompaction } from \"./transcript-serialization.js\";\n\ninterface VisibleEntry {\n\tentry: SessionEntry;\n\tindex: number;\n\tmessage: AgentMessage;\n}\n\nconst keptTailTokensByPreparation = new WeakMap<VerbatimCompactionPreparation, number>();\n\n/**\n * Return the independently estimated cost of the protected tail.\n *\n * Only the explicit estimate recorded by `prepareCompactionBoundary` (or a test)\n * is returned. The old fallback of `tokensBefore - region.tokenEstimate` was\n * removed because it mixed the authoritative whole-context count with the\n * heuristic region estimate; when no explicit estimate exists the tail simply\n * contributes nothing rather than a wrong-numbered value. Whether an estimate is\n * registered at all is visible through `hasKeptTailTokenEstimate`, which the\n * fresh rung uses to decide a tail whose size is unknown (P1 #3010).\n */\nexport function getKeptTailTokenEstimate(preparation: VerbatimCompactionPreparation): number {\n\tconst registered = keptTailTokensByPreparation.get(preparation);\n\treturn registered === undefined ? 0 : registered;\n}\n\n/**\n * Whether an independent tail estimate is registered for this preparation.\n *\n * A preparation built by `prepareCompactionBoundary` always has one. A directly\n * constructed preparation may not; the fresh rung treats a kept tail whose size\n * is unregistered as unknown and drops it conservatively rather than assuming it\n * fits (P1 #3010).\n */\nexport function hasKeptTailTokenEstimate(preparation: VerbatimCompactionPreparation): boolean {\n\treturn keptTailTokensByPreparation.has(preparation);\n}\n\n/**\n * Record the independent tail estimate for a preparation.\n *\n * `prepareCompactionBoundary` does this itself from the kept-tail messages;\n * this setter is for test preparations built directly and for any other code\n * that constructs a `VerbatimCompactionPreparation` without the boundary\n * preparer. The estimate lives in a module-private map and is never persisted.\n */\nexport function setKeptTailTokenEstimate(preparation: VerbatimCompactionPreparation, estimate: number): void {\n\tkeptTailTokensByPreparation.set(preparation, Math.max(0, estimate));\n}\n\nfunction messageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\t// Prompt state is replayed separately at the boundary, never offered for deletion.\n\tif (entry.type === \"message\") return entry.message.role === \"system\" ? undefined : entry.message;\n\tif (entry.type === \"custom_message\") {\n\t\treturn createCustomMessage(\n\t\t\tentry.customType,\n\t\t\tentry.content,\n\t\t\tentry.display,\n\t\t\tentry.details,\n\t\t\tentry.timestamp,\n\t\t\tentry.excludeFromContext,\n\t\t);\n\t}\n\tif (entry.type === \"branch_summary\" && typeof entry.summary === \"string\") {\n\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t}\n\treturn undefined;\n}\n\nfunction activeBoundary(entry: SessionEntry): entry is CompactionEntry<VerbatimCompactionDetails> {\n\tif (entry.type !== \"compaction\") return false;\n\tconst details = (entry as CompactionEntry<{ strategy?: string }>).details;\n\treturn details?.strategy === VERBATIM_COMPACTION_STRATEGY;\n}\n\n/** Context-visible entries from `start`, with append-only context edits applied to their content. */\nfunction visibleEntries(entries: SessionEntry[], start = 0, edits?: Map<string, ContextEditEntry>): VisibleEntry[] {\n\tconst visible: VisibleEntry[] = [];\n\tfor (let index = start; index < entries.length; index++) {\n\t\tconst entry = entries[index];\n\t\tconst raw = messageFromEntry(entry);\n\t\tif (!raw) continue;\n\t\tconst edit = edits?.get(entry.id);\n\t\tconst message = edit ? applyContextEdit([raw], edit)[0] : raw;\n\t\tif (message && messageIsLlmVisible(message)) visible.push({ entry, index, message });\n\t}\n\treturn visible;\n}\n\nfunction messageText(message: AgentMessage): string {\n\tconst converted = convertToLlm([message]);\n\tif (converted.length === 0) return \"\";\n\treturn converted\n\t\t.flatMap((item) =>\n\t\t\ttypeof item.content === \"string\"\n\t\t\t\t? [item.content]\n\t\t\t\t: item.content.filter((block) => block.type === \"text\").map((block) => block.text),\n\t\t)\n\t\t.join(\"\\n\");\n}\n\nexport function autoDetectCompactionQuery(entries: readonly SessionEntry[]): string {\n\tconst normalized = normalizeDerivedSessionEntries(entries);\n\tfor (let index = normalized.length - 1; index >= 0; index--) {\n\t\tconst message = messageFromEntry(normalized[index]);\n\t\tif (!message || !messageStartsLlmUserTurn(message)) continue;\n\t\tconst text = messageText(message).trim();\n\t\tif (text) return normalizeCompactionQuery(text, COMPACTION_AUTO_QUERY);\n\t}\n\treturn COMPACTION_AUTO_QUERY;\n}\n\nfunction latestActiveBoundary(\n\tentries: SessionEntry[],\n): { entry: CompactionEntry<VerbatimCompactionDetails>; index: number } | undefined {\n\tfor (let index = entries.length - 1; index >= 0; index--) {\n\t\tconst entry = entries[index];\n\t\tif (activeBoundary(entry)) return { entry, index };\n\t}\n\treturn undefined;\n}\n\n/**\n * Planner eligibility, kept separate from boundary preparation.\n *\n * `MIN_COMPACTABLE_REGION_LINES` exists to stop repeated pointless *planned*\n * compaction. A load-bearing caller has a different problem: one oversized tool\n * result can blow the context window while producing fewer than 20 compactable\n * lines, and refusing to prepare a boundary there makes the \"always completes\"\n * guarantee hollow — the fresh rung and its tail-drop rule become unreachable.\n */\nexport interface CompactionBoundaryControl {\n\t/**\n\t * Prepare a boundary even when the region is below the planner minimum.\n\t * Only `load_bearing` callers set this; the runner routes such a region\n\t * straight to the fresh rung without invoking a planner.\n\t */\n\tallowSmallRegion?: boolean;\n}\n\n/** Prepare the complete compactable transcript and its exact context-visible message tail. */\nexport function prepareCompactionBoundary(\n\tpathEntries: SessionEntry[],\n\tsettings: CompactionSettings,\n\toptions: Partial<VerbatimCompactionParameters> = {},\n\tcontrol: CompactionBoundaryControl = {},\n): VerbatimCompactionPreparation | undefined {\n\tconst entries = normalizeDerivedSessionEntries(pathEntries);\n\tconst previous = latestActiveBoundary(entries);\n\tlet regionStart = 0;\n\tif (previous) {\n\t\tconst keptIndex =\n\t\t\tprevious.entry.firstKeptEntryId === null\n\t\t\t\t? -1\n\t\t\t\t: entries.findIndex((entry) => entry.id === previous.entry.firstKeptEntryId);\n\t\tregionStart = keptIndex >= 0 ? keptIndex : previous.index + 1;\n\t}\n\n\tconst visible = visibleEntries(entries, regionStart, collectContextEdits(entries));\n\tconst parameters = normalizeCompactionParameters({ ...settings, ...options }, autoDetectCompactionQuery(entries));\n\tconst tailStart = Math.max(0, visible.length - parameters.preserve_recent);\n\tconst regionMessages = visible.slice(0, tailStart);\n\tconst tailMessages = visible.slice(tailStart);\n\n\tconst serialized = serializeConversationForCompaction(convertToLlm(regionMessages.map((item) => item.message)));\n\tconst regionText = [previous?.entry.summary, serialized]\n\t\t.filter((text): text is string => typeof text === \"string\" && text.length > 0)\n\t\t.join(\"\\n\");\n\tconst region = createNumberedRegion(regionText);\n\tif (!control.allowSmallRegion && region.lines.length < MIN_COMPACTABLE_REGION_LINES) return undefined;\n\n\tconst preparation: VerbatimCompactionPreparation = {\n\t\tfirstKeptEntryId: tailMessages[0]?.entry.id ?? null,\n\t\tregion,\n\t\tregionEntryIds: regionMessages.map((item) => item.entry.id),\n\t\tkeptTailMessageCount: tailMessages.length,\n\t\ttokensBefore: estimateProjectedContextTokens(buildSessionProjection(entries), entries).tokens,\n\t\tparameters,\n\t\tsettings,\n\t};\n\tkeptTailTokensByPreparation.set(\n\t\tpreparation,\n\t\ttailMessages.reduce((sum, item) => sum + estimateTokens(item.message), 0),\n\t);\n\treturn preparation;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"compaction-boundary.js","sourceRoot":"","sources":["../../../src/core/compaction/compaction-boundary.ts"],"names":[],"mappings":"AACA,OAAO,EACN,YAAY,EACZ,0BAA0B,EAC1B,mBAAmB,EACnB,mBAAmB,EACnB,wBAAwB,GACxB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,8BAA8B,EAAE,MAAM,mCAAmC,CAAC;AACnF,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AAE9G,OAAO,EAA2B,8BAA8B,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC1G,OAAO,EACN,qBAAqB,EACrB,6BAA6B,EAC7B,wBAAwB,GACxB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACN,4BAA4B,EAC5B,4BAA4B,GAI5B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAQzE,MAAM,2BAA2B,GAAG,IAAI,OAAO,EAAyC,CAAC;AAEzF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,wBAAwB,CAAC,WAA0C;IAClF,MAAM,UAAU,GAAG,2BAA2B,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAChE,OAAO,UAAU,KAAK,SAAS,CAA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AC,KAAK,CAAC,MAAM,GAAG,4BAA4B;QAAE,OAAO,SAAS,CAAC;IAEtG,MAAM,WAAW,GAAkC;QAClD,gBAAgB,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,IAAI,IAAI;QACnD,MAAM;QACN,cAAc,EAAE,cAAc,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3D,oBAAoB,EAAE,YAAY,CAAC,MAAM;QACzC,YAAY,EAAE,8BAA8B,CAAC,sBAAsB,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC,MAAM;QAC7F,UAAU;QACV,QAAQ;KACR,CAAC;IACF,2BAA2B,CAAC,GAAG,CAC9B,WAAW,EACX,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CACzE,CAAC;IACF,OAAO,WAAW,CAAC;AACpB,CAAC","sourcesContent":["import type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport {\n\tconvertToLlm,\n\tcreateBranchSummaryMessage,\n\tcreateCustomMessage,\n\tmessageIsLlmVisible,\n\tmessageStartsLlmUserTurn,\n} from \"../messages.ts\";\nimport { normalizeDerivedSessionEntries } from \"../session-entry-normalization.ts\";\nimport { applyContextEdit, buildSessionProjection, collectContextEdits } from \"../session-manager-history.ts\";\nimport type { CompactionEntry, ContextEditEntry, SessionEntry } from \"../session-manager-types.ts\";\nimport { type CompactionSettings, estimateProjectedContextTokens, estimateTokens } from \"./compaction.ts\";\nimport {\n\tCOMPACTION_AUTO_QUERY,\n\tnormalizeCompactionParameters,\n\tnormalizeCompactionQuery,\n} from \"./compaction-parameters.js\";\nimport {\n\tMIN_COMPACTABLE_REGION_LINES,\n\tVERBATIM_COMPACTION_STRATEGY,\n\ttype VerbatimCompactionDetails,\n\ttype VerbatimCompactionParameters,\n\ttype VerbatimCompactionPreparation,\n} from \"./compaction-types.js\";\nimport { createConversationRegion } from \"./transcript-serialization.js\";\n\ninterface VisibleEntry {\n\tentry: SessionEntry;\n\tindex: number;\n\tmessage: AgentMessage;\n}\n\nconst keptTailTokensByPreparation = new WeakMap<VerbatimCompactionPreparation, number>();\n\n/**\n * Return the independently estimated cost of the protected tail.\n *\n * Only the explicit estimate recorded by `prepareCompactionBoundary` (or a test)\n * is returned. The old fallback of `tokensBefore - region.tokenEstimate` was\n * removed because it mixed the authoritative whole-context count with the\n * heuristic region estimate; when no explicit estimate exists the tail simply\n * contributes nothing rather than a wrong-numbered value. Whether an estimate is\n * registered at all is visible through `hasKeptTailTokenEstimate`, which the\n * fresh rung uses to decide a tail whose size is unknown (P1 #3010).\n */\nexport function getKeptTailTokenEstimate(preparation: VerbatimCompactionPreparation): number {\n\tconst registered = keptTailTokensByPreparation.get(preparation);\n\treturn registered === undefined ? 0 : registered;\n}\n\n/**\n * Whether an independent tail estimate is registered for this preparation.\n *\n * A preparation built by `prepareCompactionBoundary` always has one. A directly\n * constructed preparation may not; the fresh rung treats a kept tail whose size\n * is unregistered as unknown and drops it conservatively rather than assuming it\n * fits (P1 #3010).\n */\nexport function hasKeptTailTokenEstimate(preparation: VerbatimCompactionPreparation): boolean {\n\treturn keptTailTokensByPreparation.has(preparation);\n}\n\n/**\n * Record the independent tail estimate for a preparation.\n *\n * `prepareCompactionBoundary` does this itself from the kept-tail messages;\n * this setter is for test preparations built directly and for any other code\n * that constructs a `VerbatimCompactionPreparation` without the boundary\n * preparer. The estimate lives in a module-private map and is never persisted.\n */\nexport function setKeptTailTokenEstimate(preparation: VerbatimCompactionPreparation, estimate: number): void {\n\tkeptTailTokensByPreparation.set(preparation, Math.max(0, estimate));\n}\n\nfunction messageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\t// Prompt state is replayed separately at the boundary, never offered for deletion.\n\tif (entry.type === \"message\") return entry.message.role === \"system\" ? undefined : entry.message;\n\tif (entry.type === \"custom_message\") {\n\t\treturn createCustomMessage(\n\t\t\tentry.customType,\n\t\t\tentry.content,\n\t\t\tentry.display,\n\t\t\tentry.details,\n\t\t\tentry.timestamp,\n\t\t\tentry.excludeFromContext,\n\t\t);\n\t}\n\tif (entry.type === \"branch_summary\" && typeof entry.summary === \"string\") {\n\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t}\n\treturn undefined;\n}\n\nfunction activeBoundary(entry: SessionEntry): entry is CompactionEntry<VerbatimCompactionDetails> {\n\tif (entry.type !== \"compaction\") return false;\n\tconst details = (entry as CompactionEntry<{ strategy?: string }>).details;\n\treturn details?.strategy === VERBATIM_COMPACTION_STRATEGY;\n}\n\n/** Context-visible entries from `start`, with append-only context edits applied to their content. */\nfunction visibleEntries(entries: SessionEntry[], start = 0, edits?: Map<string, ContextEditEntry>): VisibleEntry[] {\n\tconst visible: VisibleEntry[] = [];\n\tfor (let index = start; index < entries.length; index++) {\n\t\tconst entry = entries[index];\n\t\tconst raw = messageFromEntry(entry);\n\t\tif (!raw) continue;\n\t\tconst edit = edits?.get(entry.id);\n\t\tconst message = edit ? applyContextEdit([raw], edit)[0] : raw;\n\t\tif (message && messageIsLlmVisible(message)) visible.push({ entry, index, message });\n\t}\n\treturn visible;\n}\n\nfunction messageText(message: AgentMessage): string {\n\tconst converted = convertToLlm([message]);\n\tif (converted.length === 0) return \"\";\n\treturn converted\n\t\t.flatMap((item) =>\n\t\t\ttypeof item.content === \"string\"\n\t\t\t\t? [item.content]\n\t\t\t\t: item.content.filter((block) => block.type === \"text\").map((block) => block.text),\n\t\t)\n\t\t.join(\"\\n\");\n}\n\nexport function autoDetectCompactionQuery(entries: readonly SessionEntry[]): string {\n\tconst normalized = normalizeDerivedSessionEntries(entries);\n\tfor (let index = normalized.length - 1; index >= 0; index--) {\n\t\tconst message = messageFromEntry(normalized[index]);\n\t\tif (!message || !messageStartsLlmUserTurn(message)) continue;\n\t\tconst text = messageText(message).trim();\n\t\tif (text) return normalizeCompactionQuery(text, COMPACTION_AUTO_QUERY);\n\t}\n\treturn COMPACTION_AUTO_QUERY;\n}\n\nfunction latestActiveBoundary(\n\tentries: SessionEntry[],\n): { entry: CompactionEntry<VerbatimCompactionDetails>; index: number } | undefined {\n\tfor (let index = entries.length - 1; index >= 0; index--) {\n\t\tconst entry = entries[index];\n\t\tif (activeBoundary(entry)) return { entry, index };\n\t}\n\treturn undefined;\n}\n\n/**\n * Planner eligibility, kept separate from boundary preparation.\n *\n * `MIN_COMPACTABLE_REGION_LINES` exists to stop repeated pointless *planned*\n * compaction. A load-bearing caller has a different problem: one oversized tool\n * result can blow the context window while producing fewer than 20 compactable\n * lines, and refusing to prepare a boundary there makes the \"always completes\"\n * guarantee hollow — the fresh rung and its tail-drop rule become unreachable.\n */\nexport interface CompactionBoundaryControl {\n\t/**\n\t * Prepare a boundary even when the region is below the planner minimum.\n\t * Only `load_bearing` callers set this; the runner routes such a region\n\t * straight to the fresh rung without invoking a planner.\n\t */\n\tallowSmallRegion?: boolean;\n}\n\n/** Prepare the complete compactable transcript and its exact context-visible message tail. */\nexport function prepareCompactionBoundary(\n\tpathEntries: SessionEntry[],\n\tsettings: CompactionSettings,\n\toptions: Partial<VerbatimCompactionParameters> = {},\n\tcontrol: CompactionBoundaryControl = {},\n): VerbatimCompactionPreparation | undefined {\n\tconst entries = normalizeDerivedSessionEntries(pathEntries);\n\tconst previous = latestActiveBoundary(entries);\n\tlet regionStart = 0;\n\tif (previous) {\n\t\tconst keptIndex =\n\t\t\tprevious.entry.firstKeptEntryId === null\n\t\t\t\t? -1\n\t\t\t\t: entries.findIndex((entry) => entry.id === previous.entry.firstKeptEntryId);\n\t\tregionStart = keptIndex >= 0 ? keptIndex : previous.index + 1;\n\t}\n\n\tconst visible = visibleEntries(entries, regionStart, collectContextEdits(entries));\n\tconst parameters = normalizeCompactionParameters({ ...settings, ...options }, autoDetectCompactionQuery(entries));\n\tconst tailStart = Math.max(0, visible.length - parameters.preserve_recent);\n\tconst regionMessages = visible.slice(0, tailStart);\n\tconst tailMessages = visible.slice(tailStart);\n\n\tconst region = createConversationRegion(\n\t\tconvertToLlm(regionMessages.map((item) => item.message)),\n\t\tprevious?.entry.summary,\n\t);\n\tif (!control.allowSmallRegion && region.lines.length < MIN_COMPACTABLE_REGION_LINES) return undefined;\n\n\tconst preparation: VerbatimCompactionPreparation = {\n\t\tfirstKeptEntryId: tailMessages[0]?.entry.id ?? null,\n\t\tregion,\n\t\tregionEntryIds: regionMessages.map((item) => item.entry.id),\n\t\tkeptTailMessageCount: tailMessages.length,\n\t\ttokensBefore: estimateProjectedContextTokens(buildSessionProjection(entries), entries).tokens,\n\t\tparameters,\n\t\tsettings,\n\t};\n\tkeptTailTokensByPreparation.set(\n\t\tpreparation,\n\t\ttailMessages.reduce((sum, item) => sum + estimateTokens(item.message), 0),\n\t);\n\treturn preparation;\n}\n"]}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import type { RetryCallbacks, RetryPolicy } from "@bastani/pi-ai";
|
|
1
|
+
import type { ClassifierApi, ClassifierContext, ClassifierModel, ClassifierOptions, ClassifierResult, RetryCallbacks, RetryPolicy } from "@bastani/pi-ai";
|
|
2
2
|
import type { Api, Model, Usage } from "@bastani/pi-ai/compat";
|
|
3
3
|
import type { StreamFn, ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
4
|
+
import type { SessionEntry } from "../session-manager-types.ts";
|
|
4
5
|
import type { CompactedTranscript, CompactionPlannerModel, CompactionUrgency, PlannerAuth, VerbatimCompactionPreparation } from "./compaction-types.js";
|
|
5
6
|
import { type FallbackPlannerContext } from "./fallback-planner.js";
|
|
7
|
+
import type { CompactionModelSelection } from "./model-resolver.js";
|
|
6
8
|
export interface CompactionPlanOptions {
|
|
7
9
|
streamFn: StreamFn;
|
|
8
10
|
/** Absolute path of the persisted session file. Undefined for in-memory sessions. */
|
|
@@ -20,6 +22,11 @@ export interface CompactionRunRequest extends CompactionPlanOptions {
|
|
|
20
22
|
urgency: CompactionUrgency;
|
|
21
23
|
/** Configured fallback candidates. Omitted means borrowing is impossible. */
|
|
22
24
|
fallback?: FallbackPlannerContext;
|
|
25
|
+
compactionModel?: CompactionModelSelection;
|
|
26
|
+
resolveMorphApiKey?: () => Promise<string | undefined>;
|
|
27
|
+
morphFetchFn?: typeof fetch;
|
|
28
|
+
classify?: (model: ClassifierModel<ClassifierApi>, context: ClassifierContext, options?: ClassifierOptions) => Promise<ClassifierResult>;
|
|
29
|
+
summaryEntries?: SessionEntry[];
|
|
23
30
|
}
|
|
24
31
|
export type CompactionRungResult = CompactedTranscript & {
|
|
25
32
|
rung: "planned" | "fresh";
|
|
@@ -29,6 +36,14 @@ export type CompactionRungResult = CompactedTranscript & {
|
|
|
29
36
|
keptTail: boolean;
|
|
30
37
|
/** Aggregate usage across every planner request and retry in this run. */
|
|
31
38
|
usage?: Usage;
|
|
39
|
+
backend?: "planner" | "classifier" | "morph" | "summary";
|
|
40
|
+
model?: string;
|
|
41
|
+
summary?: {
|
|
42
|
+
readFiles: string[];
|
|
43
|
+
modifiedFiles: string[];
|
|
44
|
+
};
|
|
45
|
+
summaryFirstKeptEntryId?: string;
|
|
46
|
+
summaryTokensBefore?: number;
|
|
32
47
|
};
|
|
33
48
|
/**
|
|
34
49
|
* Calculate the single global line threshold from the prepared setting.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compaction-runner.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/compaction-runner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"compaction-runner.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/compaction-runner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,cAAc,EACd,WAAW,EACX,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,GAAG,E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AAC,EAAE,iBAAiB,KACvB,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC/B,cAAc,CAAC,EAAE,YAAY,EAAE,CAAC;CAChC;AAED,MAAM,MAAM,oBAAoB,GAAG,mBAAmB,GAAG;IACxD,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC;IAC1B,oEAAoE;IACpE,YAAY,CAAC,EAAE,sBAAsB,CAAC;IACtC,qEAAqE;IACrE,QAAQ,EAAE,OAAO,CAAC;IAClB,0EAA0E;IAC1E,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,OAAO,CAAC,EAAE,SAAS,GAAG,YAAY,GAAG,OAAO,GAAG,SAAS,CAAC;IACzD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE;QAAE,SAAS,EAAE,MAAM,EAAE,CAAC;QAAC,aAAa,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAC3D,uBAAuB,CAAC,EAAE,MAAM,CAAC;IACjC,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC;AA6BF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,6BAA6B,GAAG,MAAM,CAGlF;AAwDD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,6BAA6B,GAAG,mBAAmB,CAErG;AA4ID;;;;;;;;;;;GAWG;AACH,wBAAsB,qBAAqB,CAC1C,WAAW,EAAE,6BAA6B,EAC1C,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,OAAO,EAAE,oBAAoB,GAC3B,OAAO,CAAC,oBAAoB,CAAC,CAiK/B"}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { planClassifierRanges } from "./classifier-compaction.js";
|
|
1
2
|
import { getKeptTailTokenEstimate, hasKeptTailTokenEstimate } from "./compaction-boundary.js";
|
|
2
3
|
import { MIN_COMPACTABLE_REGION_LINES } from "./compaction-types.js";
|
|
3
4
|
import { reconstructCompactedTranscript, validateDeletedRanges } from "./deleted-ranges.js";
|
|
4
5
|
import { createFallbackPlannerBorrower, plannerAttemptKey, } from "./fallback-planner.js";
|
|
6
|
+
import { planMorphRanges } from "./morph-compaction.js";
|
|
7
|
+
import { runPiSummaryFallback } from "./pi-summary-fallback.js";
|
|
5
8
|
import { MALFORMED_OUTPUT_MESSAGE, NO_USABLE_RANGES_MESSAGE, planDeletedLineRanges, RangePlanError, resolvePlannerRequest, } from "./range-planner.js";
|
|
6
|
-
import { writeSuccessDiagnosticSidecar } from "./range-planner-diagnostics.js";
|
|
9
|
+
import { writeMorphDiagnosticSidecar, writeSuccessDiagnosticSidecar } from "./range-planner-diagnostics.js";
|
|
7
10
|
import { nextTrimOffset, rebaseTrimmedRanges, trimRegionHead } from "./region-trimming.js";
|
|
8
11
|
import { widenToWholeContextStats } from "./whole-context-stats.js";
|
|
9
12
|
function addUsage(total, usage) {
|
|
@@ -110,11 +113,13 @@ function buildFreshContextWindow(preparation, hardInputLimit) {
|
|
|
110
113
|
export function startNewContextWindow(preparation) {
|
|
111
114
|
return buildFreshTranscript(preparation);
|
|
112
115
|
}
|
|
113
|
-
function plannedResult(preparation, ranges, plannerModel, usage) {
|
|
116
|
+
function plannedResult(preparation, ranges, plannerModel, usage, model) {
|
|
114
117
|
const reconstructed = reconstructCompactedTranscript(preparation.region, validateDeletedRanges(ranges, preparation.region));
|
|
115
118
|
return {
|
|
116
119
|
...withWholeContextStats(reconstructed, preparation, true),
|
|
117
120
|
rung: "planned",
|
|
121
|
+
backend: "planner",
|
|
122
|
+
model,
|
|
118
123
|
...(plannerModel ? { plannerModel } : {}),
|
|
119
124
|
keptTail: true,
|
|
120
125
|
...(usage ? { usage } : {}),
|
|
@@ -136,7 +141,7 @@ function terminalError(outcome) {
|
|
|
136
141
|
if (outcome.kind === "overflowed") {
|
|
137
142
|
return new RangePlanError("Compaction range planning exceeded the model context window", 1, "", true, outcome.diagnosticPath, outcome);
|
|
138
143
|
}
|
|
139
|
-
if (outcome.kind === "providerError") {
|
|
144
|
+
if (outcome.kind === "providerError" || outcome.kind === "policyRefusal") {
|
|
140
145
|
return new RangePlanError(outcome.message, 1, "", false, outcome.diagnosticPath, outcome);
|
|
141
146
|
}
|
|
142
147
|
const message = outcome.category === "malformed_output" ? MALFORMED_OUTPUT_MESSAGE : NO_USABLE_RANGES_MESSAGE;
|
|
@@ -247,23 +252,71 @@ export async function runVerbatimCompaction(preparation, model, request) {
|
|
|
247
252
|
throw new RangePlanError(NO_USABLE_RANGES_MESSAGE, 0, "", false);
|
|
248
253
|
return freshResult(preparation, hardInputLimit);
|
|
249
254
|
}
|
|
255
|
+
const selection = request.compactionModel ?? { kind: "chat", fullId: `${model.provider}/${model.id}`, model };
|
|
250
256
|
const attempted = new Set();
|
|
251
257
|
// One borrower per run owns the monotonic cursor, so the configured list is
|
|
252
258
|
// walked exactly once no matter how many terminal outcomes occur.
|
|
253
259
|
const borrow = request.fallback
|
|
254
|
-
? createFallbackPlannerBorrower(request.fallback)
|
|
260
|
+
? createFallbackPlannerBorrower({ ...request.fallback, selectedModelId: selection.fullId })
|
|
255
261
|
: undefined;
|
|
256
|
-
|
|
257
|
-
|
|
262
|
+
let backendFailure;
|
|
263
|
+
if (selection.kind === "classifier") {
|
|
264
|
+
try {
|
|
265
|
+
const classify = request.classify;
|
|
266
|
+
if (!classify)
|
|
267
|
+
throw new Error("Compaction classifier is unavailable");
|
|
268
|
+
const ranges = await planClassifierRanges(preparation.region, preparation.parameters, (context) => classify(selection.model, context, { signal }), plannerOptions);
|
|
269
|
+
return {
|
|
270
|
+
...plannedResult(preparation, ranges, undefined, plannerUsage, selection.fullId),
|
|
271
|
+
backend: "classifier",
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
if (signal?.aborted)
|
|
276
|
+
throw new Error("Compaction cancelled");
|
|
277
|
+
backendFailure = { kind: "providerError", message: error instanceof Error ? error.message : String(error) };
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (selection.kind === "morph") {
|
|
281
|
+
try {
|
|
282
|
+
const ranges = await planMorphRanges(preparation.region, preparation.parameters, {
|
|
283
|
+
apiKey: await request.resolveMorphApiKey?.(),
|
|
284
|
+
signal,
|
|
285
|
+
fetchFn: request.morphFetchFn,
|
|
286
|
+
onDiagnostics: (diagnostics) => writeMorphDiagnosticSidecar(request.sessionFilePath, diagnostics),
|
|
287
|
+
});
|
|
288
|
+
return {
|
|
289
|
+
...plannedResult(preparation, ranges, undefined, undefined, selection.fullId),
|
|
290
|
+
backend: "morph",
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
if (signal?.aborted)
|
|
295
|
+
throw new Error("Compaction cancelled");
|
|
296
|
+
backendFailure = { kind: "providerError", message: error instanceof Error ? error.message : String(error) };
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const primaryModel = selection.kind === "chat" ? selection.model : undefined;
|
|
300
|
+
const primaryBudget = resolvePlannerRequest(primaryModel ?? model, request.thinkingLevel);
|
|
301
|
+
const primaryAuth = primaryModel
|
|
302
|
+
? await resolvePlannerAuth(request.resolveAuth, primaryModel)
|
|
303
|
+
: {
|
|
304
|
+
failure: {
|
|
305
|
+
kind: "providerError",
|
|
306
|
+
message: backendFailure?.kind === "providerError"
|
|
307
|
+
? backendFailure.message
|
|
308
|
+
: `Compaction backend ${selection.kind} is not yet available`,
|
|
309
|
+
},
|
|
310
|
+
};
|
|
258
311
|
let lastTerminal = "failure" in primaryAuth ? primaryAuth.failure : undefined;
|
|
259
|
-
let borrowed =
|
|
312
|
+
let borrowed = primaryModel !== model;
|
|
260
313
|
let planner;
|
|
261
|
-
if ("auth" in primaryAuth) {
|
|
262
|
-
planner = { model, budget: primaryBudget, auth: primaryAuth.auth };
|
|
314
|
+
if (primaryModel && "auth" in primaryAuth) {
|
|
315
|
+
planner = { model: primaryModel, budget: primaryBudget, auth: primaryAuth.auth };
|
|
263
316
|
}
|
|
264
317
|
else {
|
|
265
|
-
|
|
266
|
-
|
|
318
|
+
if (primaryModel)
|
|
319
|
+
attempted.add(plannerAttemptKey({ model: primaryModel, budget: primaryBudget, auth: {} }));
|
|
267
320
|
planner = borrow ? await borrow(attempted, request.resolveAuth) : undefined;
|
|
268
321
|
borrowed = planner !== undefined;
|
|
269
322
|
}
|
|
@@ -283,9 +336,41 @@ export async function runVerbatimCompaction(preparation, model, request) {
|
|
|
283
336
|
...(plannerModel.thinkingLevel === undefined ? {} : { thinkingLevel: plannerModel.thinkingLevel }),
|
|
284
337
|
});
|
|
285
338
|
}
|
|
286
|
-
return plannedResult(preparation, attempt.ranges, plannerModel, plannerUsage);
|
|
339
|
+
return plannedResult(preparation, attempt.ranges, plannerModel, plannerUsage, `${planner.model.provider}/${planner.model.id}`);
|
|
287
340
|
}
|
|
288
341
|
lastTerminal = attempt.outcome;
|
|
342
|
+
if (attempt.outcome.kind === "policyRefusal" && request.summaryEntries) {
|
|
343
|
+
try {
|
|
344
|
+
const summary = await runPiSummaryFallback(request.summaryEntries, preparation.settings, planner, plannerOptions);
|
|
345
|
+
return {
|
|
346
|
+
text: summary.summary,
|
|
347
|
+
ranges: [],
|
|
348
|
+
keptRanges: [],
|
|
349
|
+
stats: {
|
|
350
|
+
linesBefore: preparation.region.lines.length,
|
|
351
|
+
linesDeleted: 0,
|
|
352
|
+
linesKept: summary.summary.split("\n").length,
|
|
353
|
+
rangeCount: 0,
|
|
354
|
+
tokensBefore: summary.tokensBefore,
|
|
355
|
+
tokensAfter: summary.tokensAfter,
|
|
356
|
+
percentReduction: summary.tokensBefore > 0 ? 100 * (1 - summary.tokensAfter / summary.tokensBefore) : 0,
|
|
357
|
+
},
|
|
358
|
+
rung: "planned",
|
|
359
|
+
keptTail: true,
|
|
360
|
+
backend: "summary",
|
|
361
|
+
model: `${planner.model.provider}/${planner.model.id}`,
|
|
362
|
+
summary: { readFiles: summary.readFiles, modifiedFiles: summary.modifiedFiles },
|
|
363
|
+
summaryFirstKeptEntryId: summary.firstKeptEntryId,
|
|
364
|
+
summaryTokensBefore: summary.tokensBefore,
|
|
365
|
+
...(plannerUsage ? { usage: plannerUsage } : {}),
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
if (signal?.aborted)
|
|
370
|
+
throw new Error("Compaction cancelled");
|
|
371
|
+
lastTerminal = { kind: "providerError", message: error instanceof Error ? error.message : String(error) };
|
|
372
|
+
}
|
|
373
|
+
}
|
|
289
374
|
attempted.add(plannerAttemptKey(planner));
|
|
290
375
|
planner = borrow ? await borrow(attempted, request.resolveAuth) : undefined;
|
|
291
376
|
borrowed = planner !== undefined;
|