@caupulican/pi-adaptative 0.94.0 → 0.95.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/README.md +32 -13
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts +7 -4
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js +173 -293
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.ts +247 -392
- package/dist/bundled-resources/extensions/tmux-agent-manager/launch-profile.d.ts +52 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/launch-profile.d.ts.map +1 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/launch-profile.js +118 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/launch-profile.js.map +1 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/launch-profile.ts +169 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/templates/builder-validator.json +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/templates/provider-prompt-smoke.json +1 -1
- package/dist/bundled-resources/skills/pi-harness-learning/SKILL.md +13 -7
- package/dist/bundled-resources/skills/worker-profile-writer/SKILL.md +11 -11
- package/dist/core/agent-paths.d.ts +2 -0
- package/dist/core/agent-paths.d.ts.map +1 -1
- package/dist/core/agent-paths.js +7 -0
- package/dist/core/agent-paths.js.map +1 -1
- package/dist/core/agent-session.d.ts +11 -41
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +98 -161
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/autonomy/envelope-enforcement.d.ts +6 -1
- package/dist/core/autonomy/envelope-enforcement.d.ts.map +1 -1
- package/dist/core/autonomy/envelope-enforcement.js +26 -25
- package/dist/core/autonomy/envelope-enforcement.js.map +1 -1
- package/dist/core/autonomy/lane-private-paths.d.ts.map +1 -1
- package/dist/core/autonomy/lane-private-paths.js +2 -1
- package/dist/core/autonomy/lane-private-paths.js.map +1 -1
- package/dist/core/autonomy/lane-tool-surface.d.ts +8 -8
- package/dist/core/autonomy/lane-tool-surface.d.ts.map +1 -1
- package/dist/core/autonomy/lane-tool-surface.js +45 -30
- package/dist/core/autonomy/lane-tool-surface.js.map +1 -1
- package/dist/core/autonomy/worker-session-private-scope.d.ts +12 -0
- package/dist/core/autonomy/worker-session-private-scope.d.ts.map +1 -0
- package/dist/core/autonomy/worker-session-private-scope.js +42 -0
- package/dist/core/autonomy/worker-session-private-scope.js.map +1 -0
- package/dist/core/autonomy/worker-tool-adapter-registry.d.ts +65 -0
- package/dist/core/autonomy/worker-tool-adapter-registry.d.ts.map +1 -0
- package/dist/core/autonomy/worker-tool-adapter-registry.js +145 -0
- package/dist/core/autonomy/worker-tool-adapter-registry.js.map +1 -0
- package/dist/core/background-lane-controller.d.ts +9 -1
- package/dist/core/background-lane-controller.d.ts.map +1 -1
- package/dist/core/background-lane-controller.js +76 -6
- package/dist/core/background-lane-controller.js.map +1 -1
- package/dist/core/background-tool-task-controller.d.ts +23 -2
- package/dist/core/background-tool-task-controller.d.ts.map +1 -1
- package/dist/core/background-tool-task-controller.js +132 -17
- package/dist/core/background-tool-task-controller.js.map +1 -1
- package/dist/core/context/long-term-memory-trigger.d.ts +3 -1
- package/dist/core/context/long-term-memory-trigger.d.ts.map +1 -1
- package/dist/core/context/long-term-memory-trigger.js +1 -1
- package/dist/core/context/long-term-memory-trigger.js.map +1 -1
- package/dist/core/context/okf-memory-provider.d.ts +3 -0
- package/dist/core/context/okf-memory-provider.d.ts.map +1 -1
- package/dist/core/context/okf-memory-provider.js +66 -31
- package/dist/core/context/okf-memory-provider.js.map +1 -1
- package/dist/core/context/okf-memory.d.ts +18 -1
- package/dist/core/context/okf-memory.d.ts.map +1 -1
- package/dist/core/context/okf-memory.js +48 -0
- package/dist/core/context/okf-memory.js.map +1 -1
- package/dist/core/cost-guard-controller.d.ts +37 -0
- package/dist/core/cost-guard-controller.d.ts.map +1 -0
- package/dist/core/cost-guard-controller.js +71 -0
- package/dist/core/cost-guard-controller.js.map +1 -0
- package/dist/core/cost-guard.d.ts +9 -4
- package/dist/core/cost-guard.d.ts.map +1 -1
- package/dist/core/cost-guard.js +7 -3
- package/dist/core/cost-guard.js.map +1 -1
- package/dist/core/delegation/worker-agent-control.d.ts +4 -0
- package/dist/core/delegation/worker-agent-control.d.ts.map +1 -1
- package/dist/core/delegation/worker-agent-control.js.map +1 -1
- package/dist/core/delegation/worker-attempt-executor.d.ts +1 -1
- package/dist/core/delegation/worker-attempt-executor.d.ts.map +1 -1
- package/dist/core/delegation/worker-attempt-executor.js +0 -1
- package/dist/core/delegation/worker-attempt-executor.js.map +1 -1
- package/dist/core/delegation/worker-authority-resolver.d.ts +16 -3
- package/dist/core/delegation/worker-authority-resolver.d.ts.map +1 -1
- package/dist/core/delegation/worker-authority-resolver.js +114 -55
- package/dist/core/delegation/worker-authority-resolver.js.map +1 -1
- package/dist/core/delegation/worker-claim.d.ts +1 -2
- package/dist/core/delegation/worker-claim.d.ts.map +1 -1
- package/dist/core/delegation/worker-claim.js.map +1 -1
- package/dist/core/delegation/worker-delegation-controller.d.ts +19 -6
- package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -1
- package/dist/core/delegation/worker-delegation-controller.js +125 -40
- package/dist/core/delegation/worker-delegation-controller.js.map +1 -1
- package/dist/core/delegation/worker-delegation-request.d.ts +3 -3
- package/dist/core/delegation/worker-delegation-request.d.ts.map +1 -1
- package/dist/core/delegation/worker-delegation-request.js +13 -19
- package/dist/core/delegation/worker-delegation-request.js.map +1 -1
- package/dist/core/delegation/worker-execution-policy.d.ts +3 -2
- package/dist/core/delegation/worker-execution-policy.d.ts.map +1 -1
- package/dist/core/delegation/worker-execution-policy.js +54 -39
- package/dist/core/delegation/worker-execution-policy.js.map +1 -1
- package/dist/core/delegation/worker-fleet-limits.d.ts +3 -3
- package/dist/core/delegation/worker-fleet-limits.d.ts.map +1 -1
- package/dist/core/delegation/worker-fleet-limits.js +7 -7
- package/dist/core/delegation/worker-fleet-limits.js.map +1 -1
- package/dist/core/delegation/worker-machine-scope.d.ts +12 -0
- package/dist/core/delegation/worker-machine-scope.d.ts.map +1 -0
- package/dist/core/delegation/worker-machine-scope.js +38 -0
- package/dist/core/delegation/worker-machine-scope.js.map +1 -0
- package/dist/core/delegation/worker-notification-coordinator.d.ts +10 -0
- package/dist/core/delegation/worker-notification-coordinator.d.ts.map +1 -1
- package/dist/core/delegation/worker-notification-coordinator.js +69 -3
- package/dist/core/delegation/worker-notification-coordinator.js.map +1 -1
- package/dist/core/delegation/worker-recovery-coordinator.d.ts.map +1 -1
- package/dist/core/delegation/worker-recovery-coordinator.js +6 -1
- package/dist/core/delegation/worker-recovery-coordinator.js.map +1 -1
- package/dist/core/delegation/worker-runner.d.ts +5 -7
- package/dist/core/delegation/worker-runner.d.ts.map +1 -1
- package/dist/core/delegation/worker-runner.js +1 -4
- package/dist/core/delegation/worker-runner.js.map +1 -1
- package/dist/core/delegation/worker-write-reservation-coordinator.d.ts +3 -2
- package/dist/core/delegation/worker-write-reservation-coordinator.d.ts.map +1 -1
- package/dist/core/delegation/worker-write-reservation-coordinator.js +50 -25
- package/dist/core/delegation/worker-write-reservation-coordinator.js.map +1 -1
- package/dist/core/durable-custom-message-turn-controller.d.ts +6 -0
- package/dist/core/durable-custom-message-turn-controller.d.ts.map +1 -1
- package/dist/core/durable-custom-message-turn-controller.js +38 -15
- package/dist/core/durable-custom-message-turn-controller.js.map +1 -1
- package/dist/core/extension-binding-controller.d.ts +5 -1
- package/dist/core/extension-binding-controller.d.ts.map +1 -1
- package/dist/core/extension-binding-controller.js +15 -0
- package/dist/core/extension-binding-controller.js.map +1 -1
- package/dist/core/extensions/factory-runtime.d.ts.map +1 -1
- package/dist/core/extensions/factory-runtime.js +5 -0
- package/dist/core/extensions/factory-runtime.js.map +1 -1
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +1 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +8 -4
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/foreground-terminal-handoff-controller.d.ts +31 -0
- package/dist/core/foreground-terminal-handoff-controller.d.ts.map +1 -1
- package/dist/core/foreground-terminal-handoff-controller.js +239 -55
- package/dist/core/foreground-terminal-handoff-controller.js.map +1 -1
- package/dist/core/goals/goal-session-controller.d.ts +2 -0
- package/dist/core/goals/goal-session-controller.d.ts.map +1 -1
- package/dist/core/goals/goal-session-controller.js +8 -0
- package/dist/core/goals/goal-session-controller.js.map +1 -1
- package/dist/core/goals/goal-tool-core.d.ts +5 -4
- package/dist/core/goals/goal-tool-core.d.ts.map +1 -1
- package/dist/core/goals/goal-tool-core.js +23 -19
- package/dist/core/goals/goal-tool-core.js.map +1 -1
- package/dist/core/learning/auto-learn-settings.d.ts +84 -0
- package/dist/core/learning/auto-learn-settings.d.ts.map +1 -0
- package/dist/core/learning/auto-learn-settings.js +66 -0
- package/dist/core/learning/auto-learn-settings.js.map +1 -0
- package/dist/core/learning/learning-audit.d.ts +5 -1
- package/dist/core/learning/learning-audit.d.ts.map +1 -1
- package/dist/core/learning/learning-audit.js +32 -1
- package/dist/core/learning/learning-audit.js.map +1 -1
- package/dist/core/learning/reflection-engine.d.ts +24 -0
- package/dist/core/learning/reflection-engine.d.ts.map +1 -1
- package/dist/core/learning/reflection-engine.js +104 -8
- package/dist/core/learning/reflection-engine.js.map +1 -1
- package/dist/core/local-prefix-warm-controller.d.ts +24 -0
- package/dist/core/local-prefix-warm-controller.d.ts.map +1 -0
- package/dist/core/local-prefix-warm-controller.js +79 -0
- package/dist/core/local-prefix-warm-controller.js.map +1 -0
- package/dist/core/memory/okf-project-memory-store.d.ts +28 -0
- package/dist/core/memory/okf-project-memory-store.d.ts.map +1 -0
- package/dist/core/memory/okf-project-memory-store.js +111 -0
- package/dist/core/memory/okf-project-memory-store.js.map +1 -0
- package/dist/core/memory/providers/file-store.d.ts +44 -1
- package/dist/core/memory/providers/file-store.d.ts.map +1 -1
- package/dist/core/memory/providers/file-store.js +341 -24
- package/dist/core/memory/providers/file-store.js.map +1 -1
- package/dist/core/memory/worker-memory-tools.d.ts +5 -0
- package/dist/core/memory/worker-memory-tools.d.ts.map +1 -0
- package/dist/core/memory/worker-memory-tools.js +5 -0
- package/dist/core/memory/worker-memory-tools.js.map +1 -0
- package/dist/core/memory-controller.d.ts +10 -1
- package/dist/core/memory-controller.d.ts.map +1 -1
- package/dist/core/memory-controller.js +72 -8
- package/dist/core/memory-controller.js.map +1 -1
- package/dist/core/model-capability.d.ts +7 -0
- package/dist/core/model-capability.d.ts.map +1 -1
- package/dist/core/model-capability.js +12 -0
- package/dist/core/model-capability.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +2 -10
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-router-controller.d.ts +3 -2
- package/dist/core/model-router-controller.d.ts.map +1 -1
- package/dist/core/model-router-controller.js +3 -2
- package/dist/core/model-router-controller.js.map +1 -1
- package/dist/core/orchestration/contracts.d.ts +16 -12
- package/dist/core/orchestration/contracts.d.ts.map +1 -1
- package/dist/core/orchestration/contracts.js +2 -2
- package/dist/core/orchestration/contracts.js.map +1 -1
- package/dist/core/orchestration/lane-tool-manifests.d.ts +2 -2
- package/dist/core/orchestration/lane-tool-manifests.d.ts.map +1 -1
- package/dist/core/orchestration/lane-tool-manifests.js +6 -1
- package/dist/core/orchestration/lane-tool-manifests.js.map +1 -1
- package/dist/core/orchestration/profile-registry.d.ts.map +1 -1
- package/dist/core/orchestration/profile-registry.js +14 -0
- package/dist/core/orchestration/profile-registry.js.map +1 -1
- package/dist/core/orchestration/task-profile-writer.d.ts +7 -4
- package/dist/core/orchestration/task-profile-writer.d.ts.map +1 -1
- package/dist/core/orchestration/task-profile-writer.js +71 -59
- package/dist/core/orchestration/task-profile-writer.js.map +1 -1
- package/dist/core/orchestration/thinking-level-schema.d.ts +7 -0
- package/dist/core/orchestration/thinking-level-schema.d.ts.map +1 -0
- package/dist/core/orchestration/thinking-level-schema.js +19 -0
- package/dist/core/orchestration/thinking-level-schema.js.map +1 -0
- package/dist/core/orchestration/worker-execution-contract.d.ts.map +1 -1
- package/dist/core/orchestration/worker-execution-contract.js +12 -5
- package/dist/core/orchestration/worker-execution-contract.js.map +1 -1
- package/dist/core/provider-prompt-contracts.d.ts +1 -4
- package/dist/core/provider-prompt-contracts.d.ts.map +1 -1
- package/dist/core/provider-prompt-contracts.js +13 -29
- package/dist/core/provider-prompt-contracts.js.map +1 -1
- package/dist/core/provider-request-context-controller.d.ts +2 -0
- package/dist/core/provider-request-context-controller.d.ts.map +1 -1
- package/dist/core/provider-request-context-controller.js +10 -3
- package/dist/core/provider-request-context-controller.js.map +1 -1
- package/dist/core/reflection-controller.d.ts +63 -27
- package/dist/core/reflection-controller.d.ts.map +1 -1
- package/dist/core/reflection-controller.js +300 -132
- package/dist/core/reflection-controller.js.map +1 -1
- package/dist/core/runtime-builder.d.ts +1 -0
- package/dist/core/runtime-builder.d.ts.map +1 -1
- package/dist/core/runtime-builder.js +25 -14
- package/dist/core/runtime-builder.js.map +1 -1
- package/dist/core/secrets/credential-exposure-guard.d.ts.map +1 -1
- package/dist/core/secrets/credential-exposure-guard.js +55 -10
- package/dist/core/secrets/credential-exposure-guard.js.map +1 -1
- package/dist/core/session-role.d.ts +6 -8
- package/dist/core/session-role.d.ts.map +1 -1
- package/dist/core/session-role.js +8 -18
- package/dist/core/session-role.js.map +1 -1
- package/dist/core/settings-manager.d.ts +13 -16
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +53 -50
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skill-vault.d.ts +15 -0
- package/dist/core/skill-vault.d.ts.map +1 -1
- package/dist/core/skill-vault.js +79 -45
- package/dist/core/skill-vault.js.map +1 -1
- package/dist/core/skills.d.ts +3 -0
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +6 -6
- package/dist/core/skills.js.map +1 -1
- package/dist/core/system-prompt-builder.d.ts +2 -0
- package/dist/core/system-prompt-builder.d.ts.map +1 -1
- package/dist/core/system-prompt-builder.js +52 -12
- package/dist/core/system-prompt-builder.js.map +1 -1
- package/dist/core/system-prompt.js +1 -1
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tool-capability-policy.d.ts +3 -0
- package/dist/core/tool-capability-policy.d.ts.map +1 -1
- package/dist/core/tool-capability-policy.js +13 -2
- package/dist/core/tool-capability-policy.js.map +1 -1
- package/dist/core/toolkit/script-runner.d.ts +2 -1
- package/dist/core/toolkit/script-runner.d.ts.map +1 -1
- package/dist/core/toolkit/script-runner.js +10 -2
- package/dist/core/toolkit/script-runner.js.map +1 -1
- package/dist/core/tools/delegate-status.d.ts +3 -1
- package/dist/core/tools/delegate-status.d.ts.map +1 -1
- package/dist/core/tools/delegate-status.js +5 -1
- package/dist/core/tools/delegate-status.js.map +1 -1
- package/dist/core/tools/delegate.d.ts +4 -13
- package/dist/core/tools/delegate.d.ts.map +1 -1
- package/dist/core/tools/delegate.js +173 -73
- package/dist/core/tools/delegate.js.map +1 -1
- package/dist/core/tools/goal.d.ts +3 -4
- package/dist/core/tools/goal.d.ts.map +1 -1
- package/dist/core/tools/goal.js +24 -7
- package/dist/core/tools/goal.js.map +1 -1
- package/dist/core/tools/profile-writer.d.ts +2 -8
- package/dist/core/tools/profile-writer.d.ts.map +1 -1
- package/dist/core/tools/profile-writer.js +6 -18
- package/dist/core/tools/profile-writer.js.map +1 -1
- package/dist/core/tools/run-toolkit-script.d.ts +1 -1
- package/dist/core/tools/run-toolkit-script.d.ts.map +1 -1
- package/dist/core/tools/run-toolkit-script.js +2 -2
- package/dist/core/tools/run-toolkit-script.js.map +1 -1
- package/dist/core/tools/skill-audit.d.ts +28 -1
- package/dist/core/tools/skill-audit.d.ts.map +1 -1
- package/dist/core/tools/skill-audit.js +56 -12
- package/dist/core/tools/skill-audit.js.map +1 -1
- package/dist/core/tools/skill.d.ts +12 -1
- package/dist/core/tools/skill.d.ts.map +1 -1
- package/dist/core/tools/skill.js +52 -0
- package/dist/core/tools/skill.js.map +1 -1
- package/dist/core/tools/skillify.d.ts.map +1 -1
- package/dist/core/tools/skillify.js +16 -7
- package/dist/core/tools/skillify.js.map +1 -1
- package/dist/core/tools/task-steps.js +1 -1
- package/dist/core/tools/task-steps.js.map +1 -1
- package/dist/core/tools/tmux-dispatch.d.ts +4 -16
- package/dist/core/tools/tmux-dispatch.d.ts.map +1 -1
- package/dist/core/tools/tmux-dispatch.js +5 -20
- package/dist/core/tools/tmux-dispatch.js.map +1 -1
- package/dist/core/tools/tool-task.d.ts +1 -0
- package/dist/core/tools/tool-task.d.ts.map +1 -1
- package/dist/core/tools/tool-task.js +13 -8
- package/dist/core/tools/tool-task.js.map +1 -1
- package/dist/core/util/atomic-file.d.ts +2 -0
- package/dist/core/util/atomic-file.d.ts.map +1 -1
- package/dist/core/util/atomic-file.js +4 -0
- package/dist/core/util/atomic-file.js.map +1 -1
- package/dist/core/worktree-sync/codes.d.ts +1 -2
- package/dist/core/worktree-sync/codes.d.ts.map +1 -1
- package/dist/core/worktree-sync/codes.js +1 -2
- package/dist/core/worktree-sync/codes.js.map +1 -1
- package/dist/core/worktree-sync/lane-gate.d.ts +4 -8
- package/dist/core/worktree-sync/lane-gate.d.ts.map +1 -1
- package/dist/core/worktree-sync/lane-gate.js +5 -51
- package/dist/core/worktree-sync/lane-gate.js.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/modes/interactive/auto-learn-controller.d.ts +6 -7
- package/dist/modes/interactive/auto-learn-controller.d.ts.map +1 -1
- package/dist/modes/interactive/auto-learn-controller.js +32 -207
- package/dist/modes/interactive/auto-learn-controller.js.map +1 -1
- package/dist/modes/interactive/autonomy-commands.js +2 -2
- package/dist/modes/interactive/autonomy-commands.js.map +1 -1
- package/dist/modes/interactive/components/activity-lane.d.ts.map +1 -1
- package/dist/modes/interactive/components/activity-lane.js +12 -23
- package/dist/modes/interactive/components/activity-lane.js.map +1 -1
- package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/custom-message.js +142 -1
- package/dist/modes/interactive/components/custom-message.js.map +1 -1
- package/dist/modes/interactive/components/fitness-role-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/fitness-role-selector.js +2 -2
- package/dist/modes/interactive/components/fitness-role-selector.js.map +1 -1
- package/dist/modes/interactive/components/footer.js +1 -1
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +4 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +150 -73
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/report-commands.d.ts.map +1 -1
- package/dist/modes/interactive/report-commands.js +8 -4
- package/dist/modes/interactive/report-commands.js.map +1 -1
- package/dist/modes/interactive/settings-selector-flow.d.ts.map +1 -1
- package/dist/modes/interactive/settings-selector-flow.js +10 -0
- package/dist/modes/interactive/settings-selector-flow.js.map +1 -1
- package/docs/extensions.md +8 -0
- package/docs/harness-architecture.md +28 -32
- package/docs/index.md +1 -1
- package/docs/process-matrix.md +8 -6
- package/docs/resources.md +1 -1
- package/docs/settings.md +61 -51
- package/docs/tmux-agent-manager.md +47 -40
- package/docs/usage.md +1 -1
- package/docs/worker-profiles.md +8 -6
- package/docs/worktree-sync.md +26 -20
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +12 -12
- package/package.json +4 -4
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts +0 -133
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts.map +0 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js +0 -199
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js.map +0 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.ts +0 -312
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** The authoritative Auto Learn defaults used by runtime resolution and settings UI. */
|
|
2
|
+
export const DEFAULT_AUTO_LEARN_SETTINGS = {
|
|
3
|
+
enabled: true,
|
|
4
|
+
model: "active",
|
|
5
|
+
thinkingLevel: "low",
|
|
6
|
+
longSessionMessages: 32,
|
|
7
|
+
longSessionContextPercent: 70,
|
|
8
|
+
cooldownMinutes: 24 * 60,
|
|
9
|
+
leaseMinutes: 90,
|
|
10
|
+
maxConcurrentLearners: 1,
|
|
11
|
+
applyHighConfidence: false,
|
|
12
|
+
reflectionReview: true,
|
|
13
|
+
reflectionMinToolCalls: 12,
|
|
14
|
+
reflectionCooldownMinutes: 24 * 60,
|
|
15
|
+
complexTaskToolCalls: 12,
|
|
16
|
+
};
|
|
17
|
+
export const AUTONOMY_AUTO_LEARN_PRESETS = {
|
|
18
|
+
off: DEFAULT_AUTO_LEARN_SETTINGS,
|
|
19
|
+
safe: {
|
|
20
|
+
...DEFAULT_AUTO_LEARN_SETTINGS,
|
|
21
|
+
longSessionMessages: 64,
|
|
22
|
+
longSessionContextPercent: 85,
|
|
23
|
+
leaseMinutes: 60,
|
|
24
|
+
},
|
|
25
|
+
balanced: {
|
|
26
|
+
...DEFAULT_AUTO_LEARN_SETTINGS,
|
|
27
|
+
longSessionMessages: 64,
|
|
28
|
+
longSessionContextPercent: 85,
|
|
29
|
+
},
|
|
30
|
+
full: {
|
|
31
|
+
...DEFAULT_AUTO_LEARN_SETTINGS,
|
|
32
|
+
longSessionMessages: 64,
|
|
33
|
+
longSessionContextPercent: 85,
|
|
34
|
+
applyHighConfidence: true,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
export function getAutoLearnPreset(mode, current = {}) {
|
|
38
|
+
const preset = AUTONOMY_AUTO_LEARN_PRESETS[mode] ?? AUTONOMY_AUTO_LEARN_PRESETS.off;
|
|
39
|
+
return { ...preset, model: current.model?.trim() || preset.model };
|
|
40
|
+
}
|
|
41
|
+
export function resolveAutoLearnSettings(mode, settings = {}) {
|
|
42
|
+
const preset = getAutoLearnPreset(mode, settings);
|
|
43
|
+
return {
|
|
44
|
+
enabled: settings.enabled ?? preset.enabled,
|
|
45
|
+
model: settings.model?.trim() || preset.model,
|
|
46
|
+
thinkingLevel: settings.thinkingLevel ?? preset.thinkingLevel,
|
|
47
|
+
longSessionMessages: settings.longSessionMessages ?? preset.longSessionMessages,
|
|
48
|
+
longSessionContextPercent: settings.longSessionContextPercent ?? preset.longSessionContextPercent,
|
|
49
|
+
cooldownMinutes: settings.cooldownMinutes ?? preset.cooldownMinutes,
|
|
50
|
+
leaseMinutes: settings.leaseMinutes ?? preset.leaseMinutes,
|
|
51
|
+
maxConcurrentLearners: settings.maxConcurrentLearners ?? preset.maxConcurrentLearners,
|
|
52
|
+
applyHighConfidence: settings.applyHighConfidence ?? preset.applyHighConfidence,
|
|
53
|
+
reflectionReview: settings.reflectionReview ?? preset.reflectionReview,
|
|
54
|
+
reflectionMinToolCalls: settings.reflectionMinToolCalls ?? preset.reflectionMinToolCalls,
|
|
55
|
+
reflectionCooldownMinutes: settings.reflectionCooldownMinutes ?? preset.reflectionCooldownMinutes,
|
|
56
|
+
complexTaskToolCalls: settings.complexTaskToolCalls ?? preset.complexTaskToolCalls,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** One settings/environment gate shared by root prompt construction and current-turn cue delivery. */
|
|
60
|
+
export function isCurrentSessionReflectionEnabled(settings, env = process.env) {
|
|
61
|
+
return (settings.enabled &&
|
|
62
|
+
settings.reflectionReview &&
|
|
63
|
+
env.PI_NATIVE_REFLECTION !== "0" &&
|
|
64
|
+
env.PI_AUTO_LEARN_CHILD !== "1");
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=auto-learn-settings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto-learn-settings.js","sourceRoot":"","sources":["../../../src/core/learning/auto-learn-settings.ts"],"names":[],"mappings":"AAEA,wFAAwF;AACxF,MAAM,CAAC,MAAM,2BAA2B,GAAG;IAC1C,OAAO,EAAE,IAAI;IACb,KAAK,EAAE,QAAQ;IACf,aAAa,EAAE,KAAK;IACpB,mBAAmB,EAAE,EAAE;IACvB,yBAAyB,EAAE,EAAE;IAC7B,eAAe,EAAE,EAAE,GAAG,EAAE;IACxB,YAAY,EAAE,EAAE;IAChB,qBAAqB,EAAE,CAAC;IACxB,mBAAmB,EAAE,KAAK;IAC1B,gBAAgB,EAAE,IAAI;IACtB,sBAAsB,EAAE,EAAE;IAC1B,yBAAyB,EAAE,EAAE,GAAG,EAAE;IAClC,oBAAoB,EAAE,EAAE;CACuB,CAAC;AAEjD,MAAM,CAAC,MAAM,2BAA2B,GAAG;IAC1C,GAAG,EAAE,2BAA2B;IAChC,IAAI,EAAE;QACL,GAAG,2BAA2B;QAC9B,mBAAmB,EAAE,EAAE;QACvB,yBAAyB,EAAE,EAAE;QAC7B,YAAY,EAAE,EAAE;KAChB;IACD,QAAQ,EAAE;QACT,GAAG,2BAA2B;QAC9B,mBAAmB,EAAE,EAAE;QACvB,yBAAyB,EAAE,EAAE;KAC7B;IACD,IAAI,EAAE;QACL,GAAG,2BAA2B;QAC9B,mBAAmB,EAAE,EAAE;QACvB,yBAAyB,EAAE,EAAE;QAC7B,mBAAmB,EA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,yBAAyB,IAAI,MAAM,CAAC,yBAAyB;QACjG,oBAAoB,EAAE,QAAQ,CAAC,oBAAoB,IAAI,MAAM,CAAC,oBAAoB;KAClF,CAAC;AACH,CAAC;AAED,sGAAsG;AACtG,MAAM,UAAU,iCAAiC,CAChD,QAA2E,EAC3E,GAAG,GAAsB,OAAO,CAAC,GAAG;IAEpC,OAAO,CACN,QAAQ,CAAC,OAAO;QAChB,QAAQ,CAAC,gBAAgB;QACzB,GAAG,CAAC,oBAAoB,KAAK,GAAG;QAChC,GAAG,CAAC,mBAAmB,KAAK,GAAG,CAC/B,CAAC;AACH,CAAC","sourcesContent":["import type { AutoLearnSettings, AutonomyMode } from \"../settings-manager.ts\";\n\n/** The authoritative Auto Learn defaults used by runtime resolution and settings UI. */\nexport const DEFAULT_AUTO_LEARN_SETTINGS = {\n\tenabled: true,\n\tmodel: \"active\",\n\tthinkingLevel: \"low\",\n\tlongSessionMessages: 32,\n\tlongSessionContextPercent: 70,\n\tcooldownMinutes: 24 * 60,\n\tleaseMinutes: 90,\n\tmaxConcurrentLearners: 1,\n\tapplyHighConfidence: false,\n\treflectionReview: true,\n\treflectionMinToolCalls: 12,\n\treflectionCooldownMinutes: 24 * 60,\n\tcomplexTaskToolCalls: 12,\n} as const satisfies Required<AutoLearnSettings>;\n\nexport const AUTONOMY_AUTO_LEARN_PRESETS = {\n\toff: DEFAULT_AUTO_LEARN_SETTINGS,\n\tsafe: {\n\t\t...DEFAULT_AUTO_LEARN_SETTINGS,\n\t\tlongSessionMessages: 64,\n\t\tlongSessionContextPercent: 85,\n\t\tleaseMinutes: 60,\n\t},\n\tbalanced: {\n\t\t...DEFAULT_AUTO_LEARN_SETTINGS,\n\t\tlongSessionMessages: 64,\n\t\tlongSessionContextPercent: 85,\n\t},\n\tfull: {\n\t\t...DEFAULT_AUTO_LEARN_SETTINGS,\n\t\tlongSessionMessages: 64,\n\t\tlongSessionContextPercent: 85,\n\t\tapplyHighConfidence: true,\n\t},\n} as const satisfies Record<AutonomyMode, Required<AutoLearnSettings>>;\n\nexport function getAutoLearnPreset(mode: AutonomyMode, current: AutoLearnSettings = {}): Required<AutoLearnSettings> {\n\tconst preset = AUTONOMY_AUTO_LEARN_PRESETS[mode] ?? AUTONOMY_AUTO_LEARN_PRESETS.off;\n\treturn { ...preset, model: current.model?.trim() || preset.model };\n}\n\nexport function resolveAutoLearnSettings(\n\tmode: AutonomyMode,\n\tsettings: AutoLearnSettings = {},\n): Required<AutoLearnSettings> {\n\tconst preset = getAutoLearnPreset(mode, settings);\n\treturn {\n\t\tenabled: settings.enabled ?? preset.enabled,\n\t\tmodel: settings.model?.trim() || preset.model,\n\t\tthinkingLevel: settings.thinkingLevel ?? preset.thinkingLevel,\n\t\tlongSessionMessages: settings.longSessionMessages ?? preset.longSessionMessages,\n\t\tlongSessionContextPercent: settings.longSessionContextPercent ?? preset.longSessionContextPercent,\n\t\tcooldownMinutes: settings.cooldownMinutes ?? preset.cooldownMinutes,\n\t\tleaseMinutes: settings.leaseMinutes ?? preset.leaseMinutes,\n\t\tmaxConcurrentLearners: settings.maxConcurrentLearners ?? preset.maxConcurrentLearners,\n\t\tapplyHighConfidence: settings.applyHighConfidence ?? preset.applyHighConfidence,\n\t\treflectionReview: settings.reflectionReview ?? preset.reflectionReview,\n\t\treflectionMinToolCalls: settings.reflectionMinToolCalls ?? preset.reflectionMinToolCalls,\n\t\treflectionCooldownMinutes: settings.reflectionCooldownMinutes ?? preset.reflectionCooldownMinutes,\n\t\tcomplexTaskToolCalls: settings.complexTaskToolCalls ?? preset.complexTaskToolCalls,\n\t};\n}\n\n/** One settings/environment gate shared by root prompt construction and current-turn cue delivery. */\nexport function isCurrentSessionReflectionEnabled(\n\tsettings: Pick<Required<AutoLearnSettings>, \"enabled\" | \"reflectionReview\">,\n\tenv: NodeJS.ProcessEnv = process.env,\n): boolean {\n\treturn (\n\t\tsettings.enabled &&\n\t\tsettings.reflectionReview &&\n\t\tenv.PI_NATIVE_REFLECTION !== \"0\" &&\n\t\tenv.PI_AUTO_LEARN_CHILD !== \"1\"\n\t);\n}\n"]}
|
|
@@ -9,13 +9,17 @@ import type { ReflectionWrite } from "./reflection-engine.ts";
|
|
|
9
9
|
* log, so `/autonomy diagnostics` can explain what changed, why, and how to undo it — and
|
|
10
10
|
* `rollbackLearningWrite` can execute the inverse operation.
|
|
11
11
|
*/
|
|
12
|
-
export type LearningRollbackKind = "memory_remove" | "memory_restore" | "memory_add" | "archive_skill";
|
|
12
|
+
export type LearningRollbackKind = "memory_remove" | "memory_restore" | "memory_add" | "okf_remove" | "okf_organize" | "archive_skill";
|
|
13
13
|
export interface LearningRollbackPlan {
|
|
14
14
|
kind: LearningRollbackKind;
|
|
15
15
|
/** Text currently present because of the change (to remove/replace), or the skill name to archive. */
|
|
16
16
|
target?: string;
|
|
17
17
|
/** Original text to restore (memory_restore/memory_add). */
|
|
18
18
|
previous?: string;
|
|
19
|
+
/** Compare-and-delete guard for a structured record created by this exact audited write. */
|
|
20
|
+
expectedDigest?: string;
|
|
21
|
+
/** Organization rollback only: false when the OKF record predated this hot-memory move. */
|
|
22
|
+
removeOkf?: boolean;
|
|
19
23
|
instructions: string;
|
|
20
24
|
}
|
|
21
25
|
export type LearningAuditAction = "apply" | "apply_failed" | "propose" | "rollback";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"learning-audit.d.ts","sourceRoot":"","sources":["../../../src/core/learning/learning-audit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACnF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAIN,KAAK,sBAAsB,EAC3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AACpF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE9D;;;;;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Q,EAAE,gBAAgB,CAAC;IAC3B,QAAQ,CAAC,EAAE,oBAAoB,CAAC;IAChC,oFAAoF;IACpF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;CAClB;AAmBD,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,GAAG,qBAAqB,CAO7G;AAED;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAW/E;AAED,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,eAAe,GAAG,oBAAoB,CA0C3F;AAuCD,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,mBAAmB,CAYlF;AAED,wBAAgB,kCAAkC,CAAC,MAAM,EAAE,mBAAmB,GAAG,mBAAmB,CAMnG;AAED,eAAO,MAAM,0BAA0B,mBAAmB,CAAC;AAE3D,MAAM,MAAM,4BAA4B,GAAG,sBAAsB,CAAC,QAAQ,EAAE,mBAAmB,CAAC,CAAC;AASjG,wBAAgB,2BAA2B,CAC1C,cAAc,EAAE,IAAI,CAAC,cAAc,EAAE,mBAAmB,CAAC,EACzD,MAAM,EAAE,mBAAmB,GACzB,MAAM,CAER;AAED,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,SAAS,YAAY,EAAE,GAAG,mBAAmB,EAAE,CAEjG"}
|
|
@@ -7,6 +7,10 @@ function describeWrite(write) {
|
|
|
7
7
|
switch (write.kind) {
|
|
8
8
|
case "memory_add":
|
|
9
9
|
return `Add ${write.section} memory: ${write.text}`;
|
|
10
|
+
case "okf_add":
|
|
11
|
+
return `Add OKF ${write.type}: ${write.title}`;
|
|
12
|
+
case "okf_organize":
|
|
13
|
+
return `Organize MEMORY fact into OKF ${write.type}: ${write.title}`;
|
|
10
14
|
case "memory_replace":
|
|
11
15
|
return `Replace memory "${write.target}" with "${write.text}"`;
|
|
12
16
|
case "memory_remove":
|
|
@@ -36,6 +40,8 @@ export function contradictionsForReflectionWrite(write) {
|
|
|
36
40
|
case "memory_remove":
|
|
37
41
|
return 1;
|
|
38
42
|
case "memory_add":
|
|
43
|
+
case "okf_add":
|
|
44
|
+
case "okf_organize":
|
|
39
45
|
case "promote_skill":
|
|
40
46
|
return 0;
|
|
41
47
|
}
|
|
@@ -48,6 +54,20 @@ export function rollbackPlanForReflectionWrite(write) {
|
|
|
48
54
|
target: write.text,
|
|
49
55
|
instructions: `Remove the added ${write.section} memory text.`,
|
|
50
56
|
};
|
|
57
|
+
case "okf_add":
|
|
58
|
+
return {
|
|
59
|
+
kind: "okf_remove",
|
|
60
|
+
target: `${write.type}\0${write.title}`,
|
|
61
|
+
instructions: "Remove the added structured OKF memory record.",
|
|
62
|
+
};
|
|
63
|
+
case "okf_organize":
|
|
64
|
+
return {
|
|
65
|
+
kind: "okf_organize",
|
|
66
|
+
target: `${write.type}\0${write.title}`,
|
|
67
|
+
previous: write.sourceText,
|
|
68
|
+
removeOkf: true,
|
|
69
|
+
instructions: "Restore the exact hot-memory source first, then remove only the audited OKF record.",
|
|
70
|
+
};
|
|
51
71
|
case "memory_replace":
|
|
52
72
|
return {
|
|
53
73
|
kind: "memory_restore",
|
|
@@ -70,7 +90,14 @@ export function rollbackPlanForReflectionWrite(write) {
|
|
|
70
90
|
}
|
|
71
91
|
}
|
|
72
92
|
const AUDIT_ACTIONS = ["apply", "apply_failed", "propose", "rollback"];
|
|
73
|
-
const ROLLBACK_KINDS = [
|
|
93
|
+
const ROLLBACK_KINDS = [
|
|
94
|
+
"memory_remove",
|
|
95
|
+
"memory_restore",
|
|
96
|
+
"memory_add",
|
|
97
|
+
"okf_remove",
|
|
98
|
+
"okf_organize",
|
|
99
|
+
"archive_skill",
|
|
100
|
+
];
|
|
74
101
|
const LAYERS = ["memory", "skill", "prompt", "extension", "tool", "script", "settings", "source"];
|
|
75
102
|
function isOptionalString(value) {
|
|
76
103
|
return value === undefined || typeof value === "string";
|
|
@@ -83,6 +110,10 @@ function isLearningRollbackPlan(value) {
|
|
|
83
110
|
return false;
|
|
84
111
|
if (!isOptionalString(plan.target) || !isOptionalString(plan.previous))
|
|
85
112
|
return false;
|
|
113
|
+
if (!isOptionalString(plan.expectedDigest))
|
|
114
|
+
return false;
|
|
115
|
+
if (plan.removeOkf !== undefined && typeof plan.removeOkf !== "boolean")
|
|
116
|
+
return false;
|
|
86
117
|
return typeof plan.instructions === "string";
|
|
87
118
|
}
|
|
88
119
|
function isLearningDecisionShape(value) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"learning-audit.js","sourceRoot":"","sources":["../../../src/core/learning/learning-audit.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,qBAAqB,EACrB,mBAAmB,GAGnB,MAAM,wBAAwB,CAAC;AA2BhC;;0DAE0D;AAC1D,MAAM,CAAC,MAAM,+BAA+B,GAAG,qBAAqB,CAAC;AAgBrE,SAAS,aAAa,CAAC,KAAsB;IAC5C,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,YAAY;YAChB,OAAO,OAAO,KAAK,CAAC,OAAO,YAAY,KAAK,CAAC,IAAI,EAAE,CAAC;QACrD,KAAK,gBAAgB;YACpB,OAAO,mBAAmB,KAAK,CAAC,MAAM,WAAW,KAAK,CAAC,IAAI,GAAG,CAAC;QAChE,KAAK,eAAe;YACnB,OAAO,kBAAkB,KAAK,CAAC,MAAM,EAAE,CAAC;QACzC,KAAK,eAAe;YACnB,OAAO,kBAAkB,KAAK,CAAC,IAAI,MAAM,KAAK,CAAC,WAAW,EAAE,CAAC;IAC/D,CAAC;AACF,CAAC;AAED,MAAM,UAAU,2BAA2B,CAAC,KAAsB,EAAE,UAAkB;IACrF,OAAO;QACN,EAAE,EAAE,UAAU;QACd,KAAK,EAAE,KAAK,CAAC,IAAI,KAAK,eAAe,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ;QAC1D,OAAO,EAAE,aAAa,CAAC,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A2B;IAC7E,OAAO;QACN,GAAG,MAAM;QACT,QAAQ,EAAE,EAAE,GAAG,MAAM,CAAC,QAAQ,EAAE;QAChC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,GAAG,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAChE,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,0BAA0B,GAAG,gBAAgB,CAAC;AAI3D,MAAM,6BAA6B,GAAwD;IAC1F,UAAU,EAAE,0BAA0B;IACtC,QAAQ,EAAE,QAAQ;IAClB,OAAO,EAAE,qBAAqB;IAC9B,KAAK,EAAE,kCAAkC;CACzC,CAAC;AAEF,MAAM,UAAU,2BAA2B,CAC1C,cAAyD,EACzD,MAA2B;IAE3B,OAAO,qBAAqB,CAAC,cAAc,EAAE,6BAA6B,EAAE,MAAM,CAAC,CAAC;AACrF,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,OAAgC;IACzE,OAAO,mBAAmB,CAAC,OAAO,EAAE,6BAA6B,CAAC,CAAC;AACpE,CAAC","sourcesContent":["import type { SessionEntry, SessionManager } from \"@caupulican/pi-agent-core/node\";\nimport type { LearningDecision } from \"../autonomy/contracts.ts\";\nimport {\n\tappendSessionSnapshot,\n\tgetSessionSnapshots,\n\ttype SessionSnapshotCodec,\n\ttype SessionSnapshotPayload,\n} from \"../session-snapshot.ts\";\nimport type { DurableChangeLayer, DurableChangeProposal } from \"./learning-gate.ts\";\nimport type { ReflectionWrite } from \"./reflection-engine.ts\";\n\n/**\n * Audit + rollback metadata for durable learning changes. Every reflection-sourced write that the\n * learning policy applies (or converts to a proposal) leaves one of these records in the session\n * log, so `/autonomy diagnostics` can explain what changed, why, and how to undo it — and\n * `rollbackLearningWrite` can execute the inverse operation.\n */\n\nexport type LearningRollbackKind = \"memory_remove\" | \"memory_restore\" | \"memory_add\" | \"archive_skill\";\n\nexport interface LearningRollbackPlan {\n\tkind: LearningRollbackKind;\n\t/** Text currently present because of the change (to remove/replace), or the skill name to archive. */\n\ttarget?: string;\n\t/** Original text to restore (memory_restore/memory_add). */\n\tprevious?: string;\n\tinstructions: string;\n}\n\n// \"apply_failed\" is distinct from \"apply\": the gate decided to apply, but the underlying write tool\n// (e.g. the memory tool) refused it (budget exceeded, drift, threat) without throwing. It must never\n// be treated as \"apply\" by a rollback-eligibility check — there is nothing durable to undo.\nexport type LearningAuditAction = \"apply\" | \"apply_failed\" | \"propose\" | \"rollback\";\n\n/** reasonCode for an \"apply_failed\" audit — distinct from the gate's own reasonCode (e.g.\n * \"eligible_auto_apply\") so the record is honest about WHERE it stopped: the gate approved the\n * write, but the write tool refused it after the fact. */\nexport const APPLY_WRITE_REFUSED_REASON_CODE = \"apply_write_refused\";\n\nexport interface LearningAuditRecord {\n\tid: string;\n\tproposalId: string;\n\tlayer: DurableChangeLayer;\n\taction: LearningAuditAction;\n\tsummary: string;\n\treasonCode: string;\n\tdecision: LearningDecision;\n\trollback?: LearningRollbackPlan;\n\t/** For action \"rollback\": the audit id of the applied change this record undoes. */\n\trollbackOf?: string;\n\tcreatedAt: string;\n}\n\nfunction describeWrite(write: ReflectionWrite): string {\n\tswitch (write.kind) {\n\t\tcase \"memory_add\":\n\t\t\treturn `Add ${write.section} memory: ${write.text}`;\n\t\tcase \"memory_replace\":\n\t\t\treturn `Replace memory \"${write.target}\" with \"${write.text}\"`;\n\t\tcase \"memory_remove\":\n\t\t\treturn `Remove memory: ${write.target}`;\n\t\tcase \"promote_skill\":\n\t\t\treturn `Promote skill \"${write.name}\": ${write.description}`;\n\t}\n}\n\nexport function proposalFromReflectionWrite(write: ReflectionWrite, proposalId: string): DurableChangeProposal {\n\treturn {\n\t\tid: proposalId,\n\t\tlayer: write.kind === \"promote_skill\" ? \"skill\" : \"memory\",\n\t\tsummary: describeWrite(write),\n\t\trollbackPlan: rollbackPlanForReflectionWrite(write).instructions,\n\t};\n}\n\n/**\n * Contradiction count a reflection write carries against existing durable knowledge. A\n * `memory_replace`/`memory_remove` is only emitted when the reflection engine CONFRONTS an existing\n * fact (it supersedes or deletes it) — that supersession is the gate's contradiction signal, so such\n * a write must route through approval rather than silently overwriting prior memory. A `memory_add`\n * or `promote_skill` is purely additive and contradicts nothing.\n */\nexport function contradictionsForReflectionWrite(write: ReflectionWrite): number {\n\tswitch (write.kind) {\n\t\tcase \"memory_replace\":\n\t\tcase \"memory_remove\":\n\t\t\treturn 1;\n\t\tcase \"memory_add\":\n\t\tcase \"promote_skill\":\n\t\t\treturn 0;\n\t}\n}\n\nexport function rollbackPlanForReflectionWrite(write: ReflectionWrite): LearningRollbackPlan {\n\tswitch (write.kind) {\n\t\tcase \"memory_add\":\n\t\t\treturn {\n\t\t\t\tkind: \"memory_remove\",\n\t\t\t\ttarget: write.text,\n\t\t\t\tinstructions: `Remove the added ${write.section} memory text.`,\n\t\t\t};\n\t\tcase \"memory_replace\":\n\t\t\treturn {\n\t\t\t\tkind: \"memory_restore\",\n\t\t\t\ttarget: write.text,\n\t\t\t\tprevious: write.target,\n\t\t\t\tinstructions: \"Replace the new memory text with the original text it overwrote.\",\n\t\t\t};\n\t\tcase \"memory_remove\":\n\t\t\treturn {\n\t\t\t\tkind: \"memory_add\",\n\t\t\t\tprevious: write.target,\n\t\t\t\tinstructions: \"Re-add the removed text to the MEMORY file (it may originally have lived in USER).\",\n\t\t\t};\n\t\tcase \"promote_skill\":\n\t\t\treturn {\n\t\t\t\tkind: \"archive_skill\",\n\t\t\t\ttarget: write.name,\n\t\t\t\tinstructions: \"Archive the promoted skill so it no longer loads.\",\n\t\t\t};\n\t}\n}\n\nconst AUDIT_ACTIONS: readonly string[] = [\"apply\", \"apply_failed\", \"propose\", \"rollback\"];\nconst ROLLBACK_KINDS: readonly string[] = [\"memory_remove\", \"memory_restore\", \"memory_add\", \"archive_skill\"];\nconst LAYERS: readonly string[] = [\"memory\", \"skill\", \"prompt\", \"extension\", \"tool\", \"script\", \"settings\", \"source\"];\n\nfunction isOptionalString(value: unknown): boolean {\n\treturn value === undefined || typeof value === \"string\";\n}\n\nfunction isLearningRollbackPlan(value: unknown): value is LearningRollbackPlan {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\tconst plan = value as Record<string, unknown>;\n\tif (typeof plan.kind !== \"string\" || !ROLLBACK_KINDS.includes(plan.kind)) return false;\n\tif (!isOptionalString(plan.target) || !isOptionalString(plan.previous)) return false;\n\treturn typeof plan.instructions === \"string\";\n}\n\nfunction isLearningDecisionShape(value: unknown): value is LearningDecision {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\tconst decision = value as Record<string, unknown>;\n\treturn (\n\t\t(decision.kind === \"no-op\" || decision.kind === \"proposal\" || decision.kind === \"apply\") &&\n\t\ttypeof decision.reasonCode === \"string\" &&\n\t\ttypeof decision.confidence === \"number\" &&\n\t\ttypeof decision.summary === \"string\" &&\n\t\ttypeof decision.requiresApproval === \"boolean\"\n\t);\n}\n\nexport function isLearningAuditRecord(value: unknown): value is LearningAuditRecord {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\tconst record = value as Record<string, unknown>;\n\tif (typeof record.id !== \"string\" || record.id.length === 0) return false;\n\tif (typeof record.proposalId !== \"string\") return false;\n\tif (typeof record.layer !== \"string\" || !LAYERS.includes(record.layer)) return false;\n\tif (typeof record.action !== \"string\" || !AUDIT_ACTIONS.includes(record.action)) return false;\n\tif (typeof record.summary !== \"string\" || typeof record.reasonCode !== \"string\") return false;\n\tif (!isLearningDecisionShape(record.decision)) return false;\n\tif (record.rollback !== undefined && !isLearningRollbackPlan(record.rollback)) return false;\n\tif (!isOptionalString(record.rollbackOf)) return false;\n\treturn typeof record.createdAt === \"string\";\n}\n\nexport function cloneLearningAuditRecordForStorage(record: LearningAuditRecord): LearningAuditRecord {\n\treturn {\n\t\t...record,\n\t\tdecision: { ...record.decision },\n\t\t...(record.rollback ? { rollback: { ...record.rollback } } : {}),\n\t};\n}\n\nexport const LEARNING_AUDIT_CUSTOM_TYPE = \"learning_audit\";\n\nexport type LearningAuditSnapshotPayload = SessionSnapshotPayload<\"record\", LearningAuditRecord>;\n\nconst LEARNING_AUDIT_SNAPSHOT_CODEC: SessionSnapshotCodec<LearningAuditRecord, \"record\"> = {\n\tcustomType: LEARNING_AUDIT_CUSTOM_TYPE,\n\tvalueKey: \"record\",\n\tisValue: isLearningAuditRecord,\n\tclone: cloneLearningAuditRecordForStorage,\n};\n\nexport function appendLearningAuditSnapshot(\n\tsessionManager: Pick<SessionManager, \"appendCustomEntry\">,\n\trecord: LearningAuditRecord,\n): string {\n\treturn appendSessionSnapshot(sessionManager, LEARNING_AUDIT_SNAPSHOT_CODEC, record);\n}\n\nexport function getLearningAuditSnapshots(entries: readonly SessionEntry[]): LearningAuditRecord[] {\n\treturn getSessionSnapshots(entries, LEARNING_AUDIT_SNAPSHOT_CODEC);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"learning-audit.js","sourceRoot":"","sources":["../../../src/core/learning/learning-audit.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,qBAAqB,EACrB,mBAAmB,GAGnB,MAAM,wBAAwB,CAAC;AAqChC;;0DAE0D;AAC1D,MAAM,CAAC,MAAM,+BAA+B,GAAG,qBAAqB,CAAC;AAgBrE,SAAS,aAAa,CAAC,KAAsB;IAC5C,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,YAAY;YAChB,OAAO,OAAO,KAAK,CAAC,OAAO,YAAY,KAAK,CAAC,IAAI,EAAE,CAAC;QACrD,KAAK,SAAS;YACb,OAAO,WAAW,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,KAAK,EAAE,CAAC;QAChD,KAAK,cAAc;YAClB,OAAO,iCAAiC,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,KAAK,EAAE,CAAC;QACtE,KAAK,gBAAgB;YACpB,OAAO,mBAAmB,KAAK,CAAC,MAAM,WAAW,KAAK,CAAC,IAAI,G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AAG,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAChE,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,0BAA0B,GAAG,gBAAgB,CAAC;AAI3D,MAAM,6BAA6B,GAAwD;IAC1F,UAAU,EAAE,0BAA0B;IACtC,QAAQ,EAAE,QAAQ;IAClB,OAAO,EAAE,qBAAqB;IAC9B,KAAK,EAAE,kCAAkC;CACzC,CAAC;AAEF,MAAM,UAAU,2BAA2B,CAC1C,cAAyD,EACzD,MAA2B;IAE3B,OAAO,qBAAqB,CAAC,cAAc,EAAE,6BAA6B,EAAE,MAAM,CAAC,CAAC;AACrF,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,OAAgC;IACzE,OAAO,mBAAmB,CAAC,OAAO,EAAE,6BAA6B,CAAC,CAAC;AACpE,CAAC","sourcesContent":["import type { SessionEntry, SessionManager } from \"@caupulican/pi-agent-core/node\";\nimport type { LearningDecision } from \"../autonomy/contracts.ts\";\nimport {\n\tappendSessionSnapshot,\n\tgetSessionSnapshots,\n\ttype SessionSnapshotCodec,\n\ttype SessionSnapshotPayload,\n} from \"../session-snapshot.ts\";\nimport type { DurableChangeLayer, DurableChangeProposal } from \"./learning-gate.ts\";\nimport type { ReflectionWrite } from \"./reflection-engine.ts\";\n\n/**\n * Audit + rollback metadata for durable learning changes. Every reflection-sourced write that the\n * learning policy applies (or converts to a proposal) leaves one of these records in the session\n * log, so `/autonomy diagnostics` can explain what changed, why, and how to undo it — and\n * `rollbackLearningWrite` can execute the inverse operation.\n */\n\nexport type LearningRollbackKind =\n\t| \"memory_remove\"\n\t| \"memory_restore\"\n\t| \"memory_add\"\n\t| \"okf_remove\"\n\t| \"okf_organize\"\n\t| \"archive_skill\";\n\nexport interface LearningRollbackPlan {\n\tkind: LearningRollbackKind;\n\t/** Text currently present because of the change (to remove/replace), or the skill name to archive. */\n\ttarget?: string;\n\t/** Original text to restore (memory_restore/memory_add). */\n\tprevious?: string;\n\t/** Compare-and-delete guard for a structured record created by this exact audited write. */\n\texpectedDigest?: string;\n\t/** Organization rollback only: false when the OKF record predated this hot-memory move. */\n\tremoveOkf?: boolean;\n\tinstructions: string;\n}\n\n// \"apply_failed\" is distinct from \"apply\": the gate decided to apply, but the underlying write tool\n// (e.g. the memory tool) refused it (budget exceeded, drift, threat) without throwing. It must never\n// be treated as \"apply\" by a rollback-eligibility check — there is nothing durable to undo.\nexport type LearningAuditAction = \"apply\" | \"apply_failed\" | \"propose\" | \"rollback\";\n\n/** reasonCode for an \"apply_failed\" audit — distinct from the gate's own reasonCode (e.g.\n * \"eligible_auto_apply\") so the record is honest about WHERE it stopped: the gate approved the\n * write, but the write tool refused it after the fact. */\nexport const APPLY_WRITE_REFUSED_REASON_CODE = \"apply_write_refused\";\n\nexport interface LearningAuditRecord {\n\tid: string;\n\tproposalId: string;\n\tlayer: DurableChangeLayer;\n\taction: LearningAuditAction;\n\tsummary: string;\n\treasonCode: string;\n\tdecision: LearningDecision;\n\trollback?: LearningRollbackPlan;\n\t/** For action \"rollback\": the audit id of the applied change this record undoes. */\n\trollbackOf?: string;\n\tcreatedAt: string;\n}\n\nfunction describeWrite(write: ReflectionWrite): string {\n\tswitch (write.kind) {\n\t\tcase \"memory_add\":\n\t\t\treturn `Add ${write.section} memory: ${write.text}`;\n\t\tcase \"okf_add\":\n\t\t\treturn `Add OKF ${write.type}: ${write.title}`;\n\t\tcase \"okf_organize\":\n\t\t\treturn `Organize MEMORY fact into OKF ${write.type}: ${write.title}`;\n\t\tcase \"memory_replace\":\n\t\t\treturn `Replace memory \"${write.target}\" with \"${write.text}\"`;\n\t\tcase \"memory_remove\":\n\t\t\treturn `Remove memory: ${write.target}`;\n\t\tcase \"promote_skill\":\n\t\t\treturn `Promote skill \"${write.name}\": ${write.description}`;\n\t}\n}\n\nexport function proposalFromReflectionWrite(write: ReflectionWrite, proposalId: string): DurableChangeProposal {\n\treturn {\n\t\tid: proposalId,\n\t\tlayer: write.kind === \"promote_skill\" ? \"skill\" : \"memory\",\n\t\tsummary: describeWrite(write),\n\t\trollbackPlan: rollbackPlanForReflectionWrite(write).instructions,\n\t};\n}\n\n/**\n * Contradiction count a reflection write carries against existing durable knowledge. A\n * `memory_replace`/`memory_remove` is only emitted when the reflection engine CONFRONTS an existing\n * fact (it supersedes or deletes it) — that supersession is the gate's contradiction signal, so such\n * a write must route through approval rather than silently overwriting prior memory. A `memory_add`\n * or `promote_skill` is purely additive and contradicts nothing.\n */\nexport function contradictionsForReflectionWrite(write: ReflectionWrite): number {\n\tswitch (write.kind) {\n\t\tcase \"memory_replace\":\n\t\tcase \"memory_remove\":\n\t\t\treturn 1;\n\t\tcase \"memory_add\":\n\t\tcase \"okf_add\":\n\t\tcase \"okf_organize\":\n\t\tcase \"promote_skill\":\n\t\t\treturn 0;\n\t}\n}\n\nexport function rollbackPlanForReflectionWrite(write: ReflectionWrite): LearningRollbackPlan {\n\tswitch (write.kind) {\n\t\tcase \"memory_add\":\n\t\t\treturn {\n\t\t\t\tkind: \"memory_remove\",\n\t\t\t\ttarget: write.text,\n\t\t\t\tinstructions: `Remove the added ${write.section} memory text.`,\n\t\t\t};\n\t\tcase \"okf_add\":\n\t\t\treturn {\n\t\t\t\tkind: \"okf_remove\",\n\t\t\t\ttarget: `${write.type}\\0${write.title}`,\n\t\t\t\tinstructions: \"Remove the added structured OKF memory record.\",\n\t\t\t};\n\t\tcase \"okf_organize\":\n\t\t\treturn {\n\t\t\t\tkind: \"okf_organize\",\n\t\t\t\ttarget: `${write.type}\\0${write.title}`,\n\t\t\t\tprevious: write.sourceText,\n\t\t\t\tremoveOkf: true,\n\t\t\t\tinstructions: \"Restore the exact hot-memory source first, then remove only the audited OKF record.\",\n\t\t\t};\n\t\tcase \"memory_replace\":\n\t\t\treturn {\n\t\t\t\tkind: \"memory_restore\",\n\t\t\t\ttarget: write.text,\n\t\t\t\tprevious: write.target,\n\t\t\t\tinstructions: \"Replace the new memory text with the original text it overwrote.\",\n\t\t\t};\n\t\tcase \"memory_remove\":\n\t\t\treturn {\n\t\t\t\tkind: \"memory_add\",\n\t\t\t\tprevious: write.target,\n\t\t\t\tinstructions: \"Re-add the removed text to the MEMORY file (it may originally have lived in USER).\",\n\t\t\t};\n\t\tcase \"promote_skill\":\n\t\t\treturn {\n\t\t\t\tkind: \"archive_skill\",\n\t\t\t\ttarget: write.name,\n\t\t\t\tinstructions: \"Archive the promoted skill so it no longer loads.\",\n\t\t\t};\n\t}\n}\n\nconst AUDIT_ACTIONS: readonly string[] = [\"apply\", \"apply_failed\", \"propose\", \"rollback\"];\nconst ROLLBACK_KINDS: readonly string[] = [\n\t\"memory_remove\",\n\t\"memory_restore\",\n\t\"memory_add\",\n\t\"okf_remove\",\n\t\"okf_organize\",\n\t\"archive_skill\",\n];\nconst LAYERS: readonly string[] = [\"memory\", \"skill\", \"prompt\", \"extension\", \"tool\", \"script\", \"settings\", \"source\"];\n\nfunction isOptionalString(value: unknown): boolean {\n\treturn value === undefined || typeof value === \"string\";\n}\n\nfunction isLearningRollbackPlan(value: unknown): value is LearningRollbackPlan {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\tconst plan = value as Record<string, unknown>;\n\tif (typeof plan.kind !== \"string\" || !ROLLBACK_KINDS.includes(plan.kind)) return false;\n\tif (!isOptionalString(plan.target) || !isOptionalString(plan.previous)) return false;\n\tif (!isOptionalString(plan.expectedDigest)) return false;\n\tif (plan.removeOkf !== undefined && typeof plan.removeOkf !== \"boolean\") return false;\n\treturn typeof plan.instructions === \"string\";\n}\n\nfunction isLearningDecisionShape(value: unknown): value is LearningDecision {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\tconst decision = value as Record<string, unknown>;\n\treturn (\n\t\t(decision.kind === \"no-op\" || decision.kind === \"proposal\" || decision.kind === \"apply\") &&\n\t\ttypeof decision.reasonCode === \"string\" &&\n\t\ttypeof decision.confidence === \"number\" &&\n\t\ttypeof decision.summary === \"string\" &&\n\t\ttypeof decision.requiresApproval === \"boolean\"\n\t);\n}\n\nexport function isLearningAuditRecord(value: unknown): value is LearningAuditRecord {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\tconst record = value as Record<string, unknown>;\n\tif (typeof record.id !== \"string\" || record.id.length === 0) return false;\n\tif (typeof record.proposalId !== \"string\") return false;\n\tif (typeof record.layer !== \"string\" || !LAYERS.includes(record.layer)) return false;\n\tif (typeof record.action !== \"string\" || !AUDIT_ACTIONS.includes(record.action)) return false;\n\tif (typeof record.summary !== \"string\" || typeof record.reasonCode !== \"string\") return false;\n\tif (!isLearningDecisionShape(record.decision)) return false;\n\tif (record.rollback !== undefined && !isLearningRollbackPlan(record.rollback)) return false;\n\tif (!isOptionalString(record.rollbackOf)) return false;\n\treturn typeof record.createdAt === \"string\";\n}\n\nexport function cloneLearningAuditRecordForStorage(record: LearningAuditRecord): LearningAuditRecord {\n\treturn {\n\t\t...record,\n\t\tdecision: { ...record.decision },\n\t\t...(record.rollback ? { rollback: { ...record.rollback } } : {}),\n\t};\n}\n\nexport const LEARNING_AUDIT_CUSTOM_TYPE = \"learning_audit\";\n\nexport type LearningAuditSnapshotPayload = SessionSnapshotPayload<\"record\", LearningAuditRecord>;\n\nconst LEARNING_AUDIT_SNAPSHOT_CODEC: SessionSnapshotCodec<LearningAuditRecord, \"record\"> = {\n\tcustomType: LEARNING_AUDIT_CUSTOM_TYPE,\n\tvalueKey: \"record\",\n\tisValue: isLearningAuditRecord,\n\tclone: cloneLearningAuditRecordForStorage,\n};\n\nexport function appendLearningAuditSnapshot(\n\tsessionManager: Pick<SessionManager, \"appendCustomEntry\">,\n\trecord: LearningAuditRecord,\n): string {\n\treturn appendSessionSnapshot(sessionManager, LEARNING_AUDIT_SNAPSHOT_CODEC, record);\n}\n\nexport function getLearningAuditSnapshots(entries: readonly SessionEntry[]): LearningAuditRecord[] {\n\treturn getSessionSnapshots(entries, LEARNING_AUDIT_SNAPSHOT_CODEC);\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Usage } from "@caupulican/pi-ai";
|
|
2
|
+
import { type PiOkfType } from "../context/okf-memory.ts";
|
|
2
3
|
import { REFLECTION_SYSTEM_PROMPT } from "../provider-prompt-contracts.ts";
|
|
3
4
|
export { REFLECTION_SYSTEM_PROMPT };
|
|
4
5
|
export type StopReason = "stop" | "toolUse" | "aborted" | "error" | string;
|
|
@@ -35,6 +36,25 @@ export type ReflectionWrite = {
|
|
|
35
36
|
kind: "memory_add";
|
|
36
37
|
section: "MEMORY" | "USER";
|
|
37
38
|
text: string;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "okf_add";
|
|
41
|
+
type: PiOkfType;
|
|
42
|
+
title: string;
|
|
43
|
+
description: string;
|
|
44
|
+
scope: "project";
|
|
45
|
+
text: string;
|
|
46
|
+
tags?: string[];
|
|
47
|
+
evidenceRefs: string[];
|
|
48
|
+
} | {
|
|
49
|
+
kind: "okf_organize";
|
|
50
|
+
type: PiOkfType;
|
|
51
|
+
title: string;
|
|
52
|
+
description: string;
|
|
53
|
+
scope: "project";
|
|
54
|
+
text: string;
|
|
55
|
+
sourceText: string;
|
|
56
|
+
tags?: string[];
|
|
57
|
+
evidenceRefs: string[];
|
|
38
58
|
} | {
|
|
39
59
|
kind: "memory_replace";
|
|
40
60
|
target: string;
|
|
@@ -53,6 +73,10 @@ export interface ReflectionResult {
|
|
|
53
73
|
usage: Usage;
|
|
54
74
|
rationale: string;
|
|
55
75
|
}
|
|
76
|
+
/** Maximum number of durable writes accepted from one isolated reflection response. */
|
|
77
|
+
export declare const MAX_REFLECTION_WRITES = 32;
|
|
78
|
+
/** Maximum number of response entries inspected while looking for accepted writes. */
|
|
79
|
+
export declare const MAX_REFLECTION_SCAN_ENTRIES: number;
|
|
56
80
|
/**
|
|
57
81
|
* STATIC reflection system prompt (Hermes-parity #33). It is byte-identical across every reflection
|
|
58
82
|
* pass — the variable parts (existing memory snapshot + the turn transcript) live in the USER prompt —
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reflection-engine.d.ts","sourceRoot":"","sources":["../../../src/core/learning/reflection-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"reflection-engine.d.ts","sourceRoot":"","sources":["../../../src/core/learning/reflection-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAGN,KAAK,SAAS,EAEd,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAI3E,OAAO,EAAE,wBAAwB,EAAE,CAAC;AAEpC,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,CAAC;AAE3E,MAAM,WAAW,wBAAwB;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,KAAK,CAAC;IACb,UAAU,EAAE,UAAU,CAAC;CACvB;AAED,MAAM,MAAM,iBAAiB,GAAG,SAAS,GAAG,YAAY,GAAG,SAAS,GAAG,aAAa,GAAG,MAAM,CAAC;AAE9F,MAAM,WAAW,aAAa;IAC7B,OAAO,EAAE,iBAAiB,CAAC;IAC3B,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,OAAO,CAAC;IACvB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,YAAY,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,UAAU;IAC1B,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;CACpB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,UAAU,CA4B/D;AAED,MAAM,W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;AAE9E,MAAM,WAAW,gBAAgB;IAChC,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1B,KAAK,EAAE,KAAK,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CAClB;AAED,uFAAuF;AACvF,eAAO,MAAM,qBAAqB,KAAK,CAAC;AACxC,sFAAsF;AACtF,eAAO,MAAM,2BAA2B,QAA4B,CAAC;AAoGrE;;;;;GAKG;AACH,qBAAa,gBAAgB;IAC5B;;;;OAIG;IACG,OAAO,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAsF/D;CACD"}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { OKF_MEMORY_LIMITS, PI_OKF_TYPES, validateOkfMemoryDocumentInput, } from "../context/okf-memory.js";
|
|
1
2
|
import { REFLECTION_SYSTEM_PROMPT } from "../provider-prompt-contracts.js";
|
|
3
|
+
import { MAX_ACTIVE_SKILL_BODY_BYTES } from "../skill-vault.js";
|
|
4
|
+
import { MAX_SKILL_DESCRIPTION_LENGTH, MAX_SKILL_NAME_LENGTH } from "../skills.js";
|
|
2
5
|
export { REFLECTION_SYSTEM_PROMPT };
|
|
3
6
|
/**
|
|
4
7
|
* Pure zero-I/O heuristic to decide whether the current turn justifies a reflection run
|
|
@@ -30,6 +33,93 @@ export function decideDemand(signals) {
|
|
|
30
33
|
}
|
|
31
34
|
return { act: "skip", reason: "Signals do not justify reflection overhead", tokenBudget: 0 };
|
|
32
35
|
}
|
|
36
|
+
/** Maximum number of durable writes accepted from one isolated reflection response. */
|
|
37
|
+
export const MAX_REFLECTION_WRITES = 32;
|
|
38
|
+
/** Maximum number of response entries inspected while looking for accepted writes. */
|
|
39
|
+
export const MAX_REFLECTION_SCAN_ENTRIES = MAX_REFLECTION_WRITES * 4;
|
|
40
|
+
function isRecord(value) {
|
|
41
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
42
|
+
}
|
|
43
|
+
function isStringArray(value) {
|
|
44
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
45
|
+
}
|
|
46
|
+
function currentHotMemory(existingMemory) {
|
|
47
|
+
const marker = "## MEMORY.md:\n";
|
|
48
|
+
const markerIndex = existingMemory.indexOf(marker);
|
|
49
|
+
if (markerIndex === -1) {
|
|
50
|
+
// Unit callers may provide a raw MEMORY.md snapshot. A rendered persistent-memory block
|
|
51
|
+
// without this section, however, proves that MEMORY.md is empty; never treat OKF text as hot memory.
|
|
52
|
+
return existingMemory.includes("=== Persistent Memory (file-store) ===") ||
|
|
53
|
+
/<\s*untrusted_content\b/i.test(existingMemory)
|
|
54
|
+
? ""
|
|
55
|
+
: existingMemory;
|
|
56
|
+
}
|
|
57
|
+
const afterMemory = existingMemory.slice(markerIndex + marker.length);
|
|
58
|
+
const boundaries = [afterMemory.indexOf("\n## USER.md:"), afterMemory.search(/<\s*untrusted_content\b/i)].filter((index) => index >= 0);
|
|
59
|
+
const boundaryIndex = boundaries.length > 0 ? Math.min(...boundaries) : -1;
|
|
60
|
+
return boundaryIndex === -1 ? afterMemory : afterMemory.slice(0, boundaryIndex);
|
|
61
|
+
}
|
|
62
|
+
function containsExactHotMemoryItem(existingMemory, sourceText) {
|
|
63
|
+
if (sourceText.length === 0)
|
|
64
|
+
return false;
|
|
65
|
+
const memoryLines = currentHotMemory(existingMemory).split("\n");
|
|
66
|
+
const sourceLines = sourceText.split("\n");
|
|
67
|
+
return memoryLines.some((_, index) => sourceLines.every((line, offset) => memoryLines[index + offset] === line));
|
|
68
|
+
}
|
|
69
|
+
function boundedText(value, maxChars) {
|
|
70
|
+
return typeof value === "string" && value.length <= maxChars;
|
|
71
|
+
}
|
|
72
|
+
function boundedSkillBody(value) {
|
|
73
|
+
return typeof value === "string" && Buffer.byteLength(value, "utf8") <= MAX_ACTIVE_SKILL_BODY_BYTES;
|
|
74
|
+
}
|
|
75
|
+
/** One parser/validation path for both structured reflection operations. */
|
|
76
|
+
function parseStructuredReflectionWrite(value, existingMemory) {
|
|
77
|
+
if (!isRecord(value) || (value.kind !== "okf_add" && value.kind !== "okf_organize"))
|
|
78
|
+
return undefined;
|
|
79
|
+
if (!PI_OKF_TYPES.includes(value.type) ||
|
|
80
|
+
typeof value.title !== "string" ||
|
|
81
|
+
typeof value.description !== "string" ||
|
|
82
|
+
value.scope !== "project" ||
|
|
83
|
+
typeof value.text !== "string" ||
|
|
84
|
+
!isStringArray(value.evidenceRefs) ||
|
|
85
|
+
value.evidenceRefs.length === 0 ||
|
|
86
|
+
(value.tags !== undefined && !isStringArray(value.tags))) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
const common = {
|
|
90
|
+
type: value.type,
|
|
91
|
+
title: value.title,
|
|
92
|
+
description: value.description,
|
|
93
|
+
scope: "project",
|
|
94
|
+
text: value.text,
|
|
95
|
+
...(value.tags !== undefined ? { tags: value.tags } : {}),
|
|
96
|
+
evidenceRefs: value.evidenceRefs,
|
|
97
|
+
};
|
|
98
|
+
let write;
|
|
99
|
+
if (value.kind === "okf_organize") {
|
|
100
|
+
if (typeof value.sourceText !== "string" ||
|
|
101
|
+
value.sourceText.length === 0 ||
|
|
102
|
+
value.sourceText.length > OKF_MEMORY_LIMITS.bodyChars ||
|
|
103
|
+
!containsExactHotMemoryItem(existingMemory, value.sourceText)) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
write = { kind: "okf_organize", ...common, sourceText: value.sourceText };
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
write = { kind: "okf_add", ...common };
|
|
110
|
+
}
|
|
111
|
+
return validateOkfMemoryDocumentInput({
|
|
112
|
+
type: write.type,
|
|
113
|
+
title: write.title,
|
|
114
|
+
description: write.description,
|
|
115
|
+
scope: write.scope,
|
|
116
|
+
body: write.text,
|
|
117
|
+
tags: write.tags,
|
|
118
|
+
evidenceRefs: write.evidenceRefs,
|
|
119
|
+
}, { projectOnly: true, requireEvidence: true }).length === 0
|
|
120
|
+
? write
|
|
121
|
+
: undefined;
|
|
122
|
+
}
|
|
33
123
|
/**
|
|
34
124
|
* STATIC reflection system prompt (Hermes-parity #33). It is byte-identical across every reflection
|
|
35
125
|
* pass — the variable parts (existing memory snapshot + the turn transcript) live in the USER prompt —
|
|
@@ -69,25 +159,31 @@ Return JSON updates.`;
|
|
|
69
159
|
const rationale = parsed.rationale || "";
|
|
70
160
|
const writes = [];
|
|
71
161
|
if (Array.isArray(parsed.writes)) {
|
|
72
|
-
|
|
162
|
+
const scanCount = Math.min(parsed.writes.length, MAX_REFLECTION_SCAN_ENTRIES);
|
|
163
|
+
for (let index = 0; index < scanCount && writes.length < MAX_REFLECTION_WRITES; index++) {
|
|
164
|
+
const w = parsed.writes[index];
|
|
73
165
|
if (w && typeof w === "object") {
|
|
166
|
+
const structuredWrite = parseStructuredReflectionWrite(w, input.existingMemory);
|
|
74
167
|
if (w.kind === "memory_add" &&
|
|
75
168
|
(w.section === "MEMORY" || w.section === "USER") &&
|
|
76
|
-
|
|
169
|
+
boundedText(w.text, OKF_MEMORY_LIMITS.bodyChars)) {
|
|
77
170
|
writes.push({ kind: "memory_add", section: w.section, text: w.text });
|
|
78
171
|
}
|
|
172
|
+
else if (structuredWrite) {
|
|
173
|
+
writes.push(structuredWrite);
|
|
174
|
+
}
|
|
79
175
|
else if (w.kind === "memory_replace" &&
|
|
80
|
-
|
|
81
|
-
|
|
176
|
+
boundedText(w.target, OKF_MEMORY_LIMITS.bodyChars) &&
|
|
177
|
+
boundedText(w.text, OKF_MEMORY_LIMITS.bodyChars)) {
|
|
82
178
|
writes.push({ kind: "memory_replace", target: w.target, text: w.text });
|
|
83
179
|
}
|
|
84
|
-
else if (w.kind === "memory_remove" &&
|
|
180
|
+
else if (w.kind === "memory_remove" && boundedText(w.target, OKF_MEMORY_LIMITS.bodyChars)) {
|
|
85
181
|
writes.push({ kind: "memory_remove", target: w.target });
|
|
86
182
|
}
|
|
87
183
|
else if (w.kind === "promote_skill" &&
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
184
|
+
boundedText(w.name, MAX_SKILL_NAME_LENGTH) &&
|
|
185
|
+
boundedText(w.description, MAX_SKILL_DESCRIPTION_LENGTH) &&
|
|
186
|
+
boundedSkillBody(w.body)) {
|
|
91
187
|
writes.push({ kind: "promote_skill", name: w.name, description: w.description, body: w.body });
|
|
92
188
|
}
|
|
93
189
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reflection-engine.js","sourceRoot":"","sources":["../../../src/core/learning/reflection-engine.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAE3E,OAAO,EAAE,wBAAwB,EAAE,CAAC;AA0BpC;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,OAAsB;IAClD,IAAI,OAAO,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;QAChC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,qBAAqB,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC;IACvE,CAAC;IACD,IAAI,OAAO,CAAC,kBAAkB,GAAG,EAAE,EAAE,CAAC;QACrC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,4CAA4C,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC;IAC9F,CAAC;IAED,+FAA+F;IAC/F,MAAM,UAAU,GAAG,IAAI,CAAC;IACxB,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,CAAC,OAAO,CAAC,kBAAkB,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAE/G,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;QAC3B,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,iCAAiC,EAAE,WAAW,EAAE,CAAC;IACnF,CAAC;IACD,IAAI,OAAO,CAAC,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":["import type { Usage } from \"@caupulican/pi-ai\";\nimport { REFLECTION_SYSTEM_PROMPT } from \"../provider-prompt-contracts.ts\";\n\nexport { REFLECTION_SYSTEM_PROMPT };\n\nexport type StopReason = \"stop\" | \"toolUse\" | \"aborted\" | \"error\" | string;\n\nexport interface IsolatedCompletionResult {\n\ttext: string;\n\tusage: Usage;\n\tstopReason: StopReason;\n}\n\nexport type ReflectionTrigger = \"complex\" | \"corrective\" | \"durable\" | \"session-end\" | \"none\";\n\nexport interface DemandSignals {\n\ttrigger: ReflectionTrigger;\n\ttoolCallCount: number;\n\thadCorrection: boolean;\n\tcontextHeadroomPct: number; // 0..100\n\tusefulLately: number; // 0..1 rolling score\n}\n\nexport interface DemandPlan {\n\tact: \"skip\" | \"reflect\";\n\treason: string;\n\ttokenBudget: number;\n}\n\n/**\n * Pure zero-I/O heuristic to decide whether the current turn justifies a reflection run\n * and determine the token budget under the cheap-tool net-negative doctrine.\n */\nexport function decideDemand(signals: DemandSignals): DemandPlan {\n\tif (signals.trigger === \"none\") {\n\t\treturn { act: \"skip\", reason: \"No trigger detected\", tokenBudget: 0 };\n\t}\n\tif (signals.contextHeadroomPct < 10) {\n\t\treturn { act: \"skip\", reason: \"Context headroom is critically low (< 10%)\", tokenBudget: 0 };\n\t}\n\n\t// Dynamic token budget based on headroom (keep reflection bounded between 500 and 1500 tokens)\n\tconst baseBudget = 1000;\n\tconst tokenBudget = Math.max(500, Math.min(1500, Math.round(baseBudget * (signals.contextHeadroomPct / 100))));\n\n\tif (signals.hadCorrection) {\n\t\treturn { act: \"reflect\", reason: \"Correction detected in the turn\", tokenBudget };\n\t}\n\tif (signals.trigger === \"durable\") {\n\t\treturn { act: \"reflect\", reason: \"Durable fact or preference detected in the turn\", tokenBudget };\n\t}\n\tif (signals.trigger === \"session-end\") {\n\t\treturn { act: \"reflect\", reason: \"Session end reflection triggered\", tokenBudget };\n\t}\n\tif (signals.trigger === \"complex\") {\n\t\tif (signals.toolCallCount >= 3) {\n\t\t\treturn { act: \"reflect\", reason: `Complex turn with ${signals.toolCallCount} tool calls`, tokenBudget };\n\t\t}\n\t}\n\n\treturn { act: \"skip\", reason: \"Signals do not justify reflection overhead\", tokenBudget: 0 };\n}\n\nexport interface ReflectionInput {\n\trecentTurnText: string; // host serializes the just-finished turn\n\texistingMemory: string; // current MEMORY.md + USER.md snapshot\n\tplan: DemandPlan;\n\t// host-injected isolated completion function:\n\tcomplete: (systemPrompt: string, userPrompt: string) => Promise<IsolatedCompletionResult>;\n}\n\nexport type ReflectionWrite =\n\t| { kind: \"memory_add\"; section: \"MEMORY\" | \"USER\"; text: string }\n\t| { kind: \"memory_replace\"; target: string; text: string }\n\t| { kind: \"memory_remove\"; target: string }\n\t// R7 memory-to-behavior: promote a recurring procedural workflow into an executable skill.\n\t| { kind: \"promote_skill\"; name: string; description: string; body: string };\n\nexport interface ReflectionResult {\n\twrites: ReflectionWrite[];\n\tusage: Usage;\n\trationale: string;\n}\n\n/**\n * STATIC reflection system prompt (Hermes-parity #33). It is byte-identical across every reflection\n * pass — the variable parts (existing memory snapshot + the turn transcript) live in the USER prompt —\n * so the provider prompt-cache reuses this prefix instead of re-billing it each pass (cost guard).\n * Do NOT interpolate per-call data into this constant or caching breaks.\n */\nexport class ReflectionEngine {\n\t/**\n\t * Build the reflection prompt, call the injected isolated complete(),\n\t * parse the response, confront existing memory, and return memory writes.\n\t * Zero direct I/O.\n\t */\n\tasync reflect(input: ReflectionInput): Promise<ReflectionResult> {\n\t\tconst systemPrompt = REFLECTION_SYSTEM_PROMPT;\n\n\t\t// Variable inputs go in the USER prompt so the system prefix above stays cache-stable (#33).\n\t\tconst userPrompt = `Existing memory:\n${input.existingMemory}\n\nRecent turn transcript:\n${input.recentTurnText}\n\nReturn JSON updates.`;\n\n\t\tlet usage: Usage | undefined;\n\t\ttry {\n\t\t\tconst compResult = await input.complete(systemPrompt, userPrompt);\n\t\t\tusage = compResult.usage;\n\t\t\tconst text = compResult.text;\n\n\t\t\tconst jsonMatch = text.match(/```json\\s*([\\s\\S]*?)\\s*```/) || text.match(/{[\\s\\S]*}/);\n\t\t\tif (!jsonMatch) {\n\t\t\t\treturn {\n\t\t\t\t\twrites: [],\n\t\t\t\t\tusage: compResult.usage,\n\t\t\t\t\trationale: `Failed to locate JSON response. Raw text:\\n${text}`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst parsed = JSON.parse(jsonMatch[1] || jsonMatch[0]);\n\t\t\tconst rationale = parsed.rationale || \"\";\n\t\t\tconst writes: ReflectionWrite[] = [];\n\n\t\t\tif (Array.isArray(parsed.writes)) {\n\t\t\t\tfor (const w of parsed.writes) {\n\t\t\t\t\tif (w && typeof w === \"object\") {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tw.kind === \"memory_add\" &&\n\t\t\t\t\t\t\t(w.section === \"MEMORY\" || w.section === \"USER\") &&\n\t\t\t\t\t\t\ttypeof w.text === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_add\", section: w.section, text: w.text });\n\t\t\t\t\t\t} else if (\n\t\t\t\t\t\t\tw.kind === \"memory_replace\" &&\n\t\t\t\t\t\t\ttypeof w.target === \"string\" &&\n\t\t\t\t\t\t\ttypeof w.text === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_replace\", target: w.target, text: w.text });\n\t\t\t\t\t\t} else if (w.kind === \"memory_remove\" && typeof w.target === \"string\") {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_remove\", target: w.target });\n\t\t\t\t\t\t} else if (\n\t\t\t\t\t\t\tw.kind === \"promote_skill\" &&\n\t\t\t\t\t\t\ttypeof w.name === \"string\" &&\n\t\t\t\t\t\t\ttypeof w.description === \"string\" &&\n\t\t\t\t\t\t\ttypeof w.body === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"promote_skill\", name: w.name, description: w.description, body: w.body });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twrites,\n\t\t\t\tusage: compResult.usage,\n\t\t\t\trationale,\n\t\t\t};\n\t\t} catch (err) {\n\t\t\t// Zeroed/fallback usage representation\n\t\t\tconst emptyUsage: Usage = {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t};\n\t\t\treturn {\n\t\t\t\twrites: [],\n\t\t\t\tusage: usage ?? emptyUsage,\n\t\t\t\trationale: `Error during reflection: ${String(err)}`,\n\t\t\t};\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"reflection-engine.js","sourceRoot":"","sources":["../../../src/core/learning/reflection-engine.ts"],"names":[],"mappings":"AACA,OAAO,EACN,iBAAiB,EACjB,YAAY,EAEZ,8BAA8B,GAC9B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,4BAA4B,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAEnF,OAAO,EAAE,wBAAwB,EAAE,CAAC;AA0BpC;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,OAAsB;IAClD,IAAI,OAAO,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;QAChC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,qBAAqB,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC;IACvE,CAAC;IACD,IAAI,OAAO,CAAC,kBAAkB,GAAG,EAAE,EAAE,CAAC;QACrC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,4CAA4C,EAAE,WAAW,EAAE,CAA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,UAAU,EAAE,CAAC;gBACb,WAAW,EAAE,CAAC;gBACd,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE;aACpE,CAAC;YACF,OAAO;gBACN,MAAM,EAAE,EAAE;gBACV,KAAK,EAAE,KAAK,IAAI,UAAU;gBAC1B,SAAS,EAAE,4BAA4B,MAAM,CAAC,GAAG,CAAC,EAAE;aACpD,CAAC;QACH,CAAC;IACF,CAAC;CACD","sourcesContent":["import type { Usage } from \"@caupulican/pi-ai\";\nimport {\n\tOKF_MEMORY_LIMITS,\n\tPI_OKF_TYPES,\n\ttype PiOkfType,\n\tvalidateOkfMemoryDocumentInput,\n} from \"../context/okf-memory.ts\";\nimport { REFLECTION_SYSTEM_PROMPT } from \"../provider-prompt-contracts.ts\";\nimport { MAX_ACTIVE_SKILL_BODY_BYTES } from \"../skill-vault.ts\";\nimport { MAX_SKILL_DESCRIPTION_LENGTH, MAX_SKILL_NAME_LENGTH } from \"../skills.ts\";\n\nexport { REFLECTION_SYSTEM_PROMPT };\n\nexport type StopReason = \"stop\" | \"toolUse\" | \"aborted\" | \"error\" | string;\n\nexport interface IsolatedCompletionResult {\n\ttext: string;\n\tusage: Usage;\n\tstopReason: StopReason;\n}\n\nexport type ReflectionTrigger = \"complex\" | \"corrective\" | \"durable\" | \"session-end\" | \"none\";\n\nexport interface DemandSignals {\n\ttrigger: ReflectionTrigger;\n\ttoolCallCount: number;\n\thadCorrection: boolean;\n\tcontextHeadroomPct: number; // 0..100\n\tusefulLately: number; // 0..1 rolling score\n}\n\nexport interface DemandPlan {\n\tact: \"skip\" | \"reflect\";\n\treason: string;\n\ttokenBudget: number;\n}\n\n/**\n * Pure zero-I/O heuristic to decide whether the current turn justifies a reflection run\n * and determine the token budget under the cheap-tool net-negative doctrine.\n */\nexport function decideDemand(signals: DemandSignals): DemandPlan {\n\tif (signals.trigger === \"none\") {\n\t\treturn { act: \"skip\", reason: \"No trigger detected\", tokenBudget: 0 };\n\t}\n\tif (signals.contextHeadroomPct < 10) {\n\t\treturn { act: \"skip\", reason: \"Context headroom is critically low (< 10%)\", tokenBudget: 0 };\n\t}\n\n\t// Dynamic token budget based on headroom (keep reflection bounded between 500 and 1500 tokens)\n\tconst baseBudget = 1000;\n\tconst tokenBudget = Math.max(500, Math.min(1500, Math.round(baseBudget * (signals.contextHeadroomPct / 100))));\n\n\tif (signals.hadCorrection) {\n\t\treturn { act: \"reflect\", reason: \"Correction detected in the turn\", tokenBudget };\n\t}\n\tif (signals.trigger === \"durable\") {\n\t\treturn { act: \"reflect\", reason: \"Durable fact or preference detected in the turn\", tokenBudget };\n\t}\n\tif (signals.trigger === \"session-end\") {\n\t\treturn { act: \"reflect\", reason: \"Session end reflection triggered\", tokenBudget };\n\t}\n\tif (signals.trigger === \"complex\") {\n\t\tif (signals.toolCallCount >= 3) {\n\t\t\treturn { act: \"reflect\", reason: `Complex turn with ${signals.toolCallCount} tool calls`, tokenBudget };\n\t\t}\n\t}\n\n\treturn { act: \"skip\", reason: \"Signals do not justify reflection overhead\", tokenBudget: 0 };\n}\n\nexport interface ReflectionInput {\n\trecentTurnText: string; // host serializes the just-finished turn\n\texistingMemory: string; // current MEMORY.md + USER.md snapshot\n\tplan: DemandPlan;\n\t// host-injected isolated completion function:\n\tcomplete: (systemPrompt: string, userPrompt: string) => Promise<IsolatedCompletionResult>;\n}\n\nexport type ReflectionWrite =\n\t| { kind: \"memory_add\"; section: \"MEMORY\" | \"USER\"; text: string }\n\t| {\n\t\t\tkind: \"okf_add\";\n\t\t\ttype: PiOkfType;\n\t\t\ttitle: string;\n\t\t\tdescription: string;\n\t\t\tscope: \"project\";\n\t\t\ttext: string;\n\t\t\ttags?: string[];\n\t\t\tevidenceRefs: string[];\n\t }\n\t| {\n\t\t\tkind: \"okf_organize\";\n\t\t\ttype: PiOkfType;\n\t\t\ttitle: string;\n\t\t\tdescription: string;\n\t\t\tscope: \"project\";\n\t\t\ttext: string;\n\t\t\tsourceText: string;\n\t\t\ttags?: string[];\n\t\t\tevidenceRefs: string[];\n\t }\n\t| { kind: \"memory_replace\"; target: string; text: string }\n\t| { kind: \"memory_remove\"; target: string }\n\t// R7 memory-to-behavior: promote a recurring procedural workflow into an executable skill.\n\t| { kind: \"promote_skill\"; name: string; description: string; body: string };\n\nexport interface ReflectionResult {\n\twrites: ReflectionWrite[];\n\tusage: Usage;\n\trationale: string;\n}\n\n/** Maximum number of durable writes accepted from one isolated reflection response. */\nexport const MAX_REFLECTION_WRITES = 32;\n/** Maximum number of response entries inspected while looking for accepted writes. */\nexport const MAX_REFLECTION_SCAN_ENTRIES = MAX_REFLECTION_WRITES * 4;\n\ntype StructuredReflectionWrite = Extract<ReflectionWrite, { kind: \"okf_add\" | \"okf_organize\" }>;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction isStringArray(value: unknown): value is string[] {\n\treturn Array.isArray(value) && value.every((entry) => typeof entry === \"string\");\n}\n\nfunction currentHotMemory(existingMemory: string): string {\n\tconst marker = \"## MEMORY.md:\\n\";\n\tconst markerIndex = existingMemory.indexOf(marker);\n\tif (markerIndex === -1) {\n\t\t// Unit callers may provide a raw MEMORY.md snapshot. A rendered persistent-memory block\n\t\t// without this section, however, proves that MEMORY.md is empty; never treat OKF text as hot memory.\n\t\treturn existingMemory.includes(\"=== Persistent Memory (file-store) ===\") ||\n\t\t\t/<\\s*untrusted_content\\b/i.test(existingMemory)\n\t\t\t? \"\"\n\t\t\t: existingMemory;\n\t}\n\tconst afterMemory = existingMemory.slice(markerIndex + marker.length);\n\tconst boundaries = [afterMemory.indexOf(\"\\n## USER.md:\"), afterMemory.search(/<\\s*untrusted_content\\b/i)].filter(\n\t\t(index) => index >= 0,\n\t);\n\tconst boundaryIndex = boundaries.length > 0 ? Math.min(...boundaries) : -1;\n\treturn boundaryIndex === -1 ? afterMemory : afterMemory.slice(0, boundaryIndex);\n}\n\nfunction containsExactHotMemoryItem(existingMemory: string, sourceText: string): boolean {\n\tif (sourceText.length === 0) return false;\n\tconst memoryLines = currentHotMemory(existingMemory).split(\"\\n\");\n\tconst sourceLines = sourceText.split(\"\\n\");\n\treturn memoryLines.some((_, index) => sourceLines.every((line, offset) => memoryLines[index + offset] === line));\n}\n\nfunction boundedText(value: unknown, maxChars: number): value is string {\n\treturn typeof value === \"string\" && value.length <= maxChars;\n}\n\nfunction boundedSkillBody(value: unknown): value is string {\n\treturn typeof value === \"string\" && Buffer.byteLength(value, \"utf8\") <= MAX_ACTIVE_SKILL_BODY_BYTES;\n}\n\n/** One parser/validation path for both structured reflection operations. */\nfunction parseStructuredReflectionWrite(value: unknown, existingMemory: string): StructuredReflectionWrite | undefined {\n\tif (!isRecord(value) || (value.kind !== \"okf_add\" && value.kind !== \"okf_organize\")) return undefined;\n\tif (\n\t\t!PI_OKF_TYPES.includes(value.type as PiOkfType) ||\n\t\ttypeof value.title !== \"string\" ||\n\t\ttypeof value.description !== \"string\" ||\n\t\tvalue.scope !== \"project\" ||\n\t\ttypeof value.text !== \"string\" ||\n\t\t!isStringArray(value.evidenceRefs) ||\n\t\tvalue.evidenceRefs.length === 0 ||\n\t\t(value.tags !== undefined && !isStringArray(value.tags))\n\t) {\n\t\treturn undefined;\n\t}\n\tconst common = {\n\t\ttype: value.type as PiOkfType,\n\t\ttitle: value.title,\n\t\tdescription: value.description,\n\t\tscope: \"project\" as const,\n\t\ttext: value.text,\n\t\t...(value.tags !== undefined ? { tags: value.tags } : {}),\n\t\tevidenceRefs: value.evidenceRefs,\n\t};\n\tlet write: StructuredReflectionWrite;\n\tif (value.kind === \"okf_organize\") {\n\t\tif (\n\t\t\ttypeof value.sourceText !== \"string\" ||\n\t\t\tvalue.sourceText.length === 0 ||\n\t\t\tvalue.sourceText.length > OKF_MEMORY_LIMITS.bodyChars ||\n\t\t\t!containsExactHotMemoryItem(existingMemory, value.sourceText)\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\twrite = { kind: \"okf_organize\", ...common, sourceText: value.sourceText };\n\t} else {\n\t\twrite = { kind: \"okf_add\", ...common };\n\t}\n\treturn validateOkfMemoryDocumentInput(\n\t\t{\n\t\t\ttype: write.type,\n\t\t\ttitle: write.title,\n\t\t\tdescription: write.description,\n\t\t\tscope: write.scope,\n\t\t\tbody: write.text,\n\t\t\ttags: write.tags,\n\t\t\tevidenceRefs: write.evidenceRefs,\n\t\t},\n\t\t{ projectOnly: true, requireEvidence: true },\n\t).length === 0\n\t\t? write\n\t\t: undefined;\n}\n\n/**\n * STATIC reflection system prompt (Hermes-parity #33). It is byte-identical across every reflection\n * pass — the variable parts (existing memory snapshot + the turn transcript) live in the USER prompt —\n * so the provider prompt-cache reuses this prefix instead of re-billing it each pass (cost guard).\n * Do NOT interpolate per-call data into this constant or caching breaks.\n */\nexport class ReflectionEngine {\n\t/**\n\t * Build the reflection prompt, call the injected isolated complete(),\n\t * parse the response, confront existing memory, and return memory writes.\n\t * Zero direct I/O.\n\t */\n\tasync reflect(input: ReflectionInput): Promise<ReflectionResult> {\n\t\tconst systemPrompt = REFLECTION_SYSTEM_PROMPT;\n\n\t\t// Variable inputs go in the USER prompt so the system prefix above stays cache-stable (#33).\n\t\tconst userPrompt = `Existing memory:\n${input.existingMemory}\n\nRecent turn transcript:\n${input.recentTurnText}\n\nReturn JSON updates.`;\n\n\t\tlet usage: Usage | undefined;\n\t\ttry {\n\t\t\tconst compResult = await input.complete(systemPrompt, userPrompt);\n\t\t\tusage = compResult.usage;\n\t\t\tconst text = compResult.text;\n\n\t\t\tconst jsonMatch = text.match(/```json\\s*([\\s\\S]*?)\\s*```/) || text.match(/{[\\s\\S]*}/);\n\t\t\tif (!jsonMatch) {\n\t\t\t\treturn {\n\t\t\t\t\twrites: [],\n\t\t\t\t\tusage: compResult.usage,\n\t\t\t\t\trationale: `Failed to locate JSON response. Raw text:\\n${text}`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst parsed = JSON.parse(jsonMatch[1] || jsonMatch[0]);\n\t\t\tconst rationale = parsed.rationale || \"\";\n\t\t\tconst writes: ReflectionWrite[] = [];\n\n\t\t\tif (Array.isArray(parsed.writes)) {\n\t\t\t\tconst scanCount = Math.min(parsed.writes.length, MAX_REFLECTION_SCAN_ENTRIES);\n\t\t\t\tfor (let index = 0; index < scanCount && writes.length < MAX_REFLECTION_WRITES; index++) {\n\t\t\t\t\tconst w = parsed.writes[index];\n\t\t\t\t\tif (w && typeof w === \"object\") {\n\t\t\t\t\t\tconst structuredWrite = parseStructuredReflectionWrite(w, input.existingMemory);\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tw.kind === \"memory_add\" &&\n\t\t\t\t\t\t\t(w.section === \"MEMORY\" || w.section === \"USER\") &&\n\t\t\t\t\t\t\tboundedText(w.text, OKF_MEMORY_LIMITS.bodyChars)\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_add\", section: w.section, text: w.text });\n\t\t\t\t\t\t} else if (structuredWrite) {\n\t\t\t\t\t\t\twrites.push(structuredWrite);\n\t\t\t\t\t\t} else if (\n\t\t\t\t\t\t\tw.kind === \"memory_replace\" &&\n\t\t\t\t\t\t\tboundedText(w.target, OKF_MEMORY_LIMITS.bodyChars) &&\n\t\t\t\t\t\t\tboundedText(w.text, OKF_MEMORY_LIMITS.bodyChars)\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_replace\", target: w.target, text: w.text });\n\t\t\t\t\t\t} else if (w.kind === \"memory_remove\" && boundedText(w.target, OKF_MEMORY_LIMITS.bodyChars)) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_remove\", target: w.target });\n\t\t\t\t\t\t} else if (\n\t\t\t\t\t\t\tw.kind === \"promote_skill\" &&\n\t\t\t\t\t\t\tboundedText(w.name, MAX_SKILL_NAME_LENGTH) &&\n\t\t\t\t\t\t\tboundedText(w.description, MAX_SKILL_DESCRIPTION_LENGTH) &&\n\t\t\t\t\t\t\tboundedSkillBody(w.body)\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"promote_skill\", name: w.name, description: w.description, body: w.body });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twrites,\n\t\t\t\tusage: compResult.usage,\n\t\t\t\trationale,\n\t\t\t};\n\t\t} catch (err) {\n\t\t\t// Zeroed/fallback usage representation\n\t\t\tconst emptyUsage: Usage = {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t};\n\t\t\treturn {\n\t\t\t\twrites: [],\n\t\t\t\tusage: usage ?? emptyUsage,\n\t\t\t\trationale: `Error during reflection: ${String(err)}`,\n\t\t\t};\n\t\t}\n\t}\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Best-effort, preemptible standing-prefix warmup for loopback OpenAI-compatible models. */
|
|
2
|
+
import type { AgentTool, StreamFn } from "@caupulican/pi-agent-core/types";
|
|
3
|
+
import type { Api, Model, SimpleStreamOptions } from "@caupulican/pi-ai";
|
|
4
|
+
import type { RequestAuth } from "./request-auth.ts";
|
|
5
|
+
export interface LocalPrefixWarmControllerDeps {
|
|
6
|
+
getStreamFn(): StreamFn;
|
|
7
|
+
getTools(): AgentTool[];
|
|
8
|
+
getSystemPrompt(): string;
|
|
9
|
+
getRequestHooks(): Pick<SimpleStreamOptions, "onPayload" | "onResponse">;
|
|
10
|
+
isRawStreamSimple(streamFn: StreamFn): boolean;
|
|
11
|
+
getRequiredRequestAuth(model: Model<Api>): Promise<RequestAuth>;
|
|
12
|
+
ensureManagedModelReady(model: Model<Api>): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export declare function isWarmableLocalModel(model: Model<Api>): boolean;
|
|
15
|
+
export declare class LocalPrefixWarmController {
|
|
16
|
+
private readonly deps;
|
|
17
|
+
private active;
|
|
18
|
+
private readonly completed;
|
|
19
|
+
constructor(deps: LocalPrefixWarmControllerDeps);
|
|
20
|
+
schedule(model: Model<Api> | undefined): void;
|
|
21
|
+
cancel(): void;
|
|
22
|
+
private run;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=local-prefix-warm-controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-prefix-warm-controller.d.ts","sourceRoot":"","sources":["../../src/core/local-prefix-warm-controller.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAG7F,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAIzE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,MAAM,WAAW,6BAA6B;IAC7C,WAAW,IAAI,QAAQ,CAAC;IACxB,QAAQ,IAAI,SAAS,EAAE,CAAC;IACxB,eAAe,IAAI,MAAM,CAAC;IAC1B,eAAe,IAAI,IAAI,CAAC,mBAAmB,EAAE,WAAW,GAAG,YAAY,CAAC,CAAC;IACzE,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC;IAC/C,sBAAsB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IAChE,uBAAuB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,OAAO,CAE/D;AAED,qBAAa,yBAAyB;IACrC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAgC;IACrD,OAAO,CAAC,MAAM,CAAmG;IACjH,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAE/C,YAAY,IAAI,EAAE,6BAA6B,EAE9C;IAED,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,GAAG,IAAI,CAc5C;IAED,MAAM,IAAI,IAAI,CAMb;YAEa,GAAG;CAmCjB"}
|