@bastani/atomic 0.9.8 → 0.9.9-alpha.1
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 +28 -0
- package/dist/builtin/cursor/CHANGELOG.md +6 -0
- package/dist/builtin/cursor/package.json +2 -2
- package/dist/builtin/intercom/CHANGELOG.md +6 -0
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/CHANGELOG.md +6 -0
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/CHANGELOG.md +6 -0
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/src/runs/background/async-execution-chain.ts +2 -0
- package/dist/builtin/subagents/src/runs/background/async-execution-common.ts +7 -1
- package/dist/builtin/subagents/src/runs/background/async-execution-single.ts +2 -0
- package/dist/builtin/subagents/src/runs/background/async-execution-types.ts +8 -2
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-parallel-runner.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/chain-execution-sequential-step.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/execution-attempt.ts +2 -1
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-async.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-parallel-task.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-resume.ts +2 -0
- package/dist/builtin/subagents/src/runs/foreground/subagent-executor-single.ts +2 -0
- package/dist/builtin/subagents/src/runs/shared/pi-args.ts +6 -0
- package/dist/builtin/subagents/src/shared/types-config.ts +4 -0
- package/dist/builtin/subagents/src/shared/types-depth.ts +29 -2
- package/dist/builtin/web-access/CHANGELOG.md +6 -0
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +22 -0
- package/dist/builtin/workflows/README.md +17 -8
- package/dist/builtin/workflows/builtin/goal-prompts.ts +8 -2
- package/dist/builtin/workflows/builtin/open-claude-design-utils.ts +26 -9
- package/dist/builtin/workflows/builtin/ralph-forked-prompts.ts +1 -1
- package/dist/builtin/workflows/builtin/ralph-reviewer-prompt.ts +8 -3
- package/dist/builtin/workflows/builtin/ralph-runner.ts +2 -0
- package/dist/builtin/workflows/builtin/shared-prompts.ts +24 -1
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/durable/backend.ts +53 -15
- package/dist/builtin/workflows/src/durable/completed-catalog.ts +245 -0
- package/dist/builtin/workflows/src/durable/completed-inspection.ts +213 -0
- package/dist/builtin/workflows/src/durable/dbos-backend.ts +120 -170
- package/dist/builtin/workflows/src/durable/dbos-envelope.ts +84 -8
- package/dist/builtin/workflows/src/durable/dbos-metadata.ts +98 -0
- package/dist/builtin/workflows/src/durable/dbos-tombstone.ts +27 -0
- package/dist/builtin/workflows/src/durable/factory.ts +1 -1
- package/dist/builtin/workflows/src/durable/file-backend.ts +247 -241
- package/dist/builtin/workflows/src/durable/file-lock.ts +153 -0
- package/dist/builtin/workflows/src/durable/file-state.ts +104 -0
- package/dist/builtin/workflows/src/durable/format-version.ts +12 -0
- package/dist/builtin/workflows/src/durable/index.ts +12 -0
- package/dist/builtin/workflows/src/durable/resume-catalog.ts +16 -27
- package/dist/builtin/workflows/src/durable/resume-eligibility.ts +20 -0
- package/dist/builtin/workflows/src/durable/resume-runtime.ts +29 -29
- package/dist/builtin/workflows/src/durable/scoped-backend.ts +20 -3
- package/dist/builtin/workflows/src/durable/stage-primitive.ts +29 -8
- package/dist/builtin/workflows/src/durable/types.ts +3 -0
- package/dist/builtin/workflows/src/engine/primitives/task.ts +1 -0
- package/dist/builtin/workflows/src/engine/run.ts +14 -14
- package/dist/builtin/workflows/src/extension/extension-lifecycle.ts +39 -9
- package/dist/builtin/workflows/src/extension/extension-runtime-state.ts +10 -0
- package/dist/builtin/workflows/src/extension/runtime-durable-resume.ts +133 -0
- package/dist/builtin/workflows/src/extension/runtime.ts +15 -40
- package/dist/builtin/workflows/src/extension/wiring.ts +9 -6
- package/dist/builtin/workflows/src/extension/workflow-durable-resume-command.ts +226 -0
- package/dist/builtin/workflows/src/extension/workflow-prompts.ts +1 -0
- package/dist/builtin/workflows/src/extension/workflow-run-control-command.ts +66 -112
- package/dist/builtin/workflows/src/extension/workflow-schema.ts +5 -5
- package/dist/builtin/workflows/src/extension/workflow-tool-control.ts +16 -1
- package/dist/builtin/workflows/src/runs/foreground/executor-direct-helpers.ts +50 -43
- package/dist/builtin/workflows/src/runs/foreground/executor-direct-output.ts +91 -0
- package/dist/builtin/workflows/src/runs/foreground/executor-direct.ts +95 -46
- package/dist/builtin/workflows/src/runs/foreground/executor-stage-call.ts +3 -1
- package/dist/builtin/workflows/src/runs/foreground/executor-task-prompts.ts +1 -1
- package/dist/builtin/workflows/src/runs/foreground/executor-types.ts +3 -0
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-controller.ts +1 -1
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-options.ts +36 -7
- package/dist/builtin/workflows/src/runs/shared/worktree-cache-lifecycle.ts +25 -0
- package/dist/builtin/workflows/src/runs/shared/worktree-cwd.ts +103 -0
- package/dist/builtin/workflows/src/runs/shared/worktree-generation.ts +57 -0
- package/dist/builtin/workflows/src/runs/shared/worktree-git.ts +134 -8
- package/dist/builtin/workflows/src/runs/shared/worktree.ts +2 -0
- package/dist/builtin/workflows/src/shared/resumable-workflow-notices.ts +43 -0
- package/dist/builtin/workflows/src/shared/timing.ts +4 -0
- package/dist/builtin/workflows/src/shared/types.ts +5 -3
- package/dist/builtin/workflows/src/tui/graph-view-constants.ts +1 -0
- package/dist/builtin/workflows/src/tui/graph-view-input.ts +24 -14
- package/dist/builtin/workflows/src/tui/workflow-resume-selector.ts +43 -13
- package/dist/core/agent-session-auto-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-auto-compaction.js +5 -7
- package/dist/core/agent-session-auto-compaction.js.map +1 -1
- package/dist/core/agent-session-compaction.d.ts +6 -34
- package/dist/core/agent-session-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-compaction.js +95 -233
- package/dist/core/agent-session-compaction.js.map +1 -1
- package/dist/core/agent-session-message-queue.d.ts +0 -4
- package/dist/core/agent-session-message-queue.d.ts.map +1 -1
- package/dist/core/agent-session-message-queue.js +1 -5
- package/dist/core/agent-session-message-queue.js.map +1 -1
- package/dist/core/agent-session-methods.d.ts +5 -6
- 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-types.d.ts +2 -12
- package/dist/core/agent-session-types.d.ts.map +1 -1
- package/dist/core/agent-session-types.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +1 -1
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +1 -2
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction-boundary.d.ts +9 -0
- package/dist/core/compaction/compaction-boundary.d.ts.map +1 -0
- package/dist/core/compaction/compaction-boundary.js +115 -0
- package/dist/core/compaction/compaction-boundary.js.map +1 -0
- package/dist/core/compaction/compaction-parameters.d.ts +5 -0
- package/dist/core/compaction/compaction-parameters.d.ts.map +1 -0
- package/dist/core/compaction/compaction-parameters.js +27 -0
- package/dist/core/compaction/compaction-parameters.js.map +1 -0
- package/dist/core/compaction/compaction-runner.d.ts +14 -0
- package/dist/core/compaction/compaction-runner.d.ts.map +1 -0
- package/dist/core/compaction/compaction-runner.js +29 -0
- package/dist/core/compaction/compaction-runner.js.map +1 -0
- package/dist/core/compaction/compaction-types.d.ts +79 -0
- package/dist/core/compaction/compaction-types.d.ts.map +1 -0
- package/dist/core/compaction/compaction-types.js +6 -0
- package/dist/core/compaction/compaction-types.js.map +1 -0
- package/dist/core/compaction/deleted-ranges.d.ts +6 -0
- package/dist/core/compaction/deleted-ranges.d.ts.map +1 -0
- package/dist/core/compaction/deleted-ranges.js +134 -0
- package/dist/core/compaction/deleted-ranges.js.map +1 -0
- package/dist/core/compaction/index.d.ts +7 -1
- package/dist/core/compaction/index.d.ts.map +1 -1
- package/dist/core/compaction/index.js +7 -1
- package/dist/core/compaction/index.js.map +1 -1
- package/dist/core/compaction/range-planner.d.ts +21 -0
- package/dist/core/compaction/range-planner.d.ts.map +1 -0
- package/dist/core/compaction/range-planner.js +145 -0
- package/dist/core/compaction/range-planner.js.map +1 -0
- package/dist/core/compaction/transcript-serialization.d.ts +11 -0
- package/dist/core/compaction/transcript-serialization.d.ts.map +1 -0
- package/dist/core/compaction/transcript-serialization.js +104 -0
- package/dist/core/compaction/transcript-serialization.js.map +1 -0
- package/dist/core/extensions/context-types.d.ts +2 -2
- 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/event-results.d.ts +2 -2
- package/dist/core/extensions/event-results.d.ts.map +1 -1
- package/dist/core/extensions/event-results.js.map +1 -1
- package/dist/core/extensions/session-events.d.ts +7 -7
- package/dist/core/extensions/session-events.d.ts.map +1 -1
- package/dist/core/extensions/session-events.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/messages.d.ts +4 -0
- package/dist/core/messages.d.ts.map +1 -1
- package/dist/core/messages.js +11 -0
- package/dist/core/messages.js.map +1 -1
- package/dist/core/provider-context-usage.d.ts.map +1 -1
- package/dist/core/provider-context-usage.js +1 -2
- package/dist/core/provider-context-usage.js.map +1 -1
- package/dist/core/session-manager-archive.d.ts +2 -1
- package/dist/core/session-manager-archive.d.ts.map +1 -1
- package/dist/core/session-manager-archive.js +2 -2
- package/dist/core/session-manager-archive.js.map +1 -1
- package/dist/core/session-manager-classification.d.ts +9 -0
- package/dist/core/session-manager-classification.d.ts.map +1 -0
- package/dist/core/session-manager-classification.js +37 -0
- package/dist/core/session-manager-classification.js.map +1 -0
- package/dist/core/session-manager-core.d.ts +4 -4
- package/dist/core/session-manager-core.d.ts.map +1 -1
- package/dist/core/session-manager-core.js +13 -8
- package/dist/core/session-manager-core.js.map +1 -1
- package/dist/core/session-manager-entries.d.ts +4 -3
- package/dist/core/session-manager-entries.d.ts.map +1 -1
- package/dist/core/session-manager-entries.js +12 -10
- package/dist/core/session-manager-entries.js.map +1 -1
- package/dist/core/session-manager-history.d.ts +6 -23
- package/dist/core/session-manager-history.d.ts.map +1 -1
- package/dist/core/session-manager-history.js +23 -256
- package/dist/core/session-manager-history.js.map +1 -1
- package/dist/core/session-manager-list.d.ts.map +1 -1
- package/dist/core/session-manager-list.js +3 -3
- package/dist/core/session-manager-list.js.map +1 -1
- package/dist/core/session-manager-storage.d.ts +1 -1
- package/dist/core/session-manager-storage.d.ts.map +1 -1
- package/dist/core/session-manager-storage.js +3 -2
- package/dist/core/session-manager-storage.js.map +1 -1
- package/dist/core/session-manager-types.d.ts +14 -15
- package/dist/core/session-manager-types.d.ts.map +1 -1
- package/dist/core/session-manager-types.js.map +1 -1
- package/dist/core/session-manager.d.ts +3 -2
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +1 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +1 -1
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/main-session.d.ts +1 -0
- package/dist/main-session.d.ts.map +1 -1
- package/dist/main-session.js +7 -0
- package/dist/main-session.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/chat-message-renderer.d.ts +4 -1
- package/dist/modes/interactive/components/chat-message-renderer.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-message-renderer.js +12 -22
- package/dist/modes/interactive/components/chat-message-renderer.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-events.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-events.js +5 -11
- package/dist/modes/interactive/components/chat-session-host-events.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-rendering.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-rendering.js +3 -0
- package/dist/modes/interactive/components/chat-session-host-rendering.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-state.d.ts +1 -0
- package/dist/modes/interactive/components/chat-session-host-state.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-state.js +1 -0
- package/dist/modes/interactive/components/chat-session-host-state.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-utils.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host-utils.js +1 -1
- package/dist/modes/interactive/components/chat-session-host-utils.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.js +1 -0
- package/dist/modes/interactive/components/chat-session-host.js.map +1 -1
- package/dist/modes/interactive/components/compaction-boundary-message.d.ts +20 -0
- package/dist/modes/interactive/components/compaction-boundary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/compaction-boundary-message.js +48 -0
- package/dist/modes/interactive/components/compaction-boundary-message.js.map +1 -0
- package/dist/modes/interactive/components/index.d.ts +1 -1
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -1
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector-list.js +3 -0
- package/dist/modes/interactive/components/session-selector-list.js.map +1 -1
- package/dist/modes/interactive/interactive-agent-events.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-agent-events.js +3 -47
- package/dist/modes/interactive/interactive-agent-events.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-deps.d.ts +2 -2
- package/dist/modes/interactive/interactive-mode-deps.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-deps.js +1 -1
- package/dist/modes/interactive/interactive-mode-deps.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.d.ts +5 -3
- 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-render-chat.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-render-chat.js +21 -12
- package/dist/modes/interactive/interactive-render-chat.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +3 -5
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +1 -6
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.js +0 -4
- package/dist/modes/rpc/rpc-command-handler.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +2 -11
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/docs/compaction.md +97 -772
- package/docs/extensions.md +25 -29
- package/docs/json.md +2 -2
- package/docs/rpc.md +32 -24
- package/docs/sdk.md +3 -3
- package/docs/session-format.md +49 -30
- package/docs/sessions.md +5 -3
- package/docs/settings.md +14 -4
- package/docs/usage.md +1 -1
- package/docs/workflows.md +30 -15
- package/examples/extensions/custom-compaction.ts +14 -58
- package/examples/extensions/handoff.ts +2 -3
- package/npm-shrinkwrap.json +23 -23
- package/package.json +2 -2
- package/dist/core/compaction/context-assistant-turns.d.ts +0 -42
- package/dist/core/compaction/context-assistant-turns.d.ts.map +0 -1
- package/dist/core/compaction/context-assistant-turns.js +0 -87
- package/dist/core/compaction/context-assistant-turns.js.map +0 -1
- package/dist/core/compaction/context-compaction-critical.d.ts +0 -15
- package/dist/core/compaction/context-compaction-critical.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-critical.js +0 -57
- package/dist/core/compaction/context-compaction-critical.js.map +0 -1
- package/dist/core/compaction/context-compaction-eviction-alternates.d.ts +0 -18
- package/dist/core/compaction/context-compaction-eviction-alternates.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-eviction-alternates.js +0 -186
- package/dist/core/compaction/context-compaction-eviction-alternates.js.map +0 -1
- package/dist/core/compaction/context-compaction-eviction.d.ts +0 -12
- package/dist/core/compaction/context-compaction-eviction.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-eviction.js +0 -222
- package/dist/core/compaction/context-compaction-eviction.js.map +0 -1
- package/dist/core/compaction/context-compaction-metrics.d.ts +0 -28
- package/dist/core/compaction/context-compaction-metrics.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-metrics.js +0 -107
- package/dist/core/compaction/context-compaction-metrics.js.map +0 -1
- package/dist/core/compaction/context-compaction-prompt.d.ts +0 -9
- package/dist/core/compaction/context-compaction-prompt.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-prompt.js +0 -163
- package/dist/core/compaction/context-compaction-prompt.js.map +0 -1
- package/dist/core/compaction/context-compaction-runner.d.ts +0 -11
- package/dist/core/compaction/context-compaction-runner.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-runner.js +0 -273
- package/dist/core/compaction/context-compaction-runner.js.map +0 -1
- package/dist/core/compaction/context-compaction-strategy.d.ts +0 -5
- package/dist/core/compaction/context-compaction-strategy.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-strategy.js +0 -27
- package/dist/core/compaction/context-compaction-strategy.js.map +0 -1
- package/dist/core/compaction/context-compaction-types.d.ts +0 -75
- package/dist/core/compaction/context-compaction-types.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction-types.js +0 -6
- package/dist/core/compaction/context-compaction-types.js.map +0 -1
- package/dist/core/compaction/context-compaction.d.ts +0 -10
- package/dist/core/compaction/context-compaction.d.ts.map +0 -1
- package/dist/core/compaction/context-compaction.js +0 -8
- package/dist/core/compaction/context-compaction.js.map +0 -1
- package/dist/core/compaction/context-deletion-application.d.ts +0 -14
- package/dist/core/compaction/context-deletion-application.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-application.js +0 -261
- package/dist/core/compaction/context-deletion-application.js.map +0 -1
- package/dist/core/compaction/context-deletion-store.d.ts +0 -78
- package/dist/core/compaction/context-deletion-store.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-store.js +0 -162
- package/dist/core/compaction/context-deletion-store.js.map +0 -1
- package/dist/core/compaction/context-deletion-targets.d.ts +0 -43
- package/dist/core/compaction/context-deletion-targets.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-targets.js +0 -292
- package/dist/core/compaction/context-deletion-targets.js.map +0 -1
- package/dist/core/compaction/context-deletion-tool-definitions.d.ts +0 -193
- package/dist/core/compaction/context-deletion-tool-definitions.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-tool-definitions.js +0 -98
- package/dist/core/compaction/context-deletion-tool-definitions.js.map +0 -1
- package/dist/core/compaction/context-deletion-tool-helpers.d.ts +0 -19
- package/dist/core/compaction/context-deletion-tool-helpers.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-tool-helpers.js +0 -210
- package/dist/core/compaction/context-deletion-tool-helpers.js.map +0 -1
- package/dist/core/compaction/context-deletion-tools.d.ts +0 -4
- package/dist/core/compaction/context-deletion-tools.d.ts.map +0 -1
- package/dist/core/compaction/context-deletion-tools.js +0 -403
- package/dist/core/compaction/context-deletion-tools.js.map +0 -1
- package/dist/core/compaction/context-transcript-analysis.d.ts +0 -11
- package/dist/core/compaction/context-transcript-analysis.d.ts.map +0 -1
- package/dist/core/compaction/context-transcript-analysis.js +0 -228
- package/dist/core/compaction/context-transcript-analysis.js.map +0 -1
- package/dist/core/session-manager-tool-dependencies.d.ts +0 -10
- package/dist/core/session-manager-tool-dependencies.d.ts.map +0 -1
- package/dist/core/session-manager-tool-dependencies.js +0 -133
- package/dist/core/session-manager-tool-dependencies.js.map +0 -1
- package/dist/modes/interactive/components/context-compaction-summary-message.d.ts +0 -17
- package/dist/modes/interactive/components/context-compaction-summary-message.d.ts.map +0 -1
- package/dist/modes/interactive/components/context-compaction-summary-message.js +0 -83
- package/dist/modes/interactive/components/context-compaction-summary-message.js.map +0 -1
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
|
-
import type { ContextDeletionTarget } from "../session-manager.ts";
|
|
3
|
-
import type { CompactableTranscript } from "./context-compaction-types.ts";
|
|
4
|
-
export interface EntryTextRow {
|
|
5
|
-
entry_id: string;
|
|
6
|
-
text: string;
|
|
7
|
-
is_protected: number;
|
|
8
|
-
has_assistant_thinking_blocks: number;
|
|
9
|
-
}
|
|
10
|
-
export interface EntryReadRow extends EntryTextRow {
|
|
11
|
-
role: string;
|
|
12
|
-
token_estimate: number;
|
|
13
|
-
}
|
|
14
|
-
export interface ContentBlockTextRow {
|
|
15
|
-
entry_id: string;
|
|
16
|
-
block_index: number;
|
|
17
|
-
role: AgentMessage["role"];
|
|
18
|
-
type: string;
|
|
19
|
-
text: string;
|
|
20
|
-
entry_protected: number;
|
|
21
|
-
block_protected: number;
|
|
22
|
-
block_count: number;
|
|
23
|
-
has_assistant_thinking_blocks: number;
|
|
24
|
-
}
|
|
25
|
-
export interface ContentBlockReadRow extends ContentBlockTextRow {
|
|
26
|
-
token_estimate: number;
|
|
27
|
-
}
|
|
28
|
-
export interface StoredTranscriptEntry {
|
|
29
|
-
entryId: string;
|
|
30
|
-
role: AgentMessage["role"];
|
|
31
|
-
protected: boolean;
|
|
32
|
-
hasAssistantThinkingBlocks: boolean;
|
|
33
|
-
tokenEstimate: number;
|
|
34
|
-
text: string;
|
|
35
|
-
}
|
|
36
|
-
export interface StoredContentBlock {
|
|
37
|
-
entryPosition: number;
|
|
38
|
-
entryId: string;
|
|
39
|
-
blockIndex: number;
|
|
40
|
-
role: AgentMessage["role"];
|
|
41
|
-
type: string;
|
|
42
|
-
protected: boolean;
|
|
43
|
-
hasAssistantThinkingBlocks: boolean;
|
|
44
|
-
tokenEstimate: number;
|
|
45
|
-
text: string;
|
|
46
|
-
}
|
|
47
|
-
export interface ContextDeletionMemorySnapshot {
|
|
48
|
-
deletionTargets: ContextDeletionTarget[];
|
|
49
|
-
callCount: number;
|
|
50
|
-
lastError?: string;
|
|
51
|
-
}
|
|
52
|
-
export declare class ContextDeletionMemoryStore {
|
|
53
|
-
private readonly entries;
|
|
54
|
-
private readonly entriesById;
|
|
55
|
-
private readonly contentBlocks;
|
|
56
|
-
private readonly contentBlockCountByEntryId;
|
|
57
|
-
private deletionTargets;
|
|
58
|
-
private callCount;
|
|
59
|
-
private lastError;
|
|
60
|
-
constructor(transcript: CompactableTranscript);
|
|
61
|
-
transaction<T>(operation: () => T): T;
|
|
62
|
-
readTargets(): ContextDeletionTarget[];
|
|
63
|
-
replaceTargets(targets: readonly ContextDeletionTarget[]): void;
|
|
64
|
-
listEntriesForGrep(): EntryTextRow[];
|
|
65
|
-
listContentBlocksForGrep(): ContentBlockTextRow[];
|
|
66
|
-
getEntryForRead(entryId: string): EntryReadRow | undefined;
|
|
67
|
-
getContentBlockForRead(entryId: string, blockIndex: number): ContentBlockReadRow | undefined;
|
|
68
|
-
getGrepScanTextLength(target: "entry" | "content_block"): number;
|
|
69
|
-
incrementCallCount(): number;
|
|
70
|
-
getCallCount(): number;
|
|
71
|
-
setLastError(message: string): void;
|
|
72
|
-
clearLastError(): void;
|
|
73
|
-
getLastError(): string | undefined;
|
|
74
|
-
private snapshot;
|
|
75
|
-
private restore;
|
|
76
|
-
}
|
|
77
|
-
export declare function createContextDeletionStore(transcript: CompactableTranscript): ContextDeletionMemoryStore;
|
|
78
|
-
//# sourceMappingURL=context-deletion-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context-deletion-store.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/context-deletion-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,EAA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AAC,QAAQ;IAQhB,OAAO,CAAC,OAAO;CAKf;AAED,wBAAgB,0BAA0B,CAAC,UAAU,EAAE,qBAAqB,GAAG,0BAA0B,CAExG","sourcesContent":["import type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport type { ContextDeletionTarget } from \"../session-manager.ts\";\nimport type { CompactableTranscript } from \"./context-compaction-types.ts\";\nimport { assistantEntryHasThinkingContentBlock } from \"./context-assistant-turns.js\";\n\nexport interface EntryTextRow {\n\tentry_id: string;\n\ttext: string;\n\tis_protected: number;\n\thas_assistant_thinking_blocks: number;\n}\n\nexport interface EntryReadRow extends EntryTextRow {\n\trole: string;\n\ttoken_estimate: number;\n}\n\nexport interface ContentBlockTextRow {\n\tentry_id: string;\n\tblock_index: number;\n\trole: AgentMessage[\"role\"];\n\ttype: string;\n\ttext: string;\n\tentry_protected: number;\n\tblock_protected: number;\n\tblock_count: number;\n\thas_assistant_thinking_blocks: number;\n}\n\nexport interface ContentBlockReadRow extends ContentBlockTextRow {\n\ttoken_estimate: number;\n}\n\nexport interface StoredTranscriptEntry {\n\tentryId: string;\n\trole: AgentMessage[\"role\"];\n\tprotected: boolean;\n\thasAssistantThinkingBlocks: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\nexport interface StoredContentBlock {\n\tentryPosition: number;\n\tentryId: string;\n\tblockIndex: number;\n\trole: AgentMessage[\"role\"];\n\ttype: string;\n\tprotected: boolean;\n\thasAssistantThinkingBlocks: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\nexport interface ContextDeletionMemorySnapshot {\n\tdeletionTargets: ContextDeletionTarget[];\n\tcallCount: number;\n\tlastError?: string;\n}\n\nfunction copyDeletionTarget(target: ContextDeletionTarget): ContextDeletionTarget {\n\treturn target.kind === \"entry\"\n\t\t? { kind: \"entry\", entryId: target.entryId }\n\t\t: { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex };\n}\n\nexport class ContextDeletionMemoryStore {\n\tprivate readonly entries: StoredTranscriptEntry[];\n\tprivate readonly entriesById: Map<string, StoredTranscriptEntry>;\n\tprivate readonly contentBlocks: StoredContentBlock[];\n\tprivate readonly contentBlockCountByEntryId: Map<string, number>;\n\tprivate deletionTargets: ContextDeletionTarget[] = [];\n\tprivate callCount = 0;\n\tprivate lastError: string | undefined;\n\n\tconstructor(transcript: CompactableTranscript) {\n\t\tconst entryIds = new Set<string>();\n\t\tconst blockKeys = new Set<string>();\n\t\tthis.entries = transcript.entries.map((entry) => {\n\t\t\tif (entryIds.has(entry.entryId)) {\n\t\t\t\tthrow new Error(`Duplicate transcript entry id: ${entry.entryId}`);\n\t\t\t}\n\t\t\tentryIds.add(entry.entryId);\n\t\t\treturn {\n\t\t\t\tentryId: entry.entryId,\n\t\t\t\trole: entry.role,\n\t\t\t\tprotected: entry.protected,\n\t\t\t\thasAssistantThinkingBlocks: assistantEntryHasThinkingContentBlock(entry),\n\t\t\t\ttokenEstimate: entry.tokenEstimate,\n\t\t\t\ttext: entry.text,\n\t\t\t};\n\t\t});\n\t\tthis.entriesById = new Map<string, StoredTranscriptEntry>(this.entries.map((entry) => [entry.entryId, entry] as const));\n\t\tthis.contentBlocks = transcript.entries.flatMap((entry, entryPosition) => {\n\t\t\tconst hasAssistantThinkingBlocks = assistantEntryHasThinkingContentBlock(entry);\n\t\t\treturn entry.contentBlocks.map((block) => {\n\t\t\t\tif (block.entryId !== entry.entryId) {\n\t\t\t\t\tthrow new Error(`Transcript content block ${block.entryId}:${block.blockIndex} does not belong to entry ${entry.entryId}`);\n\t\t\t\t}\n\t\t\t\tconst blockKey = `${block.entryId}:${block.blockIndex}`;\n\t\t\t\tif (blockKeys.has(blockKey)) {\n\t\t\t\t\tthrow new Error(`Duplicate transcript content block: ${blockKey}`);\n\t\t\t\t}\n\t\t\t\tblockKeys.add(blockKey);\n\t\t\t\treturn {\n\t\t\t\t\tentryPosition,\n\t\t\t\t\tentryId: block.entryId,\n\t\t\t\t\tblockIndex: block.blockIndex,\n\t\t\t\t\trole: entry.role,\n\t\t\t\t\ttype: block.type,\n\t\t\t\t\tprotected: block.protected,\n\t\t\t\t\thasAssistantThinkingBlocks,\n\t\t\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\t\t\ttext: block.text,\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\t\tthis.contentBlockCountByEntryId = new Map();\n\t\tfor (const block of this.contentBlocks) {\n\t\t\tthis.contentBlockCountByEntryId.set(block.entryId, (this.contentBlockCountByEntryId.get(block.entryId) ?? 0) + 1);\n\t\t}\n\t}\n\n\ttransaction<T>(operation: () => T): T {\n\t\tconst snapshot = this.snapshot();\n\t\ttry {\n\t\t\treturn operation();\n\t\t} catch (error) {\n\t\t\tthis.restore(snapshot);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\treadTargets(): ContextDeletionTarget[] {\n\t\treturn this.deletionTargets.map(copyDeletionTarget);\n\t}\n\n\treplaceTargets(targets: readonly ContextDeletionTarget[]): void {\n\t\tthis.deletionTargets = targets.map(copyDeletionTarget);\n\t}\n\n\tlistEntriesForGrep(): EntryTextRow[] {\n\t\treturn this.entries.map((entry) => ({\n\t\t\tentry_id: entry.entryId,\n\t\t\ttext: entry.text,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t\thas_assistant_thinking_blocks: entry.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t}));\n\t}\n\n\tlistContentBlocksForGrep(): ContentBlockTextRow[] {\n\t\treturn [...this.contentBlocks]\n\t\t\t.sort((a, b) => a.entryPosition - b.entryPosition || a.blockIndex - b.blockIndex)\n\t\t\t.map((block) => ({\n\t\t\t\tentry_id: block.entryId,\n\t\t\t\tblock_index: block.blockIndex,\n\t\t\t\trole: block.role,\n\t\t\t\ttype: block.type,\n\t\t\t\ttext: block.text,\n\t\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t\t\thas_assistant_thinking_blocks: block.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t\t}));\n\t}\n\n\tgetEntryForRead(entryId: string): EntryReadRow | undefined {\n\t\tconst entry = this.entriesById.get(entryId);\n\t\tif (!entry) return undefined;\n\t\treturn {\n\t\t\tentry_id: entry.entryId,\n\t\t\trole: entry.role,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t\thas_assistant_thinking_blocks: entry.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t\ttoken_estimate: entry.tokenEstimate,\n\t\t\ttext: entry.text,\n\t\t};\n\t}\n\n\tgetContentBlockForRead(entryId: string, blockIndex: number): ContentBlockReadRow | undefined {\n\t\tconst block = this.contentBlocks.find((candidate) => candidate.entryId === entryId && candidate.blockIndex === blockIndex);\n\t\tif (!block) return undefined;\n\t\treturn {\n\t\t\tentry_id: block.entryId,\n\t\t\tblock_index: block.blockIndex,\n\t\t\trole: block.role,\n\t\t\ttype: block.type,\n\t\t\ttoken_estimate: block.tokenEstimate,\n\t\t\ttext: block.text,\n\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t\thas_assistant_thinking_blocks: block.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t};\n\t}\n\n\tgetGrepScanTextLength(target: \"entry\" | \"content_block\"): number {\n\t\tconst texts = target === \"entry\" ? this.entries : this.contentBlocks;\n\t\treturn texts.reduce((sum, item) => sum + item.text.length, 0);\n\t}\n\n\tincrementCallCount(): number {\n\t\tthis.callCount += 1;\n\t\treturn this.callCount;\n\t}\n\n\tgetCallCount(): number {\n\t\treturn this.callCount;\n\t}\n\n\tsetLastError(message: string): void {\n\t\tthis.lastError = message;\n\t}\n\n\tclearLastError(): void {\n\t\tthis.lastError = undefined;\n\t}\n\n\tgetLastError(): string | undefined {\n\t\treturn this.lastError;\n\t}\n\n\tprivate snapshot(): ContextDeletionMemorySnapshot {\n\t\treturn {\n\t\t\tdeletionTargets: this.readTargets(),\n\t\t\tcallCount: this.callCount,\n\t\t\t...(this.lastError === undefined ? {} : { lastError: this.lastError }),\n\t\t};\n\t}\n\n\tprivate restore(snapshot: ContextDeletionMemorySnapshot): void {\n\t\tthis.deletionTargets = snapshot.deletionTargets.map(copyDeletionTarget);\n\t\tthis.callCount = snapshot.callCount;\n\t\tthis.lastError = snapshot.lastError;\n\t}\n}\n\nexport function createContextDeletionStore(transcript: CompactableTranscript): ContextDeletionMemoryStore {\n\treturn new ContextDeletionMemoryStore(transcript);\n}\n"]}
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { assistantEntryHasThinkingContentBlock } from "./context-assistant-turns.js";
|
|
2
|
-
function copyDeletionTarget(target) {
|
|
3
|
-
return target.kind === "entry"
|
|
4
|
-
? { kind: "entry", entryId: target.entryId }
|
|
5
|
-
: { kind: "content_block", entryId: target.entryId, blockIndex: target.blockIndex };
|
|
6
|
-
}
|
|
7
|
-
export class ContextDeletionMemoryStore {
|
|
8
|
-
constructor(transcript) {
|
|
9
|
-
this.deletionTargets = [];
|
|
10
|
-
this.callCount = 0;
|
|
11
|
-
const entryIds = new Set();
|
|
12
|
-
const blockKeys = new Set();
|
|
13
|
-
this.entries = transcript.entries.map((entry) => {
|
|
14
|
-
if (entryIds.has(entry.entryId)) {
|
|
15
|
-
throw new Error(`Duplicate transcript entry id: ${entry.entryId}`);
|
|
16
|
-
}
|
|
17
|
-
entryIds.add(entry.entryId);
|
|
18
|
-
return {
|
|
19
|
-
entryId: entry.entryId,
|
|
20
|
-
role: entry.role,
|
|
21
|
-
protected: entry.protected,
|
|
22
|
-
hasAssistantThinkingBlocks: assistantEntryHasThinkingContentBlock(entry),
|
|
23
|
-
tokenEstimate: entry.tokenEstimate,
|
|
24
|
-
text: entry.text,
|
|
25
|
-
};
|
|
26
|
-
});
|
|
27
|
-
this.entriesById = new Map(this.entries.map((entry) => [entry.entryId, entry]));
|
|
28
|
-
this.contentBlocks = transcript.entries.flatMap((entry, entryPosition) => {
|
|
29
|
-
const hasAssistantThinkingBlocks = assistantEntryHasThinkingContentBlock(entry);
|
|
30
|
-
return entry.contentBlocks.map((block) => {
|
|
31
|
-
if (block.entryId !== entry.entryId) {
|
|
32
|
-
throw new Error(`Transcript content block ${block.entryId}:${block.blockIndex} does not belong to entry ${entry.entryId}`);
|
|
33
|
-
}
|
|
34
|
-
const blockKey = `${block.entryId}:${block.blockIndex}`;
|
|
35
|
-
if (blockKeys.has(blockKey)) {
|
|
36
|
-
throw new Error(`Duplicate transcript content block: ${blockKey}`);
|
|
37
|
-
}
|
|
38
|
-
blockKeys.add(blockKey);
|
|
39
|
-
return {
|
|
40
|
-
entryPosition,
|
|
41
|
-
entryId: block.entryId,
|
|
42
|
-
blockIndex: block.blockIndex,
|
|
43
|
-
role: entry.role,
|
|
44
|
-
type: block.type,
|
|
45
|
-
protected: block.protected,
|
|
46
|
-
hasAssistantThinkingBlocks,
|
|
47
|
-
tokenEstimate: block.tokenEstimate,
|
|
48
|
-
text: block.text,
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
this.contentBlockCountByEntryId = new Map();
|
|
53
|
-
for (const block of this.contentBlocks) {
|
|
54
|
-
this.contentBlockCountByEntryId.set(block.entryId, (this.contentBlockCountByEntryId.get(block.entryId) ?? 0) + 1);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
transaction(operation) {
|
|
58
|
-
const snapshot = this.snapshot();
|
|
59
|
-
try {
|
|
60
|
-
return operation();
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
this.restore(snapshot);
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
readTargets() {
|
|
68
|
-
return this.deletionTargets.map(copyDeletionTarget);
|
|
69
|
-
}
|
|
70
|
-
replaceTargets(targets) {
|
|
71
|
-
this.deletionTargets = targets.map(copyDeletionTarget);
|
|
72
|
-
}
|
|
73
|
-
listEntriesForGrep() {
|
|
74
|
-
return this.entries.map((entry) => ({
|
|
75
|
-
entry_id: entry.entryId,
|
|
76
|
-
text: entry.text,
|
|
77
|
-
is_protected: entry.protected ? 1 : 0,
|
|
78
|
-
has_assistant_thinking_blocks: entry.hasAssistantThinkingBlocks ? 1 : 0,
|
|
79
|
-
}));
|
|
80
|
-
}
|
|
81
|
-
listContentBlocksForGrep() {
|
|
82
|
-
return [...this.contentBlocks]
|
|
83
|
-
.sort((a, b) => a.entryPosition - b.entryPosition || a.blockIndex - b.blockIndex)
|
|
84
|
-
.map((block) => ({
|
|
85
|
-
entry_id: block.entryId,
|
|
86
|
-
block_index: block.blockIndex,
|
|
87
|
-
role: block.role,
|
|
88
|
-
type: block.type,
|
|
89
|
-
text: block.text,
|
|
90
|
-
entry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,
|
|
91
|
-
block_protected: block.protected ? 1 : 0,
|
|
92
|
-
block_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,
|
|
93
|
-
has_assistant_thinking_blocks: block.hasAssistantThinkingBlocks ? 1 : 0,
|
|
94
|
-
}));
|
|
95
|
-
}
|
|
96
|
-
getEntryForRead(entryId) {
|
|
97
|
-
const entry = this.entriesById.get(entryId);
|
|
98
|
-
if (!entry)
|
|
99
|
-
return undefined;
|
|
100
|
-
return {
|
|
101
|
-
entry_id: entry.entryId,
|
|
102
|
-
role: entry.role,
|
|
103
|
-
is_protected: entry.protected ? 1 : 0,
|
|
104
|
-
has_assistant_thinking_blocks: entry.hasAssistantThinkingBlocks ? 1 : 0,
|
|
105
|
-
token_estimate: entry.tokenEstimate,
|
|
106
|
-
text: entry.text,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
getContentBlockForRead(entryId, blockIndex) {
|
|
110
|
-
const block = this.contentBlocks.find((candidate) => candidate.entryId === entryId && candidate.blockIndex === blockIndex);
|
|
111
|
-
if (!block)
|
|
112
|
-
return undefined;
|
|
113
|
-
return {
|
|
114
|
-
entry_id: block.entryId,
|
|
115
|
-
block_index: block.blockIndex,
|
|
116
|
-
role: block.role,
|
|
117
|
-
type: block.type,
|
|
118
|
-
token_estimate: block.tokenEstimate,
|
|
119
|
-
text: block.text,
|
|
120
|
-
entry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,
|
|
121
|
-
block_protected: block.protected ? 1 : 0,
|
|
122
|
-
block_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,
|
|
123
|
-
has_assistant_thinking_blocks: block.hasAssistantThinkingBlocks ? 1 : 0,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
getGrepScanTextLength(target) {
|
|
127
|
-
const texts = target === "entry" ? this.entries : this.contentBlocks;
|
|
128
|
-
return texts.reduce((sum, item) => sum + item.text.length, 0);
|
|
129
|
-
}
|
|
130
|
-
incrementCallCount() {
|
|
131
|
-
this.callCount += 1;
|
|
132
|
-
return this.callCount;
|
|
133
|
-
}
|
|
134
|
-
getCallCount() {
|
|
135
|
-
return this.callCount;
|
|
136
|
-
}
|
|
137
|
-
setLastError(message) {
|
|
138
|
-
this.lastError = message;
|
|
139
|
-
}
|
|
140
|
-
clearLastError() {
|
|
141
|
-
this.lastError = undefined;
|
|
142
|
-
}
|
|
143
|
-
getLastError() {
|
|
144
|
-
return this.lastError;
|
|
145
|
-
}
|
|
146
|
-
snapshot() {
|
|
147
|
-
return {
|
|
148
|
-
deletionTargets: this.readTargets(),
|
|
149
|
-
callCount: this.callCount,
|
|
150
|
-
...(this.lastError === undefined ? {} : { lastError: this.lastError }),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
restore(snapshot) {
|
|
154
|
-
this.deletionTargets = snapshot.deletionTargets.map(copyDeletionTarget);
|
|
155
|
-
this.callCount = snapshot.callCount;
|
|
156
|
-
this.lastError = snapshot.lastError;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
export function createContextDeletionStore(transcript) {
|
|
160
|
-
return new ContextDeletionMemoryStore(transcript);
|
|
161
|
-
}
|
|
162
|
-
//# sourceMappingURL=context-deletion-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context-deletion-store.js","sourceRoot":"","sources":["../../../src/core/compaction/context-deletion-store.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,qCAAqC,EAAE,MAAM,8BAA8B,CAAC;AAyDrF,SAAS,kBAAkB,CAAC,MAA6B;IACxD,OAAO,MAAM,CAAC,IAAI,KAAK,OAAO;QAC7B,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE;QAC5C,CAAC,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC;AACtF,CAAC;AAED,MAAM,OAAO,0BAA0B;IAStC,YAAY,UAAiC;QAJrC,oBAAe,GAA4B,EAAE,CAAC;QAC9C,cAAS,GAAG,CAAC,CAAC;QAIrB,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;QACnC,MAAM,SAAS,GAAG,IAAI,GAAG,EAAU,CAAC;QACpC,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YAC/C,IAAI,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;gBACjC,MAAM,IAAI,KAAK,CAAC,kCAAkC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YACpE,CAAC;YACD,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC5B,OAAO;gBACN,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,IAAI,EAAE,KAAK,CAAC,IAAI;gBAChB,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B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","sourcesContent":["import type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport type { ContextDeletionTarget } from \"../session-manager.ts\";\nimport type { CompactableTranscript } from \"./context-compaction-types.ts\";\nimport { assistantEntryHasThinkingContentBlock } from \"./context-assistant-turns.js\";\n\nexport interface EntryTextRow {\n\tentry_id: string;\n\ttext: string;\n\tis_protected: number;\n\thas_assistant_thinking_blocks: number;\n}\n\nexport interface EntryReadRow extends EntryTextRow {\n\trole: string;\n\ttoken_estimate: number;\n}\n\nexport interface ContentBlockTextRow {\n\tentry_id: string;\n\tblock_index: number;\n\trole: AgentMessage[\"role\"];\n\ttype: string;\n\ttext: string;\n\tentry_protected: number;\n\tblock_protected: number;\n\tblock_count: number;\n\thas_assistant_thinking_blocks: number;\n}\n\nexport interface ContentBlockReadRow extends ContentBlockTextRow {\n\ttoken_estimate: number;\n}\n\nexport interface StoredTranscriptEntry {\n\tentryId: string;\n\trole: AgentMessage[\"role\"];\n\tprotected: boolean;\n\thasAssistantThinkingBlocks: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\nexport interface StoredContentBlock {\n\tentryPosition: number;\n\tentryId: string;\n\tblockIndex: number;\n\trole: AgentMessage[\"role\"];\n\ttype: string;\n\tprotected: boolean;\n\thasAssistantThinkingBlocks: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\nexport interface ContextDeletionMemorySnapshot {\n\tdeletionTargets: ContextDeletionTarget[];\n\tcallCount: number;\n\tlastError?: string;\n}\n\nfunction copyDeletionTarget(target: ContextDeletionTarget): ContextDeletionTarget {\n\treturn target.kind === \"entry\"\n\t\t? { kind: \"entry\", entryId: target.entryId }\n\t\t: { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex };\n}\n\nexport class ContextDeletionMemoryStore {\n\tprivate readonly entries: StoredTranscriptEntry[];\n\tprivate readonly entriesById: Map<string, StoredTranscriptEntry>;\n\tprivate readonly contentBlocks: StoredContentBlock[];\n\tprivate readonly contentBlockCountByEntryId: Map<string, number>;\n\tprivate deletionTargets: ContextDeletionTarget[] = [];\n\tprivate callCount = 0;\n\tprivate lastError: string | undefined;\n\n\tconstructor(transcript: CompactableTranscript) {\n\t\tconst entryIds = new Set<string>();\n\t\tconst blockKeys = new Set<string>();\n\t\tthis.entries = transcript.entries.map((entry) => {\n\t\t\tif (entryIds.has(entry.entryId)) {\n\t\t\t\tthrow new Error(`Duplicate transcript entry id: ${entry.entryId}`);\n\t\t\t}\n\t\t\tentryIds.add(entry.entryId);\n\t\t\treturn {\n\t\t\t\tentryId: entry.entryId,\n\t\t\t\trole: entry.role,\n\t\t\t\tprotected: entry.protected,\n\t\t\t\thasAssistantThinkingBlocks: assistantEntryHasThinkingContentBlock(entry),\n\t\t\t\ttokenEstimate: entry.tokenEstimate,\n\t\t\t\ttext: entry.text,\n\t\t\t};\n\t\t});\n\t\tthis.entriesById = new Map<string, StoredTranscriptEntry>(this.entries.map((entry) => [entry.entryId, entry] as const));\n\t\tthis.contentBlocks = transcript.entries.flatMap((entry, entryPosition) => {\n\t\t\tconst hasAssistantThinkingBlocks = assistantEntryHasThinkingContentBlock(entry);\n\t\t\treturn entry.contentBlocks.map((block) => {\n\t\t\t\tif (block.entryId !== entry.entryId) {\n\t\t\t\t\tthrow new Error(`Transcript content block ${block.entryId}:${block.blockIndex} does not belong to entry ${entry.entryId}`);\n\t\t\t\t}\n\t\t\t\tconst blockKey = `${block.entryId}:${block.blockIndex}`;\n\t\t\t\tif (blockKeys.has(blockKey)) {\n\t\t\t\t\tthrow new Error(`Duplicate transcript content block: ${blockKey}`);\n\t\t\t\t}\n\t\t\t\tblockKeys.add(blockKey);\n\t\t\t\treturn {\n\t\t\t\t\tentryPosition,\n\t\t\t\t\tentryId: block.entryId,\n\t\t\t\t\tblockIndex: block.blockIndex,\n\t\t\t\t\trole: entry.role,\n\t\t\t\t\ttype: block.type,\n\t\t\t\t\tprotected: block.protected,\n\t\t\t\t\thasAssistantThinkingBlocks,\n\t\t\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\t\t\ttext: block.text,\n\t\t\t\t};\n\t\t\t});\n\t\t});\n\t\tthis.contentBlockCountByEntryId = new Map();\n\t\tfor (const block of this.contentBlocks) {\n\t\t\tthis.contentBlockCountByEntryId.set(block.entryId, (this.contentBlockCountByEntryId.get(block.entryId) ?? 0) + 1);\n\t\t}\n\t}\n\n\ttransaction<T>(operation: () => T): T {\n\t\tconst snapshot = this.snapshot();\n\t\ttry {\n\t\t\treturn operation();\n\t\t} catch (error) {\n\t\t\tthis.restore(snapshot);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\treadTargets(): ContextDeletionTarget[] {\n\t\treturn this.deletionTargets.map(copyDeletionTarget);\n\t}\n\n\treplaceTargets(targets: readonly ContextDeletionTarget[]): void {\n\t\tthis.deletionTargets = targets.map(copyDeletionTarget);\n\t}\n\n\tlistEntriesForGrep(): EntryTextRow[] {\n\t\treturn this.entries.map((entry) => ({\n\t\t\tentry_id: entry.entryId,\n\t\t\ttext: entry.text,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t\thas_assistant_thinking_blocks: entry.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t}));\n\t}\n\n\tlistContentBlocksForGrep(): ContentBlockTextRow[] {\n\t\treturn [...this.contentBlocks]\n\t\t\t.sort((a, b) => a.entryPosition - b.entryPosition || a.blockIndex - b.blockIndex)\n\t\t\t.map((block) => ({\n\t\t\t\tentry_id: block.entryId,\n\t\t\t\tblock_index: block.blockIndex,\n\t\t\t\trole: block.role,\n\t\t\t\ttype: block.type,\n\t\t\t\ttext: block.text,\n\t\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t\t\thas_assistant_thinking_blocks: block.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t\t}));\n\t}\n\n\tgetEntryForRead(entryId: string): EntryReadRow | undefined {\n\t\tconst entry = this.entriesById.get(entryId);\n\t\tif (!entry) return undefined;\n\t\treturn {\n\t\t\tentry_id: entry.entryId,\n\t\t\trole: entry.role,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t\thas_assistant_thinking_blocks: entry.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t\ttoken_estimate: entry.tokenEstimate,\n\t\t\ttext: entry.text,\n\t\t};\n\t}\n\n\tgetContentBlockForRead(entryId: string, blockIndex: number): ContentBlockReadRow | undefined {\n\t\tconst block = this.contentBlocks.find((candidate) => candidate.entryId === entryId && candidate.blockIndex === blockIndex);\n\t\tif (!block) return undefined;\n\t\treturn {\n\t\t\tentry_id: block.entryId,\n\t\t\tblock_index: block.blockIndex,\n\t\t\trole: block.role,\n\t\t\ttype: block.type,\n\t\t\ttoken_estimate: block.tokenEstimate,\n\t\t\ttext: block.text,\n\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t\thas_assistant_thinking_blocks: block.hasAssistantThinkingBlocks ? 1 : 0,\n\t\t};\n\t}\n\n\tgetGrepScanTextLength(target: \"entry\" | \"content_block\"): number {\n\t\tconst texts = target === \"entry\" ? this.entries : this.contentBlocks;\n\t\treturn texts.reduce((sum, item) => sum + item.text.length, 0);\n\t}\n\n\tincrementCallCount(): number {\n\t\tthis.callCount += 1;\n\t\treturn this.callCount;\n\t}\n\n\tgetCallCount(): number {\n\t\treturn this.callCount;\n\t}\n\n\tsetLastError(message: string): void {\n\t\tthis.lastError = message;\n\t}\n\n\tclearLastError(): void {\n\t\tthis.lastError = undefined;\n\t}\n\n\tgetLastError(): string | undefined {\n\t\treturn this.lastError;\n\t}\n\n\tprivate snapshot(): ContextDeletionMemorySnapshot {\n\t\treturn {\n\t\t\tdeletionTargets: this.readTargets(),\n\t\t\tcallCount: this.callCount,\n\t\t\t...(this.lastError === undefined ? {} : { lastError: this.lastError }),\n\t\t};\n\t}\n\n\tprivate restore(snapshot: ContextDeletionMemorySnapshot): void {\n\t\tthis.deletionTargets = snapshot.deletionTargets.map(copyDeletionTarget);\n\t\tthis.callCount = snapshot.callCount;\n\t\tthis.lastError = snapshot.lastError;\n\t}\n}\n\nexport function createContextDeletionStore(transcript: CompactableTranscript): ContextDeletionMemoryStore {\n\treturn new ContextDeletionMemoryStore(transcript);\n}\n"]}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { ContextDeletionTarget } from "../session-manager.ts";
|
|
2
|
-
import type { CompactableContentBlock, CompactableTranscript, CompactableTranscriptEntry, ContextDeletionRequest } from "./context-compaction-types.ts";
|
|
3
|
-
export declare function targetKey(target: ContextDeletionTarget): string;
|
|
4
|
-
export declare function rawTargetKey(target: ContextDeletionRequest["deletions"][number]): string;
|
|
5
|
-
export declare function normalizeRawTarget(target: ContextDeletionRequest["deletions"][number]): ContextDeletionTarget;
|
|
6
|
-
export declare function assertIdOnlyDeletionTarget(target: Record<string, unknown>): void;
|
|
7
|
-
export declare function rawDeletionFromTarget(target: ContextDeletionTarget): ContextDeletionRequest["deletions"][number];
|
|
8
|
-
export declare function deletionRequestFromTargets(targets: readonly ContextDeletionTarget[]): ContextDeletionRequest;
|
|
9
|
-
export declare function getDeletedEntryIds(targets: readonly ContextDeletionTarget[]): Set<string>;
|
|
10
|
-
export declare function getDeletedContentBlocks(targets: readonly ContextDeletionTarget[]): Map<string, Set<number>>;
|
|
11
|
-
export declare function recentContextEntryBoundary(transcript: CompactableTranscript): number;
|
|
12
|
-
export declare function getRecentContextEntryIds(transcript: CompactableTranscript): Set<string>;
|
|
13
|
-
export declare function isRecentContextEntry(entry: CompactableTranscriptEntry, transcript: CompactableTranscript): boolean;
|
|
14
|
-
export declare function formatRecentContextDeletionError(transcript: CompactableTranscript, target: ContextDeletionTarget): string;
|
|
15
|
-
export declare function deletionGuidance(): string;
|
|
16
|
-
export declare function findTranscriptEntry(transcript: CompactableTranscript, entryId: string): CompactableTranscriptEntry | undefined;
|
|
17
|
-
export declare function findTranscriptContentBlock(transcript: CompactableTranscript, target: ContextDeletionTarget): CompactableContentBlock | undefined;
|
|
18
|
-
export declare function firstToolCallBlockTarget(entry: CompactableTranscriptEntry, callId: string): ContextDeletionTarget | undefined;
|
|
19
|
-
export declare function formatProtectedDeletionError(transcript: CompactableTranscript, target: ContextDeletionTarget): string;
|
|
20
|
-
export declare function formatProtectedToolDependencyError(transcript: CompactableTranscript, blockedTarget: ContextDeletionTarget, context: string): string;
|
|
21
|
-
export declare function isProtectedContextDeletionErrorMessage(message: string): boolean;
|
|
22
|
-
export declare function assertNoRecentContextDeletionTargets(transcript: CompactableTranscript, targets: readonly ContextDeletionTarget[]): void;
|
|
23
|
-
export declare function latestAssistantEntry(transcript: CompactableTranscript, deletedEntryIds?: ReadonlySet<string>): CompactableTranscriptEntry | undefined;
|
|
24
|
-
export declare function findAssistantThinkingContentBlockDeletionViolation(transcript: CompactableTranscript, targets: readonly ContextDeletionTarget[]): Extract<ContextDeletionTarget, {
|
|
25
|
-
kind: "content_block";
|
|
26
|
-
}> | undefined;
|
|
27
|
-
export declare function findLatestAssistantThinkingDeletionViolation(transcript: CompactableTranscript, targets: readonly ContextDeletionTarget[]): ContextDeletionTarget | undefined;
|
|
28
|
-
export declare function assertNoAssistantThinkingContentBlockDeletionTargets(transcript: CompactableTranscript, targets: readonly ContextDeletionTarget[]): void;
|
|
29
|
-
export declare function assertNoLatestAssistantThinkingDeletionTargets(transcript: CompactableTranscript, targets: readonly ContextDeletionTarget[]): void;
|
|
30
|
-
export declare function isToolCallBlockDeleted(entry: CompactableTranscriptEntry, callId: string, deletedEntryIds: ReadonlySet<string>, deletedContentBlocks: ReadonlyMap<string, ReadonlySet<number>>): boolean;
|
|
31
|
-
export declare function toolCallBlockIndexes(entry: CompactableTranscriptEntry, callId: string): number[];
|
|
32
|
-
export declare function addTarget(targets: ContextDeletionTarget[], target: ContextDeletionTarget): boolean;
|
|
33
|
-
export declare function deleteEntryTarget(targets: ContextDeletionTarget[], entryId: string): boolean;
|
|
34
|
-
export declare function mergeContextDeletionTargets(baseTargets: readonly ContextDeletionTarget[], additionalTargets: readonly ContextDeletionTarget[]): ContextDeletionTarget[];
|
|
35
|
-
export declare function canonicalizeEntryTargets(transcript: CompactableTranscript, targets: ContextDeletionTarget[], entry: CompactableTranscriptEntry): boolean;
|
|
36
|
-
export declare function addToolCallDeletion(transcript: CompactableTranscript, targets: ContextDeletionTarget[], entry: CompactableTranscriptEntry, callId: string): boolean;
|
|
37
|
-
/** Whether a task-bearing entry remains provider-visible after proposed block deletions. */
|
|
38
|
-
export declare function isTaskBearingEntry(entry: CompactableTranscriptEntry, deletedContentBlocks?: ReadonlySet<number>): boolean;
|
|
39
|
-
export declare function isRecentTarget(transcript: CompactableTranscript, target: ContextDeletionTarget): boolean;
|
|
40
|
-
export declare function isStaleUserImageOnlyEntry(transcript: CompactableTranscript, entry: CompactableTranscriptEntry): boolean;
|
|
41
|
-
export declare function canDeleteStaleUserImageContentBlock(transcript: CompactableTranscript, target: ContextDeletionTarget): boolean;
|
|
42
|
-
export declare function canDeleteTarget(transcript: CompactableTranscript, target: ContextDeletionTarget): boolean;
|
|
43
|
-
//# sourceMappingURL=context-deletion-targets.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context-deletion-targets.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/context-deletion-targets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,KAAK,EACX,uBAAuB,EACvB,qBAAqB,EACrB,0BAA0B,EAC1B,sBAAsB,EACtB,MAAM,+BAA+B,CAAC;AA8BvC,wBAAgB,SAAS,CAAC,MAAM,EAAE,qBAAqB,GAAG,MAAM,CAE/D;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,sBAAsB,CAAC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wBAAgB,eAAe,CAAC,UAAU,EAAE,qBAAqB,EAAE,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAUzG","sourcesContent":["import type { ContextDeletionTarget } from \"../session-manager.ts\";\nimport type {\n\tCompactableContentBlock,\n\tCompactableTranscript,\n\tCompactableTranscriptEntry,\n\tContextDeletionRequest,\n} from \"./context-compaction-types.ts\";\nimport { getTranscriptCompactionParameters } from \"./context-compaction-strategy.ts\";\nimport {\n\tanalyzeCompactableAssistantTurns,\n\tassistantEntryHasThinkingContentBlock,\n\tcompactableContentBlockIsLlmVisible,\n\ttranscriptEntryStartsNewTurn,\n} from \"./context-assistant-turns.js\";\n\ninterface AssistantTurnDeletionViolation {\n\tentryId: string;\n\tactive: boolean;\n}\n\nfunction findAssistantTurnDeletionViolation(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): AssistantTurnDeletionViolation | undefined {\n\tconst { deletedEntryIds, turns } = analyzeCompactableAssistantTurns(transcript, targets);\n\tfor (const turn of turns) {\n\t\tif (turn.signedThinkingEntryIds.length === 0) continue;\n\t\tconst deleted = turn.signedThinkingEntryIds.filter((entryId) => deletedEntryIds.has(entryId));\n\t\tif (turn.active && deleted.length > 0) return { entryId: deleted[0]!, active: true };\n\t\tif (!turn.active && deleted.length > 0 && deleted.length < turn.signedThinkingEntryIds.length) {\n\t\t\treturn { entryId: deleted[0]!, active: false };\n\t\t}\n\t}\n\treturn undefined;\n}\n\nexport function targetKey(target: ContextDeletionTarget): string {\n\treturn target.kind === \"entry\" ? `entry:${target.entryId}` : `content_block:${target.entryId}:${target.blockIndex}`;\n}\n\nexport function rawTargetKey(target: ContextDeletionRequest[\"deletions\"][number]): string {\n\treturn target.kind === \"entry\" ? `entry:${target.entryId}` : `content_block:${target.entryId}:${target.blockIndex}`;\n}\n\nexport function normalizeRawTarget(target: ContextDeletionRequest[\"deletions\"][number]): ContextDeletionTarget {\n\tif (target.kind === \"entry\") return { kind: \"entry\", entryId: target.entryId };\n\treturn { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex as number };\n}\n\nexport function assertIdOnlyDeletionTarget(target: Record<string, unknown>): void {\n\tconst allowedKeys = target.kind === \"content_block\" ? new Set([\"kind\", \"entryId\", \"blockIndex\"]) : new Set([\"kind\", \"entryId\"]);\n\tfor (const key of Object.keys(target)) {\n\t\tif (!allowedKeys.has(key)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Deletion target includes unsupported property ${JSON.stringify(key)}; context deletion targets are id-only and must contain only kind, entryId${target.kind === \"content_block\" ? \", and blockIndex\" : \"\"}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\nexport function rawDeletionFromTarget(target: ContextDeletionTarget): ContextDeletionRequest[\"deletions\"][number] {\n\tif (target.kind === \"entry\") return { kind: \"entry\", entryId: target.entryId };\n\treturn { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex };\n}\n\nexport function deletionRequestFromTargets(targets: readonly ContextDeletionTarget[]): ContextDeletionRequest {\n\treturn { deletions: targets.map(rawDeletionFromTarget) };\n}\n\nexport function getDeletedEntryIds(targets: readonly ContextDeletionTarget[]): Set<string> {\n\treturn new Set(targets.filter((target) => target.kind === \"entry\").map((target) => target.entryId));\n}\n\nexport function getDeletedContentBlocks(targets: readonly ContextDeletionTarget[]): Map<string, Set<number>> {\n\tconst blocksByEntry = new Map<string, Set<number>>();\n\tfor (const target of targets) {\n\t\tif (target.kind !== \"content_block\") continue;\n\t\tconst blocks = blocksByEntry.get(target.entryId) ?? new Set<number>();\n\t\tblocks.add(target.blockIndex);\n\t\tblocksByEntry.set(target.entryId, blocks);\n\t}\n\treturn blocksByEntry;\n}\n\nexport function recentContextEntryBoundary(transcript: CompactableTranscript): number {\n\tconst { preserve_recent } = getTranscriptCompactionParameters(transcript);\n\treturn preserve_recent > 0 ? Math.max(0, transcript.entries.length - preserve_recent) : transcript.entries.length;\n}\n\nexport function getRecentContextEntryIds(transcript: CompactableTranscript): Set<string> {\n\tconst { preserve_recent } = getTranscriptCompactionParameters(transcript);\n\tif (preserve_recent <= 0) return new Set();\n\treturn new Set(transcript.entries.slice(recentContextEntryBoundary(transcript)).map((entry) => entry.entryId));\n}\n\nexport function isRecentContextEntry(entry: CompactableTranscriptEntry, transcript: CompactableTranscript): boolean {\n\tconst { preserve_recent } = getTranscriptCompactionParameters(transcript);\n\tif (preserve_recent <= 0) return false;\n\tconst entryIndex = transcript.entries.findIndex((candidate) => candidate.entryId === entry.entryId);\n\treturn entryIndex >= 0 && entryIndex >= recentContextEntryBoundary(transcript);\n}\n\nexport function formatRecentContextDeletionError(transcript: CompactableTranscript, target: ContextDeletionTarget): string {\n\tconst { preserve_recent } = getTranscriptCompactionParameters(transcript);\n\tconst recentWindow = `last ${preserve_recent} context ${preserve_recent === 1 ? \"entry\" : \"entries\"}`;\n\tif (target.kind === \"entry\") {\n\t\treturn `Cannot delete recent context entry ${target.entryId} because the ${recentWindow} must remain available for active continuity. Choose an older entry.`;\n\t}\n\treturn `Cannot delete content block ${target.entryId}:${target.blockIndex} because entry ${target.entryId} is one of the ${recentWindow} that must remain available for active continuity. Choose an older entry or content block.`;\n}\n\nexport function deletionGuidance(): string {\n\treturn \"Choose another deletion candidate.\";\n}\n\nexport function findTranscriptEntry(transcript: CompactableTranscript, entryId: string): CompactableTranscriptEntry | undefined {\n\treturn transcript.entries.find((entry) => entry.entryId === entryId);\n}\n\nexport function findTranscriptContentBlock(\n\ttranscript: CompactableTranscript,\n\ttarget: ContextDeletionTarget,\n): CompactableContentBlock | undefined {\n\tif (target.kind !== \"content_block\") return undefined;\n\treturn findTranscriptEntry(transcript, target.entryId)?.contentBlocks.find((block) => block.blockIndex === target.blockIndex);\n}\n\nexport function firstToolCallBlockTarget(\n\tentry: CompactableTranscriptEntry,\n\tcallId: string,\n): ContextDeletionTarget | undefined {\n\tconst blockIndex = toolCallBlockIndexes(entry, callId)[0];\n\treturn blockIndex === undefined ? undefined : { kind: \"content_block\", entryId: entry.entryId, blockIndex };\n}\n\nexport function formatProtectedDeletionError(transcript: CompactableTranscript, target: ContextDeletionTarget): string {\n\tconst entry = findTranscriptEntry(transcript, target.entryId);\n\tif (target.kind === \"entry\") {\n\t\tconst toolResultSuffix = entry?.toolResultFor ? ` for tool call ${entry.toolResultFor}` : \"\";\n\t\tconst toolCallSuffix = entry && entry.toolCallIds.length > 0 ? ` containing tool call ${entry.toolCallIds.join(\", \")}` : \"\";\n\t\treturn `Deletion target ${target.entryId}${toolResultSuffix}${toolCallSuffix} is protected. ${deletionGuidance()}`;\n\t}\n\n\tconst block = findTranscriptContentBlock(transcript, target);\n\tconst toolBlockSuffix = block?.toolCallId ? ` It is a protected tool block for tool call ${block.toolCallId}.` : \"\";\n\treturn `Content block ${target.entryId}:${target.blockIndex} is protected.${toolBlockSuffix} ${deletionGuidance()}`;\n}\n\nexport function formatProtectedToolDependencyError(\n\ttranscript: CompactableTranscript,\n\tblockedTarget: ContextDeletionTarget,\n\tcontext: string,\n): string {\n\tconst protectedMessage = formatProtectedDeletionError(transcript, blockedTarget);\n\treturn `${context} ${protectedMessage}`;\n}\n\n// Keep legacy protection phrases recognizable for callers handling errors\n// produced by older compaction plans and extension versions.\nexport function isProtectedContextDeletionErrorMessage(message: string): boolean {\n\treturn (\n\t\t/\\bprotected\\b/i.test(message) ||\n\t\t/Cannot delete (?:recent context entry|content block .* because entry .* is one of the last)/u.test(message) ||\n\t\t/latest assistant message|active assistant tool-use turn|completed assistant tool-use turn|thinking\\/redacted_thinking block in (?:the latest|a retained) assistant message/u.test(message)\n\t);\n}\n\nexport function assertNoRecentContextDeletionTargets(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): void {\n\tconst recentEntryIds = getRecentContextEntryIds(transcript);\n\tfor (const target of targets) {\n\t\tif (recentEntryIds.has(target.entryId)) {\n\t\t\tthrow new Error(formatRecentContextDeletionError(transcript, target));\n\t\t}\n\t}\n}\n\nexport function latestAssistantEntry(\n\ttranscript: CompactableTranscript,\n\tdeletedEntryIds: ReadonlySet<string> = new Set<string>(),\n): CompactableTranscriptEntry | undefined {\n\tfor (let index = transcript.entries.length - 1; index >= 0; index--) {\n\t\tconst entry = transcript.entries[index];\n\t\tif (entry.role === \"assistant\" && !deletedEntryIds.has(entry.entryId)) return entry;\n\t}\n\treturn undefined;\n}\n\nexport function findAssistantThinkingContentBlockDeletionViolation(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): Extract<ContextDeletionTarget, { kind: \"content_block\" }> | undefined {\n\tconst deletedEntryIds = getDeletedEntryIds(targets);\n\tfor (const target of targets) {\n\t\tif (target.kind !== \"content_block\") continue;\n\t\tif (deletedEntryIds.has(target.entryId)) continue;\n\t\tconst entry = findTranscriptEntry(transcript, target.entryId);\n\t\tif (entry && assistantEntryHasThinkingContentBlock(entry)) return target;\n\t}\n\treturn undefined;\n}\n\nexport function findLatestAssistantThinkingDeletionViolation(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): ContextDeletionTarget | undefined {\n\tconst violation = findAssistantTurnDeletionViolation(transcript, targets);\n\tif (!violation) return undefined;\n\treturn targets.find((target) => target.kind === \"entry\" && target.entryId === violation.entryId);\n}\n\nexport function assertNoAssistantThinkingContentBlockDeletionTargets(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): void {\n\tconst violation = findAssistantThinkingContentBlockDeletionViolation(transcript, targets);\n\tif (!violation) return;\n\tthrow new Error(\n\t\t`Cannot delete content block ${violation.entryId}:${violation.blockIndex} because a thinking/redacted_thinking block in a retained assistant message must remain unmodified; retained assistant messages containing thinking/redacted_thinking content blocks are all-or-nothing`,\n\t);\n}\n\nexport function assertNoLatestAssistantThinkingDeletionTargets(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): void {\n\tconst violation = findAssistantTurnDeletionViolation(transcript, targets);\n\tif (!violation) return;\n\tif (violation.active) {\n\t\tthrow new Error(\n\t\t\t`Cannot delete assistant entry ${violation.entryId} because the active assistant tool-use turn must retain every thinking/redacted_thinking-bearing assistant entry`,\n\t\t);\n\t}\n\tthrow new Error(\n\t\t`Cannot delete assistant entry ${violation.entryId} because a completed assistant tool-use turn must retain all or omit all thinking/redacted_thinking-bearing assistant entries`,\n\t);\n}\n\nexport function isToolCallBlockDeleted(\n\tentry: CompactableTranscriptEntry,\n\tcallId: string,\n\tdeletedEntryIds: ReadonlySet<string>,\n\tdeletedContentBlocks: ReadonlyMap<string, ReadonlySet<number>>,\n): boolean {\n\tif (deletedEntryIds.has(entry.entryId)) return true;\n\tconst deletedBlocks = deletedContentBlocks.get(entry.entryId);\n\tif (!deletedBlocks) return false;\n\treturn entry.contentBlocks.some((block) => block.toolCallId === callId && deletedBlocks.has(block.blockIndex));\n}\n\nexport function toolCallBlockIndexes(entry: CompactableTranscriptEntry, callId: string): number[] {\n\treturn entry.contentBlocks\n\t\t.filter((block) => block.toolCallId === callId)\n\t\t.map((block) => block.blockIndex);\n}\n\nexport function addTarget(targets: ContextDeletionTarget[], target: ContextDeletionTarget): boolean {\n\tif (targets.some((existing) => targetKey(existing) === targetKey(target))) return false;\n\ttargets.push(target);\n\treturn true;\n}\n\nexport function deleteEntryTarget(targets: ContextDeletionTarget[], entryId: string): boolean {\n\tlet changed = false;\n\tfor (let index = targets.length - 1; index >= 0; index--) {\n\t\tconst target = targets[index];\n\t\tif (target.kind === \"content_block\" && target.entryId === entryId) {\n\t\t\ttargets.splice(index, 1);\n\t\t\tchanged = true;\n\t\t}\n\t}\n\treturn addTarget(targets, { kind: \"entry\", entryId }) || changed;\n}\n\nexport function mergeContextDeletionTargets(\n\tbaseTargets: readonly ContextDeletionTarget[],\n\tadditionalTargets: readonly ContextDeletionTarget[],\n): ContextDeletionTarget[] {\n\tconst targets = [...baseTargets];\n\tfor (const target of additionalTargets) {\n\t\tif (target.kind === \"entry\") {\n\t\t\tdeleteEntryTarget(targets, target.entryId);\n\t\t\tcontinue;\n\t\t}\n\t\tif (!getDeletedEntryIds(targets).has(target.entryId)) {\n\t\t\taddTarget(targets, target);\n\t\t}\n\t}\n\treturn targets;\n}\n\nexport function canonicalizeEntryTargets(\n\ttranscript: CompactableTranscript,\n\ttargets: ContextDeletionTarget[],\n\tentry: CompactableTranscriptEntry,\n): boolean {\n\tif (!canDeleteTarget(transcript, { kind: \"entry\", entryId: entry.entryId })) return false;\n\tif (getDeletedEntryIds(targets).has(entry.entryId)) return false;\n\tconst deletedBlocks = getDeletedContentBlocks(targets).get(entry.entryId);\n\tif (!deletedBlocks || !entry.contentBlocks.every((block) => deletedBlocks.has(block.blockIndex))) return false;\n\t// Only repair/promote when dependency reconciliation reaches this entry. Non-tool entries that\n\t// request every block individually stay invalid so the assistant must choose explicit entry deletion.\n\treturn deleteEntryTarget(targets, entry.entryId);\n}\n\nexport function addToolCallDeletion(\n\ttranscript: CompactableTranscript,\n\ttargets: ContextDeletionTarget[],\n\tentry: CompactableTranscriptEntry,\n\tcallId: string,\n): boolean {\n\tif (assistantEntryHasThinkingContentBlock(entry)) {\n\t\tif (!canDeleteTarget(transcript, { kind: \"entry\", entryId: entry.entryId })) return false;\n\t\treturn deleteEntryTarget(targets, entry.entryId);\n\t}\n\n\tlet changed = false;\n\tfor (const blockIndex of toolCallBlockIndexes(entry, callId)) {\n\t\tconst target: ContextDeletionTarget = { kind: \"content_block\", entryId: entry.entryId, blockIndex };\n\t\tif (!canDeleteTarget(transcript, target)) continue;\n\t\tif (!getDeletedEntryIds(targets).has(entry.entryId)) {\n\t\t\tchanged = addTarget(targets, target) || changed;\n\t\t}\n\t}\n\treturn canonicalizeEntryTargets(transcript, targets, entry) || changed;\n}\n\n/** Whether a task-bearing entry remains provider-visible after proposed block deletions. */\nexport function isTaskBearingEntry(\n\tentry: CompactableTranscriptEntry,\n\tdeletedContentBlocks: ReadonlySet<number> = new Set<number>(),\n): boolean {\n\tconst taskRole =\n\t\tentry.role === \"user\" ||\n\t\tentry.role === \"custom\" ||\n\t\tentry.role === \"branchSummary\" ||\n\t\tentry.entryType === \"branch_summary\";\n\treturn taskRole && transcriptEntryStartsNewTurn(entry, deletedContentBlocks);\n}\n\nexport function isRecentTarget(transcript: CompactableTranscript, target: ContextDeletionTarget): boolean {\n\tconst entry = transcript.entries.find((candidate) => candidate.entryId === target.entryId);\n\treturn entry !== undefined && isRecentContextEntry(entry, transcript);\n}\n\nexport function isStaleUserImageOnlyEntry(transcript: CompactableTranscript, entry: CompactableTranscriptEntry): boolean {\n\tif (entry.role !== \"user\" || isRecentContextEntry(entry, transcript)) return false;\n\tconst visibleBlocks = entry.contentBlocks.filter((block) => compactableContentBlockIsLlmVisible(entry, block));\n\tif (visibleBlocks.length === 0 || !visibleBlocks.every((block) => block.type === \"image\")) return false;\n\treturn transcript.entries.some(\n\t\t(candidate) => candidate.entryId !== entry.entryId && isTaskBearingEntry(candidate),\n\t);\n}\n\nexport function canDeleteStaleUserImageContentBlock(\n\ttranscript: CompactableTranscript,\n\ttarget: ContextDeletionTarget,\n): boolean {\n\tif (target.kind !== \"content_block\") return false;\n\tconst entry = transcript.entries.find((candidate) => candidate.entryId === target.entryId);\n\tif (!entry || entry.role !== \"user\" || isRecentTarget(transcript, target)) return false;\n\tconst block = entry.contentBlocks.find((candidate) => candidate.blockIndex === target.blockIndex);\n\tif (!block || block.type !== \"image\") return false;\n\treturn entry.contentBlocks.some(\n\t\t(candidate) =>\n\t\t\tcandidate.blockIndex !== target.blockIndex &&\n\t\t\tcandidate.type !== \"image\" &&\n\t\t\tcompactableContentBlockIsLlmVisible(entry, candidate),\n\t);\n}\n\nexport function canDeleteTarget(transcript: CompactableTranscript, target: ContextDeletionTarget): boolean {\n\tconst entry = transcript.entries.find((candidate) => candidate.entryId === target.entryId);\n\tif (!entry) return false;\n\tif (isRecentTarget(transcript, target)) return false;\n\tif (target.kind === \"entry\") return isStaleUserImageOnlyEntry(transcript, entry) || !entry.protected;\n\tconst block = entry.contentBlocks.find((candidate) => candidate.blockIndex === target.blockIndex);\n\tif (!block) return false;\n\tif (canDeleteStaleUserImageContentBlock(transcript, target)) return true;\n\tif (entry.protected) return false;\n\treturn !block.protected;\n}\n\n"]}
|