gentle-pi 4.5.1 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -15
- package/assets/agents/gentle-ai-explore.md +1 -0
- package/assets/agents/gentle-ai-verify.md +8 -1
- package/assets/agents/gentle-ai-worker.md +4 -0
- package/assets/agents/jd-fix-agent.md +1 -0
- package/assets/agents/jd-judge-a.md +1 -0
- package/assets/agents/jd-judge-b.md +1 -0
- package/assets/design-canvas/canvas.css +13 -1
- package/assets/design-canvas/canvas.js +11 -2
- package/assets/install-wizard/wizard.js +1 -0
- package/assets/migrations/agent-body-history.json +162 -0
- package/assets/orchestrator-delegation-reference.md +7 -7
- package/assets/orchestrator-delegation.md +3 -3
- package/assets/orchestrator-full-delegation-card.md +65 -0
- package/assets/orchestrator-full-delegation.md +34 -0
- package/assets/orchestrator-memory.md +3 -3
- package/assets/orchestrator-prompts.md +1 -1
- package/assets/orchestrator-skills.md +1 -1
- package/assets/orchestrator-verification.md +14 -8
- package/assets/orchestrator.md +11 -11
- package/bin/gentle-shell.mjs +225 -82
- package/docs/agent-status.md +61 -0
- package/docs/bundled-install.md +7 -5
- package/docs/ci-performance.md +11 -0
- package/docs/delegated-verification.md +35 -7
- package/docs/fast-mode.md +15 -0
- package/docs/gentle-agents-activity.md +22 -0
- package/docs/gentle-shell.md +113 -25
- package/docs/install-wizard.md +2 -2
- package/docs/mcp-status.md +52 -0
- package/docs/permission-modes.md +9 -4
- package/docs/prompt-budget.md +28 -0
- package/docs/readme-reference.md +154 -50
- package/docs/render-cache-performance.md +11 -1
- package/docs/review-coordinator.md +33 -0
- package/docs/review-request-context.md +19 -0
- package/docs/review-windows-acl.md +18 -0
- package/docs/session-profile-format.md +20 -5
- package/docs/sound-notifications.md +1 -1
- package/docs/star-prompt.md +32 -0
- package/docs/voice.md +6 -3
- package/docs/work-continuity.md +46 -0
- package/docs/yolo-mode.md +5 -5
- package/extensions/ask-user-choice.ts +1 -0
- package/extensions/ask-user-question.ts +2 -0
- package/extensions/child-safety.ts +3 -0
- package/extensions/codegraph-tools.ts +38 -4
- package/extensions/gentle-agent-status.ts +99 -0
- package/extensions/gentle-agents.ts +803 -84
- package/extensions/gentle-ai.ts +2491 -596
- package/extensions/gentle-fast.ts +61 -0
- package/extensions/gentle-guide.ts +174 -0
- package/extensions/gentle-herdr-activity.ts +21 -1
- package/extensions/gentle-herdr-agent.ts +144 -0
- package/extensions/gentle-program-identity.ts +61 -0
- package/extensions/gentle-shell.ts +895 -120
- package/extensions/gentle-star-prompt.ts +334 -0
- package/extensions/gentle-stats.ts +5 -5
- package/extensions/gentle-todo.ts +59 -23
- package/extensions/gentle-tps.ts +48 -0
- package/extensions/history/store.ts +18 -12
- package/extensions/quiet-tools.ts +38 -8
- package/extensions/skill-registry.ts +210 -21
- package/extensions/startup-banner.ts +69 -10
- package/lib/agent-assets.ts +74 -17
- package/lib/agent-profile-pin.ts +3 -3
- package/lib/agent-profiles.ts +161 -3
- package/lib/agent-status.ts +105 -0
- package/lib/agents-config.ts +58 -3
- package/lib/agents-history.ts +24 -5
- package/lib/agents-keys.ts +8 -16
- package/lib/agents-messaging.ts +47 -5
- package/lib/agents-protocol.ts +80 -4
- package/lib/agents-runner.ts +287 -15
- package/lib/agents-view.ts +6 -1
- package/lib/agents-widget.ts +132 -9
- package/lib/auto-permission-runtime.ts +1 -1
- package/lib/bounded-writer-admission.ts +13 -1
- package/lib/bridge-cost.ts +28 -0
- package/lib/card-content-policy.ts +8 -6
- package/lib/codemode-renderer.ts +16 -0
- package/lib/command-palette-catalog.ts +1 -0
- package/lib/command-palette.ts +154 -25
- package/lib/context-backstop.ts +14 -5
- package/lib/destructive-command-guard.ts +48 -4
- package/lib/execution-surface-guard.ts +20 -0
- package/lib/exploration-budget.ts +64 -0
- package/lib/gentle-ai-binary.ts +1 -1
- package/lib/gentle-design.ts +3 -0
- package/lib/gentle-guide.ts +201 -0
- package/lib/gentle-shell-bundled.ts +6 -19
- package/lib/gentle-shell-launcher.ts +47 -8
- package/lib/herdr-activity.ts +23 -5
- package/lib/herdr-busy.ts +45 -0
- package/lib/herdr-task-summary-policy.ts +85 -0
- package/lib/image-attachments.ts +33 -0
- package/lib/image-disk-cache.ts +137 -0
- package/lib/inprocess-reviewer.ts +282 -132
- package/lib/jobs-sidebar-state.ts +5 -6
- package/lib/jobs-view.ts +198 -11
- package/lib/jobs-widget.ts +16 -52
- package/lib/keymaps-customize.ts +81 -0
- package/lib/keymaps.ts +291 -0
- package/lib/model-fallback-notice.ts +61 -0
- package/lib/model-fallback-policy.ts +141 -0
- package/lib/model-fallback-store.ts +143 -0
- package/lib/model-routing-authority.ts +32 -6
- package/lib/nan-provider.ts +3 -2
- package/lib/native-choice-list.ts +20 -2
- package/lib/native-review-cli.ts +103 -21
- package/lib/notification-customize.ts +48 -51
- package/lib/openai-fast-tier.ts +22 -0
- package/lib/orchestrator-discovery.ts +5 -1
- package/lib/orchestrator-fallback.ts +75 -0
- package/lib/orchestrator-mode-policy.ts +105 -0
- package/lib/pasted-image-paths.ts +113 -0
- package/lib/questionnaire/questionnaire-view.ts +225 -18
- package/lib/rdd-mode-control.ts +32 -0
- package/lib/reply-language-anchor.ts +104 -0
- package/lib/review-blocker.ts +92 -0
- package/lib/review-candidate-symlink-scan.ts +136 -0
- package/lib/review-candidate-view-owner.ts +85 -44
- package/lib/review-candidate-view.ts +10 -7
- package/lib/review-continuation-registry.ts +99 -0
- package/lib/review-coordinator.ts +163 -0
- package/lib/review-failure-facts.ts +49 -0
- package/lib/review-host-relay.ts +51 -11
- package/lib/review-integration-v2.ts +29 -8
- package/lib/review-relay-phase-timing.ts +45 -0
- package/lib/review-repository.ts +5 -0
- package/lib/review-start-context.ts +17 -0
- package/lib/session-profile-append-controller.ts +21 -6
- package/lib/session-profile-authority.ts +119 -0
- package/lib/session-profile-binding.ts +54 -5
- package/lib/session-profile-freeze.ts +107 -9
- package/lib/session-profile-integration.ts +313 -0
- package/lib/session-profile-persistence.ts +63 -9
- package/lib/shell-bar.ts +269 -72
- package/lib/shell-card.ts +26 -6
- package/lib/shell-changes.ts +2 -0
- package/lib/shell-page.ts +120 -0
- package/lib/shell-panel.ts +163 -0
- package/lib/shell-prompt.ts +53 -1
- package/lib/shell-sidebar-layout.ts +198 -53
- package/lib/shell-sidebar.ts +61 -1
- package/lib/shell-stats.ts +123 -0
- package/lib/shell-todo.ts +79 -1
- package/lib/shell-usage.ts +12 -2
- package/lib/star-prompt.ts +197 -0
- package/lib/verifier-admission.ts +18 -8
- package/lib/verifier-settle-reminder.ts +64 -0
- package/lib/vim-editor-adapter.ts +6 -1
- package/lib/visual-customization-policy.ts +67 -12
- package/lib/visual-customize-view.ts +373 -40
- package/lib/visual-layout-preview.ts +47 -0
- package/lib/visual-profiles.ts +2 -2
- package/lib/windows-session-transport.ts +53 -2
- package/lib/yolo-session-policy.ts +68 -16
- package/package.json +2 -1
- package/runtime/gentle-ai-binary.mjs +1 -1
- package/runtime/gentle-shell-bundled.mjs +6 -19
- package/runtime/gentle-shell-launcher.mjs +46 -7
- package/runtime/native-review-cli.mjs +100 -18
- package/runtime/review-integration-v2.mjs +29 -8
- package/runtime/windows-session-transport.ps1 +60 -3
- package/scripts/bootstrap.sh +4 -1
- package/scripts/build-full-delegation-card.mjs +175 -0
- package/scripts/bundled-install.mjs +38 -8
- package/scripts/download-progress.mjs +68 -0
- package/scripts/gentle-ai-installer.mjs +41 -18
- package/scripts/gentle-ai-proxy.mjs +294 -0
- package/scripts/install-gentle-ai.mjs +14 -1
- package/scripts/installer-preflight.mjs +6 -6
- package/scripts/installer-runner.mjs +27 -12
- package/scripts/installer-server.mjs +8 -7
- package/scripts/pi-update.mjs +113 -3
- package/scripts/render-cache-host.mjs +72 -0
- package/scripts/render-cache-performance-worker.mjs +5 -4
- package/scripts/render-cache-performance.mjs +6 -1
- package/scripts/run-test-suite.mjs +41 -13
- package/scripts/shell-snapshot.mjs +340 -0
- package/scripts/test-packed-runner.mjs +1 -1
- package/scripts/update-agent-body-history.mjs +41 -0
- package/scripts/verify-installer-tap.mjs +45 -0
- package/scripts/verify-package-files.mjs +14 -2
- package/scripts/voice-worker.cjs +32 -6
- package/skills/design/SKILL.md +11 -8
- package/skills/gentle-ai/SKILL.md +4 -4
- package/skills/gentle-ui-design/SKILL.md +46 -0
- package/skills/gentle-ui-design/references/adaptive-interfaces.md +46 -0
- package/skills/gentle-ui-design/references/browse-and-compare.md +62 -0
- package/skills/gentle-ui-design/references/composition-and-feedback.md +56 -0
- package/skills/gentle-ui-design/references/task-and-content.md +60 -0
- package/skills/skill-registry/SKILL.md +4 -2
- package/skills/tui-design/NOTICE.md +5 -0
- package/skills/tui-design/SKILL.md +56 -0
- package/skills/tui-design/references/commands.md +75 -0
- package/skills/tui-design/references/critique.md +48 -0
- package/skills/tui-design/references/design-doc.md +44 -0
- package/skills/tui-design/references/terminal-craft.md +64 -0
- package/skills/tui-design/references/visual-loop.md +32 -0
- package/skills/work-unit-commits/SKILL.md +6 -7
- package/tests/agent-home.test.ts +28 -9
- package/tests/agent-profiles-workflow.test.ts +163 -0
- package/tests/agent-risk-escalation.test.ts +1 -1
- package/tests/agent-status.test.ts +143 -0
- package/tests/agents-config.test.ts +96 -0
- package/tests/agents-history.test.ts +31 -0
- package/tests/agents-jobs-frames.test.ts +93 -0
- package/tests/agents-prompt-lifecycle-runtime.test.ts +376 -0
- package/tests/agents-protocol.test.ts +53 -1
- package/tests/agents-runner-drain.test.ts +193 -0
- package/tests/agents-runner.test.ts +345 -4
- package/tests/agents-view.test.ts +27 -0
- package/tests/agents-widget.test.ts +312 -10
- package/tests/append-system-prompt-route.test.ts +4 -1
- package/tests/ask-user-choice.test.ts +4 -3
- package/tests/ask-user-question.test.ts +97 -0
- package/tests/auto-permission-runtime.test.ts +1 -1
- package/tests/autonomous-guard.test.ts +346 -10
- package/tests/bounded-scan-contract.test.ts +39 -0
- package/tests/bounded-stop-contract.test.ts +43 -3
- package/tests/bridge-cost.test.ts +47 -0
- package/tests/bundled-install.test.ts +36 -9
- package/tests/card-content-policy.test.ts +3 -3
- package/tests/chained-pr-skill.test.ts +8 -6
- package/tests/child-safety.test.ts +11 -0
- package/tests/ci-concurrency.test.ts +11 -0
- package/tests/ci-merge-base-identity.test.ts +114 -0
- package/tests/codegraph-tools.test.ts +80 -5
- package/tests/codemode-rendering.test.ts +22 -2
- package/tests/command-palette.test.ts +300 -3
- package/tests/commit-slice-signal.test.ts +11 -9
- package/tests/delegate-for-reason-contract.test.ts +8 -7
- package/tests/design-canvas-assets.test.ts +185 -2
- package/tests/destructive-command-guard.test.ts +52 -0
- package/tests/download-progress.test.ts +71 -0
- package/tests/execution-surface-guard.test.ts +55 -0
- package/tests/exploration-budget.test.ts +128 -0
- package/tests/fixtures/agent-assets/gentle-ai-worker.strict-tdd-era.md +122 -0
- package/tests/fixtures/measure-orchestrator-prompt.mjs +2 -1
- package/tests/fixtures/primary-session-prompt/gentleman.golden.md +26 -14
- package/tests/fixtures/proxy-tls/proxy.crt +19 -0
- package/tests/fixtures/proxy-tls/proxy.key +28 -0
- package/tests/fixtures/session-profile-v1/gentle-ai-reviewer.ts +89 -0
- package/tests/fixtures/session-profile-v1/replay.ts +172 -0
- package/tests/fixtures/session-profile-v1/session-profile-authority.ts +113 -0
- package/tests/fixtures/session-profile-v1/session-profile-binding.ts +83 -0
- package/tests/fixtures/session-profile-v1/session-profile-disk-reader.ts +182 -0
- package/tests/fixtures/session-profile-v1/session-profile-freeze.ts +290 -0
- package/tests/fixtures/session-profile-v1/session-profile-persistence.ts +195 -0
- package/tests/fixtures/session-profile-v1/v1-inputs.ts +16 -0
- package/tests/fixtures/windows-pipe-availability.ps1 +26 -0
- package/tests/fixtures/windows-session-bootstrap.ps1 +6 -2
- package/tests/full-delegation-closure.test.ts +131 -0
- package/tests/full-delegation-notices.test.ts +76 -0
- package/tests/generic-agent-tools.test.ts +3 -3
- package/tests/gentle-agent-status.test.ts +74 -0
- package/tests/gentle-agents-child-jobs.test.ts +237 -0
- package/tests/gentle-agents.test.ts +1491 -38
- package/tests/gentle-ai-binary.test.ts +3 -1
- package/tests/gentle-ai-elapsed-store.test.ts +19 -0
- package/tests/gentle-ai-exploration-budget.test.ts +151 -0
- package/tests/gentle-ai-installer.test.ts +134 -51
- package/tests/gentle-ai-proxy.test.ts +434 -0
- package/tests/gentle-ai.test.ts +1101 -52
- package/tests/gentle-design.test.ts +21 -0
- package/tests/gentle-fast.test.ts +73 -0
- package/tests/gentle-guide.test.ts +208 -0
- package/tests/gentle-herdr-activity.test.ts +80 -3
- package/tests/gentle-herdr-agent.test.ts +111 -0
- package/tests/gentle-program-identity.test.ts +197 -0
- package/tests/gentle-shell-bin.test.ts +172 -26
- package/tests/gentle-shell-bundled.test.ts +207 -31
- package/tests/gentle-shell-launcher.test.ts +93 -10
- package/tests/gentle-shell.test.ts +1937 -66
- package/tests/gentle-stats.test.ts +15 -2
- package/tests/gentle-todo.test.ts +67 -4
- package/tests/gentle-tps.test.ts +123 -0
- package/tests/gentle-ui-design-skill.test.ts +59 -0
- package/tests/herdr-busy.test.ts +59 -0
- package/tests/herdr-task-summary-policy.test.ts +132 -0
- package/tests/history-store-private-mode.test.ts +40 -0
- package/tests/image-disk-cache.test.ts +163 -0
- package/tests/image-paste-chips.test.ts +42 -0
- package/tests/independent-recovery-delivery.test.ts +83 -0
- package/tests/inprocess-reviewer.test.ts +298 -4
- package/tests/installer-posix-bootstrap.test.ts +68 -6
- package/tests/installer-preflight.test.ts +5 -4
- package/tests/installer-runner.test.ts +79 -44
- package/tests/installer-server.test.ts +14 -11
- package/tests/installer-windows-diagnostics.test.ts +4 -5
- package/tests/isolated-config-home.ts +28 -0
- package/tests/jobs-view.test.ts +260 -6
- package/tests/jobs-widget.test.ts +25 -37
- package/tests/keymaps-customize.test.ts +114 -0
- package/tests/keymaps.test.ts +103 -0
- package/tests/maintainer/mcp-status.maintest.ts +88 -0
- package/tests/maintainer/pi-update-source.maintest.ts +116 -0
- package/tests/modal-mouse-boundaries.test.ts +136 -0
- package/tests/model-fallback-notice.test.ts +45 -0
- package/tests/model-fallback-policy.test.ts +83 -0
- package/tests/model-fallback-store.test.ts +111 -0
- package/tests/model-routing-authority.test.ts +178 -2
- package/tests/nan-provider.test.ts +4 -4
- package/tests/native-choice-list.test.ts +77 -2
- package/tests/native-review-capability-contract.test.ts +7 -1
- package/tests/native-review-cli.test.ts +269 -1
- package/tests/no-self-pause-contract.test.ts +22 -0
- package/tests/notification-customize.test.ts +58 -34
- package/tests/odd-routing-canonical-ratchet.test.ts +6 -6
- package/tests/odd-routing-contract.test.ts +1 -1
- package/tests/orchestrator-budget.test.ts +7 -2
- package/tests/orchestrator-delegation-mode.test.ts +153 -0
- package/tests/orchestrator-fallback.test.ts +82 -0
- package/tests/orchestrator-mode-policy.test.ts +175 -0
- package/tests/orchestrator-prompt-budget.test.ts +133 -0
- package/tests/orchestrator-rdd-ownership.test.ts +2 -2
- package/tests/package-manifest.test.ts +169 -14
- package/tests/pasted-image-paths.test.ts +118 -0
- package/tests/permission-allowlist.test.ts +202 -0
- package/tests/persona-reply-language.test.ts +20 -1
- package/tests/pi-update-launcher.test.ts +40 -8
- package/tests/pi-update-source.test.ts +162 -0
- package/tests/pnpm-workspace-config.test.ts +58 -0
- package/tests/primary-session-prompt.test.ts +13 -0
- package/tests/profile-fast-tier.test.ts +311 -0
- package/tests/profile-pin.test.ts +29 -2
- package/tests/profile-tier-footer.test.ts +84 -0
- package/tests/profiles-panel-delegation-mode.test.ts +422 -0
- package/tests/questionnaire-view.test.ts +265 -0
- package/tests/quiet-bash-runtime.test.ts +1 -1
- package/tests/quiet-tool-rendering.test.ts +48 -0
- package/tests/rdd-aware-verification-contract.test.ts +16 -11
- package/tests/rdd-customize.test.ts +287 -0
- package/tests/rdd-mode-cache.test.ts +450 -0
- package/tests/rdd-mode-control.test.ts +407 -0
- package/tests/rdd-mode-scope-regression.test.ts +137 -0
- package/tests/rdd-off-independent-regression.test.ts +69 -0
- package/tests/rdd-status-line.test.ts +18 -0
- package/tests/render-cache-performance.test.ts +171 -2
- package/tests/reply-language-anchor.test.ts +173 -0
- package/tests/review-agent-end-preflight.test.ts +1 -1
- package/tests/review-blocker.test.ts +193 -0
- package/tests/review-candidate-owner-retry.test.ts +1 -1
- package/tests/review-candidate-owner-sweep.test.ts +93 -0
- package/tests/review-candidate-symlink-scan.test.ts +157 -0
- package/tests/review-candidate-view.test.ts +52 -1
- package/tests/review-candidate-windows-boundary.test.ts +131 -0
- package/tests/review-candidate-windows-probe.test.ts +63 -0
- package/tests/review-committed-restart-ack.test.ts +129 -0
- package/tests/review-continuation-registry.test.ts +76 -0
- package/tests/review-controller-native-routing.test.ts +381 -11
- package/tests/review-controller-workspace-root.test.ts +230 -3
- package/tests/review-controller.test.ts +96 -11
- package/tests/review-coordinator-runtime.test.ts +117 -0
- package/tests/review-coordinator.test.ts +195 -0
- package/tests/review-failure-facts.test.ts +87 -0
- package/tests/review-gate.test.ts +24 -0
- package/tests/review-host-relay-routing.test.ts +258 -4
- package/tests/review-host-relay.test.ts +39 -0
- package/tests/review-inspect-preflight.test.ts +197 -0
- package/tests/review-integration-v2-forward.test.ts +59 -0
- package/tests/review-integration-v2.test.ts +46 -3
- package/tests/review-json-arguments.test.ts +65 -10
- package/tests/review-last-event-closure.test.ts +68 -0
- package/tests/review-loadout-rdd-off-sdk.test.ts +65 -31
- package/tests/review-owner-identity.test.ts +60 -24
- package/tests/review-relay-phase-timing.test.ts +128 -0
- package/tests/review-relay-transport-agent.test.ts +12 -2
- package/tests/review-repository.test.ts +52 -1
- package/tests/review-reviewer-evidence.test.ts +183 -0
- package/tests/review-risk-assessment.test.ts +74 -3
- package/tests/review-session-standing-permission-controller.test.ts +21 -1
- package/tests/review-sidebar-state.test.ts +5 -5
- package/tests/review-start-context.test.ts +19 -0
- package/tests/review-tool-prose-budget.test.ts +121 -0
- package/tests/run-test-suite.test.ts +183 -6
- package/tests/runtime-harness.mjs +4 -1
- package/tests/sensitive-path-tools.test.ts +250 -0
- package/tests/session-profile-append-controller-pi.test.ts +60 -31
- package/tests/session-profile-append-controller.test.ts +63 -0
- package/tests/session-profile-authority.test.ts +259 -0
- package/tests/session-profile-binding.test.ts +34 -1
- package/tests/session-profile-disk-reader-pi.test.ts +58 -16
- package/tests/session-profile-extension.test.ts +615 -0
- package/tests/session-profile-freeze.test.ts +110 -2
- package/tests/session-profile-integration.test.ts +857 -0
- package/tests/session-profile-persistence.test.ts +57 -0
- package/tests/session-profile-v1-compat.test.ts +255 -0
- package/tests/shell-bar.test.ts +274 -72
- package/tests/shell-card.test.ts +13 -1
- package/tests/shell-page.test.ts +108 -0
- package/tests/shell-panel.test.ts +360 -0
- package/tests/shell-prompt.test.ts +54 -0
- package/tests/shell-render-cache.test.ts +5 -0
- package/tests/shell-sidebar-fullscreen.test.ts +13 -1
- package/tests/shell-sidebar-layout.test.ts +447 -44
- package/tests/shell-sidebar-scroll-benchmark.test.ts +1 -1
- package/tests/shell-stats.test.ts +90 -0
- package/tests/shell-surface-transparent.test.ts +89 -0
- package/tests/shell-todo.test.ts +12 -0
- package/tests/shell-usage.test.ts +14 -0
- package/tests/skill-registry.test.ts +902 -2
- package/tests/star-prompt.test.ts +470 -0
- package/tests/startup-banner.test.ts +209 -2
- package/tests/startup-home-repo.test.ts +2 -0
- package/tests/support/review-coordinator-runtime.ts +69 -0
- package/tests/task-size-routing-contract.test.ts +50 -4
- package/tests/verifier-admission.test.ts +49 -0
- package/tests/verifier-settle-reminder.test.ts +281 -0
- package/tests/verify-first-launch-contract.test.ts +134 -8
- package/tests/verify-installer-tap.test.ts +57 -0
- package/tests/vim-editor-adapter.test.ts +2 -2
- package/tests/visual-customization-policy.test.ts +95 -8
- package/tests/visual-customize-view.test.ts +818 -9
- package/tests/visual-layout-preview.test.ts +42 -0
- package/tests/visual-profiles.test.ts +11 -0
- package/tests/voice-worker.test.ts +73 -2
- package/tests/windows-session-bootstrap.test.ts +105 -2
- package/tests/windows-session-transport.test.ts +126 -0
- package/tests/work-continuity-contract.test.ts +114 -0
- package/tests/writer-feature-document-admission.test.ts +92 -0
- package/tests/writer-surfaces.test.ts +12 -10
- package/tests/yolo-customize.test.ts +57 -15
- package/tests/yolo-mode-runtime.test.ts +4 -2
- package/tests/yolo-mode.test.ts +39 -20
- package/tests/yolo-session-policy.test.ts +270 -1
package/lib/keymaps.ts
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { parseKey } from "@earendil-works/pi-tui";
|
|
5
|
+
import { gentlePiConfigHome } from "./agent-home.ts";
|
|
6
|
+
|
|
7
|
+
// User keymaps: the single registry every Gentle shortcut reads. Each action
|
|
8
|
+
// keeps its historical environment variable (which still wins, so existing
|
|
9
|
+
// setups are unchanged) and its default key; `keymaps.json` under the Gentle
|
|
10
|
+
// config home stores what the user assigned in /gentle:customize or the
|
|
11
|
+
// command palette. Pi reads extension shortcuts only at startup and on
|
|
12
|
+
// /reload, so a saved change applies after a reload. Pure apart from the
|
|
13
|
+
// read/write helpers, so the extensions and their tests share it.
|
|
14
|
+
|
|
15
|
+
export const KEYMAPS_FILE = "keymaps.json";
|
|
16
|
+
const KEYMAPS_SCHEMA = 1;
|
|
17
|
+
export const KEYMAP_RELOAD_NOTICE = "Run /reload to apply it.";
|
|
18
|
+
const COMMAND_PREFIX = "command:";
|
|
19
|
+
|
|
20
|
+
export interface KeymapAction {
|
|
21
|
+
id: string;
|
|
22
|
+
label: string;
|
|
23
|
+
/** The environment variable that overrides this key: "" or "off" disables it. */
|
|
24
|
+
env: string;
|
|
25
|
+
defaultKey?: string;
|
|
26
|
+
/** The slash command this action opens, so the palette can show its key. */
|
|
27
|
+
command?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const KEYMAP_ACTIONS: readonly KeymapAction[] = [
|
|
31
|
+
{ id: "commands", label: "Command palette", env: "GENTLE_PI_COMMANDS_KEY", defaultKey: "alt+k", command: "gentle:commands" },
|
|
32
|
+
{ id: "changes", label: "Browse changes", env: "GENTLE_PI_SHELL_CHANGES_KEY", defaultKey: "alt+g", command: "gentle:changes" },
|
|
33
|
+
{ id: "usage", label: "Subscription usage", env: "GENTLE_PI_SHELL_USAGE_KEY", defaultKey: "alt+u", command: "gentle:usage" },
|
|
34
|
+
{ id: "preset", label: "Cycle visual preset", env: "GENTLE_PI_SHELL_PRESET_KEY", defaultKey: "alt+l" },
|
|
35
|
+
{ id: "agents.view", label: "Subagents overlay", env: "GENTLE_PI_AGENTS_VIEW_KEY", defaultKey: "alt+a", command: "gentle:agents" },
|
|
36
|
+
{ id: "agents.stop", label: "Stop active subagents", env: "GENTLE_PI_AGENTS_STOP_KEY", defaultKey: "alt+s" },
|
|
37
|
+
{ id: "agents.collapse", label: "Collapse the agents card", env: "GENTLE_PI_AGENTS_KEY", defaultKey: "ctrl+shift+a" },
|
|
38
|
+
{ id: "jobs.collapse", label: "Collapse the jobs card", env: "GENTLE_PI_JOBS_KEY", defaultKey: "ctrl+shift+j" },
|
|
39
|
+
{ id: "todo.collapse", label: "Collapse the todo list", env: "GENTLE_PI_TODO_KEY", defaultKey: "ctrl+shift+t" },
|
|
40
|
+
{ id: "stats.view", label: "Usage stats", env: "GENTLE_PI_STATS_VIEW_KEY", command: "gentle:stats" },
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
// Pi refuses extension shortcuts on these editor-global keybindings
|
|
44
|
+
// (RESERVED_KEYBINDINGS_FOR_EXTENSION_CONFLICTS in Pi's extension runner,
|
|
45
|
+
// which is not exported): a Gentle keymap on one of their keys is skipped.
|
|
46
|
+
export const RESERVED_PI_KEYBINDINGS: readonly string[] = [
|
|
47
|
+
"app.interrupt", "app.clear", "app.exit", "app.suspend", "app.thinking.cycle",
|
|
48
|
+
"app.model.cycleForward", "app.model.cycleBackward", "app.model.select", "app.tools.expand",
|
|
49
|
+
"app.thinking.toggle", "app.editor.external", "app.message.copy", "app.message.followUp",
|
|
50
|
+
"tui.input.submit", "tui.select.confirm", "tui.select.cancel", "tui.input.copy", "tui.editor.deleteToLineEnd",
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
/** Saved assignments: a key, or null for an action the user disabled. */
|
|
54
|
+
export type KeymapBindings = Readonly<Record<string, string | null>>;
|
|
55
|
+
|
|
56
|
+
export interface KeymapFile {
|
|
57
|
+
bindings: KeymapBindings;
|
|
58
|
+
path: string;
|
|
59
|
+
/** True when the file exists but could not be read or parsed; its bindings are then ignored. */
|
|
60
|
+
malformed: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The subset of Pi's KeybindingsManager the checks need. */
|
|
64
|
+
export interface PiKeybindings {
|
|
65
|
+
getKeys(keybinding: string): readonly string[];
|
|
66
|
+
getResolvedBindings?(): Readonly<Record<string, unknown>>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type KeymapSource = "env" | "saved" | "default" | "none";
|
|
70
|
+
|
|
71
|
+
export type KeymapCheck =
|
|
72
|
+
| { kind: "ok" }
|
|
73
|
+
| { kind: "blocked"; message: string }
|
|
74
|
+
| { kind: "confirm"; message: string; displaces?: string };
|
|
75
|
+
|
|
76
|
+
const ACTIONS_BY_ID = new Map(KEYMAP_ACTIONS.map((action) => [action.id, action]));
|
|
77
|
+
const MODIFIER_ORDER = ["ctrl", "shift", "alt", "super"];
|
|
78
|
+
|
|
79
|
+
export function keymapAction(id: string): KeymapAction | undefined {
|
|
80
|
+
return ACTIONS_BY_ID.get(id);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The keymap id of a slash command: its built-in action when it has one, else `command:<name>`. */
|
|
84
|
+
export function commandKeymapId(command: string): string {
|
|
85
|
+
return KEYMAP_ACTIONS.find((action) => action.command === command)?.id ?? `${COMMAND_PREFIX}${command}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The slash command a `command:<name>` keymap runs, or undefined for a built-in action id. */
|
|
89
|
+
export function keymapCommand(id: string): string | undefined {
|
|
90
|
+
return id.startsWith(COMMAND_PREFIX) && id.length > COMMAND_PREFIX.length ? id.slice(COMMAND_PREFIX.length) : undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function keymapLabel(id: string): string {
|
|
94
|
+
return keymapAction(id)?.label ?? `/${keymapCommand(id) ?? id}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function envValue(action: KeymapAction | undefined, env: NodeJS.ProcessEnv): string | undefined {
|
|
98
|
+
return action ? env[action.env]?.trim() : undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The key an action uses: its environment variable when set ("" or "off"
|
|
103
|
+
* disables it), else the saved assignment (null disables it), else its
|
|
104
|
+
* default. A `command:<name>` id has no environment variable or default.
|
|
105
|
+
*/
|
|
106
|
+
export function effectiveKey(id: string, env: NodeJS.ProcessEnv = process.env, bindings: KeymapBindings = {}): string | undefined {
|
|
107
|
+
const action = keymapAction(id);
|
|
108
|
+
const fromEnv = envValue(action, env);
|
|
109
|
+
if (fromEnv !== undefined) return fromEnv === "" || fromEnv.toLowerCase() === "off" ? undefined : fromEnv;
|
|
110
|
+
if (Object.hasOwn(bindings, id)) return bindings[id] ?? undefined;
|
|
111
|
+
return action?.defaultKey;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function keymapSource(id: string, env: NodeJS.ProcessEnv = process.env, bindings: KeymapBindings = {}): KeymapSource {
|
|
115
|
+
if (envValue(keymapAction(id), env) !== undefined) return "env";
|
|
116
|
+
if (Object.hasOwn(bindings, id)) return "saved";
|
|
117
|
+
return keymapAction(id)?.defaultKey ? "default" : "none";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Every `command:<name>` id with a saved key, in file order. */
|
|
121
|
+
export function commandKeymapIds(bindings: KeymapBindings): string[] {
|
|
122
|
+
return Object.keys(bindings).filter((id) => keymapCommand(id) !== undefined && bindings[id]);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Lowercase and order modifiers as ctrl, shift, alt, super; undefined for an unknown modifier. */
|
|
126
|
+
export function normalizeKeyId(key: string): string | undefined {
|
|
127
|
+
const parts = key.trim().toLowerCase().split("+");
|
|
128
|
+
const base = parts.pop();
|
|
129
|
+
if (!base) return undefined;
|
|
130
|
+
const modifiers = new Set<string>();
|
|
131
|
+
for (const part of parts) {
|
|
132
|
+
if (!MODIFIER_ORDER.includes(part)) return undefined;
|
|
133
|
+
modifiers.add(part);
|
|
134
|
+
}
|
|
135
|
+
return [...MODIFIER_ORDER.filter((modifier) => modifiers.has(modifier)), base].join("+");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The assignable key of one raw key press. A key without ctrl, alt or super
|
|
140
|
+
* (letters, Enter, arrows, shift+letter) would stop the user from typing, so
|
|
141
|
+
* only modified keys and f1-f12 are accepted.
|
|
142
|
+
*/
|
|
143
|
+
export function keyFromInput(data: string): { key: string } | { error: string } {
|
|
144
|
+
const parsed = parseKey(data);
|
|
145
|
+
const key = parsed === undefined ? undefined : normalizeKeyId(parsed);
|
|
146
|
+
if (key === undefined) return { error: "That key is not recognized by the terminal. Try another combination." };
|
|
147
|
+
const modifiers = key.split("+").slice(0, -1);
|
|
148
|
+
if (modifiers.some((modifier) => modifier !== "shift") || /^f([1-9]|1[0-2])$/.test(key)) return { key };
|
|
149
|
+
return { error: `${key} is needed for typing. Use a combination with ctrl or alt, or f1-f12.` };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function sameKey(left: string | undefined, right: string): boolean {
|
|
153
|
+
return left !== undefined && normalizeKeyId(left) === normalizeKeyId(right);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function keysOf(value: unknown): string[] {
|
|
157
|
+
if (typeof value === "string") return [value];
|
|
158
|
+
return Array.isArray(value) ? value.filter((item): item is string => typeof item === "string") : [];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The id of the action already on `key`, excluding `ownId`. */
|
|
162
|
+
export function keymapOwner(key: string, env: NodeJS.ProcessEnv, bindings: KeymapBindings, ownId?: string): string | undefined {
|
|
163
|
+
const ids = [...KEYMAP_ACTIONS.map((action) => action.id), ...commandKeymapIds(bindings)];
|
|
164
|
+
return ids.find((id) => id !== ownId && sameKey(effectiveKey(id, env, bindings), key));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Why `id` cannot be changed here: its environment variable controls it. */
|
|
168
|
+
export function keymapEnvLock(id: string, env: NodeJS.ProcessEnv): string | undefined {
|
|
169
|
+
const action = keymapAction(id);
|
|
170
|
+
return action && envValue(action, env) !== undefined ? `${action.label} is set by ${action.env}; unset it to change the key here.` : undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Whether `key` can go on `id`: blocked when the action is controlled by its
|
|
175
|
+
* environment variable or the key is one Pi reserves; a confirmation when the
|
|
176
|
+
* key belongs to another Gentle action (which then loses it) or overrides a
|
|
177
|
+
* non-reserved Pi keybinding.
|
|
178
|
+
*/
|
|
179
|
+
export function checkKeymap(id: string, key: string, env: NodeJS.ProcessEnv, bindings: KeymapBindings, keybindings?: PiKeybindings): KeymapCheck {
|
|
180
|
+
const locked = keymapEnvLock(id, env);
|
|
181
|
+
if (locked) return { kind: "blocked", message: locked };
|
|
182
|
+
if (keybindings) {
|
|
183
|
+
const reserved = RESERVED_PI_KEYBINDINGS.find((binding) => keybindings.getKeys(binding).some((piKey) => sameKey(piKey, key)));
|
|
184
|
+
if (reserved) return { kind: "blocked", message: `${key} is reserved by Pi (${reserved}); choose another key.` };
|
|
185
|
+
}
|
|
186
|
+
const owner = keymapOwner(key, env, bindings, id);
|
|
187
|
+
if (owner) {
|
|
188
|
+
const ownerAction = keymapAction(owner);
|
|
189
|
+
if (ownerAction && envValue(ownerAction, env) !== undefined) return { kind: "blocked", message: `${key} is used by ${ownerAction.label} through ${ownerAction.env}.` };
|
|
190
|
+
return { kind: "confirm", message: `${key} is used by ${keymapLabel(owner)}. Move it to ${keymapLabel(id)}?`, displaces: owner };
|
|
191
|
+
}
|
|
192
|
+
const resolved = keybindings?.getResolvedBindings?.() ?? {};
|
|
193
|
+
const builtin = Object.keys(resolved).find((binding) => keysOf(resolved[binding]).some((piKey) => sameKey(piKey, key)));
|
|
194
|
+
if (builtin) return { kind: "confirm", message: `${key} is Pi's ${builtin}. Use it for ${keymapLabel(id)} instead?` };
|
|
195
|
+
return { kind: "ok" };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Why `file` cannot be rewritten; undefined when it can. */
|
|
199
|
+
export function keymapsUnwritable(file: KeymapFile): string | undefined {
|
|
200
|
+
return file.malformed ? `Keymaps: ${file.path} is unreadable or malformed; fix or delete it to change keys.` : undefined;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export type KeymapChange =
|
|
204
|
+
| { kind: "refused"; message: string; type: "warning" | "error" }
|
|
205
|
+
| { kind: "ready"; key: string; bindings: Record<string, string | null>; confirm?: string };
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The change one raw key press makes to `id`: refused (environment lock,
|
|
209
|
+
* unwritable file, typing key, reserved key) or the new bindings, with the
|
|
210
|
+
* question to ask first when the key moves from another action.
|
|
211
|
+
*/
|
|
212
|
+
export function prepareKeymapChange(id: string, data: string, env: NodeJS.ProcessEnv, file: KeymapFile, keybindings?: PiKeybindings): KeymapChange {
|
|
213
|
+
const locked = keymapEnvLock(id, env);
|
|
214
|
+
if (locked) return { kind: "refused", message: locked, type: "warning" };
|
|
215
|
+
const unwritable = keymapsUnwritable(file);
|
|
216
|
+
if (unwritable) return { kind: "refused", message: unwritable, type: "error" };
|
|
217
|
+
const parsed = keyFromInput(data);
|
|
218
|
+
if ("error" in parsed) return { kind: "refused", message: parsed.error, type: "warning" };
|
|
219
|
+
const check = checkKeymap(id, parsed.key, env, file.bindings, keybindings);
|
|
220
|
+
if (check.kind === "blocked") return { kind: "refused", message: check.message, type: "warning" };
|
|
221
|
+
const confirm = check.kind === "confirm" ? check : undefined;
|
|
222
|
+
return { kind: "ready", key: parsed.key, bindings: assignKeymap(file.bindings, id, parsed.key, confirm?.displaces), confirm: confirm?.message };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** What the user is told after a saved change. */
|
|
226
|
+
export function keymapSavedNotice(id: string, name: string, env: NodeJS.ProcessEnv, bindings: KeymapBindings): string {
|
|
227
|
+
return `${name}: ${effectiveKey(id, env, bindings) ?? "none"}. ${KEYMAP_RELOAD_NOTICE}`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** A copy of `bindings` with `id` on `key` (null disables it) and `displaces`, when given, disabled. */
|
|
231
|
+
export function assignKeymap(bindings: KeymapBindings, id: string, key: string | null, displaces?: string): Record<string, string | null> {
|
|
232
|
+
const next: Record<string, string | null> = { ...bindings, [id]: key === null ? null : normalizeKeyId(key) ?? key };
|
|
233
|
+
if (displaces && displaces !== id) next[displaces] = null;
|
|
234
|
+
return next;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** A copy of `bindings` without `id`, so it goes back to its default key. */
|
|
238
|
+
export function resetKeymap(bindings: KeymapBindings, id: string): Record<string, string | null> {
|
|
239
|
+
const next: Record<string, string | null> = { ...bindings };
|
|
240
|
+
delete next[id];
|
|
241
|
+
return next;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function parseKeymapFile(raw: string): KeymapBindings | undefined {
|
|
245
|
+
try {
|
|
246
|
+
const value: unknown = JSON.parse(raw);
|
|
247
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
|
|
248
|
+
const { schema, bindings } = value as { schema?: unknown; bindings?: unknown };
|
|
249
|
+
if (schema !== KEYMAPS_SCHEMA || typeof bindings !== "object" || bindings === null || Array.isArray(bindings)) return undefined;
|
|
250
|
+
const result: Record<string, string | null> = {};
|
|
251
|
+
for (const [id, key] of Object.entries(bindings)) {
|
|
252
|
+
if (id === "" || (key !== null && (typeof key !== "string" || normalizeKeyId(key) === undefined))) return undefined;
|
|
253
|
+
result[id] = key;
|
|
254
|
+
}
|
|
255
|
+
return result;
|
|
256
|
+
} catch {
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function readKeymaps(configHome: string): KeymapFile {
|
|
262
|
+
const path = join(configHome, KEYMAPS_FILE);
|
|
263
|
+
let raw: string;
|
|
264
|
+
try {
|
|
265
|
+
raw = readFileSync(path, "utf8");
|
|
266
|
+
} catch (error) {
|
|
267
|
+
const missing = typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
268
|
+
return { bindings: {}, path, malformed: !missing };
|
|
269
|
+
}
|
|
270
|
+
const bindings = parseKeymapFile(raw);
|
|
271
|
+
return { bindings: bindings ?? {}, path, malformed: bindings === undefined };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** The keymaps file of the Gentle config home `env` resolves to. */
|
|
275
|
+
export function loadKeymaps(env: NodeJS.ProcessEnv = process.env): KeymapFile {
|
|
276
|
+
return readKeymaps(gentlePiConfigHome(env));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Same-directory rename, so a reader never sees a partially written file. */
|
|
280
|
+
export function writeKeymaps(configHome: string, bindings: KeymapBindings): string {
|
|
281
|
+
const path = join(configHome, KEYMAPS_FILE);
|
|
282
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
283
|
+
mkdirSync(configHome, { recursive: true });
|
|
284
|
+
try {
|
|
285
|
+
writeFileSync(temporary, `${JSON.stringify({ schema: KEYMAPS_SCHEMA, bindings }, null, "\t")}\n`, { flag: "wx", mode: 0o600 });
|
|
286
|
+
renameSync(temporary, path);
|
|
287
|
+
} finally {
|
|
288
|
+
try { unlinkSync(temporary); } catch { /* Rename already consumed the temporary file. */ }
|
|
289
|
+
}
|
|
290
|
+
return path;
|
|
291
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { profilesFilePath, readProfilesFileResult } from "./agent-profiles.ts";
|
|
2
|
+
import { TERMINAL_ERROR_CATEGORY, type TerminalErrorCategory } from "./agents-protocol.ts";
|
|
3
|
+
import { parseModelRef } from "./agents-config.ts";
|
|
4
|
+
import { chainFor, fallbacksFilePath, readFallbacksFile, type FallbackChain } from "./model-fallback-store.ts";
|
|
5
|
+
import { computeModelPriceRatio, type ModelPriceLookup } from "./model-price-ratio.ts";
|
|
6
|
+
|
|
7
|
+
// What a fallback says and records, shared by the orchestrator and the agents.
|
|
8
|
+
|
|
9
|
+
/** Session entry appended on every switch. */
|
|
10
|
+
export const MODEL_FALLBACK_ENTRY = "gentle-pi.model-fallback/v1";
|
|
11
|
+
|
|
12
|
+
export interface FallbackChange {
|
|
13
|
+
agent: string;
|
|
14
|
+
from: string;
|
|
15
|
+
to: string;
|
|
16
|
+
category: TerminalErrorCategory;
|
|
17
|
+
/** The run started on the fallback because the primary was still cooling down. */
|
|
18
|
+
launch?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const REASON: Partial<Record<TerminalErrorCategory, string>> = {
|
|
22
|
+
[TERMINAL_ERROR_CATEGORY.QUOTA]: "its usage limit",
|
|
23
|
+
[TERMINAL_ERROR_CATEGORY.AUTH]: "an authentication error",
|
|
24
|
+
[TERMINAL_ERROR_CATEGORY.RATE_LIMIT]: "a rate limit",
|
|
25
|
+
[TERMINAL_ERROR_CATEGORY.PROVIDER_UNAVAILABLE]: "a provider outage",
|
|
26
|
+
[TERMINAL_ERROR_CATEGORY.TIMEOUT]: "a timeout",
|
|
27
|
+
[TERMINAL_ERROR_CATEGORY.NETWORK]: "a network error",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** The message that resumes a run after its model changed. */
|
|
31
|
+
export function fallbackContinuePrompt(from: string | undefined, to: string, category: TerminalErrorCategory): string {
|
|
32
|
+
return `The previous model${from ? ` (${from})` : ""} stopped with a provider error (${category}). You are now running on ${to}. Continue the task from where it stopped; do not repeat steps that already completed.`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** One line for the human; a pricier fallback says by how much, an unpriced one says so. */
|
|
36
|
+
export function fallbackNotice(change: FallbackChange, prices?: ModelPriceLookup): string {
|
|
37
|
+
let price = "";
|
|
38
|
+
const from = parseModelRef(change.from);
|
|
39
|
+
const to = parseModelRef(change.to);
|
|
40
|
+
if (prices && from && to) {
|
|
41
|
+
const ratio = computeModelPriceRatio(to, from, prices);
|
|
42
|
+
if (ratio.kind === "unknown") price = " (price unknown)";
|
|
43
|
+
else if (ratio.blended >= 1.05) price = ` (${(Math.round(ratio.blended * 10) / 10).toFixed(1)}x the price)`;
|
|
44
|
+
}
|
|
45
|
+
const reason = REASON[change.category] ?? change.category;
|
|
46
|
+
return change.launch
|
|
47
|
+
? `${change.agent}: ${change.from} is still cooling down after ${reason}; starting on ${change.to}${price}.`
|
|
48
|
+
: `${change.agent}: ${change.from} hit ${reason}; continuing on ${change.to}${price}.`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The chain for `agent` under the launch's profile, or the active profile when the launch names none. */
|
|
52
|
+
export function profileFallbackChain(configHome: string, profile: string | undefined, agent: string): FallbackChain {
|
|
53
|
+
const read = readFallbacksFile(fallbacksFilePath(configHome));
|
|
54
|
+
if (read.status !== "valid") return [];
|
|
55
|
+
let name = profile;
|
|
56
|
+
if (name === undefined) {
|
|
57
|
+
const profiles = readProfilesFileResult(profilesFilePath(configHome));
|
|
58
|
+
name = profiles.status === "valid" ? profiles.file.active : undefined;
|
|
59
|
+
}
|
|
60
|
+
return chainFor(read.file, name, agent);
|
|
61
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { TERMINAL_ERROR_CATEGORY, type TerminalErrorCategory } from "./agents-protocol.ts";
|
|
2
|
+
import type { FallbackChain } from "./model-fallback-store.ts";
|
|
3
|
+
import type { ThinkingLevel } from "./model-routing-authority.ts";
|
|
4
|
+
|
|
5
|
+
// When a run moves to its next model, and which one. A terminal error only
|
|
6
|
+
// reaches us after Pi spent its own retries on the same model, so the error
|
|
7
|
+
// category alone decides: limits and unusable credentials move on, transient
|
|
8
|
+
// provider trouble moves on (Pi already retried), and request problems never
|
|
9
|
+
// do, since another model would fail the same way.
|
|
10
|
+
|
|
11
|
+
const FALL_BACK = new Set<TerminalErrorCategory>([
|
|
12
|
+
TERMINAL_ERROR_CATEGORY.QUOTA,
|
|
13
|
+
TERMINAL_ERROR_CATEGORY.AUTH,
|
|
14
|
+
TERMINAL_ERROR_CATEGORY.RATE_LIMIT,
|
|
15
|
+
TERMINAL_ERROR_CATEGORY.PROVIDER_UNAVAILABLE,
|
|
16
|
+
TERMINAL_ERROR_CATEGORY.TIMEOUT,
|
|
17
|
+
TERMINAL_ERROR_CATEGORY.NETWORK,
|
|
18
|
+
]);
|
|
19
|
+
const EXHAUSTED = new Set<TerminalErrorCategory>([TERMINAL_ERROR_CATEGORY.QUOTA, TERMINAL_ERROR_CATEGORY.AUTH]);
|
|
20
|
+
|
|
21
|
+
/** How long a failed model is skipped by every agent of the process. */
|
|
22
|
+
export const COOLDOWN_MS = { exhausted: 30 * 60_000, transient: 2 * 60_000 } as const;
|
|
23
|
+
|
|
24
|
+
export function shouldFallBack(category: TerminalErrorCategory | undefined): boolean {
|
|
25
|
+
return category !== undefined && FALL_BACK.has(category);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const providerOf = (model: string) => (model.includes("/") ? model.slice(0, model.indexOf("/")) : undefined);
|
|
29
|
+
|
|
30
|
+
export class ModelCooldown {
|
|
31
|
+
private readonly until = new Map<string, number>();
|
|
32
|
+
private readonly why = new Map<string, TerminalErrorCategory>();
|
|
33
|
+
/** Providers whose subscription window is full, until it resets. */
|
|
34
|
+
private readonly providers = new Map<string, number>();
|
|
35
|
+
markProvider(provider: string, until: number): void {
|
|
36
|
+
this.providers.set(provider, until);
|
|
37
|
+
}
|
|
38
|
+
clearProvider(provider: string): void {
|
|
39
|
+
this.providers.delete(provider);
|
|
40
|
+
}
|
|
41
|
+
private providerWaits(model: string, now: number): boolean {
|
|
42
|
+
const provider = providerOf(model);
|
|
43
|
+
const until = provider === undefined ? undefined : this.providers.get(provider);
|
|
44
|
+
if (until === undefined) return false;
|
|
45
|
+
if (now < until) return true;
|
|
46
|
+
this.providers.delete(provider!);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
mark(model: string, category: TerminalErrorCategory, now: number): void {
|
|
50
|
+
if (!shouldFallBack(category)) return;
|
|
51
|
+
const until = now + (EXHAUSTED.has(category) ? COOLDOWN_MS.exhausted : COOLDOWN_MS.transient);
|
|
52
|
+
if (until >= (this.until.get(model) ?? 0)) this.why.set(model, category);
|
|
53
|
+
this.until.set(model, Math.max(this.until.get(model) ?? 0, until));
|
|
54
|
+
}
|
|
55
|
+
/** A model whose own usage limit is spent waits until that limit resets. */
|
|
56
|
+
markModelUntil(model: string, until: number): void {
|
|
57
|
+
if (until <= (this.until.get(model) ?? 0)) return;
|
|
58
|
+
this.until.set(model, until);
|
|
59
|
+
this.why.set(model, TERMINAL_ERROR_CATEGORY.QUOTA);
|
|
60
|
+
}
|
|
61
|
+
/** Why a model is cooling down, while it is. */
|
|
62
|
+
reason(model: string, now: number): TerminalErrorCategory | undefined {
|
|
63
|
+
if (this.available(model, now)) return undefined;
|
|
64
|
+
return this.why.get(model) ?? TERMINAL_ERROR_CATEGORY.QUOTA;
|
|
65
|
+
}
|
|
66
|
+
available(model: string, now: number): boolean {
|
|
67
|
+
if (this.providerWaits(model, now)) return false;
|
|
68
|
+
const until = this.until.get(model);
|
|
69
|
+
if (until === undefined) return true;
|
|
70
|
+
if (now < until) return false;
|
|
71
|
+
this.until.delete(model);
|
|
72
|
+
this.why.delete(model);
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type UsageLike = {
|
|
78
|
+
provider: string;
|
|
79
|
+
limits: ReadonlyArray<{ name?: string; scope?: "provider"; limitReached: boolean; windows: ReadonlyArray<{ label?: string; windowSeconds?: number; usedPercent: number; resetAt: number | null }> }>;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Mirrors a /gentle:usage reading into the cooldown. A spent limit that
|
|
84
|
+
* covers the whole provider (`scope: "provider"`, Codex's main limit), or
|
|
85
|
+
* every limit spent at once, makes the provider wait until the earliest
|
|
86
|
+
* reset. Any other spent limit names one model (NaN reports one per model,
|
|
87
|
+
* Codex extras use display names, matched in id form): only that model waits. The whole
|
|
88
|
+
* provider waits only when every limit is spent, until the earliest reset;
|
|
89
|
+
* a reading with room releases it. A full window whose reset already passed
|
|
90
|
+
* is a stale reading and blocks nothing; a spent limit with no reset time at
|
|
91
|
+
* all waits the usual exhausted time. A model's own failures stay.
|
|
92
|
+
*/
|
|
93
|
+
export function syncUsageCooldown(usage: UsageLike, cooldown: ModelCooldown, now: number): void {
|
|
94
|
+
const spentUntil = (limit: UsageLike["limits"][number]): number | undefined => {
|
|
95
|
+
const full = limit.windows.filter((window) => limit.limitReached || window.usedPercent >= 100);
|
|
96
|
+
if (!limit.limitReached && full.length === 0) return undefined;
|
|
97
|
+
const resets = full.map((window) => window.resetAt).filter((reset): reset is number => typeof reset === "number");
|
|
98
|
+
if (resets.length === 0) return now + COOLDOWN_MS.exhausted;
|
|
99
|
+
const future = resets.filter((reset) => reset > now);
|
|
100
|
+
return future.length ? Math.min(...future) : undefined;
|
|
101
|
+
};
|
|
102
|
+
const spent = usage.limits.map(spentUntil);
|
|
103
|
+
// The provider waits when a limit that covers all of it is spent, or when every limit is.
|
|
104
|
+
const covering = usage.limits.flatMap((limit, index) => (limit.scope === "provider" && spent[index] !== undefined ? [spent[index]!] : []));
|
|
105
|
+
const all = spent.length > 0 && spent.every((until) => until !== undefined);
|
|
106
|
+
const providerUntil = covering.length ? Math.min(...covering) : all ? Math.min(...(spent as number[])) : undefined;
|
|
107
|
+
if (providerUntil !== undefined) cooldown.markProvider(usage.provider, providerUntil);
|
|
108
|
+
else cooldown.clearProvider(usage.provider);
|
|
109
|
+
usage.limits.forEach((limit, index) => {
|
|
110
|
+
const until = spent[index];
|
|
111
|
+
// Display names ("GPT-5.3 Codex_Spark") are matched in model-id form.
|
|
112
|
+
const id = limit.name?.trim().toLowerCase().replace(/[\s_]+/g, "-");
|
|
113
|
+
if (until !== undefined && limit.scope !== "provider" && id && SAFE_MODEL_NAME.test(id)) cooldown.markModelUntil(`${usage.provider}/${id}`, until);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const SAFE_MODEL_NAME = /^[A-Za-z0-9._~:@+%-]+$/;
|
|
118
|
+
|
|
119
|
+
const SHARED = Symbol.for("gentle-pi.model-fallback.cooldown");
|
|
120
|
+
/** One registry per process: the orchestrator and the agents runner share what ran out. */
|
|
121
|
+
export function sharedCooldown(): ModelCooldown {
|
|
122
|
+
const host = globalThis as unknown as Record<symbol, ModelCooldown | undefined>;
|
|
123
|
+
return (host[SHARED] ??= new ModelCooldown());
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export type ModelPick = { model: string | undefined; thinking?: ThinkingLevel; fallback: boolean };
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The model a run should use: the primary while it is usable and untried,
|
|
130
|
+
* else the first chain step neither tried in this run nor cooling down.
|
|
131
|
+
* Undefined when nothing is left. An undefined primary means "inherit".
|
|
132
|
+
*/
|
|
133
|
+
export function pickModel(primary: string | undefined, chain: FallbackChain, state: { cooldown: ModelCooldown; now: number; tried?: ReadonlySet<string> }): ModelPick | undefined {
|
|
134
|
+
const usable = (model: string) => !state.tried?.has(model) && state.cooldown.available(model, state.now);
|
|
135
|
+
if (primary === undefined ? !state.tried?.size : usable(primary)) return { model: primary, fallback: false };
|
|
136
|
+
for (const step of chain) {
|
|
137
|
+
if (step.model === primary || !usable(step.model)) continue;
|
|
138
|
+
return step.thinking ? { model: step.model, thinking: step.thinking, fallback: true } : { model: step.model, fallback: true };
|
|
139
|
+
}
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { isValidProfileName, writeJsonFileAtomicallySync } from "./agent-profiles.ts";
|
|
4
|
+
import { isSafeAgentName, isThinkingLevel, SAFE_MODEL_ID_PATTERN, type ThinkingLevel } from "./model-routing-authority.ts";
|
|
5
|
+
|
|
6
|
+
// Fallback chains per profile and agent. They live in their own file next to
|
|
7
|
+
// profiles.json: the profiles reader rejects unknown keys, so keeping chains
|
|
8
|
+
// apart lets older versions read profiles.json unchanged (they just never
|
|
9
|
+
// fall back). Every model in a chain is one the user picked.
|
|
10
|
+
|
|
11
|
+
export const FALLBACKS_KIND = "gentle-pi.profile_model_fallbacks";
|
|
12
|
+
export const FALLBACKS_VERSION = 1;
|
|
13
|
+
/** The chain for every agent of a profile that has none of its own. */
|
|
14
|
+
export const DEFAULT_CHAIN_KEY = "*";
|
|
15
|
+
export const MAX_CHAIN_LENGTH = 8;
|
|
16
|
+
|
|
17
|
+
export interface FallbackStep {
|
|
18
|
+
model: string;
|
|
19
|
+
thinking?: ThinkingLevel;
|
|
20
|
+
}
|
|
21
|
+
export type FallbackChain = readonly FallbackStep[];
|
|
22
|
+
export interface FallbacksFile {
|
|
23
|
+
kind: typeof FALLBACKS_KIND;
|
|
24
|
+
version: typeof FALLBACKS_VERSION;
|
|
25
|
+
/** profile name → agent name (or `*`) → ordered fallbacks */
|
|
26
|
+
profiles: Record<string, Record<string, FallbackStep[]>>;
|
|
27
|
+
}
|
|
28
|
+
export type FallbacksReadResult = { status: "missing" } | { status: "invalid" } | { status: "valid"; file: FallbacksFile };
|
|
29
|
+
|
|
30
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
31
|
+
const exactKeys = (value: Record<string, unknown>, allowed: readonly string[]) => Object.keys(value).every((key) => allowed.includes(key));
|
|
32
|
+
|
|
33
|
+
export function fallbacksFilePath(configHome: string): string {
|
|
34
|
+
return join(configHome, "profile-fallbacks.json");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function emptyFallbacksFile(): FallbacksFile {
|
|
38
|
+
return { kind: FALLBACKS_KIND, version: FALLBACKS_VERSION, profiles: {} };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function normalizeStep(value: unknown): FallbackStep | undefined {
|
|
42
|
+
if (!isRecord(value) || !exactKeys(value, ["model", "thinking"])) return undefined;
|
|
43
|
+
if (typeof value.model !== "string" || !value.model || !SAFE_MODEL_ID_PATTERN.test(value.model)) return undefined;
|
|
44
|
+
const thinking = value.thinking;
|
|
45
|
+
if (thinking === undefined) return { model: value.model };
|
|
46
|
+
return isThinkingLevel(thinking) ? { model: value.model, thinking } : undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function normalizeChain(value: unknown): FallbackStep[] | undefined {
|
|
50
|
+
if (!Array.isArray(value) || value.length > MAX_CHAIN_LENGTH) return undefined;
|
|
51
|
+
const steps = value.map(normalizeStep);
|
|
52
|
+
if (steps.some((step) => step === undefined)) return undefined;
|
|
53
|
+
const models = steps.map((step) => step!.model);
|
|
54
|
+
return new Set(models).size === models.length ? (steps as FallbackStep[]) : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const validAgentKey = (key: string) => key === DEFAULT_CHAIN_KEY || isSafeAgentName(key);
|
|
58
|
+
|
|
59
|
+
export function normalizeFallbacksFile(value: unknown): FallbacksFile | undefined {
|
|
60
|
+
if (!isRecord(value) || !exactKeys(value, ["kind", "version", "profiles"])) return undefined;
|
|
61
|
+
if (value.kind !== FALLBACKS_KIND || value.version !== FALLBACKS_VERSION || !isRecord(value.profiles)) return undefined;
|
|
62
|
+
const profiles: FallbacksFile["profiles"] = {};
|
|
63
|
+
for (const [name, agents] of Object.entries(value.profiles)) {
|
|
64
|
+
if (!isValidProfileName(name) || !isRecord(agents)) return undefined;
|
|
65
|
+
const chains: Record<string, FallbackStep[]> = {};
|
|
66
|
+
for (const [agent, chain] of Object.entries(agents)) {
|
|
67
|
+
const steps = validAgentKey(agent) ? normalizeChain(chain) : undefined;
|
|
68
|
+
if (!steps) return undefined;
|
|
69
|
+
if (steps.length) chains[agent] = steps;
|
|
70
|
+
}
|
|
71
|
+
if (Object.keys(chains).length) profiles[name] = chains;
|
|
72
|
+
}
|
|
73
|
+
return { kind: FALLBACKS_KIND, version: FALLBACKS_VERSION, profiles };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function parseFallbacksFileText(text: string): { status: "invalid" } | { status: "valid"; file: FallbacksFile } {
|
|
77
|
+
try {
|
|
78
|
+
const file = normalizeFallbacksFile(JSON.parse(text));
|
|
79
|
+
return file ? { status: "valid", file } : { status: "invalid" };
|
|
80
|
+
} catch {
|
|
81
|
+
return { status: "invalid" };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Reads the chains; a broken file is reported, never rewritten. */
|
|
86
|
+
export function readFallbacksFile(path: string): FallbacksReadResult {
|
|
87
|
+
if (!existsSync(path)) return { status: "missing" };
|
|
88
|
+
try {
|
|
89
|
+
return parseFallbacksFileText(readFileSync(path, "utf8"));
|
|
90
|
+
} catch {
|
|
91
|
+
return { status: "invalid" };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function writeFallbacksFile(path: string, file: FallbacksFile): void {
|
|
96
|
+
const normalized = normalizeFallbacksFile(file);
|
|
97
|
+
if (!normalized) throw new TypeError("Invalid fallback chains");
|
|
98
|
+
writeJsonFileAtomicallySync(path, `${JSON.stringify(normalized, null, 2)}\n`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The fallbacks for `agent` under `profile`: its own chain, else the profile's `*` chain, else none. */
|
|
102
|
+
export function chainFor(file: FallbacksFile, profile: string | undefined, agent: string): FallbackChain {
|
|
103
|
+
if (!profile) return [];
|
|
104
|
+
const chains = file.profiles[profile];
|
|
105
|
+
return chains?.[agent] ?? chains?.[DEFAULT_CHAIN_KEY] ?? [];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Replaces one chain; an empty chain removes the entry (and an emptied profile). */
|
|
109
|
+
export function setChain(file: FallbacksFile, profile: string, agent: string, steps: FallbackChain): FallbacksFile {
|
|
110
|
+
if (!isValidProfileName(profile)) throw new TypeError(`Invalid profile name: ${profile}`);
|
|
111
|
+
if (!validAgentKey(agent)) throw new TypeError(`Invalid agent name: ${agent}`);
|
|
112
|
+
const chain = normalizeChain(steps.map((step) => ({ ...step })));
|
|
113
|
+
if (!chain) {
|
|
114
|
+
const models = steps.map((step) => step.model);
|
|
115
|
+
throw new TypeError(new Set(models).size !== models.length ? "A model can appear once per chain" : `Invalid fallback chain for ${agent}`);
|
|
116
|
+
}
|
|
117
|
+
const chains = { ...(file.profiles[profile] ?? {}) };
|
|
118
|
+
if (chain.length) chains[agent] = chain;
|
|
119
|
+
else delete chains[agent];
|
|
120
|
+
const profiles = { ...file.profiles };
|
|
121
|
+
if (Object.keys(chains).length) profiles[profile] = chains;
|
|
122
|
+
else delete profiles[profile];
|
|
123
|
+
return { ...file, profiles };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Follows a profile rename (`to`) or deletion (`to` undefined). */
|
|
127
|
+
export function renameProfileFallbacks(file: FallbacksFile, from: string, to: string | undefined): FallbacksFile {
|
|
128
|
+
if (!(from in file.profiles)) return file;
|
|
129
|
+
const profiles = { ...file.profiles };
|
|
130
|
+
const chains = profiles[from]!;
|
|
131
|
+
delete profiles[from];
|
|
132
|
+
if (to) profiles[to] = chains;
|
|
133
|
+
return { ...file, profiles };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Keeps the chains on disk with a renamed (`to`) or deleted profile; false when there was nothing to follow or the file is broken. */
|
|
137
|
+
export function followProfileRename(configHome: string, from: string, to: string | undefined): boolean {
|
|
138
|
+
const path = fallbacksFilePath(configHome);
|
|
139
|
+
const read = readFallbacksFile(path);
|
|
140
|
+
if (read.status !== "valid" || !(from in read.file.profiles)) return false;
|
|
141
|
+
writeFallbacksFile(path, renameProfileFallbacks(read.file, from, to));
|
|
142
|
+
return true;
|
|
143
|
+
}
|