@caupulican/pi-adaptative 0.81.39 → 0.81.40
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 +52 -0
- package/README.md +2 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts +2 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js +2 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.ts +3 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts +9 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js +290 -107
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.ts +357 -122
- package/dist/bundled-resources/skills/tool-call-repair/references/repair-catalogue.md +2 -2
- package/dist/cli/args.d.ts +2 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +9 -0
- package/dist/cli/args.js.map +1 -1
- package/dist/core/agent-directory-layout.d.ts +15 -0
- package/dist/core/agent-directory-layout.d.ts.map +1 -0
- package/dist/core/agent-directory-layout.js +72 -0
- package/dist/core/agent-directory-layout.js.map +1 -0
- package/dist/core/agent-paths.d.ts +19 -2
- package/dist/core/agent-paths.d.ts.map +1 -1
- package/dist/core/agent-paths.js +67 -4
- package/dist/core/agent-paths.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts +52 -8
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +397 -79
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts +1 -0
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +1 -0
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +71 -136
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +477 -1318
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/autonomy/approval-gate.d.ts +3 -2
- package/dist/core/autonomy/approval-gate.d.ts.map +1 -1
- package/dist/core/autonomy/approval-gate.js +6 -28
- package/dist/core/autonomy/approval-gate.js.map +1 -1
- package/dist/core/autonomy/bounded-completion.d.ts +1 -1
- package/dist/core/autonomy/bounded-completion.d.ts.map +1 -1
- package/dist/core/autonomy/bounded-completion.js +1 -1
- package/dist/core/autonomy/bounded-completion.js.map +1 -1
- package/dist/core/autonomy/contracts.d.ts +20 -12
- package/dist/core/autonomy/contracts.d.ts.map +1 -1
- package/dist/core/autonomy/contracts.js.map +1 -1
- package/dist/core/autonomy/foreground-envelope.d.ts +8 -2
- package/dist/core/autonomy/foreground-envelope.d.ts.map +1 -1
- package/dist/core/autonomy/foreground-envelope.js +8 -25
- package/dist/core/autonomy/foreground-envelope.js.map +1 -1
- package/dist/core/autonomy/gates.d.ts.map +1 -1
- package/dist/core/autonomy/gates.js +8 -10
- package/dist/core/autonomy/gates.js.map +1 -1
- package/dist/core/autonomy/lane-private-paths.d.ts +2 -0
- package/dist/core/autonomy/lane-private-paths.d.ts.map +1 -0
- package/dist/core/autonomy/lane-private-paths.js +16 -0
- package/dist/core/autonomy/lane-private-paths.js.map +1 -0
- package/dist/core/autonomy/lane-tool-surface.d.ts +10 -0
- package/dist/core/autonomy/lane-tool-surface.d.ts.map +1 -1
- package/dist/core/autonomy/lane-tool-surface.js +46 -8
- package/dist/core/autonomy/lane-tool-surface.js.map +1 -1
- package/dist/core/autonomy/lane-tracker.d.ts +14 -0
- package/dist/core/autonomy/lane-tracker.d.ts.map +1 -1
- package/dist/core/autonomy/lane-tracker.js +38 -0
- package/dist/core/autonomy/lane-tracker.js.map +1 -1
- package/dist/core/autonomy/risk-assessment.d.ts +1 -2
- package/dist/core/autonomy/risk-assessment.d.ts.map +1 -1
- package/dist/core/autonomy/risk-assessment.js +1 -123
- package/dist/core/autonomy/risk-assessment.js.map +1 -1
- package/dist/core/autonomy/session-lane-record.d.ts +4 -4
- package/dist/core/autonomy/session-lane-record.d.ts.map +1 -1
- package/dist/core/autonomy/session-lane-record.js +16 -29
- package/dist/core/autonomy/session-lane-record.js.map +1 -1
- package/dist/core/autonomy/telemetry-events.d.ts +1 -1
- package/dist/core/autonomy/telemetry-events.d.ts.map +1 -1
- package/dist/core/autonomy/telemetry-events.js +1 -1
- package/dist/core/autonomy/telemetry-events.js.map +1 -1
- package/dist/core/autonomy-telemetry.d.ts +7 -7
- package/dist/core/autonomy-telemetry.d.ts.map +1 -1
- package/dist/core/autonomy-telemetry.js +9 -9
- package/dist/core/autonomy-telemetry.js.map +1 -1
- package/dist/core/background-lane-controller.d.ts +54 -236
- package/dist/core/background-lane-controller.d.ts.map +1 -1
- package/dist/core/background-lane-controller.js +140 -1273
- package/dist/core/background-lane-controller.js.map +1 -1
- package/dist/core/capability-contract.d.ts +4 -0
- package/dist/core/capability-contract.d.ts.map +1 -0
- package/dist/core/capability-contract.js +28 -0
- package/dist/core/capability-contract.js.map +1 -0
- package/dist/core/compaction-controller.d.ts +90 -0
- package/dist/core/compaction-controller.d.ts.map +1 -0
- package/dist/core/compaction-controller.js +452 -0
- package/dist/core/compaction-controller.js.map +1 -0
- package/dist/core/compaction-support.d.ts +12 -11
- package/dist/core/compaction-support.d.ts.map +1 -1
- package/dist/core/compaction-support.js.map +1 -1
- package/dist/core/context/context-artifacts.d.ts +6 -2
- package/dist/core/context/context-artifacts.d.ts.map +1 -1
- package/dist/core/context/context-artifacts.js +34 -2
- package/dist/core/context/context-artifacts.js.map +1 -1
- package/dist/core/context/context-store-retention.d.ts +8 -2
- package/dist/core/context/context-store-retention.d.ts.map +1 -1
- package/dist/core/context/context-store-retention.js +158 -35
- package/dist/core/context/context-store-retention.js.map +1 -1
- package/dist/core/context/okf-memory.d.ts.map +1 -1
- package/dist/core/context/okf-memory.js +9 -11
- package/dist/core/context/okf-memory.js.map +1 -1
- package/dist/core/context/sqlite-runtime-index.d.ts.map +1 -1
- package/dist/core/context/sqlite-runtime-index.js +6 -8
- package/dist/core/context/sqlite-runtime-index.js.map +1 -1
- package/dist/core/context-gc.d.ts +3 -0
- package/dist/core/context-gc.d.ts.map +1 -1
- package/dist/core/context-gc.js +10 -5
- package/dist/core/context-gc.js.map +1 -1
- package/dist/core/context-pipeline.d.ts.map +1 -1
- package/dist/core/context-pipeline.js +9 -4
- package/dist/core/context-pipeline.js.map +1 -1
- package/dist/core/default-tool-surface.d.ts.map +1 -1
- package/dist/core/default-tool-surface.js +1 -0
- package/dist/core/default-tool-surface.js.map +1 -1
- package/dist/core/delegation/managed-lane-controller.d.ts +34 -0
- package/dist/core/delegation/managed-lane-controller.d.ts.map +1 -0
- package/dist/core/delegation/managed-lane-controller.js +217 -0
- package/dist/core/delegation/managed-lane-controller.js.map +1 -0
- package/dist/core/delegation/session-worker-claim.d.ts +44 -0
- package/dist/core/delegation/session-worker-claim.d.ts.map +1 -0
- package/dist/core/delegation/session-worker-claim.js +98 -0
- package/dist/core/delegation/session-worker-claim.js.map +1 -0
- package/dist/core/delegation/worker-actions.d.ts +7 -5
- package/dist/core/delegation/worker-actions.d.ts.map +1 -1
- package/dist/core/delegation/worker-actions.js +20 -4
- package/dist/core/delegation/worker-actions.js.map +1 -1
- package/dist/core/delegation/{worker-result.d.ts → worker-claim.d.ts} +13 -13
- package/dist/core/delegation/worker-claim.d.ts.map +1 -0
- package/dist/core/delegation/{worker-result.js → worker-claim.js} +61 -51
- package/dist/core/delegation/worker-claim.js.map +1 -0
- package/dist/core/delegation/worker-delegation-controller.d.ts +110 -0
- package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -0
- package/dist/core/delegation/worker-delegation-controller.js +861 -0
- package/dist/core/delegation/worker-delegation-controller.js.map +1 -0
- package/dist/core/delegation/worker-delegation-request.d.ts +11 -0
- package/dist/core/delegation/worker-delegation-request.d.ts.map +1 -0
- package/dist/core/delegation/worker-delegation-request.js +2 -0
- package/dist/core/delegation/worker-delegation-request.js.map +1 -0
- package/dist/core/delegation/worker-dispatch-scheduler.d.ts +40 -0
- package/dist/core/delegation/worker-dispatch-scheduler.d.ts.map +1 -0
- package/dist/core/delegation/worker-dispatch-scheduler.js +93 -0
- package/dist/core/delegation/worker-dispatch-scheduler.js.map +1 -0
- package/dist/core/delegation/worker-execution-policy.d.ts +62 -0
- package/dist/core/delegation/worker-execution-policy.d.ts.map +1 -0
- package/dist/core/delegation/worker-execution-policy.js +160 -0
- package/dist/core/delegation/worker-execution-policy.js.map +1 -0
- package/dist/core/delegation/worker-lane-projection.d.ts +14 -0
- package/dist/core/delegation/worker-lane-projection.d.ts.map +1 -0
- package/dist/core/delegation/worker-lane-projection.js +100 -0
- package/dist/core/delegation/worker-lane-projection.js.map +1 -0
- package/dist/core/delegation/worker-lifecycle.d.ts +97 -0
- package/dist/core/delegation/worker-lifecycle.d.ts.map +1 -0
- package/dist/core/delegation/worker-lifecycle.js +339 -0
- package/dist/core/delegation/worker-lifecycle.js.map +1 -0
- package/dist/core/delegation/worker-notification-coordinator.d.ts +36 -0
- package/dist/core/delegation/worker-notification-coordinator.d.ts.map +1 -0
- package/dist/core/delegation/worker-notification-coordinator.js +90 -0
- package/dist/core/delegation/worker-notification-coordinator.js.map +1 -0
- package/dist/core/delegation/worker-profile-resolver.d.ts +55 -0
- package/dist/core/delegation/worker-profile-resolver.d.ts.map +1 -0
- package/dist/core/delegation/worker-profile-resolver.js +113 -0
- package/dist/core/delegation/worker-profile-resolver.js.map +1 -0
- package/dist/core/delegation/worker-runner.d.ts +25 -14
- package/dist/core/delegation/worker-runner.d.ts.map +1 -1
- package/dist/core/delegation/worker-runner.js +122 -53
- package/dist/core/delegation/worker-runner.js.map +1 -1
- package/dist/core/delegation/worker-task-label.d.ts +3 -0
- package/dist/core/delegation/worker-task-label.d.ts.map +1 -0
- package/dist/core/delegation/worker-task-label.js +11 -0
- package/dist/core/delegation/worker-task-label.js.map +1 -0
- package/dist/core/doctor.d.ts +8 -0
- package/dist/core/doctor.d.ts.map +1 -1
- package/dist/core/doctor.js +32 -1
- package/dist/core/doctor.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts +5 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +50 -15
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +6 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/storage.d.ts +8 -0
- package/dist/core/extensions/storage.d.ts.map +1 -0
- package/dist/core/extensions/storage.js +28 -0
- package/dist/core/extensions/storage.js.map +1 -0
- package/dist/core/extensions/types.d.ts +70 -21
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/failure-corpus.d.ts +1 -15
- package/dist/core/failure-corpus.d.ts.map +1 -1
- package/dist/core/failure-corpus.js +9 -37
- package/dist/core/failure-corpus.js.map +1 -1
- package/dist/core/foreground-recovery-controller.d.ts +52 -0
- package/dist/core/foreground-recovery-controller.d.ts.map +1 -0
- package/dist/core/foreground-recovery-controller.js +111 -0
- package/dist/core/foreground-recovery-controller.js.map +1 -0
- package/dist/core/gateways/channel-provider.d.ts +3 -0
- package/dist/core/gateways/channel-provider.d.ts.map +1 -1
- package/dist/core/gateways/channel-provider.js +31 -6
- package/dist/core/gateways/channel-provider.js.map +1 -1
- package/dist/core/goals/goal-acceptance.d.ts +11 -0
- package/dist/core/goals/goal-acceptance.d.ts.map +1 -0
- package/dist/core/goals/goal-acceptance.js +25 -0
- package/dist/core/goals/goal-acceptance.js.map +1 -0
- package/dist/core/goals/goal-auto-continue-controller.d.ts +27 -0
- package/dist/core/goals/goal-auto-continue-controller.d.ts.map +1 -0
- package/dist/core/goals/goal-auto-continue-controller.js +93 -0
- package/dist/core/goals/goal-auto-continue-controller.js.map +1 -0
- package/dist/core/goals/goal-continuation-controller.d.ts +1 -1
- package/dist/core/goals/goal-continuation-controller.d.ts.map +1 -1
- package/dist/core/goals/goal-continuation-controller.js +10 -0
- package/dist/core/goals/goal-continuation-controller.js.map +1 -1
- package/dist/core/goals/goal-continuation-prompt.d.ts +1 -1
- package/dist/core/goals/goal-continuation-prompt.d.ts.map +1 -1
- package/dist/core/goals/goal-continuation-prompt.js +6 -6
- package/dist/core/goals/goal-continuation-prompt.js.map +1 -1
- package/dist/core/goals/goal-lifecycle.d.ts +20 -0
- package/dist/core/goals/goal-lifecycle.d.ts.map +1 -0
- package/dist/core/goals/goal-lifecycle.js +17 -0
- package/dist/core/goals/goal-lifecycle.js.map +1 -0
- package/dist/core/goals/goal-runtime-snapshot.d.ts +11 -9
- package/dist/core/goals/goal-runtime-snapshot.d.ts.map +1 -1
- package/dist/core/goals/goal-runtime-snapshot.js +10 -8
- package/dist/core/goals/goal-runtime-snapshot.js.map +1 -1
- package/dist/core/goals/goal-state.d.ts +1 -0
- package/dist/core/goals/goal-state.d.ts.map +1 -1
- package/dist/core/goals/goal-state.js +7 -10
- package/dist/core/goals/goal-state.js.map +1 -1
- package/dist/core/goals/goal-tool-core.d.ts +8 -6
- package/dist/core/goals/goal-tool-core.d.ts.map +1 -1
- package/dist/core/goals/goal-tool-core.js +11 -41
- package/dist/core/goals/goal-tool-core.js.map +1 -1
- package/dist/core/goals/session-goal-state.d.ts +2 -4
- package/dist/core/goals/session-goal-state.d.ts.map +1 -1
- package/dist/core/goals/session-goal-state.js +10 -33
- package/dist/core/goals/session-goal-state.js.map +1 -1
- package/dist/core/human-input.d.ts +100 -0
- package/dist/core/human-input.d.ts.map +1 -0
- package/dist/core/human-input.js +362 -0
- package/dist/core/human-input.js.map +1 -0
- package/dist/core/index.d.ts +9 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +9 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/{telemetry.d.ts → install-telemetry.d.ts} +1 -1
- package/dist/core/install-telemetry.d.ts.map +1 -0
- package/dist/core/{telemetry.js → install-telemetry.js} +1 -1
- package/dist/core/install-telemetry.js.map +1 -0
- package/dist/core/keybindings.d.ts +17 -1
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +24 -6
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/learning/learning-audit.d.ts +2 -4
- package/dist/core/learning/learning-audit.d.ts.map +1 -1
- package/dist/core/learning/learning-audit.js +9 -29
- package/dist/core/learning/learning-audit.js.map +1 -1
- package/dist/core/learning/learning-gate.d.ts.map +1 -1
- package/dist/core/learning/learning-gate.js +1 -6
- package/dist/core/learning/learning-gate.js.map +1 -1
- package/dist/core/learning/session-learning-decision.d.ts +2 -4
- package/dist/core/learning/session-learning-decision.d.ts.map +1 -1
- package/dist/core/learning/session-learning-decision.js +9 -29
- package/dist/core/learning/session-learning-decision.js.map +1 -1
- package/dist/core/memory-controller.d.ts.map +1 -1
- package/dist/core/memory-controller.js +4 -4
- package/dist/core/memory-controller.js.map +1 -1
- package/dist/core/model-capability.d.ts.map +1 -1
- package/dist/core/model-capability.js +1 -0
- package/dist/core/model-capability.js.map +1 -1
- package/dist/core/model-resolver.d.ts +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +6 -9
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/model-router/intent-classifier.d.ts +1 -4
- package/dist/core/model-router/intent-classifier.d.ts.map +1 -1
- package/dist/core/model-router/intent-classifier.js +1 -156
- package/dist/core/model-router/intent-classifier.js.map +1 -1
- package/dist/core/model-router-controller.d.ts.map +1 -1
- package/dist/core/model-router-controller.js +2 -2
- package/dist/core/model-router-controller.js.map +1 -1
- package/dist/core/model-selection-controller.d.ts +4 -4
- package/dist/core/model-selection-controller.d.ts.map +1 -1
- package/dist/core/model-selection-controller.js.map +1 -1
- package/dist/core/model-tool-protocol.d.ts +23 -0
- package/dist/core/model-tool-protocol.d.ts.map +1 -0
- package/dist/core/model-tool-protocol.js +43 -0
- package/dist/core/model-tool-protocol.js.map +1 -0
- package/dist/core/models/adaptation-store.d.ts +4 -6
- package/dist/core/models/adaptation-store.d.ts.map +1 -1
- package/dist/core/models/adaptation-store.js +95 -83
- package/dist/core/models/adaptation-store.js.map +1 -1
- package/dist/core/models/fitness-store.d.ts +2 -12
- package/dist/core/models/fitness-store.d.ts.map +1 -1
- package/dist/core/models/fitness-store.js +84 -57
- package/dist/core/models/fitness-store.js.map +1 -1
- package/dist/core/models/host-state-store.d.ts +34 -0
- package/dist/core/models/host-state-store.d.ts.map +1 -0
- package/dist/core/models/host-state-store.js +86 -0
- package/dist/core/models/host-state-store.js.map +1 -0
- package/dist/core/orchestration/agent-resume.d.ts +24 -0
- package/dist/core/orchestration/agent-resume.d.ts.map +1 -0
- package/dist/core/orchestration/agent-resume.js +90 -0
- package/dist/core/orchestration/agent-resume.js.map +1 -0
- package/dist/core/orchestration/capability-gateway.d.ts +61 -0
- package/dist/core/orchestration/capability-gateway.d.ts.map +1 -0
- package/dist/core/orchestration/capability-gateway.js +152 -0
- package/dist/core/orchestration/capability-gateway.js.map +1 -0
- package/dist/core/orchestration/contracts.d.ts +343 -0
- package/dist/core/orchestration/contracts.d.ts.map +1 -0
- package/dist/core/orchestration/contracts.js +135 -0
- package/dist/core/orchestration/contracts.js.map +1 -0
- package/dist/core/orchestration/delegation-ledger.d.ts +56 -0
- package/dist/core/orchestration/delegation-ledger.d.ts.map +1 -0
- package/dist/core/orchestration/delegation-ledger.js +197 -0
- package/dist/core/orchestration/delegation-ledger.js.map +1 -0
- package/dist/core/orchestration/event-store.d.ts +83 -0
- package/dist/core/orchestration/event-store.d.ts.map +1 -0
- package/dist/core/orchestration/event-store.js +560 -0
- package/dist/core/orchestration/event-store.js.map +1 -0
- package/dist/core/orchestration/lane-tool-manifests.d.ts +9 -0
- package/dist/core/orchestration/lane-tool-manifests.d.ts.map +1 -0
- package/dist/core/orchestration/lane-tool-manifests.js +37 -0
- package/dist/core/orchestration/lane-tool-manifests.js.map +1 -0
- package/dist/core/orchestration/model-binding.d.ts +15 -0
- package/dist/core/orchestration/model-binding.d.ts.map +1 -0
- package/dist/core/orchestration/model-binding.js +29 -0
- package/dist/core/orchestration/model-binding.js.map +1 -0
- package/dist/core/orchestration/policy-compiler.d.ts +49 -0
- package/dist/core/orchestration/policy-compiler.d.ts.map +1 -0
- package/dist/core/orchestration/policy-compiler.js +191 -0
- package/dist/core/orchestration/policy-compiler.js.map +1 -0
- package/dist/core/orchestration/policy-gate.d.ts +18 -0
- package/dist/core/orchestration/policy-gate.d.ts.map +1 -0
- package/dist/core/orchestration/policy-gate.js +24 -0
- package/dist/core/orchestration/policy-gate.js.map +1 -0
- package/dist/core/orchestration/profile-registry.d.ts +51 -0
- package/dist/core/orchestration/profile-registry.d.ts.map +1 -0
- package/dist/core/orchestration/profile-registry.js +393 -0
- package/dist/core/orchestration/profile-registry.js.map +1 -0
- package/dist/core/orchestration/profile-store.d.ts +31 -0
- package/dist/core/orchestration/profile-store.d.ts.map +1 -0
- package/dist/core/orchestration/profile-store.js +136 -0
- package/dist/core/orchestration/profile-store.js.map +1 -0
- package/dist/core/orchestration/risk-budget.d.ts +9 -0
- package/dist/core/orchestration/risk-budget.d.ts.map +1 -0
- package/dist/core/orchestration/risk-budget.js +48 -0
- package/dist/core/orchestration/risk-budget.js.map +1 -0
- package/dist/core/orchestration/task-runtime.d.ts +168 -0
- package/dist/core/orchestration/task-runtime.d.ts.map +1 -0
- package/dist/core/orchestration/task-runtime.js +1600 -0
- package/dist/core/orchestration/task-runtime.js.map +1 -0
- package/dist/core/orchestration/work-state-projection.d.ts +32 -0
- package/dist/core/orchestration/work-state-projection.d.ts.map +1 -0
- package/dist/core/orchestration/work-state-projection.js +116 -0
- package/dist/core/orchestration/work-state-projection.js.map +1 -0
- package/dist/core/orchestration/worker-execution-contract.d.ts +21 -0
- package/dist/core/orchestration/worker-execution-contract.d.ts.map +1 -0
- package/dist/core/orchestration/worker-execution-contract.js +179 -0
- package/dist/core/orchestration/worker-execution-contract.js.map +1 -0
- package/dist/core/orchestration/worker-result-adapter.d.ts +21 -0
- package/dist/core/orchestration/worker-result-adapter.d.ts.map +1 -0
- package/dist/core/orchestration/worker-result-adapter.js +109 -0
- package/dist/core/orchestration/worker-result-adapter.js.map +1 -0
- package/dist/core/process-identity.d.ts +11 -0
- package/dist/core/process-identity.d.ts.map +1 -0
- package/dist/core/process-identity.js +26 -0
- package/dist/core/process-identity.js.map +1 -0
- package/dist/core/process-matrix/codes.d.ts +14 -3
- package/dist/core/process-matrix/codes.d.ts.map +1 -1
- package/dist/core/process-matrix/codes.js.map +1 -1
- package/dist/core/process-matrix/resume-launcher.d.ts +26 -0
- package/dist/core/process-matrix/resume-launcher.d.ts.map +1 -0
- package/dist/core/process-matrix/resume-launcher.js +62 -0
- package/dist/core/process-matrix/resume-launcher.js.map +1 -0
- package/dist/core/process-matrix/runtime.d.ts +37 -22
- package/dist/core/process-matrix/runtime.d.ts.map +1 -1
- package/dist/core/process-matrix/runtime.js +399 -76
- package/dist/core/process-matrix/runtime.js.map +1 -1
- package/dist/core/process-matrix/store.d.ts +5 -2
- package/dist/core/process-matrix/store.d.ts.map +1 -1
- package/dist/core/process-matrix/store.js +123 -14
- package/dist/core/process-matrix/store.js.map +1 -1
- package/dist/core/process-matrix/supervisor.d.ts +10 -7
- package/dist/core/process-matrix/supervisor.d.ts.map +1 -1
- package/dist/core/process-matrix/supervisor.js +45 -14
- package/dist/core/process-matrix/supervisor.js.map +1 -1
- package/dist/core/profile-registry.d.ts.map +1 -1
- package/dist/core/profile-registry.js +6 -8
- package/dist/core/profile-registry.js.map +1 -1
- package/dist/core/reflection-controller.d.ts +4 -4
- package/dist/core/reflection-controller.d.ts.map +1 -1
- package/dist/core/reflection-controller.js +2 -1
- package/dist/core/reflection-controller.js.map +1 -1
- package/dist/core/reload-blockers.d.ts.map +1 -1
- package/dist/core/reload-blockers.js +6 -8
- package/dist/core/reload-blockers.js.map +1 -1
- package/dist/core/research/evidence-bundle.d.ts.map +1 -1
- package/dist/core/research/evidence-bundle.js +1 -6
- package/dist/core/research/evidence-bundle.js.map +1 -1
- package/dist/core/research/lane-model-resolver.d.ts +33 -0
- package/dist/core/research/lane-model-resolver.d.ts.map +1 -0
- package/dist/core/research/lane-model-resolver.js +65 -0
- package/dist/core/research/lane-model-resolver.js.map +1 -0
- package/dist/core/research/model-fitness-controller.d.ts +38 -0
- package/dist/core/research/model-fitness-controller.d.ts.map +1 -0
- package/dist/core/research/model-fitness-controller.js +106 -0
- package/dist/core/research/model-fitness-controller.js.map +1 -0
- package/dist/core/research/model-fitness.d.ts +1 -1
- package/dist/core/research/model-fitness.d.ts.map +1 -1
- package/dist/core/research/model-fitness.js +4 -4
- package/dist/core/research/model-fitness.js.map +1 -1
- package/dist/core/research/research-gate.d.ts.map +1 -1
- package/dist/core/research/research-gate.js +7 -7
- package/dist/core/research/research-gate.js.map +1 -1
- package/dist/core/research/research-lane-controller.d.ts +62 -0
- package/dist/core/research/research-lane-controller.d.ts.map +1 -0
- package/dist/core/research/research-lane-controller.js +292 -0
- package/dist/core/research/research-lane-controller.js.map +1 -0
- package/dist/core/research/session-evidence-bundle.d.ts +2 -4
- package/dist/core/research/session-evidence-bundle.d.ts.map +1 -1
- package/dist/core/research/session-evidence-bundle.js +10 -46
- package/dist/core/research/session-evidence-bundle.js.map +1 -1
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +7 -2
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/risk-classifier.d.ts +6 -0
- package/dist/core/risk-classifier.d.ts.map +1 -0
- package/dist/core/risk-classifier.js +225 -0
- package/dist/core/risk-classifier.js.map +1 -0
- package/dist/core/runtime-builder.d.ts +26 -20
- package/dist/core/runtime-builder.d.ts.map +1 -1
- package/dist/core/runtime-builder.js +237 -178
- package/dist/core/runtime-builder.js.map +1 -1
- package/dist/core/sdk.d.ts +6 -3
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +61 -24
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-analytics.d.ts.map +1 -1
- package/dist/core/session-analytics.js +28 -1
- package/dist/core/session-analytics.js.map +1 -1
- package/dist/core/session-image-store.d.ts +33 -0
- package/dist/core/session-image-store.d.ts.map +1 -0
- package/dist/core/session-image-store.js +283 -0
- package/dist/core/session-image-store.js.map +1 -0
- package/dist/core/session-role.d.ts +6 -4
- package/dist/core/session-role.d.ts.map +1 -1
- package/dist/core/session-role.js +10 -4
- package/dist/core/session-role.js.map +1 -1
- package/dist/core/session-snapshot.d.ts +34 -0
- package/dist/core/session-snapshot.d.ts.map +1 -0
- package/dist/core/session-snapshot.js +68 -0
- package/dist/core/session-snapshot.js.map +1 -0
- package/dist/core/session-supervision-runtime.d.ts +31 -0
- package/dist/core/session-supervision-runtime.d.ts.map +1 -0
- package/dist/core/session-supervision-runtime.js +97 -0
- package/dist/core/session-supervision-runtime.js.map +1 -0
- package/dist/core/settings-manager.d.ts +9 -4
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +80 -28
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/tasks/session-task-state.d.ts +2 -4
- package/dist/core/tasks/session-task-state.d.ts.map +1 -1
- package/dist/core/tasks/session-task-state.js +10 -28
- package/dist/core/tasks/session-task-state.js.map +1 -1
- package/dist/core/tasks/task-projection.d.ts +20 -0
- package/dist/core/tasks/task-projection.d.ts.map +1 -0
- package/dist/core/tasks/task-projection.js +47 -0
- package/dist/core/tasks/task-projection.js.map +1 -0
- package/dist/core/tasks/task-state.d.ts +4 -0
- package/dist/core/tasks/task-state.d.ts.map +1 -1
- package/dist/core/tasks/task-state.js +28 -11
- package/dist/core/tasks/task-state.js.map +1 -1
- package/dist/core/tool-capability-policy.d.ts +13 -0
- package/dist/core/tool-capability-policy.d.ts.map +1 -0
- package/dist/core/tool-capability-policy.js +85 -0
- package/dist/core/tool-capability-policy.js.map +1 -0
- package/dist/core/tool-protocol-controller.d.ts +91 -0
- package/dist/core/tool-protocol-controller.d.ts.map +1 -0
- package/dist/core/tool-protocol-controller.js +512 -0
- package/dist/core/tool-protocol-controller.js.map +1 -0
- package/dist/core/tool-recovery-log-records.d.ts +2 -3
- package/dist/core/tool-recovery-log-records.d.ts.map +1 -1
- package/dist/core/tool-recovery-log-records.js +9 -35
- package/dist/core/tool-recovery-log-records.js.map +1 -1
- package/dist/core/tool-selection/tool-performance-store.d.ts +3 -7
- package/dist/core/tool-selection/tool-performance-store.d.ts.map +1 -1
- package/dist/core/tool-selection/tool-performance-store.js +97 -138
- package/dist/core/tool-selection/tool-performance-store.js.map +1 -1
- package/dist/core/tools/ask-question.d.ts +124 -0
- package/dist/core/tools/ask-question.d.ts.map +1 -0
- package/dist/core/tools/ask-question.js +693 -0
- package/dist/core/tools/ask-question.js.map +1 -0
- package/dist/core/tools/delegate-status.d.ts +31 -3
- package/dist/core/tools/delegate-status.d.ts.map +1 -1
- package/dist/core/tools/delegate-status.js +149 -29
- package/dist/core/tools/delegate-status.js.map +1 -1
- package/dist/core/tools/delegate.d.ts +15 -13
- package/dist/core/tools/delegate.d.ts.map +1 -1
- package/dist/core/tools/delegate.js +108 -23
- package/dist/core/tools/delegate.js.map +1 -1
- package/dist/core/tools/extensionify.d.ts.map +1 -1
- package/dist/core/tools/extensionify.js +1 -0
- package/dist/core/tools/extensionify.js.map +1 -1
- package/dist/core/tools/goal.d.ts +5 -5
- package/dist/core/tools/goal.d.ts.map +1 -1
- package/dist/core/tools/goal.js +10 -10
- package/dist/core/tools/goal.js.map +1 -1
- package/dist/core/tools/model-fitness.d.ts.map +1 -1
- package/dist/core/tools/model-fitness.js +2 -2
- package/dist/core/tools/model-fitness.js.map +1 -1
- package/dist/core/tools/orchestration-panel.d.ts +43 -0
- package/dist/core/tools/orchestration-panel.d.ts.map +1 -0
- package/dist/core/tools/orchestration-panel.js +168 -0
- package/dist/core/tools/orchestration-panel.js.map +1 -0
- package/dist/core/tools/run-process.d.ts +26 -0
- package/dist/core/tools/run-process.d.ts.map +1 -0
- package/dist/core/tools/run-process.js +138 -0
- package/dist/core/tools/run-process.js.map +1 -0
- package/dist/core/tools/task-steps.d.ts +2 -0
- package/dist/core/tools/task-steps.d.ts.map +1 -1
- package/dist/core/tools/task-steps.js +102 -0
- package/dist/core/tools/task-steps.js.map +1 -1
- package/dist/core/tools/tmux-dispatch.d.ts +3 -5
- package/dist/core/tools/tmux-dispatch.d.ts.map +1 -1
- package/dist/core/tools/tmux-dispatch.js +4 -4
- package/dist/core/tools/tmux-dispatch.js.map +1 -1
- package/dist/core/tools/windows-shell-engine.d.ts.map +1 -1
- package/dist/core/tools/windows-shell-engine.js +4 -6
- package/dist/core/tools/windows-shell-engine.js.map +1 -1
- package/dist/core/util/bounded-jsonl.d.ts +10 -0
- package/dist/core/util/bounded-jsonl.d.ts.map +1 -0
- package/dist/core/util/bounded-jsonl.js +75 -0
- package/dist/core/util/bounded-jsonl.js.map +1 -0
- package/dist/core/util/value-guards.d.ts +5 -0
- package/dist/core/util/value-guards.d.ts.map +1 -0
- package/dist/core/util/value-guards.js +12 -0
- package/dist/core/util/value-guards.js.map +1 -0
- package/dist/core/worktree-sync/store.d.ts +3 -3
- package/dist/core/worktree-sync/store.d.ts.map +1 -1
- package/dist/core/worktree-sync/store.js +10 -5
- package/dist/core/worktree-sync/store.js.map +1 -1
- package/dist/index.d.ts +13 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +128 -44
- package/dist/main.js.map +1 -1
- package/dist/migrations.d.ts +4 -6
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +186 -28
- package/dist/migrations.js.map +1 -1
- package/dist/modes/interactive/clipboard-input.d.ts +7 -1
- package/dist/modes/interactive/clipboard-input.d.ts.map +1 -1
- package/dist/modes/interactive/clipboard-input.js +44 -6
- package/dist/modes/interactive/clipboard-input.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +2 -2
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/config-backup.d.ts.map +1 -1
- package/dist/modes/interactive/config-backup.js +5 -4
- package/dist/modes/interactive/config-backup.js.map +1 -1
- package/dist/modes/interactive/extension-ui-host.d.ts +14 -0
- package/dist/modes/interactive/extension-ui-host.d.ts.map +1 -1
- package/dist/modes/interactive/extension-ui-host.js +76 -15
- package/dist/modes/interactive/extension-ui-host.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +3 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +63 -1
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/key-handlers.d.ts.map +1 -1
- package/dist/modes/interactive/key-handlers.js +1 -1
- package/dist/modes/interactive/key-handlers.js.map +1 -1
- package/dist/modes/interactive/profile-menu-controller.d.ts.map +1 -1
- package/dist/modes/interactive/profile-menu-controller.js +7 -7
- package/dist/modes/interactive/profile-menu-controller.js.map +1 -1
- package/dist/modes/interactive/report-commands.d.ts.map +1 -1
- package/dist/modes/interactive/report-commands.js +21 -0
- package/dist/modes/interactive/report-commands.js.map +1 -1
- package/dist/modes/interactive/session-flow-commands.d.ts +17 -2
- package/dist/modes/interactive/session-flow-commands.d.ts.map +1 -1
- package/dist/modes/interactive/session-flow-commands.js +50 -17
- package/dist/modes/interactive/session-flow-commands.js.map +1 -1
- package/dist/modes/interactive/session-io-commands.d.ts.map +1 -1
- package/dist/modes/interactive/session-io-commands.js +16 -6
- package/dist/modes/interactive/session-io-commands.js.map +1 -1
- package/dist/modes/interactive/session-replacement-errors.d.ts +7 -0
- package/dist/modes/interactive/session-replacement-errors.d.ts.map +1 -0
- package/dist/modes/interactive/session-replacement-errors.js +15 -0
- package/dist/modes/interactive/session-replacement-errors.js.map +1 -0
- package/dist/modes/interactive/startup-checks.d.ts.map +1 -1
- package/dist/modes/interactive/startup-checks.js +1 -1
- package/dist/modes/interactive/startup-checks.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +82 -23
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +15 -0
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js +138 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/utils/clipboard-image.d.ts.map +1 -1
- package/dist/utils/clipboard-image.js +2 -13
- package/dist/utils/clipboard-image.js.map +1 -1
- package/dist/utils/clipboard-native.d.ts +1 -0
- package/dist/utils/clipboard-native.d.ts.map +1 -1
- package/dist/utils/clipboard-native.js.map +1 -1
- package/dist/utils/clipboard.d.ts +1 -0
- package/dist/utils/clipboard.d.ts.map +1 -1
- package/dist/utils/clipboard.js +52 -1
- package/dist/utils/clipboard.js.map +1 -1
- package/dist/utils/platform.d.ts +3 -0
- package/dist/utils/platform.d.ts.map +1 -0
- package/dist/utils/platform.js +8 -0
- package/dist/utils/platform.js.map +1 -0
- package/dist/utils/work-directory.d.ts +11 -0
- package/dist/utils/work-directory.d.ts.map +1 -1
- package/dist/utils/work-directory.js +30 -9
- package/dist/utils/work-directory.js.map +1 -1
- package/docs/UI_POLISH_ORCHESTRATION.md +44 -0
- package/docs/UI_QUALITY_ORCHESTRATION.md +30 -0
- package/docs/docs.json +4 -0
- package/docs/extensions.md +62 -2
- package/docs/harness-architecture.md +156 -0
- package/docs/keybindings.md +11 -1
- package/docs/process-matrix.md +59 -26
- package/docs/rpc.md +45 -2
- package/docs/sessions.md +15 -0
- package/docs/settings.md +125 -11
- package/docs/task-steps.md +8 -0
- package/docs/telemetry.md +19 -0
- package/docs/tmux-agent-manager.md +10 -3
- package/docs/ui-reference/README.md +9 -0
- package/docs/ui-reference/orchestration-after.txt +7 -0
- package/docs/ui-reference/orchestration-before.txt +7 -0
- package/docs/ui-reference/orchestration-reference.txt +6 -0
- package/docs/usage.md +14 -2
- package/docs/work-directory.md +19 -8
- 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/question.ts +34 -245
- package/examples/extensions/questionnaire.ts +41 -407
- 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/core/delegation/session-worker-result.d.ts +0 -46
- package/dist/core/delegation/session-worker-result.d.ts.map +0 -1
- package/dist/core/delegation/session-worker-result.js +0 -113
- package/dist/core/delegation/session-worker-result.js.map +0 -1
- package/dist/core/delegation/worker-result.d.ts.map +0 -1
- package/dist/core/delegation/worker-result.js.map +0 -1
- package/dist/core/telemetry.d.ts.map +0 -1
- package/dist/core/telemetry.js.map +0 -1
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { formatToolRepairStandingRule, formatVariantEnvelope, generateTextToolProtocolPrimer, parseTextToolCalls, TEXT_TOOL_PROTOCOL_VARIANTS, } from "@caupulican/pi-ai";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import { getProcessWorkRun } from "../utils/work-directory.js";
|
|
6
|
+
import { MODEL_TOOL_PROTOCOL_VERSION, resolveModelToolProtocol, } from "./model-tool-protocol.js";
|
|
7
|
+
import { formatToolRepairHealthReport } from "./tool-repair-health.js";
|
|
8
|
+
import { resolveCurrentToolRepairSettings } from "./tool-repair-settings.js";
|
|
9
|
+
const MODEL_ADAPTATION_REPAIR_THRESHOLD = 3;
|
|
10
|
+
const TEXT_TOOL_PROTOCOL_TRIALS_PER_VARIANT = 2;
|
|
11
|
+
const TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD = 3;
|
|
12
|
+
const TEXT_TOOL_PROTOCOL_STEER_INTERVAL = 5;
|
|
13
|
+
const AUTO_TOOL_PROBE_FRESHNESS_MS = 15 * 60 * 1000;
|
|
14
|
+
const TEXT_TOOL_PROTOCOL_ECHO_TOOL = {
|
|
15
|
+
name: "echo",
|
|
16
|
+
description: "Echo calibration data",
|
|
17
|
+
parameters: Type.Object({ data: Type.String() }),
|
|
18
|
+
};
|
|
19
|
+
const NATIVE_TOOL_PROBE_READ_TOOL = {
|
|
20
|
+
name: "read",
|
|
21
|
+
description: "Read file contents",
|
|
22
|
+
parameters: Type.Object({ path: Type.String() }),
|
|
23
|
+
};
|
|
24
|
+
/** Owns model tool-protocol selection, probing, circuit breaking, and repair teaching. */
|
|
25
|
+
export class ToolProtocolController {
|
|
26
|
+
repairModeSessionCounts = new Map();
|
|
27
|
+
parseFailures = new Map();
|
|
28
|
+
parseObservedThisTurn = false;
|
|
29
|
+
correctiveSteerCount = 0;
|
|
30
|
+
probeUsageReportSeq = 0;
|
|
31
|
+
autoProbedModels = new Set();
|
|
32
|
+
validationOutcomeThisTurn;
|
|
33
|
+
deps;
|
|
34
|
+
constructor(deps) {
|
|
35
|
+
this.deps = deps;
|
|
36
|
+
}
|
|
37
|
+
applyRepairLayerSettings() {
|
|
38
|
+
this.deps.agent.toolArgumentTeachEnabled = this.getRepairSettings().teach;
|
|
39
|
+
}
|
|
40
|
+
resetTurnState() {
|
|
41
|
+
this.parseObservedThisTurn = false;
|
|
42
|
+
this.validationOutcomeThisTurn = undefined;
|
|
43
|
+
}
|
|
44
|
+
getAdaptationRulesForPrompt() {
|
|
45
|
+
if (!this.getRepairSettings().teach)
|
|
46
|
+
return [];
|
|
47
|
+
const modelKey = this.modelKey(this.deps.agent.state.model);
|
|
48
|
+
return modelKey ? this.deps.adaptationStore.get(modelKey).rules : [];
|
|
49
|
+
}
|
|
50
|
+
resolveModelToolProtocol(model) {
|
|
51
|
+
return resolveModelToolProtocol({
|
|
52
|
+
model,
|
|
53
|
+
settingsOverride: this.getRepairSettings().textProtocol,
|
|
54
|
+
adaptation: this.deps.adaptationStore.get(this.modelRef(model)),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async ensureActiveModelProtocol() {
|
|
58
|
+
const model = this.deps.agent.state.model;
|
|
59
|
+
const resolution = this.resolveModelToolProtocol(model);
|
|
60
|
+
this.deps.agent.textToolCallProtocol = resolution.protocol;
|
|
61
|
+
if (resolution.reasonCode !== "probe_calibration_missing" &&
|
|
62
|
+
resolution.reasonCode !== "probe_calibration_failed" &&
|
|
63
|
+
resolution.reasonCode !== "probe_calibration_invalid") {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const modelKey = this.modelRef(model);
|
|
67
|
+
this.deps.emitWarning(resolution.reasonCode === "probe_calibration_failed"
|
|
68
|
+
? `Text tool protocol calibration for ${modelKey} previously failed (variants tried: ${(resolution.variantsTried ?? []).join(", ")}); falling back to native tool calls this turn. Run /toolprobe ${modelKey} to recalibrate.`
|
|
69
|
+
: `Text tool protocol for ${modelKey} has no valid calibration on record; falling back to native tool calls this turn. Run /toolprobe ${modelKey} to calibrate.`);
|
|
70
|
+
}
|
|
71
|
+
getToolProbeVerdict(model) {
|
|
72
|
+
return this.deps.adaptationStore.get(this.modelRef(model)).toolProbe?.status;
|
|
73
|
+
}
|
|
74
|
+
async probeToolCalling(target) {
|
|
75
|
+
const models = await this.resolveToolProbeModels(target);
|
|
76
|
+
if (models.length === 0)
|
|
77
|
+
throw new Error("No available models to probe.");
|
|
78
|
+
const results = [];
|
|
79
|
+
for (const model of models)
|
|
80
|
+
results.push(await this.probeToolCallingForModel(model));
|
|
81
|
+
return { results, table: this.formatToolProbeReport(results) };
|
|
82
|
+
}
|
|
83
|
+
async probeToolCallingForModel(model) {
|
|
84
|
+
const modelKey = this.modelRef(model);
|
|
85
|
+
const probedAt = new Date().toISOString();
|
|
86
|
+
let nativeGrade = "absent";
|
|
87
|
+
let diagnostic;
|
|
88
|
+
try {
|
|
89
|
+
nativeGrade = await this.gradeNativeToolCallingForModel(model, "pi-native-probe");
|
|
90
|
+
if (nativeGrade === "task") {
|
|
91
|
+
this.storeToolProbe(modelKey, {
|
|
92
|
+
version: MODEL_TOOL_PROTOCOL_VERSION,
|
|
93
|
+
status: "native",
|
|
94
|
+
probedAt,
|
|
95
|
+
nativeGrade,
|
|
96
|
+
});
|
|
97
|
+
return { model: modelKey, verdict: "native", nativeGrade };
|
|
98
|
+
}
|
|
99
|
+
diagnostic =
|
|
100
|
+
nativeGrade === "echo-only"
|
|
101
|
+
? "Native echo probe passed but task-scale read probe failed."
|
|
102
|
+
: "Native task-scale read and echo probes did not produce provider-native tool calls.";
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
diagnostic = error instanceof Error ? error.message : String(error);
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
const calibrated = await this.calibrateTextToolProtocolForModel(model, modelKey, { persistFailure: false });
|
|
109
|
+
if (calibrated.status === "calibrated") {
|
|
110
|
+
this.storeToolProbe(modelKey, {
|
|
111
|
+
version: MODEL_TOOL_PROTOCOL_VERSION,
|
|
112
|
+
status: "text-protocol",
|
|
113
|
+
probedAt: calibrated.calibratedAt,
|
|
114
|
+
variant: calibrated.variant,
|
|
115
|
+
nativeGrade,
|
|
116
|
+
diagnostic,
|
|
117
|
+
});
|
|
118
|
+
return { model: modelKey, verdict: "text-protocol", variant: calibrated.variant, nativeGrade, diagnostic };
|
|
119
|
+
}
|
|
120
|
+
diagnostic = `${diagnostic ? `${diagnostic} ` : ""}Text protocol variants failed: ${calibrated.variantsTried.join(", ")}`;
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
diagnostic = error instanceof Error ? error.message : String(error);
|
|
124
|
+
}
|
|
125
|
+
this.storeToolProbe(modelKey, {
|
|
126
|
+
version: MODEL_TOOL_PROTOCOL_VERSION,
|
|
127
|
+
status: "none",
|
|
128
|
+
probedAt,
|
|
129
|
+
nativeGrade,
|
|
130
|
+
diagnostic,
|
|
131
|
+
});
|
|
132
|
+
return { model: modelKey, verdict: "none", nativeGrade, diagnostic };
|
|
133
|
+
}
|
|
134
|
+
handleTextProtocolParse(event) {
|
|
135
|
+
this.parseObservedThisTurn = true;
|
|
136
|
+
const modelKey = `${event.provider}/${event.model}`;
|
|
137
|
+
if (event.status === "parsed")
|
|
138
|
+
return;
|
|
139
|
+
this.maybeInjectCorrectiveSteer(event.variant);
|
|
140
|
+
const signature = `${event.variant}:${event.reason ?? "failed"}`;
|
|
141
|
+
const previous = this.parseFailures.get(modelKey);
|
|
142
|
+
const repeats = previous?.signature === signature ? previous.repeats + 1 : 1;
|
|
143
|
+
this.parseFailures.set(modelKey, { signature, repeats });
|
|
144
|
+
if (repeats < TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD)
|
|
145
|
+
return;
|
|
146
|
+
const profile = this.deps.adaptationStore.get(modelKey);
|
|
147
|
+
if (profile.protocol?.version === MODEL_TOOL_PROTOCOL_VERSION && profile.protocol.status !== "failed") {
|
|
148
|
+
this.deps.adaptationStore.removeProtocol(modelKey);
|
|
149
|
+
this.deps.agent.textToolCallProtocol = undefined;
|
|
150
|
+
}
|
|
151
|
+
this.parseFailures.delete(modelKey);
|
|
152
|
+
const probedAt = new Date().toISOString();
|
|
153
|
+
this.deps.adaptationStore.setToolProbe(modelKey, {
|
|
154
|
+
version: MODEL_TOOL_PROTOCOL_VERSION,
|
|
155
|
+
status: "none",
|
|
156
|
+
probedAt,
|
|
157
|
+
nativeGrade: profile.toolProbe?.nativeGrade,
|
|
158
|
+
diagnostic: `Text protocol parsing failed ${TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD}x in a row with signature "${signature}".`,
|
|
159
|
+
}, probedAt);
|
|
160
|
+
this.deps.emitWarning(`Text tool protocol for ${modelKey} stopped parsing after ${TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD} attempts; demoted to native fallback. Run /toolprobe ${modelKey} to recalibrate.`);
|
|
161
|
+
}
|
|
162
|
+
handleValidationOutcome(event) {
|
|
163
|
+
if (event.source !== "text-protocol")
|
|
164
|
+
return;
|
|
165
|
+
const protocol = this.deps.agent.textToolCallProtocol;
|
|
166
|
+
const variant = protocol === true ? "tool-tag" : protocol ? protocol.variant : undefined;
|
|
167
|
+
if (!variant)
|
|
168
|
+
return;
|
|
169
|
+
const status = event.outcome === "bounced" ? "failed" : "parsed";
|
|
170
|
+
if (this.validationOutcomeThisTurn?.status === "parsed" && status === "failed")
|
|
171
|
+
return;
|
|
172
|
+
this.validationOutcomeThisTurn = {
|
|
173
|
+
provider: event.provider ?? this.deps.agent.state.model.provider,
|
|
174
|
+
model: event.model ?? this.deps.agent.state.model.id,
|
|
175
|
+
variant,
|
|
176
|
+
status,
|
|
177
|
+
callCount: 1,
|
|
178
|
+
textLength: 0,
|
|
179
|
+
...(status === "failed" && {
|
|
180
|
+
reason: event.errorKeywords?.includes("unknown_tool") ? "unknown-tool" : "validation-failed",
|
|
181
|
+
}),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
recordParseOutcomeFromLastAssistant() {
|
|
185
|
+
const validationOutcome = this.validationOutcomeThisTurn;
|
|
186
|
+
this.validationOutcomeThisTurn = undefined;
|
|
187
|
+
if (validationOutcome?.status === "parsed") {
|
|
188
|
+
this.parseObservedThisTurn = true;
|
|
189
|
+
this.parseFailures.delete(`${validationOutcome.provider}/${validationOutcome.model}`);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (validationOutcome) {
|
|
193
|
+
this.handleTextProtocolParse(validationOutcome);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (this.parseObservedThisTurn)
|
|
197
|
+
return;
|
|
198
|
+
const protocol = this.deps.agent.textToolCallProtocol;
|
|
199
|
+
if (protocol === false || protocol === true || !protocol?.variant)
|
|
200
|
+
return;
|
|
201
|
+
const response = this.deps.findLastAssistantMessage();
|
|
202
|
+
if (!response)
|
|
203
|
+
return;
|
|
204
|
+
const responseText = response.content
|
|
205
|
+
.filter((content) => content.type === "text")
|
|
206
|
+
.map((content) => content.text)
|
|
207
|
+
.join("\n");
|
|
208
|
+
if (!responseText)
|
|
209
|
+
return;
|
|
210
|
+
const parsed = parseTextToolCalls(responseText, this.deps.agent.state.tools);
|
|
211
|
+
const attempted = parsed.attempted || this.looksLikeTextProtocolAttempt(responseText);
|
|
212
|
+
if (!attempted)
|
|
213
|
+
return;
|
|
214
|
+
this.handleTextProtocolParse({
|
|
215
|
+
provider: this.deps.agent.state.model.provider,
|
|
216
|
+
model: this.deps.agent.state.model.id,
|
|
217
|
+
variant: protocol.variant,
|
|
218
|
+
status: parsed.calls.length > 0 ? "parsed" : "failed",
|
|
219
|
+
reason: parsed.failure,
|
|
220
|
+
callCount: parsed.calls.length,
|
|
221
|
+
textLength: responseText.length,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
tagAdaptationRuleTeaching(event) {
|
|
225
|
+
if (!this.getRepairSettings().teach || event.taught !== "none")
|
|
226
|
+
return event;
|
|
227
|
+
const modelKey = event.provider && event.model
|
|
228
|
+
? `${event.provider}/${event.model}`
|
|
229
|
+
: this.modelKey(this.deps.agent.state.model);
|
|
230
|
+
if (!modelKey)
|
|
231
|
+
return event;
|
|
232
|
+
try {
|
|
233
|
+
const rules = this.deps.adaptationStore.get(modelKey).rules;
|
|
234
|
+
const modes = new Set([...event.failureModes, ...event.repairsApplied]);
|
|
235
|
+
if (rules.some((rule) => modes.has(rule.mode)))
|
|
236
|
+
return { ...event, taught: "rule" };
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
// Best-effort telemetry tagging.
|
|
240
|
+
}
|
|
241
|
+
return event;
|
|
242
|
+
}
|
|
243
|
+
handleAdaptationTelemetry(event) {
|
|
244
|
+
if (!this.getRepairSettings().teach || event.outcome !== "repaired" || event.repairsApplied.length === 0)
|
|
245
|
+
return;
|
|
246
|
+
const modelKey = event.provider && event.model
|
|
247
|
+
? `${event.provider}/${event.model}`
|
|
248
|
+
: this.modelKey(this.deps.agent.state.model);
|
|
249
|
+
if (!modelKey)
|
|
250
|
+
return;
|
|
251
|
+
try {
|
|
252
|
+
for (const mode of [...new Set(event.repairsApplied)]) {
|
|
253
|
+
const profile = this.deps.adaptationStore.get(modelKey);
|
|
254
|
+
const stats = profile.teachStats[mode] ?? { taught: 0, recurrenceBefore: 0, recurrenceAfter: 0 };
|
|
255
|
+
if (profile.rules.some((rule) => rule.mode === mode)) {
|
|
256
|
+
this.deps.adaptationStore.markRuleFired(modelKey, mode);
|
|
257
|
+
this.deps.adaptationStore.setTeachStats(modelKey, mode, {
|
|
258
|
+
...stats,
|
|
259
|
+
recurrenceAfter: stats.recurrenceAfter + 1,
|
|
260
|
+
});
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const recurrenceBefore = stats.recurrenceBefore + 1;
|
|
264
|
+
this.deps.adaptationStore.setTeachStats(modelKey, mode, { ...stats, recurrenceBefore });
|
|
265
|
+
const sessionCount = this.repairSessionCount(modelKey, mode);
|
|
266
|
+
if (sessionCount >= MODEL_ADAPTATION_REPAIR_THRESHOLD ||
|
|
267
|
+
recurrenceBefore >= MODEL_ADAPTATION_REPAIR_THRESHOLD) {
|
|
268
|
+
this.deps.adaptationStore.addRule(modelKey, { mode, text: formatToolRepairStandingRule(mode) });
|
|
269
|
+
this.deps.adaptationStore.setTeachStats(modelKey, mode, {
|
|
270
|
+
...stats,
|
|
271
|
+
taught: stats.taught + 1,
|
|
272
|
+
recurrenceBefore,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
// Best-effort adaptation persistence.
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
maybeAutoProbe(model) {
|
|
282
|
+
const modelKey = this.modelRef(model);
|
|
283
|
+
if (this.autoProbedModels.has(modelKey) || this.hasFreshToolProbeVerdict(modelKey))
|
|
284
|
+
return;
|
|
285
|
+
this.autoProbedModels.add(modelKey);
|
|
286
|
+
void this.deps
|
|
287
|
+
.probeForAuto(model)
|
|
288
|
+
.then((result) => {
|
|
289
|
+
if (this.deps.isDisposed())
|
|
290
|
+
return;
|
|
291
|
+
const detail = result.verdict === "text-protocol"
|
|
292
|
+
? ` (variant ${result.variant}); it will use the text tool protocol starting next turn`
|
|
293
|
+
: result.verdict === "none"
|
|
294
|
+
? " — no working tool-call path was found; run /toolprobe for details"
|
|
295
|
+
: "; native tool calls stay in use";
|
|
296
|
+
this.deps.emitWarning(`Auto-probed ${modelKey} after repeated native tool-call validation failures: verdict "${result.verdict}"${detail}.`);
|
|
297
|
+
})
|
|
298
|
+
.catch((error) => {
|
|
299
|
+
if (this.deps.isDisposed())
|
|
300
|
+
return;
|
|
301
|
+
this.deps.emitWarning(`Auto-probe for ${modelKey} (triggered by repeated tool-call validation failures) did not complete: ${error instanceof Error ? error.message : String(error)}.`);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
formatRepairHealth(stats) {
|
|
305
|
+
return formatToolRepairHealthReport(this.deps.adaptationStore, new Date(), stats);
|
|
306
|
+
}
|
|
307
|
+
removeRepairRule(model, mode) {
|
|
308
|
+
return this.deps.adaptationStore.removeRule(model, mode);
|
|
309
|
+
}
|
|
310
|
+
resetProtocolCalibration(model) {
|
|
311
|
+
const removed = this.deps.adaptationStore.removeProtocol(model);
|
|
312
|
+
this.parseFailures.delete(model);
|
|
313
|
+
return removed;
|
|
314
|
+
}
|
|
315
|
+
getRepairSettings() {
|
|
316
|
+
return resolveCurrentToolRepairSettings(this.deps.settingsManager.settings);
|
|
317
|
+
}
|
|
318
|
+
modelKey(model) {
|
|
319
|
+
return model ? this.modelRef(model) : undefined;
|
|
320
|
+
}
|
|
321
|
+
modelRef(model) {
|
|
322
|
+
return `${model.provider}/${model.id}`;
|
|
323
|
+
}
|
|
324
|
+
async streamForProbe(model, context, options) {
|
|
325
|
+
let requestOptions = options;
|
|
326
|
+
if (this.deps.isRawStreamSimple(this.deps.agent.streamFn)) {
|
|
327
|
+
const auth = await this.deps.getRequiredRequestAuth(model);
|
|
328
|
+
requestOptions = {
|
|
329
|
+
...options,
|
|
330
|
+
apiKey: auth.apiKey,
|
|
331
|
+
headers: auth.headers || options.headers ? { ...auth.headers, ...options.headers } : undefined,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
return this.deps.agent.streamFn(model, context, requestOptions);
|
|
335
|
+
}
|
|
336
|
+
nextProbeUsageReportId(model, kind) {
|
|
337
|
+
return `tool-probe:${this.modelRef(model)}:${kind}:${this.probeUsageReportSeq++}`;
|
|
338
|
+
}
|
|
339
|
+
async resolveProbeStreamCountingUsage(stream, label, reportId) {
|
|
340
|
+
const message = await stream.result();
|
|
341
|
+
const usage = message.usage;
|
|
342
|
+
if (usage && (usage.cost.total > 0 || usage.totalTokens > 0)) {
|
|
343
|
+
this.deps.addSpawnedUsage(usage, { label, reportId });
|
|
344
|
+
}
|
|
345
|
+
return message;
|
|
346
|
+
}
|
|
347
|
+
textProtocolCalibrationContext(variant, token) {
|
|
348
|
+
const primer = generateTextToolProtocolPrimer([TEXT_TOOL_PROTOCOL_ECHO_TOOL], { variant });
|
|
349
|
+
const instruction = `Text tool protocol calibration trial. Using the protocol above, call echo with data exactly "${token}". Output only the tool-call envelope.`;
|
|
350
|
+
return {
|
|
351
|
+
systemPrompt: `${primer}\n\n${instruction}`,
|
|
352
|
+
messages: [{ role: "user", content: [{ type: "text", text: instruction }], timestamp: Date.now() }],
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
messageHasToolCallWithStringArgument(message, toolName, argName, argValue) {
|
|
356
|
+
return message.content.some((block) => {
|
|
357
|
+
if (block.type !== "toolCall" || block.name !== toolName)
|
|
358
|
+
return false;
|
|
359
|
+
const args = block.arguments;
|
|
360
|
+
return (typeof args === "object" &&
|
|
361
|
+
args !== null &&
|
|
362
|
+
!Array.isArray(args) &&
|
|
363
|
+
args[argName] === argValue);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
nativeToolProbeSystemPrompt(instruction) {
|
|
367
|
+
const base = (this.deps.agent.state.systemPrompt ?? "").trim();
|
|
368
|
+
return base ? `${base}\n\n${instruction}` : instruction;
|
|
369
|
+
}
|
|
370
|
+
async runNativeReadTaskProbeTrial(model, path) {
|
|
371
|
+
const instruction = `Native tool-call capability probe: task-scale read. Use provider-native tool calling, not prose. ` +
|
|
372
|
+
`Call read exactly once with path exactly "${path}".`;
|
|
373
|
+
const stream = await this.streamForProbe(model, {
|
|
374
|
+
systemPrompt: this.nativeToolProbeSystemPrompt(instruction),
|
|
375
|
+
messages: [{ role: "user", content: [{ type: "text", text: instruction }], timestamp: Date.now() }],
|
|
376
|
+
tools: [NATIVE_TOOL_PROBE_READ_TOOL],
|
|
377
|
+
}, { textToolCallProtocol: false, maxRetries: 0, temperature: 0, maxTokens: 768 });
|
|
378
|
+
const message = await this.resolveProbeStreamCountingUsage(stream, "tool-probe", this.nextProbeUsageReportId(model, "read-task"));
|
|
379
|
+
return this.messageHasToolCallWithStringArgument(message, "read", "path", path);
|
|
380
|
+
}
|
|
381
|
+
async runNativeEchoToolProbeTrial(model, token) {
|
|
382
|
+
const instruction = `Native tool-call capability probe: echo-only. Use provider-native tool calling, not prose. ` +
|
|
383
|
+
`Call echo with data exactly "${token}".`;
|
|
384
|
+
const stream = await this.streamForProbe(model, {
|
|
385
|
+
systemPrompt: this.nativeToolProbeSystemPrompt(instruction),
|
|
386
|
+
messages: [{ role: "user", content: [{ type: "text", text: instruction }], timestamp: Date.now() }],
|
|
387
|
+
tools: [TEXT_TOOL_PROTOCOL_ECHO_TOOL],
|
|
388
|
+
}, { textToolCallProtocol: false, maxRetries: 0, temperature: 0, maxTokens: 256 });
|
|
389
|
+
const message = await this.resolveProbeStreamCountingUsage(stream, "tool-probe", this.nextProbeUsageReportId(model, "echo"));
|
|
390
|
+
return this.messageHasToolCallWithStringArgument(message, "echo", "data", token);
|
|
391
|
+
}
|
|
392
|
+
async gradeNativeToolCallingForModel(model, token) {
|
|
393
|
+
const path = join(getProcessWorkRun(this.deps.agentDir, "probes", "native-tools").path, `pi-native-probe-${process.pid}-${Date.now()}.txt`);
|
|
394
|
+
writeFileSync(path, token, "utf-8");
|
|
395
|
+
try {
|
|
396
|
+
if (await this.runNativeReadTaskProbeTrial(model, path))
|
|
397
|
+
return "task";
|
|
398
|
+
if (await this.runNativeEchoToolProbeTrial(model, token))
|
|
399
|
+
return "echo-only";
|
|
400
|
+
return "absent";
|
|
401
|
+
}
|
|
402
|
+
finally {
|
|
403
|
+
rmSync(path, { force: true });
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
async runTextProtocolTrial(model, variant, token) {
|
|
407
|
+
const stream = await this.streamForProbe(model, this.textProtocolCalibrationContext(variant, token), {
|
|
408
|
+
textToolCallProtocol: false,
|
|
409
|
+
maxRetries: 0,
|
|
410
|
+
temperature: 0,
|
|
411
|
+
maxTokens: 256,
|
|
412
|
+
});
|
|
413
|
+
const message = await this.resolveProbeStreamCountingUsage(stream, "text-protocol-calibration", this.nextProbeUsageReportId(model, `text-protocol:${variant}`));
|
|
414
|
+
const text = message.content
|
|
415
|
+
.filter((block) => block.type === "text")
|
|
416
|
+
.map((block) => block.text)
|
|
417
|
+
.join("\n")
|
|
418
|
+
.trim();
|
|
419
|
+
if (!text)
|
|
420
|
+
return false;
|
|
421
|
+
const parsed = parseTextToolCalls(text, [TEXT_TOOL_PROTOCOL_ECHO_TOOL]);
|
|
422
|
+
return parsed.calls.some((call) => call.name === "echo" && call.arguments.data === token);
|
|
423
|
+
}
|
|
424
|
+
async calibrateTextToolProtocolForModel(model, modelKey, options) {
|
|
425
|
+
const variantsTried = [];
|
|
426
|
+
for (const variant of TEXT_TOOL_PROTOCOL_VARIANTS) {
|
|
427
|
+
variantsTried.push(variant);
|
|
428
|
+
let passed = true;
|
|
429
|
+
for (let trial = 0; trial < TEXT_TOOL_PROTOCOL_TRIALS_PER_VARIANT; trial++) {
|
|
430
|
+
if (!(await this.runTextProtocolTrial(model, variant, `pi-calibration-${trial + 1}`))) {
|
|
431
|
+
passed = false;
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (passed) {
|
|
436
|
+
const calibratedAt = new Date().toISOString();
|
|
437
|
+
if (modelKey) {
|
|
438
|
+
this.deps.adaptationStore.setProtocol(modelKey, { version: MODEL_TOOL_PROTOCOL_VERSION, status: "calibrated", variant, calibratedAt }, calibratedAt);
|
|
439
|
+
}
|
|
440
|
+
return { status: "calibrated", variant, calibratedAt };
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const attemptedAt = new Date().toISOString();
|
|
444
|
+
if (modelKey && options.persistFailure) {
|
|
445
|
+
this.deps.adaptationStore.setProtocol(modelKey, { version: MODEL_TOOL_PROTOCOL_VERSION, status: "failed", attemptedAt, variantsTried }, attemptedAt);
|
|
446
|
+
}
|
|
447
|
+
return { status: "failed", attemptedAt, variantsTried };
|
|
448
|
+
}
|
|
449
|
+
formatToolProbeReport(results) {
|
|
450
|
+
const lines = [
|
|
451
|
+
"Tool probe results:",
|
|
452
|
+
"Model | Verdict | Variant | Native grade | Diagnostic",
|
|
453
|
+
"--- | --- | --- | --- | ---",
|
|
454
|
+
];
|
|
455
|
+
for (const result of results) {
|
|
456
|
+
lines.push([
|
|
457
|
+
result.model,
|
|
458
|
+
result.verdict,
|
|
459
|
+
result.variant ?? "-",
|
|
460
|
+
result.nativeGrade ?? "-",
|
|
461
|
+
result.diagnostic ? result.diagnostic.replace(/\s+/g, " ").slice(0, 160) : "-",
|
|
462
|
+
].join(" | "));
|
|
463
|
+
}
|
|
464
|
+
return lines.join("\n");
|
|
465
|
+
}
|
|
466
|
+
storeToolProbe(modelKey, probe) {
|
|
467
|
+
this.deps.adaptationStore.setToolProbe(modelKey, probe, probe.probedAt);
|
|
468
|
+
}
|
|
469
|
+
async resolveToolProbeModels(target) {
|
|
470
|
+
const trimmed = target?.trim();
|
|
471
|
+
if (!trimmed)
|
|
472
|
+
return this.deps.getModelRegistry().getAvailable();
|
|
473
|
+
const [provider, ...modelParts] = trimmed.split("/");
|
|
474
|
+
const modelId = modelParts.join("/");
|
|
475
|
+
if (!provider || !modelId)
|
|
476
|
+
throw new Error("Usage: /toolprobe [provider/model]");
|
|
477
|
+
const exact = this.deps.getModelRegistry().find(provider, modelId);
|
|
478
|
+
if (exact)
|
|
479
|
+
return [exact];
|
|
480
|
+
const current = this.deps.agent.state.model;
|
|
481
|
+
if (current?.provider === provider && current.id === modelId)
|
|
482
|
+
return [current];
|
|
483
|
+
throw new Error(`Model not found: ${trimmed}`);
|
|
484
|
+
}
|
|
485
|
+
maybeInjectCorrectiveSteer(variant) {
|
|
486
|
+
this.correctiveSteerCount++;
|
|
487
|
+
if (this.correctiveSteerCount !== 1 && this.correctiveSteerCount % TEXT_TOOL_PROTOCOL_STEER_INTERVAL !== 0) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
const reminder = `Reminder: to call a tool, emit exactly this envelope shape: ${formatVariantEnvelope(variant, "TOOL", '{"arg":"value"}')} — no other format is recognized. Reasoning may appear as prose before the envelope, never inside it.`;
|
|
491
|
+
this.deps.sendCorrectiveSteer(reminder).catch(() => {
|
|
492
|
+
// Best-effort steer.
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
looksLikeTextProtocolAttempt(text) {
|
|
496
|
+
return /<pi:call\b|<tool_call\b|```(?:tool|tool_call)[\s\S]*"name"\s*:/i.test(text);
|
|
497
|
+
}
|
|
498
|
+
repairSessionCount(modelKey, mode) {
|
|
499
|
+
const key = `${modelKey}\0${mode}`;
|
|
500
|
+
const count = (this.repairModeSessionCounts.get(key) ?? 0) + 1;
|
|
501
|
+
this.repairModeSessionCounts.set(key, count);
|
|
502
|
+
return count;
|
|
503
|
+
}
|
|
504
|
+
hasFreshToolProbeVerdict(modelKey) {
|
|
505
|
+
const probedAt = this.deps.adaptationStore.get(modelKey).toolProbe?.probedAt;
|
|
506
|
+
if (!probedAt)
|
|
507
|
+
return false;
|
|
508
|
+
const age = Date.now() - new Date(probedAt).getTime();
|
|
509
|
+
return Number.isFinite(age) && age >= 0 && age < AUTO_TOOL_PROBE_FRESHNESS_MS;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
//# sourceMappingURL=tool-protocol-controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-protocol-controller.js","sourceRoot":"","sources":["../../src/core/tool-protocol-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAiBjC,OAAO,EACN,4BAA4B,EAC5B,qBAAqB,EACrB,8BAA8B,EAC9B,kBAAkB,EAClB,2BAA2B,GAC3B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAE/D,OAAO,EACN,2BAA2B,EAE3B,wBAAwB,GACxB,MAAM,0BAA0B,CAAC;AAUlC,OAAO,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAC;AACvE,OAAO,EAAE,gCAAgC,EAAE,MAAM,2BAA2B,CAAC;AAE7E,MAAM,iCAAiC,GAAG,CAAC,CAAC;AAC5C,MAAM,qCAAqC,GAAG,CAAC,CAAC;AAChD,MAAM,0CAA0C,GAAG,CAAC,CAAC;AACrD,MAAM,i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C;QAC7C,OAAO,KAAK,CAAC;IAAA,CACb;IAEO,wBAAwB,CAAC,QAAgB,EAAW;QAC3D,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,SAAS,EAAE,QAAQ,CAAC;QAC7E,IAAI,CAAC,QAAQ;YAAE,OAAO,KAAK,CAAC;QAC5B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC,OAAO,EAAE,CAAC;QACtD,OAAO,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,GAAG,GAAG,4BAA4B,CAAC;IAAA,CAC9E;CACD","sourcesContent":["import { rmSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport type { Agent, StreamFn } from \"@caupulican/pi-agent-core\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tAssistantMessageEventStream,\n\tContext,\n\tModel,\n\tSimpleStreamOptions,\n\tTextContent,\n\tTextToolProtocolParseEvent,\n\tTextToolProtocolVariant,\n\tTool,\n\tToolArgumentValidationTelemetryEvent,\n\tToolRepairModeName,\n\tUsage,\n} from \"@caupulican/pi-ai\";\nimport {\n\tformatToolRepairStandingRule,\n\tformatVariantEnvelope,\n\tgenerateTextToolProtocolPrimer,\n\tparseTextToolCalls,\n\tTEXT_TOOL_PROTOCOL_VARIANTS,\n} from \"@caupulican/pi-ai\";\nimport { Type } from \"typebox\";\nimport { getProcessWorkRun } from \"../utils/work-directory.ts\";\nimport type { ModelRegistry } from \"./model-registry.ts\";\nimport {\n\tMODEL_TOOL_PROTOCOL_VERSION,\n\ttype ModelToolProtocolResolution,\n\tresolveModelToolProtocol,\n} from \"./model-tool-protocol.ts\";\nimport type {\n\tModelAdaptationRule,\n\tModelAdaptationStore,\n\tModelToolProbe,\n\tModelToolProbeVerdict,\n\tNativeToolProbeGrade,\n} from \"./models/adaptation-store.ts\";\nimport type { SettingsManager } from \"./settings-manager.ts\";\nimport type { ToolRecoveryLoggerStats } from \"./tool-recovery-logger.ts\";\nimport { formatToolRepairHealthReport } from \"./tool-repair-health.ts\";\nimport { resolveCurrentToolRepairSettings } from \"./tool-repair-settings.ts\";\n\nconst MODEL_ADAPTATION_REPAIR_THRESHOLD = 3;\nconst TEXT_TOOL_PROTOCOL_TRIALS_PER_VARIANT = 2;\nconst TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD = 3;\nconst TEXT_TOOL_PROTOCOL_STEER_INTERVAL = 5;\nconst AUTO_TOOL_PROBE_FRESHNESS_MS = 15 * 60 * 1000;\nconst TEXT_TOOL_PROTOCOL_ECHO_TOOL = {\n\tname: \"echo\",\n\tdescription: \"Echo calibration data\",\n\tparameters: Type.Object({ data: Type.String() }),\n} satisfies Tool;\nconst NATIVE_TOOL_PROBE_READ_TOOL = {\n\tname: \"read\",\n\tdescription: \"Read file contents\",\n\tparameters: Type.Object({ path: Type.String() }),\n} satisfies Tool;\n\nexport type ToolProbeVerdict = \"native\" | \"text-protocol\" | \"none\";\n\nexport interface ToolProbeResult {\n\tmodel: string;\n\tverdict: ToolProbeVerdict;\n\tvariant?: TextToolProtocolVariant;\n\tnativeGrade?: NativeToolProbeGrade;\n\tdiagnostic?: string;\n}\n\nexport interface ToolProbeReport {\n\tresults: ToolProbeResult[];\n\ttable: string;\n}\n\nexport interface ToolProtocolControllerDeps {\n\tagent: Agent;\n\tagentDir: string;\n\tsettingsManager: SettingsManager;\n\tgetModelRegistry(): ModelRegistry;\n\tadaptationStore: ModelAdaptationStore;\n\tisRawStreamSimple(fn: StreamFn): boolean;\n\tgetRequiredRequestAuth(model: Model<Api>): Promise<{ apiKey: string; headers?: Record<string, string> }>;\n\taddSpawnedUsage(usage: Usage, opts: { label?: string; reportId: string }): string | undefined;\n\temitWarning(message: string): void;\n\tsendCorrectiveSteer(message: string): Promise<void>;\n\tfindLastAssistantMessage(): AssistantMessage | undefined;\n\tisDisposed(): boolean;\n\tprobeForAuto(model: Model<Api>): Promise<ToolProbeResult>;\n}\n\n/** Owns model tool-protocol selection, probing, circuit breaking, and repair teaching. */\nexport class ToolProtocolController {\n\tprivate readonly repairModeSessionCounts = new Map<string, number>();\n\tprivate readonly parseFailures = new Map<string, { signature: string; repeats: number }>();\n\tprivate parseObservedThisTurn = false;\n\tprivate correctiveSteerCount = 0;\n\tprivate probeUsageReportSeq = 0;\n\tprivate readonly autoProbedModels = new Set<string>();\n\tprivate validationOutcomeThisTurn: TextToolProtocolParseEvent | undefined;\n\tprivate readonly deps: ToolProtocolControllerDeps;\n\n\tconstructor(deps: ToolProtocolControllerDeps) {\n\t\tthis.deps = deps;\n\t}\n\n\tapplyRepairLayerSettings(): void {\n\t\tthis.deps.agent.toolArgumentTeachEnabled = this.getRepairSettings().teach;\n\t}\n\n\tresetTurnState(): void {\n\t\tthis.parseObservedThisTurn = false;\n\t\tthis.validationOutcomeThisTurn = undefined;\n\t}\n\n\tgetAdaptationRulesForPrompt(): ModelAdaptationRule[] {\n\t\tif (!this.getRepairSettings().teach) return [];\n\t\tconst modelKey = this.modelKey(this.deps.agent.state.model);\n\t\treturn modelKey ? this.deps.adaptationStore.get(modelKey).rules : [];\n\t}\n\n\tresolveModelToolProtocol(model: Model<Api>): ModelToolProtocolResolution {\n\t\treturn resolveModelToolProtocol({\n\t\t\tmodel,\n\t\t\tsettingsOverride: this.getRepairSettings().textProtocol,\n\t\t\tadaptation: this.deps.adaptationStore.get(this.modelRef(model)),\n\t\t});\n\t}\n\n\tasync ensureActiveModelProtocol(): Promise<void> {\n\t\tconst model = this.deps.agent.state.model;\n\t\tconst resolution = this.resolveModelToolProtocol(model);\n\t\tthis.deps.agent.textToolCallProtocol = resolution.protocol;\n\t\tif (\n\t\t\tresolution.reasonCode !== \"probe_calibration_missing\" &&\n\t\t\tresolution.reasonCode !== \"probe_calibration_failed\" &&\n\t\t\tresolution.reasonCode !== \"probe_calibration_invalid\"\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tconst modelKey = this.modelRef(model);\n\t\tthis.deps.emitWarning(\n\t\t\tresolution.reasonCode === \"probe_calibration_failed\"\n\t\t\t\t? `Text tool protocol calibration for ${modelKey} previously failed (variants tried: ${(resolution.variantsTried ?? []).join(\", \")}); falling back to native tool calls this turn. Run /toolprobe ${modelKey} to recalibrate.`\n\t\t\t\t: `Text tool protocol for ${modelKey} has no valid calibration on record; falling back to native tool calls this turn. Run /toolprobe ${modelKey} to calibrate.`,\n\t\t);\n\t}\n\n\tgetToolProbeVerdict(model: Model<Api>): ModelToolProbeVerdict | undefined {\n\t\treturn this.deps.adaptationStore.get(this.modelRef(model)).toolProbe?.status;\n\t}\n\n\tasync probeToolCalling(target?: string): Promise<ToolProbeReport> {\n\t\tconst models = await this.resolveToolProbeModels(target);\n\t\tif (models.length === 0) throw new Error(\"No available models to probe.\");\n\t\tconst results: ToolProbeResult[] = [];\n\t\tfor (const model of models) results.push(await this.probeToolCallingForModel(model));\n\t\treturn { results, table: this.formatToolProbeReport(results) };\n\t}\n\n\tasync probeToolCallingForModel(model: Model<Api>): Promise<ToolProbeResult> {\n\t\tconst modelKey = this.modelRef(model);\n\t\tconst probedAt = new Date().toISOString();\n\t\tlet nativeGrade: NativeToolProbeGrade = \"absent\";\n\t\tlet diagnostic: string | undefined;\n\t\ttry {\n\t\t\tnativeGrade = await this.gradeNativeToolCallingForModel(model, \"pi-native-probe\");\n\t\t\tif (nativeGrade === \"task\") {\n\t\t\t\tthis.storeToolProbe(modelKey, {\n\t\t\t\t\tversion: MODEL_TOOL_PROTOCOL_VERSION,\n\t\t\t\t\tstatus: \"native\",\n\t\t\t\t\tprobedAt,\n\t\t\t\t\tnativeGrade,\n\t\t\t\t});\n\t\t\t\treturn { model: modelKey, verdict: \"native\", nativeGrade };\n\t\t\t}\n\t\t\tdiagnostic =\n\t\t\t\tnativeGrade === \"echo-only\"\n\t\t\t\t\t? \"Native echo probe passed but task-scale read probe failed.\"\n\t\t\t\t\t: \"Native task-scale read and echo probes did not produce provider-native tool calls.\";\n\t\t} catch (error) {\n\t\t\tdiagnostic = error instanceof Error ? error.message : String(error);\n\t\t}\n\n\t\ttry {\n\t\t\tconst calibrated = await this.calibrateTextToolProtocolForModel(model, modelKey, { persistFailure: false });\n\t\t\tif (calibrated.status === \"calibrated\") {\n\t\t\t\tthis.storeToolProbe(modelKey, {\n\t\t\t\t\tversion: MODEL_TOOL_PROTOCOL_VERSION,\n\t\t\t\t\tstatus: \"text-protocol\",\n\t\t\t\t\tprobedAt: calibrated.calibratedAt,\n\t\t\t\t\tvariant: calibrated.variant,\n\t\t\t\t\tnativeGrade,\n\t\t\t\t\tdiagnostic,\n\t\t\t\t});\n\t\t\t\treturn { model: modelKey, verdict: \"text-protocol\", variant: calibrated.variant, nativeGrade, diagnostic };\n\t\t\t}\n\t\t\tdiagnostic = `${diagnostic ? `${diagnostic} ` : \"\"}Text protocol variants failed: ${calibrated.variantsTried.join(\", \")}`;\n\t\t} catch (error) {\n\t\t\tdiagnostic = error instanceof Error ? error.message : String(error);\n\t\t}\n\n\t\tthis.storeToolProbe(modelKey, {\n\t\t\tversion: MODEL_TOOL_PROTOCOL_VERSION,\n\t\t\tstatus: \"none\",\n\t\t\tprobedAt,\n\t\t\tnativeGrade,\n\t\t\tdiagnostic,\n\t\t});\n\t\treturn { model: modelKey, verdict: \"none\", nativeGrade, diagnostic };\n\t}\n\n\thandleTextProtocolParse(event: TextToolProtocolParseEvent): void {\n\t\tthis.parseObservedThisTurn = true;\n\t\tconst modelKey = `${event.provider}/${event.model}`;\n\t\tif (event.status === \"parsed\") return;\n\t\tthis.maybeInjectCorrectiveSteer(event.variant);\n\t\tconst signature = `${event.variant}:${event.reason ?? \"failed\"}`;\n\t\tconst previous = this.parseFailures.get(modelKey);\n\t\tconst repeats = previous?.signature === signature ? previous.repeats + 1 : 1;\n\t\tthis.parseFailures.set(modelKey, { signature, repeats });\n\t\tif (repeats < TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD) return;\n\n\t\tconst profile = this.deps.adaptationStore.get(modelKey);\n\t\tif (profile.protocol?.version === MODEL_TOOL_PROTOCOL_VERSION && profile.protocol.status !== \"failed\") {\n\t\t\tthis.deps.adaptationStore.removeProtocol(modelKey);\n\t\t\tthis.deps.agent.textToolCallProtocol = undefined;\n\t\t}\n\t\tthis.parseFailures.delete(modelKey);\n\t\tconst probedAt = new Date().toISOString();\n\t\tthis.deps.adaptationStore.setToolProbe(\n\t\t\tmodelKey,\n\t\t\t{\n\t\t\t\tversion: MODEL_TOOL_PROTOCOL_VERSION,\n\t\t\t\tstatus: \"none\",\n\t\t\t\tprobedAt,\n\t\t\t\tnativeGrade: profile.toolProbe?.nativeGrade,\n\t\t\t\tdiagnostic: `Text protocol parsing failed ${TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD}x in a row with signature \"${signature}\".`,\n\t\t\t},\n\t\t\tprobedAt,\n\t\t);\n\t\tthis.deps.emitWarning(\n\t\t\t`Text tool protocol for ${modelKey} stopped parsing after ${TEXT_TOOL_PROTOCOL_PARSE_FAILURE_THRESHOLD} attempts; demoted to native fallback. Run /toolprobe ${modelKey} to recalibrate.`,\n\t\t);\n\t}\n\n\thandleValidationOutcome(event: ToolArgumentValidationTelemetryEvent): void {\n\t\tif (event.source !== \"text-protocol\") return;\n\t\tconst protocol = this.deps.agent.textToolCallProtocol;\n\t\tconst variant = protocol === true ? \"tool-tag\" : protocol ? protocol.variant : undefined;\n\t\tif (!variant) return;\n\t\tconst status = event.outcome === \"bounced\" ? \"failed\" : \"parsed\";\n\t\tif (this.validationOutcomeThisTurn?.status === \"parsed\" && status === \"failed\") return;\n\t\tthis.validationOutcomeThisTurn = {\n\t\t\tprovider: event.provider ?? this.deps.agent.state.model.provider,\n\t\t\tmodel: event.model ?? this.deps.agent.state.model.id,\n\t\t\tvariant,\n\t\t\tstatus,\n\t\t\tcallCount: 1,\n\t\t\ttextLength: 0,\n\t\t\t...(status === \"failed\" && {\n\t\t\t\treason: event.errorKeywords?.includes(\"unknown_tool\") ? \"unknown-tool\" : \"validation-failed\",\n\t\t\t}),\n\t\t};\n\t}\n\n\trecordParseOutcomeFromLastAssistant(): void {\n\t\tconst validationOutcome = this.validationOutcomeThisTurn;\n\t\tthis.validationOutcomeThisTurn = undefined;\n\t\tif (validationOutcome?.status === \"parsed\") {\n\t\t\tthis.parseObservedThisTurn = true;\n\t\t\tthis.parseFailures.delete(`${validationOutcome.provider}/${validationOutcome.model}`);\n\t\t\treturn;\n\t\t}\n\t\tif (validationOutcome) {\n\t\t\tthis.handleTextProtocolParse(validationOutcome);\n\t\t\treturn;\n\t\t}\n\t\tif (this.parseObservedThisTurn) return;\n\t\tconst protocol = this.deps.agent.textToolCallProtocol;\n\t\tif (protocol === false || protocol === true || !protocol?.variant) return;\n\t\tconst response = this.deps.findLastAssistantMessage();\n\t\tif (!response) return;\n\t\tconst responseText = response.content\n\t\t\t.filter((content): content is TextContent => content.type === \"text\")\n\t\t\t.map((content) => content.text)\n\t\t\t.join(\"\\n\");\n\t\tif (!responseText) return;\n\n\t\tconst parsed = parseTextToolCalls(responseText, this.deps.agent.state.tools);\n\t\tconst attempted = parsed.attempted || this.looksLikeTextProtocolAttempt(responseText);\n\t\tif (!attempted) return;\n\t\tthis.handleTextProtocolParse({\n\t\t\tprovider: this.deps.agent.state.model.provider,\n\t\t\tmodel: this.deps.agent.state.model.id,\n\t\t\tvariant: protocol.variant,\n\t\t\tstatus: parsed.calls.length > 0 ? \"parsed\" : \"failed\",\n\t\t\treason: parsed.failure,\n\t\t\tcallCount: parsed.calls.length,\n\t\t\ttextLength: responseText.length,\n\t\t});\n\t}\n\n\ttagAdaptationRuleTeaching(event: ToolArgumentValidationTelemetryEvent): ToolArgumentValidationTelemetryEvent {\n\t\tif (!this.getRepairSettings().teach || event.taught !== \"none\") return event;\n\t\tconst modelKey =\n\t\t\tevent.provider && event.model\n\t\t\t\t? `${event.provider}/${event.model}`\n\t\t\t\t: this.modelKey(this.deps.agent.state.model);\n\t\tif (!modelKey) return event;\n\t\ttry {\n\t\t\tconst rules = this.deps.adaptationStore.get(modelKey).rules;\n\t\t\tconst modes = new Set([...event.failureModes, ...event.repairsApplied]);\n\t\t\tif (rules.some((rule) => modes.has(rule.mode as ToolRepairModeName))) return { ...event, taught: \"rule\" };\n\t\t} catch {\n\t\t\t// Best-effort telemetry tagging.\n\t\t}\n\t\treturn event;\n\t}\n\n\thandleAdaptationTelemetry(event: ToolArgumentValidationTelemetryEvent): void {\n\t\tif (!this.getRepairSettings().teach || event.outcome !== \"repaired\" || event.repairsApplied.length === 0) return;\n\t\tconst modelKey =\n\t\t\tevent.provider && event.model\n\t\t\t\t? `${event.provider}/${event.model}`\n\t\t\t\t: this.modelKey(this.deps.agent.state.model);\n\t\tif (!modelKey) return;\n\t\ttry {\n\t\t\tfor (const mode of [...new Set(event.repairsApplied)]) {\n\t\t\t\tconst profile = this.deps.adaptationStore.get(modelKey);\n\t\t\t\tconst stats = profile.teachStats[mode] ?? { taught: 0, recurrenceBefore: 0, recurrenceAfter: 0 };\n\t\t\t\tif (profile.rules.some((rule) => rule.mode === mode)) {\n\t\t\t\t\tthis.deps.adaptationStore.markRuleFired(modelKey, mode);\n\t\t\t\t\tthis.deps.adaptationStore.setTeachStats(modelKey, mode, {\n\t\t\t\t\t\t...stats,\n\t\t\t\t\t\trecurrenceAfter: stats.recurrenceAfter + 1,\n\t\t\t\t\t});\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst recurrenceBefore = stats.recurrenceBefore + 1;\n\t\t\t\tthis.deps.adaptationStore.setTeachStats(modelKey, mode, { ...stats, recurrenceBefore });\n\t\t\t\tconst sessionCount = this.repairSessionCount(modelKey, mode);\n\t\t\t\tif (\n\t\t\t\t\tsessionCount >= MODEL_ADAPTATION_REPAIR_THRESHOLD ||\n\t\t\t\t\trecurrenceBefore >= MODEL_ADAPTATION_REPAIR_THRESHOLD\n\t\t\t\t) {\n\t\t\t\t\tthis.deps.adaptationStore.addRule(modelKey, { mode, text: formatToolRepairStandingRule(mode) });\n\t\t\t\t\tthis.deps.adaptationStore.setTeachStats(modelKey, mode, {\n\t\t\t\t\t\t...stats,\n\t\t\t\t\t\ttaught: stats.taught + 1,\n\t\t\t\t\t\trecurrenceBefore,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Best-effort adaptation persistence.\n\t\t}\n\t}\n\n\tmaybeAutoProbe(model: Model<Api>): void {\n\t\tconst modelKey = this.modelRef(model);\n\t\tif (this.autoProbedModels.has(modelKey) || this.hasFreshToolProbeVerdict(modelKey)) return;\n\t\tthis.autoProbedModels.add(modelKey);\n\t\tvoid this.deps\n\t\t\t.probeForAuto(model)\n\t\t\t.then((result) => {\n\t\t\t\tif (this.deps.isDisposed()) return;\n\t\t\t\tconst detail =\n\t\t\t\t\tresult.verdict === \"text-protocol\"\n\t\t\t\t\t\t? ` (variant ${result.variant}); it will use the text tool protocol starting next turn`\n\t\t\t\t\t\t: result.verdict === \"none\"\n\t\t\t\t\t\t\t? \" — no working tool-call path was found; run /toolprobe for details\"\n\t\t\t\t\t\t\t: \"; native tool calls stay in use\";\n\t\t\t\tthis.deps.emitWarning(\n\t\t\t\t\t`Auto-probed ${modelKey} after repeated native tool-call validation failures: verdict \"${result.verdict}\"${detail}.`,\n\t\t\t\t);\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\tif (this.deps.isDisposed()) return;\n\t\t\t\tthis.deps.emitWarning(\n\t\t\t\t\t`Auto-probe for ${modelKey} (triggered by repeated tool-call validation failures) did not complete: ${error instanceof Error ? error.message : String(error)}.`,\n\t\t\t\t);\n\t\t\t});\n\t}\n\n\tformatRepairHealth(stats?: ToolRecoveryLoggerStats): string {\n\t\treturn formatToolRepairHealthReport(this.deps.adaptationStore, new Date(), stats);\n\t}\n\n\tremoveRepairRule(model: string, mode: string): boolean {\n\t\treturn this.deps.adaptationStore.removeRule(model, mode);\n\t}\n\n\tresetProtocolCalibration(model: string): boolean {\n\t\tconst removed = this.deps.adaptationStore.removeProtocol(model);\n\t\tthis.parseFailures.delete(model);\n\t\treturn removed;\n\t}\n\n\tgetRepairSettings() {\n\t\treturn resolveCurrentToolRepairSettings(this.deps.settingsManager.settings);\n\t}\n\n\tprivate modelKey(model: Model<Api> | undefined): string | undefined {\n\t\treturn model ? this.modelRef(model) : undefined;\n\t}\n\n\tprivate modelRef(model: Model<Api>): string {\n\t\treturn `${model.provider}/${model.id}`;\n\t}\n\n\tprivate async streamForProbe(model: Model<Api>, context: Context, options: SimpleStreamOptions) {\n\t\tlet requestOptions = options;\n\t\tif (this.deps.isRawStreamSimple(this.deps.agent.streamFn)) {\n\t\t\tconst auth = await this.deps.getRequiredRequestAuth(model);\n\t\t\trequestOptions = {\n\t\t\t\t...options,\n\t\t\t\tapiKey: auth.apiKey,\n\t\t\t\theaders: auth.headers || options.headers ? { ...auth.headers, ...options.headers } : undefined,\n\t\t\t};\n\t\t}\n\t\treturn this.deps.agent.streamFn(model, context, requestOptions);\n\t}\n\n\tprivate nextProbeUsageReportId(model: Model<Api>, kind: string): string {\n\t\treturn `tool-probe:${this.modelRef(model)}:${kind}:${this.probeUsageReportSeq++}`;\n\t}\n\n\tprivate async resolveProbeStreamCountingUsage(\n\t\tstream: AssistantMessageEventStream,\n\t\tlabel: string,\n\t\treportId: string,\n\t): Promise<AssistantMessage> {\n\t\tconst message = await stream.result();\n\t\tconst usage = message.usage;\n\t\tif (usage && (usage.cost.total > 0 || usage.totalTokens > 0)) {\n\t\t\tthis.deps.addSpawnedUsage(usage, { label, reportId });\n\t\t}\n\t\treturn message;\n\t}\n\n\tprivate textProtocolCalibrationContext(variant: TextToolProtocolVariant, token: string): Context {\n\t\tconst primer = generateTextToolProtocolPrimer([TEXT_TOOL_PROTOCOL_ECHO_TOOL], { variant });\n\t\tconst instruction = `Text tool protocol calibration trial. Using the protocol above, call echo with data exactly \"${token}\". Output only the tool-call envelope.`;\n\t\treturn {\n\t\t\tsystemPrompt: `${primer}\\n\\n${instruction}`,\n\t\t\tmessages: [{ role: \"user\", content: [{ type: \"text\", text: instruction }], timestamp: Date.now() }],\n\t\t};\n\t}\n\n\tprivate messageHasToolCallWithStringArgument(\n\t\tmessage: AssistantMessage,\n\t\ttoolName: string,\n\t\targName: string,\n\t\targValue: string,\n\t): boolean {\n\t\treturn message.content.some((block) => {\n\t\t\tif (block.type !== \"toolCall\" || block.name !== toolName) return false;\n\t\t\tconst args = block.arguments as unknown;\n\t\t\treturn (\n\t\t\t\ttypeof args === \"object\" &&\n\t\t\t\targs !== null &&\n\t\t\t\t!Array.isArray(args) &&\n\t\t\t\t(args as Record<string, unknown>)[argName] === argValue\n\t\t\t);\n\t\t});\n\t}\n\n\tprivate nativeToolProbeSystemPrompt(instruction: string): string {\n\t\tconst base = (this.deps.agent.state.systemPrompt ?? \"\").trim();\n\t\treturn base ? `${base}\\n\\n${instruction}` : instruction;\n\t}\n\n\tprivate async runNativeReadTaskProbeTrial(model: Model<Api>, path: string): Promise<boolean> {\n\t\tconst instruction =\n\t\t\t`Native tool-call capability probe: task-scale read. Use provider-native tool calling, not prose. ` +\n\t\t\t`Call read exactly once with path exactly \"${path}\".`;\n\t\tconst stream = await this.streamForProbe(\n\t\t\tmodel,\n\t\t\t{\n\t\t\t\tsystemPrompt: this.nativeToolProbeSystemPrompt(instruction),\n\t\t\t\tmessages: [{ role: \"user\", content: [{ type: \"text\", text: instruction }], timestamp: Date.now() }],\n\t\t\t\ttools: [NATIVE_TOOL_PROBE_READ_TOOL],\n\t\t\t},\n\t\t\t{ textToolCallProtocol: false, maxRetries: 0, temperature: 0, maxTokens: 768 },\n\t\t);\n\t\tconst message = await this.resolveProbeStreamCountingUsage(\n\t\t\tstream,\n\t\t\t\"tool-probe\",\n\t\t\tthis.nextProbeUsageReportId(model, \"read-task\"),\n\t\t);\n\t\treturn this.messageHasToolCallWithStringArgument(message, \"read\", \"path\", path);\n\t}\n\n\tprivate async runNativeEchoToolProbeTrial(model: Model<Api>, token: string): Promise<boolean> {\n\t\tconst instruction =\n\t\t\t`Native tool-call capability probe: echo-only. Use provider-native tool calling, not prose. ` +\n\t\t\t`Call echo with data exactly \"${token}\".`;\n\t\tconst stream = await this.streamForProbe(\n\t\t\tmodel,\n\t\t\t{\n\t\t\t\tsystemPrompt: this.nativeToolProbeSystemPrompt(instruction),\n\t\t\t\tmessages: [{ role: \"user\", content: [{ type: \"text\", text: instruction }], timestamp: Date.now() }],\n\t\t\t\ttools: [TEXT_TOOL_PROTOCOL_ECHO_TOOL],\n\t\t\t},\n\t\t\t{ textToolCallProtocol: false, maxRetries: 0, temperature: 0, maxTokens: 256 },\n\t\t);\n\t\tconst message = await this.resolveProbeStreamCountingUsage(\n\t\t\tstream,\n\t\t\t\"tool-probe\",\n\t\t\tthis.nextProbeUsageReportId(model, \"echo\"),\n\t\t);\n\t\treturn this.messageHasToolCallWithStringArgument(message, \"echo\", \"data\", token);\n\t}\n\n\tprivate async gradeNativeToolCallingForModel(model: Model<Api>, token: string): Promise<NativeToolProbeGrade> {\n\t\tconst path = join(\n\t\t\tgetProcessWorkRun(this.deps.agentDir, \"probes\", \"native-tools\").path,\n\t\t\t`pi-native-probe-${process.pid}-${Date.now()}.txt`,\n\t\t);\n\t\twriteFileSync(path, token, \"utf-8\");\n\t\ttry {\n\t\t\tif (await this.runNativeReadTaskProbeTrial(model, path)) return \"task\";\n\t\t\tif (await this.runNativeEchoToolProbeTrial(model, token)) return \"echo-only\";\n\t\t\treturn \"absent\";\n\t\t} finally {\n\t\t\trmSync(path, { force: true });\n\t\t}\n\t}\n\n\tprivate async runTextProtocolTrial(\n\t\tmodel: Model<Api>,\n\t\tvariant: TextToolProtocolVariant,\n\t\ttoken: string,\n\t): Promise<boolean> {\n\t\tconst stream = await this.streamForProbe(model, this.textProtocolCalibrationContext(variant, token), {\n\t\t\ttextToolCallProtocol: false,\n\t\t\tmaxRetries: 0,\n\t\t\ttemperature: 0,\n\t\t\tmaxTokens: 256,\n\t\t});\n\t\tconst message = await this.resolveProbeStreamCountingUsage(\n\t\t\tstream,\n\t\t\t\"text-protocol-calibration\",\n\t\t\tthis.nextProbeUsageReportId(model, `text-protocol:${variant}`),\n\t\t);\n\t\tconst text = message.content\n\t\t\t.filter((block): block is TextContent => block.type === \"text\")\n\t\t\t.map((block) => block.text)\n\t\t\t.join(\"\\n\")\n\t\t\t.trim();\n\t\tif (!text) return false;\n\t\tconst parsed = parseTextToolCalls(text, [TEXT_TOOL_PROTOCOL_ECHO_TOOL]);\n\t\treturn parsed.calls.some((call) => call.name === \"echo\" && call.arguments.data === token);\n\t}\n\n\tprivate async calibrateTextToolProtocolForModel(\n\t\tmodel: Model<Api>,\n\t\tmodelKey: string | undefined,\n\t\toptions: { persistFailure: boolean },\n\t): Promise<\n\t\t| { status: \"calibrated\"; variant: TextToolProtocolVariant; calibratedAt: string }\n\t\t| { status: \"failed\"; attemptedAt: string; variantsTried: string[] }\n\t> {\n\t\tconst variantsTried: string[] = [];\n\t\tfor (const variant of TEXT_TOOL_PROTOCOL_VARIANTS) {\n\t\t\tvariantsTried.push(variant);\n\t\t\tlet passed = true;\n\t\t\tfor (let trial = 0; trial < TEXT_TOOL_PROTOCOL_TRIALS_PER_VARIANT; trial++) {\n\t\t\t\tif (!(await this.runTextProtocolTrial(model, variant, `pi-calibration-${trial + 1}`))) {\n\t\t\t\t\tpassed = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (passed) {\n\t\t\t\tconst calibratedAt = new Date().toISOString();\n\t\t\t\tif (modelKey) {\n\t\t\t\t\tthis.deps.adaptationStore.setProtocol(\n\t\t\t\t\t\tmodelKey,\n\t\t\t\t\t\t{ version: MODEL_TOOL_PROTOCOL_VERSION, status: \"calibrated\", variant, calibratedAt },\n\t\t\t\t\t\tcalibratedAt,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treturn { status: \"calibrated\", variant, calibratedAt };\n\t\t\t}\n\t\t}\n\n\t\tconst attemptedAt = new Date().toISOString();\n\t\tif (modelKey && options.persistFailure) {\n\t\t\tthis.deps.adaptationStore.setProtocol(\n\t\t\t\tmodelKey,\n\t\t\t\t{ version: MODEL_TOOL_PROTOCOL_VERSION, status: \"failed\", attemptedAt, variantsTried },\n\t\t\t\tattemptedAt,\n\t\t\t);\n\t\t}\n\t\treturn { status: \"failed\", attemptedAt, variantsTried };\n\t}\n\n\tprivate formatToolProbeReport(results: readonly ToolProbeResult[]): string {\n\t\tconst lines = [\n\t\t\t\"Tool probe results:\",\n\t\t\t\"Model | Verdict | Variant | Native grade | Diagnostic\",\n\t\t\t\"--- | --- | --- | --- | ---\",\n\t\t];\n\t\tfor (const result of results) {\n\t\t\tlines.push(\n\t\t\t\t[\n\t\t\t\t\tresult.model,\n\t\t\t\t\tresult.verdict,\n\t\t\t\t\tresult.variant ?? \"-\",\n\t\t\t\t\tresult.nativeGrade ?? \"-\",\n\t\t\t\t\tresult.diagnostic ? result.diagnostic.replace(/\\s+/g, \" \").slice(0, 160) : \"-\",\n\t\t\t\t].join(\" | \"),\n\t\t\t);\n\t\t}\n\t\treturn lines.join(\"\\n\");\n\t}\n\n\tprivate storeToolProbe(modelKey: string, probe: ModelToolProbe): void {\n\t\tthis.deps.adaptationStore.setToolProbe(modelKey, probe, probe.probedAt);\n\t}\n\n\tprivate async resolveToolProbeModels(target?: string): Promise<Model<Api>[]> {\n\t\tconst trimmed = target?.trim();\n\t\tif (!trimmed) return this.deps.getModelRegistry().getAvailable();\n\t\tconst [provider, ...modelParts] = trimmed.split(\"/\");\n\t\tconst modelId = modelParts.join(\"/\");\n\t\tif (!provider || !modelId) throw new Error(\"Usage: /toolprobe [provider/model]\");\n\t\tconst exact = this.deps.getModelRegistry().find(provider, modelId);\n\t\tif (exact) return [exact];\n\t\tconst current = this.deps.agent.state.model;\n\t\tif (current?.provider === provider && current.id === modelId) return [current];\n\t\tthrow new Error(`Model not found: ${trimmed}`);\n\t}\n\n\tprivate maybeInjectCorrectiveSteer(variant: TextToolProtocolVariant): void {\n\t\tthis.correctiveSteerCount++;\n\t\tif (this.correctiveSteerCount !== 1 && this.correctiveSteerCount % TEXT_TOOL_PROTOCOL_STEER_INTERVAL !== 0) {\n\t\t\treturn;\n\t\t}\n\t\tconst reminder = `Reminder: to call a tool, emit exactly this envelope shape: ${formatVariantEnvelope(variant, \"TOOL\", '{\"arg\":\"value\"}')} — no other format is recognized. Reasoning may appear as prose before the envelope, never inside it.`;\n\t\tthis.deps.sendCorrectiveSteer(reminder).catch(() => {\n\t\t\t// Best-effort steer.\n\t\t});\n\t}\n\n\tprivate looksLikeTextProtocolAttempt(text: string): boolean {\n\t\treturn /<pi:call\\b|<tool_call\\b|```(?:tool|tool_call)[\\s\\S]*\"name\"\\s*:/i.test(text);\n\t}\n\n\tprivate repairSessionCount(modelKey: string, mode: ToolRepairModeName): number {\n\t\tconst key = `${modelKey}\\0${mode}`;\n\t\tconst count = (this.repairModeSessionCounts.get(key) ?? 0) + 1;\n\t\tthis.repairModeSessionCounts.set(key, count);\n\t\treturn count;\n\t}\n\n\tprivate hasFreshToolProbeVerdict(modelKey: string): boolean {\n\t\tconst probedAt = this.deps.adaptationStore.get(modelKey).toolProbe?.probedAt;\n\t\tif (!probedAt) return false;\n\t\tconst age = Date.now() - new Date(probedAt).getTime();\n\t\treturn Number.isFinite(age) && age >= 0 && age < AUTO_TOOL_PROBE_FRESHNESS_MS;\n\t}\n}\n"]}
|
|
@@ -23,9 +23,8 @@ export declare function isToolArgumentValidationLogRecord(value: unknown): value
|
|
|
23
23
|
/**
|
|
24
24
|
* Append + rotate under a single exclusive lock on `eventLogPath`. Without the lock, a concurrent
|
|
25
25
|
* writer (e.g. another session's tool-recovery worker) could append between this call's read and its
|
|
26
|
-
* rotated rewrite, and that append would be silently discarded when the rewrite lands
|
|
27
|
-
* tmp+rename
|
|
28
|
-
* a partially-rewritten (torn) log file.
|
|
26
|
+
* rotated rewrite, and that append would be silently discarded when the rewrite lands. The shared
|
|
27
|
+
* bounded JSONL sink also uses atomic tmp+rename rotation so readers never observe torn log files.
|
|
29
28
|
*/
|
|
30
29
|
export declare function writeToolRecoveryLogRecord(entry: ToolRecoveryLogWorkerRecord): void;
|
|
31
30
|
//# sourceMappingURL=tool-recovery-log-records.d.ts.map
|