@caupulican/pi-adaptative 0.81.37 → 0.81.39
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 +91 -0
- package/README.md +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/README.md +18 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts +131 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.d.ts.map +1 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js +196 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.js.map +1 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/dispatch-grant.ts +308 -0
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts +22 -2
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.d.ts.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js +585 -24
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.js.map +1 -1
- package/dist/bundled-resources/extensions/tmux-agent-manager/index.ts +749 -27
- package/dist/bundled-resources/runtimes/pi-shell-engine/commands/__init__.py +43 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/commands/fs.py +270 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/commands/search.py +252 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/commands/strings.py +399 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/commands/text.py +575 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/context.py +52 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/errors.py +49 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/exec.py +734 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/expand.py +238 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/main.py +132 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/nodes.py +116 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/parser.py +287 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/proc.py +137 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/state.py +100 -0
- package/dist/bundled-resources/runtimes/pi-shell-engine/tokens.py +579 -0
- package/dist/bundled-resources/skills/tool-call-repair/SKILL.md +19 -9
- package/dist/bundled-resources/skills/tool-call-repair/references/failure-grammar.md +22 -5
- package/dist/bundled-resources/skills/tool-call-repair/references/repair-catalogue.md +5 -5
- package/dist/bundled-resources/skills/tool-call-repair/references/text-protocol-grammar.md +28 -7
- package/dist/cli/args.d.ts +3 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +15 -0
- package/dist/cli/args.js.map +1 -1
- package/dist/core/agent-paths.d.ts +49 -0
- package/dist/core/agent-paths.d.ts.map +1 -0
- package/dist/core/agent-paths.js +107 -0
- package/dist/core/agent-paths.js.map +1 -0
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +3 -3
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +234 -17
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +484 -62
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/autonomy/contracts.d.ts +9 -0
- package/dist/core/autonomy/contracts.d.ts.map +1 -1
- package/dist/core/autonomy/contracts.js.map +1 -1
- package/dist/core/autonomy/lane-tracker.d.ts +10 -1
- package/dist/core/autonomy/lane-tracker.d.ts.map +1 -1
- package/dist/core/autonomy/lane-tracker.js +5 -1
- package/dist/core/autonomy/lane-tracker.js.map +1 -1
- package/dist/core/background-lane-controller.d.ts +122 -6
- package/dist/core/background-lane-controller.d.ts.map +1 -1
- package/dist/core/background-lane-controller.js +447 -90
- package/dist/core/background-lane-controller.js.map +1 -1
- package/dist/core/bash-execution-controller.d.ts +4 -0
- package/dist/core/bash-execution-controller.d.ts.map +1 -1
- package/dist/core/bash-execution-controller.js +7 -1
- package/dist/core/bash-execution-controller.js.map +1 -1
- package/dist/core/compaction-support.d.ts +13 -3
- package/dist/core/compaction-support.d.ts.map +1 -1
- package/dist/core/compaction-support.js +43 -7
- package/dist/core/compaction-support.js.map +1 -1
- package/dist/core/context/context-audit.d.ts +33 -1
- package/dist/core/context/context-audit.d.ts.map +1 -1
- package/dist/core/context/context-audit.js +31 -5
- package/dist/core/context/context-audit.js.map +1 -1
- package/dist/core/context/sqlite-runtime-index.d.ts.map +1 -1
- package/dist/core/context/sqlite-runtime-index.js +13 -7
- package/dist/core/context/sqlite-runtime-index.js.map +1 -1
- package/dist/core/context-gc.d.ts.map +1 -1
- package/dist/core/context-gc.js +16 -1
- package/dist/core/context-gc.js.map +1 -1
- package/dist/core/context-pipeline.d.ts +26 -4
- package/dist/core/context-pipeline.d.ts.map +1 -1
- package/dist/core/context-pipeline.js +137 -14
- package/dist/core/context-pipeline.js.map +1 -1
- package/dist/core/cost-guard.d.ts +19 -3
- package/dist/core/cost-guard.d.ts.map +1 -1
- package/dist/core/cost-guard.js +18 -3
- package/dist/core/cost-guard.js.map +1 -1
- package/dist/core/delegation/session-worker-result.d.ts +34 -4
- package/dist/core/delegation/session-worker-result.d.ts.map +1 -1
- package/dist/core/delegation/session-worker-result.js +64 -4
- package/dist/core/delegation/session-worker-result.js.map +1 -1
- package/dist/core/delegation/worker-actions.d.ts.map +1 -1
- package/dist/core/delegation/worker-actions.js +1 -1
- package/dist/core/delegation/worker-actions.js.map +1 -1
- package/dist/core/delegation/worker-result.d.ts +42 -1
- package/dist/core/delegation/worker-result.d.ts.map +1 -1
- package/dist/core/delegation/worker-result.js +68 -0
- package/dist/core/delegation/worker-result.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +23 -7
- 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 +1 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +51 -0
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/goal-loop-controller.d.ts +17 -1
- package/dist/core/goal-loop-controller.d.ts.map +1 -1
- package/dist/core/goal-loop-controller.js +79 -11
- package/dist/core/goal-loop-controller.js.map +1 -1
- package/dist/core/goals/goal-continuation-controller.d.ts +48 -2
- package/dist/core/goals/goal-continuation-controller.d.ts.map +1 -1
- package/dist/core/goals/goal-continuation-controller.js +77 -0
- package/dist/core/goals/goal-continuation-controller.js.map +1 -1
- package/dist/core/goals/goal-continuation-defaults.d.ts +39 -0
- package/dist/core/goals/goal-continuation-defaults.d.ts.map +1 -1
- package/dist/core/goals/goal-continuation-defaults.js +42 -0
- package/dist/core/goals/goal-continuation-defaults.js.map +1 -1
- package/dist/core/goals/goal-continuation-prompt.d.ts +4 -0
- package/dist/core/goals/goal-continuation-prompt.d.ts.map +1 -1
- package/dist/core/goals/goal-continuation-prompt.js +51 -10
- package/dist/core/goals/goal-continuation-prompt.js.map +1 -1
- package/dist/core/goals/goal-runtime-snapshot.d.ts +101 -2
- package/dist/core/goals/goal-runtime-snapshot.d.ts.map +1 -1
- package/dist/core/goals/goal-runtime-snapshot.js +87 -4
- package/dist/core/goals/goal-runtime-snapshot.js.map +1 -1
- package/dist/core/goals/goal-state.d.ts +89 -1
- package/dist/core/goals/goal-state.d.ts.map +1 -1
- package/dist/core/goals/goal-state.js +71 -5
- package/dist/core/goals/goal-state.js.map +1 -1
- package/dist/core/goals/goal-tool-core.d.ts +56 -2
- package/dist/core/goals/goal-tool-core.d.ts.map +1 -1
- package/dist/core/goals/goal-tool-core.js +111 -5
- package/dist/core/goals/goal-tool-core.js.map +1 -1
- package/dist/core/goals/session-goal-state.d.ts +11 -2
- package/dist/core/goals/session-goal-state.d.ts.map +1 -1
- package/dist/core/goals/session-goal-state.js +32 -17
- package/dist/core/goals/session-goal-state.js.map +1 -1
- package/dist/core/keybindings.d.ts +10 -0
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +10 -2
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/learning/observation-store.d.ts +12 -3
- package/dist/core/learning/observation-store.d.ts.map +1 -1
- package/dist/core/learning/observation-store.js +30 -15
- package/dist/core/learning/observation-store.js.map +1 -1
- package/dist/core/learning/skill-curator.d.ts +5 -1
- package/dist/core/learning/skill-curator.d.ts.map +1 -1
- package/dist/core/learning/skill-curator.js +21 -19
- package/dist/core/learning/skill-curator.js.map +1 -1
- package/dist/core/local-runtime-controller.d.ts +65 -3
- package/dist/core/local-runtime-controller.d.ts.map +1 -1
- package/dist/core/local-runtime-controller.js +186 -26
- package/dist/core/local-runtime-controller.js.map +1 -1
- package/dist/core/memory/providers/file-store.d.ts +1 -1
- package/dist/core/memory/providers/file-store.d.ts.map +1 -1
- package/dist/core/memory/providers/file-store.js +6 -6
- package/dist/core/memory/providers/file-store.js.map +1 -1
- package/dist/core/model-capability.d.ts +34 -0
- package/dist/core/model-capability.d.ts.map +1 -1
- package/dist/core/model-capability.js +42 -1
- package/dist/core/model-capability.js.map +1 -1
- package/dist/core/model-router/tool-escalation.d.ts +15 -0
- package/dist/core/model-router/tool-escalation.d.ts.map +1 -1
- package/dist/core/model-router/tool-escalation.js +23 -1
- package/dist/core/model-router/tool-escalation.js.map +1 -1
- package/dist/core/model-router-controller.d.ts +34 -7
- package/dist/core/model-router-controller.d.ts.map +1 -1
- package/dist/core/model-router-controller.js +95 -16
- package/dist/core/model-router-controller.js.map +1 -1
- package/dist/core/models/adaptation-store.d.ts +7 -0
- package/dist/core/models/adaptation-store.d.ts.map +1 -1
- package/dist/core/models/adaptation-store.js +47 -11
- package/dist/core/models/adaptation-store.js.map +1 -1
- package/dist/core/models/default-model-suggestions.d.ts.map +1 -1
- package/dist/core/models/default-model-suggestions.js +17 -0
- package/dist/core/models/default-model-suggestions.js.map +1 -1
- package/dist/core/models/fitness-store.d.ts +3 -0
- package/dist/core/models/fitness-store.d.ts.map +1 -1
- package/dist/core/models/fitness-store.js +11 -2
- package/dist/core/models/fitness-store.js.map +1 -1
- package/dist/core/models/llamacpp-runtime.d.ts +180 -0
- package/dist/core/models/llamacpp-runtime.d.ts.map +1 -0
- package/dist/core/models/llamacpp-runtime.js +475 -0
- package/dist/core/models/llamacpp-runtime.js.map +1 -0
- package/dist/core/models/local-registration.d.ts +40 -0
- package/dist/core/models/local-registration.d.ts.map +1 -1
- package/dist/core/models/local-registration.js +94 -5
- package/dist/core/models/local-registration.js.map +1 -1
- package/dist/core/models/local-runtime.d.ts.map +1 -1
- package/dist/core/models/local-runtime.js +9 -7
- package/dist/core/models/local-runtime.js.map +1 -1
- package/dist/core/models/model-ref.d.ts +7 -0
- package/dist/core/models/model-ref.d.ts.map +1 -1
- package/dist/core/models/model-ref.js +26 -0
- package/dist/core/models/model-ref.js.map +1 -1
- package/dist/core/models/needle-runtime.d.ts +257 -0
- package/dist/core/models/needle-runtime.d.ts.map +1 -0
- package/dist/core/models/needle-runtime.js +519 -0
- package/dist/core/models/needle-runtime.js.map +1 -0
- package/dist/core/models/prism-llamacpp-lifecycle.d.ts +89 -0
- package/dist/core/models/prism-llamacpp-lifecycle.d.ts.map +1 -0
- package/dist/core/models/prism-llamacpp-lifecycle.js +121 -0
- package/dist/core/models/prism-llamacpp-lifecycle.js.map +1 -0
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +11 -10
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/process-matrix/codes.d.ts +72 -0
- package/dist/core/process-matrix/codes.d.ts.map +1 -0
- package/dist/core/process-matrix/codes.js +15 -0
- package/dist/core/process-matrix/codes.js.map +1 -0
- package/dist/core/process-matrix/runtime.d.ts +63 -0
- package/dist/core/process-matrix/runtime.d.ts.map +1 -0
- package/dist/core/process-matrix/runtime.js +310 -0
- package/dist/core/process-matrix/runtime.js.map +1 -0
- package/dist/core/process-matrix/store.d.ts +22 -0
- package/dist/core/process-matrix/store.d.ts.map +1 -0
- package/dist/core/process-matrix/store.js +80 -0
- package/dist/core/process-matrix/store.js.map +1 -0
- package/dist/core/process-matrix/supervisor.d.ts +72 -0
- package/dist/core/process-matrix/supervisor.d.ts.map +1 -0
- package/dist/core/process-matrix/supervisor.js +130 -0
- package/dist/core/process-matrix/supervisor.js.map +1 -0
- package/dist/core/profile-registry.d.ts +7 -0
- package/dist/core/profile-registry.d.ts.map +1 -1
- package/dist/core/profile-registry.js +20 -0
- package/dist/core/profile-registry.js.map +1 -1
- package/dist/core/python-runtime.d.ts.map +1 -1
- package/dist/core/python-runtime.js +3 -3
- package/dist/core/python-runtime.js.map +1 -1
- package/dist/core/reflection-controller.d.ts +29 -5
- package/dist/core/reflection-controller.d.ts.map +1 -1
- package/dist/core/reflection-controller.js +215 -126
- package/dist/core/reflection-controller.js.map +1 -1
- package/dist/core/reload-blockers.d.ts +36 -0
- package/dist/core/reload-blockers.d.ts.map +1 -1
- package/dist/core/reload-blockers.js +44 -0
- package/dist/core/reload-blockers.js.map +1 -1
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +8 -7
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/runtime-builder.d.ts +58 -5
- package/dist/core/runtime-builder.d.ts.map +1 -1
- package/dist/core/runtime-builder.js +209 -25
- package/dist/core/runtime-builder.js.map +1 -1
- package/dist/core/scout-controller.d.ts +6 -0
- package/dist/core/scout-controller.d.ts.map +1 -1
- package/dist/core/scout-controller.js +66 -52
- package/dist/core/scout-controller.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +3 -3
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-role.d.ts +31 -0
- package/dist/core/session-role.d.ts.map +1 -0
- package/dist/core/session-role.js +52 -0
- package/dist/core/session-role.js.map +1 -0
- package/dist/core/settings-manager.d.ts +42 -3
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +94 -60
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/system-prompt-builder.d.ts +12 -0
- package/dist/core/system-prompt-builder.d.ts.map +1 -1
- package/dist/core/system-prompt-builder.js +6 -0
- package/dist/core/system-prompt-builder.js.map +1 -1
- package/dist/core/system-prompt.d.ts +14 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +20 -3
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tasks/session-task-state.d.ts +11 -2
- package/dist/core/tasks/session-task-state.d.ts.map +1 -1
- package/dist/core/tasks/session-task-state.js +27 -11
- package/dist/core/tasks/session-task-state.js.map +1 -1
- package/dist/core/tasks/task-contract-monitor.d.ts +45 -0
- package/dist/core/tasks/task-contract-monitor.d.ts.map +1 -0
- package/dist/core/tasks/task-contract-monitor.js +56 -0
- package/dist/core/tasks/task-contract-monitor.js.map +1 -0
- package/dist/core/tasks/task-state.d.ts +8 -0
- package/dist/core/tasks/task-state.d.ts.map +1 -1
- package/dist/core/tasks/task-state.js +28 -1
- package/dist/core/tasks/task-state.js.map +1 -1
- package/dist/core/tool-gate-controller.d.ts.map +1 -1
- package/dist/core/tool-gate-controller.js +5 -0
- package/dist/core/tool-gate-controller.js.map +1 -1
- package/dist/core/tool-recovery-log-records.d.ts +7 -0
- package/dist/core/tool-recovery-log-records.d.ts.map +1 -1
- package/dist/core/tool-recovery-log-records.js +14 -6
- package/dist/core/tool-recovery-log-records.js.map +1 -1
- package/dist/core/tool-selection/promotion.d.ts +54 -0
- package/dist/core/tool-selection/promotion.d.ts.map +1 -0
- package/dist/core/tool-selection/promotion.js +81 -0
- package/dist/core/tool-selection/promotion.js.map +1 -0
- package/dist/core/tool-selection/tool-performance-store.d.ts +37 -0
- package/dist/core/tool-selection/tool-performance-store.d.ts.map +1 -1
- package/dist/core/tool-selection/tool-performance-store.js +87 -3
- package/dist/core/tool-selection/tool-performance-store.js.map +1 -1
- package/dist/core/tool-selection/tool-selection-controller.d.ts +45 -0
- package/dist/core/tool-selection/tool-selection-controller.d.ts.map +1 -1
- package/dist/core/tool-selection/tool-selection-controller.js +96 -0
- package/dist/core/tool-selection/tool-selection-controller.js.map +1 -1
- package/dist/core/tools/bash.d.ts +18 -0
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +97 -18
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/delegate-status.d.ts +14 -0
- package/dist/core/tools/delegate-status.d.ts.map +1 -1
- package/dist/core/tools/delegate-status.js +82 -7
- package/dist/core/tools/delegate-status.js.map +1 -1
- package/dist/core/tools/delegate.d.ts.map +1 -1
- package/dist/core/tools/delegate.js +25 -8
- package/dist/core/tools/delegate.js.map +1 -1
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +52 -44
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/goal.d.ts +94 -3
- package/dist/core/tools/goal.d.ts.map +1 -1
- package/dist/core/tools/goal.js +165 -15
- package/dist/core/tools/goal.js.map +1 -1
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +5 -4
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/model-fitness.d.ts +7 -0
- 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/render-utils.d.ts.map +1 -1
- package/dist/core/tools/render-utils.js +1 -1
- package/dist/core/tools/render-utils.js.map +1 -1
- package/dist/core/tools/shell-contract-router.d.ts +6 -1
- package/dist/core/tools/shell-contract-router.d.ts.map +1 -1
- package/dist/core/tools/shell-contract-router.js +69 -13
- package/dist/core/tools/shell-contract-router.js.map +1 -1
- package/dist/core/tools/shell-session.d.ts +89 -0
- package/dist/core/tools/shell-session.d.ts.map +1 -0
- package/dist/core/tools/shell-session.js +432 -0
- package/dist/core/tools/shell-session.js.map +1 -0
- package/dist/core/tools/task-steps.d.ts +4 -0
- package/dist/core/tools/task-steps.d.ts.map +1 -1
- package/dist/core/tools/task-steps.js +63 -8
- package/dist/core/tools/task-steps.js.map +1 -1
- package/dist/core/tools/tmux-dispatch.d.ts +86 -0
- package/dist/core/tools/tmux-dispatch.d.ts.map +1 -0
- package/dist/core/tools/tmux-dispatch.js +91 -0
- package/dist/core/tools/tmux-dispatch.js.map +1 -0
- package/dist/core/tools/windows-shell-engine.d.ts +42 -0
- package/dist/core/tools/windows-shell-engine.d.ts.map +1 -0
- package/dist/core/tools/windows-shell-engine.js +153 -0
- package/dist/core/tools/windows-shell-engine.js.map +1 -0
- package/dist/core/tools/windows-shell-state.d.ts +40 -0
- package/dist/core/tools/windows-shell-state.d.ts.map +1 -0
- package/dist/core/tools/windows-shell-state.js +59 -0
- package/dist/core/tools/windows-shell-state.js.map +1 -0
- package/dist/core/tools/worktree-sync.d.ts +24 -0
- package/dist/core/tools/worktree-sync.d.ts.map +1 -0
- package/dist/core/tools/worktree-sync.js +338 -0
- package/dist/core/tools/worktree-sync.js.map +1 -0
- package/dist/core/trust-manager.d.ts +4 -1
- package/dist/core/trust-manager.d.ts.map +1 -1
- package/dist/core/trust-manager.js +20 -2
- package/dist/core/trust-manager.js.map +1 -1
- package/dist/core/util/atomic-file.d.ts +55 -0
- package/dist/core/util/atomic-file.d.ts.map +1 -0
- package/dist/core/util/atomic-file.js +255 -0
- package/dist/core/util/atomic-file.js.map +1 -0
- package/dist/core/util/minimatch-cache.d.ts +33 -0
- package/dist/core/util/minimatch-cache.d.ts.map +1 -0
- package/dist/core/util/minimatch-cache.js +0 -0
- package/dist/core/util/minimatch-cache.js.map +1 -0
- package/dist/core/worktree-sync/codes.d.ts +227 -0
- package/dist/core/worktree-sync/codes.d.ts.map +1 -0
- package/dist/core/worktree-sync/codes.js +14 -0
- package/dist/core/worktree-sync/codes.js.map +1 -0
- package/dist/core/worktree-sync/git-engine.d.ts +156 -0
- package/dist/core/worktree-sync/git-engine.d.ts.map +1 -0
- package/dist/core/worktree-sync/git-engine.js +1191 -0
- package/dist/core/worktree-sync/git-engine.js.map +1 -0
- package/dist/core/worktree-sync/lane-gate.d.ts +75 -0
- package/dist/core/worktree-sync/lane-gate.d.ts.map +1 -0
- package/dist/core/worktree-sync/lane-gate.js +360 -0
- package/dist/core/worktree-sync/lane-gate.js.map +1 -0
- package/dist/core/worktree-sync/runtime.d.ts +47 -0
- package/dist/core/worktree-sync/runtime.d.ts.map +1 -0
- package/dist/core/worktree-sync/runtime.js +96 -0
- package/dist/core/worktree-sync/runtime.js.map +1 -0
- package/dist/core/worktree-sync/store.d.ts +69 -0
- package/dist/core/worktree-sync/store.d.ts.map +1 -0
- package/dist/core/worktree-sync/store.js +247 -0
- package/dist/core/worktree-sync/store.js.map +1 -0
- package/dist/core/worktree-sync/watcher.d.ts +29 -0
- package/dist/core/worktree-sync/watcher.d.ts.map +1 -0
- package/dist/core/worktree-sync/watcher.js +93 -0
- package/dist/core/worktree-sync/watcher.js.map +1 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +94 -0
- package/dist/main.js.map +1 -1
- package/dist/migrations.d.ts +9 -0
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +38 -0
- package/dist/migrations.js.map +1 -1
- package/dist/modes/interactive/auto-learn-controller.d.ts +16 -1
- package/dist/modes/interactive/auto-learn-controller.d.ts.map +1 -1
- package/dist/modes/interactive/auto-learn-controller.js +50 -8
- package/dist/modes/interactive/auto-learn-controller.js.map +1 -1
- package/dist/modes/interactive/components/profile-resource-editor.d.ts.map +1 -1
- package/dist/modes/interactive/components/profile-resource-editor.js +23 -1
- package/dist/modes/interactive/components/profile-resource-editor.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +4 -0
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/local-model-commands.d.ts +43 -0
- package/dist/modes/interactive/local-model-commands.d.ts.map +1 -1
- package/dist/modes/interactive/local-model-commands.js +290 -3
- package/dist/modes/interactive/local-model-commands.js.map +1 -1
- package/dist/modes/interactive/session-flow-commands.d.ts.map +1 -1
- package/dist/modes/interactive/session-flow-commands.js +24 -4
- package/dist/modes/interactive/session-flow-commands.js.map +1 -1
- package/dist/utils/fs-watch.d.ts +11 -0
- package/dist/utils/fs-watch.d.ts.map +1 -1
- package/dist/utils/fs-watch.js +20 -2
- package/dist/utils/fs-watch.js.map +1 -1
- package/dist/utils/highlight-js-languages.d.ts +4 -0
- package/dist/utils/highlight-js-languages.d.ts.map +1 -0
- package/dist/utils/highlight-js-languages.js +573 -0
- package/dist/utils/highlight-js-languages.js.map +1 -0
- package/dist/utils/shell.d.ts +7 -1
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +39 -9
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/syntax-highlight.d.ts.map +1 -1
- package/dist/utils/syntax-highlight.js +53 -5
- package/dist/utils/syntax-highlight.js.map +1 -1
- package/dist/utils/tools-manager.d.ts.map +1 -1
- package/dist/utils/tools-manager.js +112 -1
- package/dist/utils/tools-manager.js.map +1 -1
- package/docs/development.md +2 -0
- package/docs/packages.md +1 -1
- package/docs/process-matrix.md +120 -0
- package/docs/settings.md +5 -2
- package/docs/tmux-agent-manager.md +85 -2
- package/docs/windows.md +52 -3
- package/docs/work-directory.md +29 -0
- package/docs/worktree-sync.md +250 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +12 -12
- package/package.json +10 -4
- package/docs/integration-sweep-builder-blueprint-2026-07-09.md +0 -365
- package/docs/integration-sweep-resume-2026-07-09.md +0 -407
|
@@ -0,0 +1 @@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AAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAiCpG;YAEa,eAAe;YAYf,aAAa;YA+Bb,WAAW;YA+BX,oBAAoB;IAYlC,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,eAAe;IAQvB;;;;;;OAMG;IACH,OAAO,CAAC,oBAAoB;IAQ5B;;;;;OAKG;IACG,aAAa,CAClB,IAAI,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EACpC,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GACnC,OAAO,CAAC,mBAAmB,CAAC,CA0C9B;YAEa,SAAS;IAWvB;;;;OAIG;IACG,KAAK,CAAC,IAAI,EAAE;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;KACf,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAsC5B;IAED,IAAI,IAAI;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAS3B;IAED,SAAS,IAAI,OAAO,CAEnB;CACD","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport {\n\tcreateWriteStream,\n\ttype Dirent,\n\texistsSync,\n\tmkdirSync,\n\treaddirSync,\n\treadFileSync,\n\trmSync,\n\tstatSync,\n\twriteFileSync,\n} from \"node:fs\";\nimport { arch as osArch, platform as osPlatform } from \"node:os\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { Readable, Writable } from \"node:stream\";\nimport { pipeline } from \"node:stream/promises\";\nimport { spawnProcess, spawnProcessSync, waitForChildProcessWithTermination } from \"../../utils/child-process.ts\";\nimport { killProcessTree, trackDetachedChildPid, untrackDetachedChildPid } from \"../../utils/shell.ts\";\nimport { modelsDir as agentModelsDir, runtimesDir as agentRuntimesDir } from \"../agent-paths.ts\";\n\n/**\n * Managed runtime for prism-ml 1-bit GGUF models (Bonsai-27B first). The provider requires their\n * llama.cpp fork (Q1_0_g128 hybrid-attention kernels) — stock llama.cpp/Ollama cannot serve these\n * weights — so this module owns runtime install (prebuilt release download+extract), GGUF\n * downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable\n * seams for fetch/spawn/exists, pi-owned directories under agentDir (runtimes/, models/),\n * detached+tracked child processes, onProgress as best-effort UI feedback, honest error taxonomies\n * instead of silent fallbacks.\n *\n * The fork publishes precompiled per-platform archives on GitHub Releases (no local build): pin a\n * release tag, download the matching asset, extract it, and locate the `llama-server` binary once\n * at install time (release archive layouts vary — bin/ vs build/bin/ at the top level — so this is\n * a deterministic one-time scan, not a fallback). The found relative path and backend (cpu/cuda)\n * are persisted to a manifest so later `detect()`/`serve()` calls never need to re-scan.\n */\n\n/**\n * Pinned Prism ML llama.cpp fork release — verified via\n * `GET https://api.github.com/repos/PrismML-Eng/llama.cpp/releases/tags/prism-b9594-38c66ad` on\n * 2026-07-18 (release exists, all assets referenced below are present on it). Bump here (and\n * re-verify with the same API call) when the provider ships new kernels.\n */\nexport const PRISM_LLAMACPP_PINNED_RELEASE = \"prism-b9594-38c66ad\";\n\nexport const PRISM_LLAMACPP_RELEASES_BASE_URL = \"https://github.com/PrismML-Eng/llama.cpp/releases/download\";\n\nexport interface PrismModelDescriptor {\n\trepo: string;\n\tfile: string;\n\tmmprojFile?: string;\n\tdisplayName: string;\n}\n\n/** Curated first-model descriptor; the `/models add` wiring task consumes this. */\nexport const BONSAI_27B: PrismModelDescriptor = {\n\trepo: \"prism-ml/Bonsai-27B-gguf\",\n\tfile: \"Bonsai-27B-Q1_0.gguf\",\n\tmmprojFile: \"Bonsai-27B-mmproj-Q8_0.gguf\",\n\tdisplayName: \"Bonsai-27B (1-bit Q1_0 + vision)\",\n};\n\nexport type PrismBackend = \"cpu\" | \"cuda\";\nexport type PrismAssetKind = \"tar-gz\" | \"zip\";\n\nexport interface PrismLlamaAsset {\n\tname: string;\n\tkind: PrismAssetKind;\n\tbackend: PrismBackend;\n}\n\n// Verbatim asset names from the pinned release (see PRISM_LLAMACPP_PINNED_RELEASE doc comment) —\n// hardcoded rather than templated from the tag because the provider's naming isn't fully\n// consistent across platforms (Windows CPU assets drop the tag/commit segment entirely; the\n// Windows CUDA asset uses a different build-number segment, \"b1\" not \"b9594\").\nconst LINUX_X64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-x64.tar.gz\";\nconst LINUX_ARM64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-arm64.tar.gz\";\nconst LINUX_X64_CUDA_ASSET = \"llama-prism-b9594-38c66ad-bin-linux-cuda-12.4-x64.tar.gz\";\nconst MACOS_ARM64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-arm64.tar.gz\";\nconst MACOS_X64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-x64.tar.gz\";\nconst WIN_X64_CPU_ASSET = \"llama-bin-win-cpu-x64.zip\";\nconst WIN_ARM64_CPU_ASSET = \"llama-bin-win-cpu-arm64.zip\";\n\n/**\n * Maps a platform/arch/GPU triple to the exact Prism llama.cpp release asset for\n * {@link PRISM_LLAMACPP_PINNED_RELEASE} — verified against the real GitHub release, not guessed.\n * CPU asset by default; the CUDA 12.4 variant is only offered for linux x64 with an NVIDIA GPU\n * present (a 12.8 variant also exists upstream but has no wired caller yet). Windows CUDA needs a\n * companion `cudart-*` archive and is out of scope for v1 — Windows always resolves to the CPU zip\n * regardless of `hasNvidiaGpu`. Pure and exported so it's independently testable.\n */\nexport function resolvePrismLlamaAsset(\n\tplat: string,\n\tarchitecture: string,\n\thasNvidiaGpu: boolean,\n): PrismLlamaAsset | undefined {\n\tif (plat === \"linux\") {\n\t\tif (architecture === \"x64\") {\n\t\t\treturn hasNvidiaGpu\n\t\t\t\t? { name: LINUX_X64_CUDA_ASSET, kind: \"tar-gz\", backend: \"cuda\" }\n\t\t\t\t: { name: LINUX_X64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\t}\n\t\tif (architecture === \"arm64\") return { name: LINUX_ARM64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"darwin\") {\n\t\tif (architecture === \"arm64\") return { name: MACOS_ARM64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\tif (architecture === \"x64\") return { name: MACOS_X64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"win32\") {\n\t\tif (architecture === \"x64\") return { name: WIN_X64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\tif (architecture === \"arm64\") return { name: WIN_ARM64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\treturn undefined;\n}\n\nexport interface PrismDetectResult {\n\truntimeInstalled: boolean;\n\tbinaryPath?: string;\n\trelease?: string;\n}\n\nexport interface PrismDownloadResult {\n\tok: boolean;\n\tpath?: string;\n\tskipped?: boolean;\n\terror?: string;\n}\n\nexport type PrismServeResult = { ok: true; baseUrl: string } | { ok: false; error: string };\n\ninterface PrismInstallManifest {\n\trelease: string;\n\tbinaryRelPath: string;\n\tbackend: PrismBackend;\n}\n\ntype PrismSpawnOptions = { detached?: boolean; stdio?: \"ignore\"; env?: NodeJS.ProcessEnv };\ntype PrismSpawnFn = (\n\tcommand: string,\n\targs: string[],\n\toptions: PrismSpawnOptions,\n) => Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\">;\ntype PrismExtractArchiveFn = (\n\tinput: Readable,\n\tdestDir: string,\n\tkind: PrismAssetKind,\n) => Promise<{ ok: boolean; error?: string }>;\n\nexport interface PrismLlamaCppDeps {\n\tfetchFn?: typeof fetch;\n\tspawnFn?: PrismSpawnFn;\n\texistsFn?: (path: string) => boolean;\n\tsleepFn?: (ms: number) => Promise<void>;\n\t/** Whether a named command exists on PATH (nvidia-smi, tar — for zip extraction on Windows). */\n\thasCommand?: (command: string) => boolean;\n\t/** Decided once at install time and persisted as `backend` — serve() reads the persisted value\n\t * so `-ngl 99` reflects what was actually installed, not the host's current GPU state. */\n\thasNvidiaGpu?: () => boolean;\n\tplatform?: () => string;\n\tarch?: () => string;\n\t/** Runs the extraction step for a downloaded archive. Injectable so installManaged's\n\t * download->extract->scan orchestration is testable without a real tar/unzip pipeline; defaults\n\t * to the real spawn-based extractor. */\n\textractArchive?: PrismExtractArchiveFn;\n\t/** Health-poll bounds for serve(); default ~120s (240 * 500ms), both overridable for tests. */\n\thealthPollAttempts?: number;\n\thealthPollIntervalMs?: number;\n}\n\nconst EXTRACTION_TIMEOUT_MS = 10 * 60_000;\nconst COMMAND_KILL_GRACE_MS = 2_000;\nconst DEFAULT_HEALTH_POLL_ATTEMPTS = 240;\nconst DEFAULT_HEALTH_POLL_INTERVAL_MS = 500;\nconst HEALTH_CHECK_TIMEOUT_MS = 2_000;\nconst HEAD_REQUEST_TIMEOUT_MS = 10_000;\n\nfunction parseContentLength(header: string | null): number | undefined {\n\tif (!header) return undefined;\n\tconst value = Number(header);\n\treturn Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\n/**\n * `spawnProcess(..., { stdio: [\"pipe\", ...] })` always yields a non-null `stdin` at runtime, but\n * child-process.ts's typed overload only narrows `.stdin` for the capture-only stdio shape used\n * elsewhere. This makes the true invariant explicit and fails loudly instead of silently\n * misbehaving if it's ever violated by a future stdio change.\n */\nfunction requireStdin(proc: ChildProcess, label: string): Writable {\n\tif (!proc.stdin) throw new Error(`${label}: no stdin pipe`);\n\treturn proc.stdin;\n}\n\nexport class PrismLlamaCppRuntime {\n\tprivate readonly _agentDir: string;\n\tprivate readonly _fetch: typeof fetch;\n\tprivate readonly _spawn: PrismSpawnFn;\n\tprivate readonly _exists: (path: string) => boolean;\n\tprivate readonly _sleep: (ms: number) => Promise<void>;\n\tprivate readonly _hasCommand: (command: string) => boolean;\n\tprivate readonly _hasNvidiaGpu: () => boolean;\n\tprivate readonly _platform: () => string;\n\tprivate readonly _arch: () => string;\n\tprivate readonly _extractArchiveOverride: PrismExtractArchiveFn | undefined;\n\tprivate readonly _healthPollAttempts: number;\n\tprivate readonly _healthPollIntervalMs: number;\n\tprivate _child: Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\"> | undefined;\n\n\tconstructor(args: { agentDir: string; deps?: PrismLlamaCppDeps }) {\n\t\tthis._agentDir = args.agentDir;\n\t\tthis._fetch = args.deps?.fetchFn ?? fetch;\n\t\tthis._spawn =\n\t\t\targs.deps?.spawnFn ?? ((command, argv, options) => spawn(command, argv, { ...options, stdio: \"ignore\" }));\n\t\tthis._exists = args.deps?.existsFn ?? existsSync;\n\t\tthis._sleep = args.deps?.sleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));\n\t\tthis._hasCommand =\n\t\t\targs.deps?.hasCommand ??\n\t\t\t((command) =>\n\t\t\t\tspawnProcessSync(command, [\"--version\"], { encoding: \"utf8\", timeout: 5_000 }).error === undefined);\n\t\tthis._hasNvidiaGpu = args.deps?.hasNvidiaGpu ?? (() => this._hasCommand(\"nvidia-smi\"));\n\t\tthis._platform = args.deps?.platform ?? osPlatform;\n\t\tthis._arch = args.deps?.arch ?? osArch;\n\t\tthis._extractArchiveOverride = args.deps?.extractArchive;\n\t\tthis._healthPollAttempts = args.deps?.healthPollAttempts ?? DEFAULT_HEALTH_POLL_ATTEMPTS;\n\t\tthis._healthPollIntervalMs = args.deps?.healthPollIntervalMs ?? DEFAULT_HEALTH_POLL_INTERVAL_MS;\n\t}\n\n\truntimeDir(): string {\n\t\treturn agentRuntimesDir(\"prism-llamacpp\", this._agentDir);\n\t}\n\n\tmodelsDir(): string {\n\t\treturn agentModelsDir(\"llamacpp\", this._agentDir);\n\t}\n\n\tprivate _binaryName(): string {\n\t\treturn this._platform() === \"win32\" ? \"llama-server.exe\" : \"llama-server\";\n\t}\n\n\tprivate _manifestPath(): string {\n\t\treturn join(this.runtimeDir(), \"install.json\");\n\t}\n\n\tprivate _readManifest(): PrismInstallManifest | undefined {\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(readFileSync(this._manifestPath(), \"utf8\")) as {\n\t\t\t\trelease?: unknown;\n\t\t\t\tbinaryRelPath?: unknown;\n\t\t\t\tbackend?: unknown;\n\t\t\t};\n\t\t\tif (\n\t\t\t\ttypeof parsed.release === \"string\" &&\n\t\t\t\ttypeof parsed.binaryRelPath === \"string\" &&\n\t\t\t\t(parsed.backend === \"cpu\" || parsed.backend === \"cuda\")\n\t\t\t) {\n\t\t\t\treturn { release: parsed.release, binaryRelPath: parsed.binaryRelPath, backend: parsed.backend };\n\t\t\t}\n\t\t\treturn undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _writeManifest(manifest: PrismInstallManifest): void {\n\t\twriteFileSync(this._manifestPath(), JSON.stringify(manifest, null, \"\\t\"));\n\t}\n\n\tasync detect(): Promise<PrismDetectResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { runtimeInstalled: false };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tconst runtimeInstalled = this._exists(binaryPath);\n\t\treturn {\n\t\t\truntimeInstalled,\n\t\t\tbinaryPath: runtimeInstalled ? binaryPath : undefined,\n\t\t\trelease: manifest.release,\n\t\t};\n\t}\n\n\t/** One-time recursive scan for the `llama-server` binary inside a freshly extracted release\n\t * archive — layouts vary (bin/ vs build/bin/ at the top level) across platforms/backends, so\n\t * this locates it deterministically instead of assuming a fixed path, then the result is\n\t * persisted so later calls never need to re-scan. */\n\tprivate _findBinaryRelPath(root: string): string | undefined {\n\t\tconst targetName = this._binaryName();\n\t\tconst stack: string[] = [root];\n\t\twhile (stack.length > 0) {\n\t\t\tconst dir = stack.pop();\n\t\t\tif (dir === undefined) continue;\n\t\t\tlet entries: Dirent[];\n\t\t\ttry {\n\t\t\t\tentries = readdirSync(dir, { withFileTypes: true });\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const entry of entries) {\n\t\t\t\tconst full = join(dir, entry.name);\n\t\t\t\tif (entry.isDirectory()) stack.push(full);\n\t\t\t\telse if (entry.isFile() && entry.name === targetName) return relative(root, full);\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Download the pinned release asset for this host and extract it (consent-gated by the caller,\n\t * same contract as OllamaRuntime#installManaged — this method only does the mechanical\n\t * download+extract+locate and reports the outcome honestly). No compiler toolchain required:\n\t * the fork ships prebuilt binaries.\n\t */\n\tasync installManaged(onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\tconst asset = resolvePrismLlamaAsset(this._platform(), this._arch(), this._hasNvidiaGpu());\n\t\tif (!asset) return { ok: false, error: \"unsupported-platform\" };\n\n\t\tonProgress?.(`Downloading ${asset.name}…`);\n\t\tconst downloadUrl = `${PRISM_LLAMACPP_RELEASES_BASE_URL}/${PRISM_LLAMACPP_PINNED_RELEASE}/${asset.name}`;\n\t\tlet response: Response;\n\t\ttry {\n\t\t\tresponse = await this._fetch(downloadUrl);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t\tif (!response.ok || !response.body) {\n\t\t\treturn { ok: false, error: `download-fail: HTTP ${response.status}` };\n\t\t}\n\n\t\tconst destDir = this.runtimeDir();\n\t\tmkdirSync(destDir, { recursive: true });\n\n\t\tonProgress?.(`Extracting ${asset.name}…`);\n\t\tconst extract = this._extractArchiveOverride ?? ((input, dest, kind) => this._extractArchive(input, dest, kind));\n\t\tconst extracted = await extract(response.body as unknown as Readable, destDir, asset.kind);\n\t\tif (!extracted.ok) return extracted;\n\n\t\tonProgress?.(\"Locating llama-server binary…\");\n\t\tconst binaryRelPath = this._findBinaryRelPath(destDir);\n\t\tif (!binaryRelPath) {\n\t\t\treturn { ok: false, error: \"binary-missing: no llama-server binary found in the extracted archive\" };\n\t\t}\n\n\t\tthis._writeManifest({ release: PRISM_LLAMACPP_PINNED_RELEASE, binaryRelPath, backend: asset.backend });\n\t\tonProgress?.(\"Prism llama.cpp runtime installed.\");\n\t\treturn { ok: true };\n\t}\n\n\tprivate async _extractArchive(\n\t\tinput: Readable,\n\t\tdestDir: string,\n\t\tkind: PrismAssetKind,\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\treturn kind === \"zip\" ? await this._extractZip(input, destDir) : await this._extractTarGz(input, destDir);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `extract-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t}\n\n\tprivate async _extractTarGz(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\tconst tarProc = spawnProcess(\"tar\", [\"-xzf\", \"-\", \"-C\", destDir], {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: [\"pipe\", \"ignore\", \"ignore\"],\n\t\t});\n\t\tconst terminationController = new AbortController();\n\t\tconst processWait = waitForChildProcessWithTermination(tarProc, {\n\t\t\tsignal: terminationController.signal,\n\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t});\n\t\ttry {\n\t\t\tawait pipeline(input, requireStdin(tarProc, \"tar\"));\n\t\t} catch (error) {\n\t\t\tterminationController.abort();\n\t\t\tawait processWait.catch(() => {});\n\t\t\tthrow error;\n\t\t}\n\t\tconst terminal = await processWait;\n\t\tif (terminal.reason === \"timeout\" || terminal.code !== 0) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\terror:\n\t\t\t\t\tterminal.reason === \"timeout\"\n\t\t\t\t\t\t? `extract-fail: tar extraction timed out after ${EXTRACTION_TIMEOUT_MS}ms`\n\t\t\t\t\t\t: `extract-fail: tar exited with code ${terminal.code ?? \"unknown\"}`,\n\t\t\t};\n\t\t}\n\t\treturn { ok: true };\n\t}\n\n\tprivate async _extractZip(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\t// Zip's central directory needs seekable file access — buffer to a temp file first.\n\t\tconst zipPath = join(destDir, \"..\", `prism-llamacpp-download-${process.pid}-${Date.now()}.zip`);\n\t\tawait pipeline(input, createWriteStream(zipPath));\n\t\ttry {\n\t\t\tconst extractCommand = this._platform() === \"win32\" && this._hasCommand(\"tar\") ? \"tar\" : \"unzip\";\n\t\t\tconst args = extractCommand === \"tar\" ? [\"-xf\", zipPath, \"-C\", destDir] : [\"-q\", zipPath, \"-d\", destDir];\n\t\t\tconst proc = spawnProcess(extractCommand, args, { detached: process.platform !== \"win32\", stdio: \"ignore\" });\n\t\t\tconst terminal = await waitForChildProcessWithTermination(proc, {\n\t\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t\t});\n\t\t\tif (terminal.reason === \"timeout\" || terminal.code !== 0) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\terror:\n\t\t\t\t\t\tterminal.reason === \"timeout\"\n\t\t\t\t\t\t\t? `extract-fail: ${extractCommand} timed out after ${EXTRACTION_TIMEOUT_MS}ms`\n\t\t\t\t\t\t\t: `extract-fail: ${extractCommand} exited with code ${terminal.code ?? \"unknown\"}`,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { ok: true };\n\t\t} finally {\n\t\t\ttry {\n\t\t\t\trmSync(zipPath, { force: true });\n\t\t\t} catch {\n\t\t\t\t// best-effort cleanup\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async _remoteContentLength(url: string): Promise<number | undefined> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(url, {\n\t\t\t\tmethod: \"HEAD\",\n\t\t\t\tsignal: AbortSignal.timeout(HEAD_REQUEST_TIMEOUT_MS),\n\t\t\t});\n\t\t\treturn response.ok ? parseContentLength(response.headers.get(\"content-length\")) : undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _fileSizeBytes(path: string): number | undefined {\n\t\ttry {\n\t\t\treturn statSync(path).size;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _cleanupPartial(path: string): void {\n\t\ttry {\n\t\t\trmSync(path, { force: true });\n\t\t} catch {\n\t\t\t// best-effort cleanup\n\t\t}\n\t}\n\n\t/**\n\t * Wait until a write stream's underlying fd is actually closed. `pipeline()` destroys the\n\t * stream on error but its promise can settle before the fd close completes (Windows in\n\t * particular keeps the file handle open a tick longer) — unlinking before that races with the\n\t * OS and can leave the \"partial\" file behind or fail the delete outright. Resolves immediately\n\t * if the stream already reports `closed`.\n\t */\n\tprivate _waitForStreamClosed(stream: Writable & { closed?: boolean }): Promise<void> {\n\t\tif (stream.closed) return Promise.resolve();\n\t\treturn new Promise((resolve) => {\n\t\t\tstream.once(\"close\", () => resolve());\n\t\t\tstream.destroy();\n\t\t});\n\t}\n\n\t/**\n\t * Stream a GGUF (or mmproj) file from Hugging Face into pi's owned models dir. Skips a re-download\n\t * when the local file already matches the remote size; verifies size when the response reports\n\t * `content-length` and deletes the partial file on any failure or mismatch — never leaves a\n\t * corrupt/truncated weight file behind for a later load to silently misread.\n\t */\n\tasync downloadModel(\n\t\targs: { repo: string; file: string },\n\t\tonProgress?: (status: string) => void,\n\t): Promise<PrismDownloadResult> {\n\t\tconst destPath = join(this.modelsDir(), args.repo, args.file);\n\t\tconst url = `https://huggingface.co/${args.repo}/resolve/main/${args.file}`;\n\t\tmkdirSync(dirname(destPath), { recursive: true });\n\n\t\tif (this._exists(destPath)) {\n\t\t\tconst remoteSize = await this._remoteContentLength(url);\n\t\t\tif (remoteSize !== undefined && remoteSize === this._fileSizeBytes(destPath)) {\n\t\t\t\tonProgress?.(`${args.file} already downloaded (${remoteSize} bytes) — skipping.`);\n\t\t\t\treturn { ok: true, path: destPath, skipped: true };\n\t\t\t}\n\t\t}\n\n\t\tonProgress?.(`Downloading ${args.file} from ${args.repo}…`);\n\t\tlet response: Response;\n\t\ttry {\n\t\t\tresponse = await this._fetch(url);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t\tif (!response.ok || !response.body) {\n\t\t\treturn { ok: false, error: `download-fail: HTTP ${response.status}` };\n\t\t}\n\n\t\tconst expectedBytes = parseContentLength(response.headers.get(\"content-length\"));\n\t\tconst writeStream = createWriteStream(destPath);\n\t\ttry {\n\t\t\tawait pipeline(response.body as unknown as Readable, writeStream);\n\t\t} catch (error) {\n\t\t\tawait this._waitForStreamClosed(writeStream);\n\t\t\tthis._cleanupPartial(destPath);\n\t\t\treturn { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\n\t\tconst actualBytes = this._fileSizeBytes(destPath);\n\t\tif (expectedBytes !== undefined && actualBytes !== expectedBytes) {\n\t\t\tthis._cleanupPartial(destPath);\n\t\t\treturn { ok: false, error: `size-mismatch: expected ${expectedBytes} bytes, got ${actualBytes ?? 0}` };\n\t\t}\n\n\t\tonProgress?.(`${args.file} downloaded (${actualBytes ?? 0} bytes).`);\n\t\treturn { ok: true, path: destPath };\n\t}\n\n\tprivate async _healthUp(baseUrl: string): Promise<boolean> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${baseUrl}/health`, {\n\t\t\t\tsignal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS),\n\t\t\t});\n\t\t\treturn response.ok;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Spawn `llama-server` detached+tracked (killed on parent shutdown even if pi crashes without\n\t * calling stop()) and poll `/health` until ready. `-ngl 99` is only added when the installed\n\t * asset's persisted `backend` is `\"cuda\"`, not from the host's current GPU state.\n\t */\n\tasync serve(args: {\n\t\tmodelPath: string;\n\t\tmmprojPath?: string;\n\t\tport: number;\n\t\tnumCtx: number;\n\t}): Promise<PrismServeResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { ok: false, error: \"binary-missing\" };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tif (!this._exists(binaryPath)) return { ok: false, error: \"binary-missing\" };\n\n\t\tconst baseUrl = `http://127.0.0.1:${args.port}`;\n\t\tconst argv = [\n\t\t\t\"-m\",\n\t\t\targs.modelPath,\n\t\t\t...(args.mmprojPath ? [\"--mmproj\", args.mmprojPath] : []),\n\t\t\t\"--host\",\n\t\t\t\"127.0.0.1\",\n\t\t\t\"--port\",\n\t\t\tString(args.port),\n\t\t\t\"-c\",\n\t\t\tString(args.numCtx),\n\t\t\t...(manifest.backend === \"cuda\" ? [\"-ngl\", \"99\"] : []),\n\t\t];\n\n\t\tconst child = this._spawn(binaryPath, argv, {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: \"ignore\",\n\t\t\tenv: process.env,\n\t\t});\n\t\tif (child.pid) trackDetachedChildPid(child.pid);\n\t\tchild.unref?.();\n\t\tchild.on(\"exit\", () => {\n\t\t\tthis._child = undefined;\n\t\t});\n\t\tthis._child = child;\n\n\t\tfor (let attempt = 0; attempt < this._healthPollAttempts; attempt++) {\n\t\t\tif (await this._healthUp(baseUrl)) return { ok: true, baseUrl };\n\t\t\tawait this._sleep(this._healthPollIntervalMs);\n\t\t}\n\t\tthis.stop();\n\t\treturn { ok: false, error: \"health-timeout\" };\n\t}\n\n\tstop(): { stopped: boolean } {\n\t\tconst child = this._child;\n\t\tif (!child) return { stopped: false };\n\t\tthis._child = undefined;\n\t\tif (child.pid) {\n\t\t\tuntrackDetachedChildPid(child.pid);\n\t\t\tkillProcessTree(child.pid);\n\t\t}\n\t\treturn { stopped: true };\n\t}\n\n\tisRunning(): boolean {\n\t\treturn this._child !== undefined;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { arch as osArch, platform as osPlatform } from "node:os";
|
|
4
|
+
import { dirname, join, relative } from "node:path";
|
|
5
|
+
import { pipeline } from "node:stream/promises";
|
|
6
|
+
import { spawnProcess, spawnProcessSync, waitForChildProcessWithTermination } from "../../utils/child-process.js";
|
|
7
|
+
import { killProcessTree, trackDetachedChildPid, untrackDetachedChildPid } from "../../utils/shell.js";
|
|
8
|
+
import { modelsDir as agentModelsDir, runtimesDir as agentRuntimesDir } from "../agent-paths.js";
|
|
9
|
+
/**
|
|
10
|
+
* Managed runtime for prism-ml 1-bit GGUF models (Bonsai-27B first). The provider requires their
|
|
11
|
+
* llama.cpp fork (Q1_0_g128 hybrid-attention kernels) — stock llama.cpp/Ollama cannot serve these
|
|
12
|
+
* weights — so this module owns runtime install (prebuilt release download+extract), GGUF
|
|
13
|
+
* downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable
|
|
14
|
+
* seams for fetch/spawn/exists, pi-owned directories under agentDir (runtimes/, models/),
|
|
15
|
+
* detached+tracked child processes, onProgress as best-effort UI feedback, honest error taxonomies
|
|
16
|
+
* instead of silent fallbacks.
|
|
17
|
+
*
|
|
18
|
+
* The fork publishes precompiled per-platform archives on GitHub Releases (no local build): pin a
|
|
19
|
+
* release tag, download the matching asset, extract it, and locate the `llama-server` binary once
|
|
20
|
+
* at install time (release archive layouts vary — bin/ vs build/bin/ at the top level — so this is
|
|
21
|
+
* a deterministic one-time scan, not a fallback). The found relative path and backend (cpu/cuda)
|
|
22
|
+
* are persisted to a manifest so later `detect()`/`serve()` calls never need to re-scan.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Pinned Prism ML llama.cpp fork release — verified via
|
|
26
|
+
* `GET https://api.github.com/repos/PrismML-Eng/llama.cpp/releases/tags/prism-b9594-38c66ad` on
|
|
27
|
+
* 2026-07-18 (release exists, all assets referenced below are present on it). Bump here (and
|
|
28
|
+
* re-verify with the same API call) when the provider ships new kernels.
|
|
29
|
+
*/
|
|
30
|
+
export const PRISM_LLAMACPP_PINNED_RELEASE = "prism-b9594-38c66ad";
|
|
31
|
+
export const PRISM_LLAMACPP_RELEASES_BASE_URL = "https://github.com/PrismML-Eng/llama.cpp/releases/download";
|
|
32
|
+
/** Curated first-model descriptor; the `/models add` wiring task consumes this. */
|
|
33
|
+
export const BONSAI_27B = {
|
|
34
|
+
repo: "prism-ml/Bonsai-27B-gguf",
|
|
35
|
+
file: "Bonsai-27B-Q1_0.gguf",
|
|
36
|
+
mmprojFile: "Bonsai-27B-mmproj-Q8_0.gguf",
|
|
37
|
+
displayName: "Bonsai-27B (1-bit Q1_0 + vision)",
|
|
38
|
+
};
|
|
39
|
+
// Verbatim asset names from the pinned release (see PRISM_LLAMACPP_PINNED_RELEASE doc comment) —
|
|
40
|
+
// hardcoded rather than templated from the tag because the provider's naming isn't fully
|
|
41
|
+
// consistent across platforms (Windows CPU assets drop the tag/commit segment entirely; the
|
|
42
|
+
// Windows CUDA asset uses a different build-number segment, "b1" not "b9594").
|
|
43
|
+
const LINUX_X64_CPU_ASSET = "llama-prism-b9594-38c66ad-bin-ubuntu-x64.tar.gz";
|
|
44
|
+
const LINUX_ARM64_CPU_ASSET = "llama-prism-b9594-38c66ad-bin-ubuntu-arm64.tar.gz";
|
|
45
|
+
const LINUX_X64_CUDA_ASSET = "llama-prism-b9594-38c66ad-bin-linux-cuda-12.4-x64.tar.gz";
|
|
46
|
+
const MACOS_ARM64_ASSET = "llama-prism-b9594-38c66ad-bin-macos-arm64.tar.gz";
|
|
47
|
+
const MACOS_X64_ASSET = "llama-prism-b9594-38c66ad-bin-macos-x64.tar.gz";
|
|
48
|
+
const WIN_X64_CPU_ASSET = "llama-bin-win-cpu-x64.zip";
|
|
49
|
+
const WIN_ARM64_CPU_ASSET = "llama-bin-win-cpu-arm64.zip";
|
|
50
|
+
/**
|
|
51
|
+
* Maps a platform/arch/GPU triple to the exact Prism llama.cpp release asset for
|
|
52
|
+
* {@link PRISM_LLAMACPP_PINNED_RELEASE} — verified against the real GitHub release, not guessed.
|
|
53
|
+
* CPU asset by default; the CUDA 12.4 variant is only offered for linux x64 with an NVIDIA GPU
|
|
54
|
+
* present (a 12.8 variant also exists upstream but has no wired caller yet). Windows CUDA needs a
|
|
55
|
+
* companion `cudart-*` archive and is out of scope for v1 — Windows always resolves to the CPU zip
|
|
56
|
+
* regardless of `hasNvidiaGpu`. Pure and exported so it's independently testable.
|
|
57
|
+
*/
|
|
58
|
+
export function resolvePrismLlamaAsset(plat, architecture, hasNvidiaGpu) {
|
|
59
|
+
if (plat === "linux") {
|
|
60
|
+
if (architecture === "x64") {
|
|
61
|
+
return hasNvidiaGpu
|
|
62
|
+
? { name: LINUX_X64_CUDA_ASSET, kind: "tar-gz", backend: "cuda" }
|
|
63
|
+
: { name: LINUX_X64_CPU_ASSET, kind: "tar-gz", backend: "cpu" };
|
|
64
|
+
}
|
|
65
|
+
if (architecture === "arm64")
|
|
66
|
+
return { name: LINUX_ARM64_CPU_ASSET, kind: "tar-gz", backend: "cpu" };
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
if (plat === "darwin") {
|
|
70
|
+
if (architecture === "arm64")
|
|
71
|
+
return { name: MACOS_ARM64_ASSET, kind: "tar-gz", backend: "cpu" };
|
|
72
|
+
if (architecture === "x64")
|
|
73
|
+
return { name: MACOS_X64_ASSET, kind: "tar-gz", backend: "cpu" };
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
if (plat === "win32") {
|
|
77
|
+
if (architecture === "x64")
|
|
78
|
+
return { name: WIN_X64_CPU_ASSET, kind: "zip", backend: "cpu" };
|
|
79
|
+
if (architecture === "arm64")
|
|
80
|
+
return { name: WIN_ARM64_CPU_ASSET, kind: "zip", backend: "cpu" };
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const EXTRACTION_TIMEOUT_MS = 10 * 60_000;
|
|
86
|
+
const COMMAND_KILL_GRACE_MS = 2_000;
|
|
87
|
+
const DEFAULT_HEALTH_POLL_ATTEMPTS = 240;
|
|
88
|
+
const DEFAULT_HEALTH_POLL_INTERVAL_MS = 500;
|
|
89
|
+
const HEALTH_CHECK_TIMEOUT_MS = 2_000;
|
|
90
|
+
const HEAD_REQUEST_TIMEOUT_MS = 10_000;
|
|
91
|
+
function parseContentLength(header) {
|
|
92
|
+
if (!header)
|
|
93
|
+
return undefined;
|
|
94
|
+
const value = Number(header);
|
|
95
|
+
return Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* `spawnProcess(..., { stdio: ["pipe", ...] })` always yields a non-null `stdin` at runtime, but
|
|
99
|
+
* child-process.ts's typed overload only narrows `.stdin` for the capture-only stdio shape used
|
|
100
|
+
* elsewhere. This makes the true invariant explicit and fails loudly instead of silently
|
|
101
|
+
* misbehaving if it's ever violated by a future stdio change.
|
|
102
|
+
*/
|
|
103
|
+
function requireStdin(proc, label) {
|
|
104
|
+
if (!proc.stdin)
|
|
105
|
+
throw new Error(`${label}: no stdin pipe`);
|
|
106
|
+
return proc.stdin;
|
|
107
|
+
}
|
|
108
|
+
export class PrismLlamaCppRuntime {
|
|
109
|
+
_agentDir;
|
|
110
|
+
_fetch;
|
|
111
|
+
_spawn;
|
|
112
|
+
_exists;
|
|
113
|
+
_sleep;
|
|
114
|
+
_hasCommand;
|
|
115
|
+
_hasNvidiaGpu;
|
|
116
|
+
_platform;
|
|
117
|
+
_arch;
|
|
118
|
+
_extractArchiveOverride;
|
|
119
|
+
_healthPollAttempts;
|
|
120
|
+
_healthPollIntervalMs;
|
|
121
|
+
_child;
|
|
122
|
+
constructor(args) {
|
|
123
|
+
this._agentDir = args.agentDir;
|
|
124
|
+
this._fetch = args.deps?.fetchFn ?? fetch;
|
|
125
|
+
this._spawn =
|
|
126
|
+
args.deps?.spawnFn ?? ((command, argv, options) => spawn(command, argv, { ...options, stdio: "ignore" }));
|
|
127
|
+
this._exists = args.deps?.existsFn ?? existsSync;
|
|
128
|
+
this._sleep = args.deps?.sleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
129
|
+
this._hasCommand =
|
|
130
|
+
args.deps?.hasCommand ??
|
|
131
|
+
((command) => spawnProcessSync(command, ["--version"], { encoding: "utf8", timeout: 5_000 }).error === undefined);
|
|
132
|
+
this._hasNvidiaGpu = args.deps?.hasNvidiaGpu ?? (() => this._hasCommand("nvidia-smi"));
|
|
133
|
+
this._platform = args.deps?.platform ?? osPlatform;
|
|
134
|
+
this._arch = args.deps?.arch ?? osArch;
|
|
135
|
+
this._extractArchiveOverride = args.deps?.extractArchive;
|
|
136
|
+
this._healthPollAttempts = args.deps?.healthPollAttempts ?? DEFAULT_HEALTH_POLL_ATTEMPTS;
|
|
137
|
+
this._healthPollIntervalMs = args.deps?.healthPollIntervalMs ?? DEFAULT_HEALTH_POLL_INTERVAL_MS;
|
|
138
|
+
}
|
|
139
|
+
runtimeDir() {
|
|
140
|
+
return agentRuntimesDir("prism-llamacpp", this._agentDir);
|
|
141
|
+
}
|
|
142
|
+
modelsDir() {
|
|
143
|
+
return agentModelsDir("llamacpp", this._agentDir);
|
|
144
|
+
}
|
|
145
|
+
_binaryName() {
|
|
146
|
+
return this._platform() === "win32" ? "llama-server.exe" : "llama-server";
|
|
147
|
+
}
|
|
148
|
+
_manifestPath() {
|
|
149
|
+
return join(this.runtimeDir(), "install.json");
|
|
150
|
+
}
|
|
151
|
+
_readManifest() {
|
|
152
|
+
try {
|
|
153
|
+
const parsed = JSON.parse(readFileSync(this._manifestPath(), "utf8"));
|
|
154
|
+
if (typeof parsed.release === "string" &&
|
|
155
|
+
typeof parsed.binaryRelPath === "string" &&
|
|
156
|
+
(parsed.backend === "cpu" || parsed.backend === "cuda")) {
|
|
157
|
+
return { release: parsed.release, binaryRelPath: parsed.binaryRelPath, backend: parsed.backend };
|
|
158
|
+
}
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
_writeManifest(manifest) {
|
|
166
|
+
writeFileSync(this._manifestPath(), JSON.stringify(manifest, null, "\t"));
|
|
167
|
+
}
|
|
168
|
+
async detect() {
|
|
169
|
+
const manifest = this._readManifest();
|
|
170
|
+
if (!manifest)
|
|
171
|
+
return { runtimeInstalled: false };
|
|
172
|
+
const binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);
|
|
173
|
+
const runtimeInstalled = this._exists(binaryPath);
|
|
174
|
+
return {
|
|
175
|
+
runtimeInstalled,
|
|
176
|
+
binaryPath: runtimeInstalled ? binaryPath : undefined,
|
|
177
|
+
release: manifest.release,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/** One-time recursive scan for the `llama-server` binary inside a freshly extracted release
|
|
181
|
+
* archive — layouts vary (bin/ vs build/bin/ at the top level) across platforms/backends, so
|
|
182
|
+
* this locates it deterministically instead of assuming a fixed path, then the result is
|
|
183
|
+
* persisted so later calls never need to re-scan. */
|
|
184
|
+
_findBinaryRelPath(root) {
|
|
185
|
+
const targetName = this._binaryName();
|
|
186
|
+
const stack = [root];
|
|
187
|
+
while (stack.length > 0) {
|
|
188
|
+
const dir = stack.pop();
|
|
189
|
+
if (dir === undefined)
|
|
190
|
+
continue;
|
|
191
|
+
let entries;
|
|
192
|
+
try {
|
|
193
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
for (const entry of entries) {
|
|
199
|
+
const full = join(dir, entry.name);
|
|
200
|
+
if (entry.isDirectory())
|
|
201
|
+
stack.push(full);
|
|
202
|
+
else if (entry.isFile() && entry.name === targetName)
|
|
203
|
+
return relative(root, full);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Download the pinned release asset for this host and extract it (consent-gated by the caller,
|
|
210
|
+
* same contract as OllamaRuntime#installManaged — this method only does the mechanical
|
|
211
|
+
* download+extract+locate and reports the outcome honestly). No compiler toolchain required:
|
|
212
|
+
* the fork ships prebuilt binaries.
|
|
213
|
+
*/
|
|
214
|
+
async installManaged(onProgress) {
|
|
215
|
+
const asset = resolvePrismLlamaAsset(this._platform(), this._arch(), this._hasNvidiaGpu());
|
|
216
|
+
if (!asset)
|
|
217
|
+
return { ok: false, error: "unsupported-platform" };
|
|
218
|
+
onProgress?.(`Downloading ${asset.name}…`);
|
|
219
|
+
const downloadUrl = `${PRISM_LLAMACPP_RELEASES_BASE_URL}/${PRISM_LLAMACPP_PINNED_RELEASE}/${asset.name}`;
|
|
220
|
+
let response;
|
|
221
|
+
try {
|
|
222
|
+
response = await this._fetch(downloadUrl);
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
return { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };
|
|
226
|
+
}
|
|
227
|
+
if (!response.ok || !response.body) {
|
|
228
|
+
return { ok: false, error: `download-fail: HTTP ${response.status}` };
|
|
229
|
+
}
|
|
230
|
+
const destDir = this.runtimeDir();
|
|
231
|
+
mkdirSync(destDir, { recursive: true });
|
|
232
|
+
onProgress?.(`Extracting ${asset.name}…`);
|
|
233
|
+
const extract = this._extractArchiveOverride ?? ((input, dest, kind) => this._extractArchive(input, dest, kind));
|
|
234
|
+
const extracted = await extract(response.body, destDir, asset.kind);
|
|
235
|
+
if (!extracted.ok)
|
|
236
|
+
return extracted;
|
|
237
|
+
onProgress?.("Locating llama-server binary…");
|
|
238
|
+
const binaryRelPath = this._findBinaryRelPath(destDir);
|
|
239
|
+
if (!binaryRelPath) {
|
|
240
|
+
return { ok: false, error: "binary-missing: no llama-server binary found in the extracted archive" };
|
|
241
|
+
}
|
|
242
|
+
this._writeManifest({ release: PRISM_LLAMACPP_PINNED_RELEASE, binaryRelPath, backend: asset.backend });
|
|
243
|
+
onProgress?.("Prism llama.cpp runtime installed.");
|
|
244
|
+
return { ok: true };
|
|
245
|
+
}
|
|
246
|
+
async _extractArchive(input, destDir, kind) {
|
|
247
|
+
try {
|
|
248
|
+
return kind === "zip" ? await this._extractZip(input, destDir) : await this._extractTarGz(input, destDir);
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
return { ok: false, error: `extract-fail: ${error instanceof Error ? error.message : String(error)}` };
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async _extractTarGz(input, destDir) {
|
|
255
|
+
const tarProc = spawnProcess("tar", ["-xzf", "-", "-C", destDir], {
|
|
256
|
+
detached: process.platform !== "win32",
|
|
257
|
+
stdio: ["pipe", "ignore", "ignore"],
|
|
258
|
+
});
|
|
259
|
+
const terminationController = new AbortController();
|
|
260
|
+
const processWait = waitForChildProcessWithTermination(tarProc, {
|
|
261
|
+
signal: terminationController.signal,
|
|
262
|
+
timeoutMs: EXTRACTION_TIMEOUT_MS,
|
|
263
|
+
killGraceMs: COMMAND_KILL_GRACE_MS,
|
|
264
|
+
});
|
|
265
|
+
try {
|
|
266
|
+
await pipeline(input, requireStdin(tarProc, "tar"));
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
terminationController.abort();
|
|
270
|
+
await processWait.catch(() => { });
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
const terminal = await processWait;
|
|
274
|
+
if (terminal.reason === "timeout" || terminal.code !== 0) {
|
|
275
|
+
return {
|
|
276
|
+
ok: false,
|
|
277
|
+
error: terminal.reason === "timeout"
|
|
278
|
+
? `extract-fail: tar extraction timed out after ${EXTRACTION_TIMEOUT_MS}ms`
|
|
279
|
+
: `extract-fail: tar exited with code ${terminal.code ?? "unknown"}`,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return { ok: true };
|
|
283
|
+
}
|
|
284
|
+
async _extractZip(input, destDir) {
|
|
285
|
+
// Zip's central directory needs seekable file access — buffer to a temp file first.
|
|
286
|
+
const zipPath = join(destDir, "..", `prism-llamacpp-download-${process.pid}-${Date.now()}.zip`);
|
|
287
|
+
await pipeline(input, createWriteStream(zipPath));
|
|
288
|
+
try {
|
|
289
|
+
const extractCommand = this._platform() === "win32" && this._hasCommand("tar") ? "tar" : "unzip";
|
|
290
|
+
const args = extractCommand === "tar" ? ["-xf", zipPath, "-C", destDir] : ["-q", zipPath, "-d", destDir];
|
|
291
|
+
const proc = spawnProcess(extractCommand, args, { detached: process.platform !== "win32", stdio: "ignore" });
|
|
292
|
+
const terminal = await waitForChildProcessWithTermination(proc, {
|
|
293
|
+
timeoutMs: EXTRACTION_TIMEOUT_MS,
|
|
294
|
+
killGraceMs: COMMAND_KILL_GRACE_MS,
|
|
295
|
+
});
|
|
296
|
+
if (terminal.reason === "timeout" || terminal.code !== 0) {
|
|
297
|
+
return {
|
|
298
|
+
ok: false,
|
|
299
|
+
error: terminal.reason === "timeout"
|
|
300
|
+
? `extract-fail: ${extractCommand} timed out after ${EXTRACTION_TIMEOUT_MS}ms`
|
|
301
|
+
: `extract-fail: ${extractCommand} exited with code ${terminal.code ?? "unknown"}`,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
return { ok: true };
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
try {
|
|
308
|
+
rmSync(zipPath, { force: true });
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
// best-effort cleanup
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
async _remoteContentLength(url) {
|
|
316
|
+
try {
|
|
317
|
+
const response = await this._fetch(url, {
|
|
318
|
+
method: "HEAD",
|
|
319
|
+
signal: AbortSignal.timeout(HEAD_REQUEST_TIMEOUT_MS),
|
|
320
|
+
});
|
|
321
|
+
return response.ok ? parseContentLength(response.headers.get("content-length")) : undefined;
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
_fileSizeBytes(path) {
|
|
328
|
+
try {
|
|
329
|
+
return statSync(path).size;
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
_cleanupPartial(path) {
|
|
336
|
+
try {
|
|
337
|
+
rmSync(path, { force: true });
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
// best-effort cleanup
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Wait until a write stream's underlying fd is actually closed. `pipeline()` destroys the
|
|
345
|
+
* stream on error but its promise can settle before the fd close completes (Windows in
|
|
346
|
+
* particular keeps the file handle open a tick longer) — unlinking before that races with the
|
|
347
|
+
* OS and can leave the "partial" file behind or fail the delete outright. Resolves immediately
|
|
348
|
+
* if the stream already reports `closed`.
|
|
349
|
+
*/
|
|
350
|
+
_waitForStreamClosed(stream) {
|
|
351
|
+
if (stream.closed)
|
|
352
|
+
return Promise.resolve();
|
|
353
|
+
return new Promise((resolve) => {
|
|
354
|
+
stream.once("close", () => resolve());
|
|
355
|
+
stream.destroy();
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Stream a GGUF (or mmproj) file from Hugging Face into pi's owned models dir. Skips a re-download
|
|
360
|
+
* when the local file already matches the remote size; verifies size when the response reports
|
|
361
|
+
* `content-length` and deletes the partial file on any failure or mismatch — never leaves a
|
|
362
|
+
* corrupt/truncated weight file behind for a later load to silently misread.
|
|
363
|
+
*/
|
|
364
|
+
async downloadModel(args, onProgress) {
|
|
365
|
+
const destPath = join(this.modelsDir(), args.repo, args.file);
|
|
366
|
+
const url = `https://huggingface.co/${args.repo}/resolve/main/${args.file}`;
|
|
367
|
+
mkdirSync(dirname(destPath), { recursive: true });
|
|
368
|
+
if (this._exists(destPath)) {
|
|
369
|
+
const remoteSize = await this._remoteContentLength(url);
|
|
370
|
+
if (remoteSize !== undefined && remoteSize === this._fileSizeBytes(destPath)) {
|
|
371
|
+
onProgress?.(`${args.file} already downloaded (${remoteSize} bytes) — skipping.`);
|
|
372
|
+
return { ok: true, path: destPath, skipped: true };
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
onProgress?.(`Downloading ${args.file} from ${args.repo}…`);
|
|
376
|
+
let response;
|
|
377
|
+
try {
|
|
378
|
+
response = await this._fetch(url);
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
return { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };
|
|
382
|
+
}
|
|
383
|
+
if (!response.ok || !response.body) {
|
|
384
|
+
return { ok: false, error: `download-fail: HTTP ${response.status}` };
|
|
385
|
+
}
|
|
386
|
+
const expectedBytes = parseContentLength(response.headers.get("content-length"));
|
|
387
|
+
const writeStream = createWriteStream(destPath);
|
|
388
|
+
try {
|
|
389
|
+
await pipeline(response.body, writeStream);
|
|
390
|
+
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
await this._waitForStreamClosed(writeStream);
|
|
393
|
+
this._cleanupPartial(destPath);
|
|
394
|
+
return { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };
|
|
395
|
+
}
|
|
396
|
+
const actualBytes = this._fileSizeBytes(destPath);
|
|
397
|
+
if (expectedBytes !== undefined && actualBytes !== expectedBytes) {
|
|
398
|
+
this._cleanupPartial(destPath);
|
|
399
|
+
return { ok: false, error: `size-mismatch: expected ${expectedBytes} bytes, got ${actualBytes ?? 0}` };
|
|
400
|
+
}
|
|
401
|
+
onProgress?.(`${args.file} downloaded (${actualBytes ?? 0} bytes).`);
|
|
402
|
+
return { ok: true, path: destPath };
|
|
403
|
+
}
|
|
404
|
+
async _healthUp(baseUrl) {
|
|
405
|
+
try {
|
|
406
|
+
const response = await this._fetch(`${baseUrl}/health`, {
|
|
407
|
+
signal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS),
|
|
408
|
+
});
|
|
409
|
+
return response.ok;
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Spawn `llama-server` detached+tracked (killed on parent shutdown even if pi crashes without
|
|
417
|
+
* calling stop()) and poll `/health` until ready. `-ngl 99` is only added when the installed
|
|
418
|
+
* asset's persisted `backend` is `"cuda"`, not from the host's current GPU state.
|
|
419
|
+
*/
|
|
420
|
+
async serve(args) {
|
|
421
|
+
const manifest = this._readManifest();
|
|
422
|
+
if (!manifest)
|
|
423
|
+
return { ok: false, error: "binary-missing" };
|
|
424
|
+
const binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);
|
|
425
|
+
if (!this._exists(binaryPath))
|
|
426
|
+
return { ok: false, error: "binary-missing" };
|
|
427
|
+
const baseUrl = `http://127.0.0.1:${args.port}`;
|
|
428
|
+
const argv = [
|
|
429
|
+
"-m",
|
|
430
|
+
args.modelPath,
|
|
431
|
+
...(args.mmprojPath ? ["--mmproj", args.mmprojPath] : []),
|
|
432
|
+
"--host",
|
|
433
|
+
"127.0.0.1",
|
|
434
|
+
"--port",
|
|
435
|
+
String(args.port),
|
|
436
|
+
"-c",
|
|
437
|
+
String(args.numCtx),
|
|
438
|
+
...(manifest.backend === "cuda" ? ["-ngl", "99"] : []),
|
|
439
|
+
];
|
|
440
|
+
const child = this._spawn(binaryPath, argv, {
|
|
441
|
+
detached: process.platform !== "win32",
|
|
442
|
+
stdio: "ignore",
|
|
443
|
+
env: process.env,
|
|
444
|
+
});
|
|
445
|
+
if (child.pid)
|
|
446
|
+
trackDetachedChildPid(child.pid);
|
|
447
|
+
child.unref?.();
|
|
448
|
+
child.on("exit", () => {
|
|
449
|
+
this._child = undefined;
|
|
450
|
+
});
|
|
451
|
+
this._child = child;
|
|
452
|
+
for (let attempt = 0; attempt < this._healthPollAttempts; attempt++) {
|
|
453
|
+
if (await this._healthUp(baseUrl))
|
|
454
|
+
return { ok: true, baseUrl };
|
|
455
|
+
await this._sleep(this._healthPollIntervalMs);
|
|
456
|
+
}
|
|
457
|
+
this.stop();
|
|
458
|
+
return { ok: false, error: "health-timeout" };
|
|
459
|
+
}
|
|
460
|
+
stop() {
|
|
461
|
+
const child = this._child;
|
|
462
|
+
if (!child)
|
|
463
|
+
return { stopped: false };
|
|
464
|
+
this._child = undefined;
|
|
465
|
+
if (child.pid) {
|
|
466
|
+
untrackDetachedChildPid(child.pid);
|
|
467
|
+
killProcessTree(child.pid);
|
|
468
|
+
}
|
|
469
|
+
return { stopped: true };
|
|
470
|
+
}
|
|
471
|
+
isRunning() {
|
|
472
|
+
return this._child !== undefined;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
//# sourceMappingURL=llamacpp-runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llamacpp-runtime.js","sourceRoot":"","sources":["../../../src/core/models/llamacpp-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EACN,iBAAiB,EAEjB,UAAU,EACV,SAAS,EACT,WAAW,EACX,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,aAAa,GACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,IAAI,IAAI,MAAM,EAAE,QAAQ,IAAI,UAAU,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEpD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,kCAAkC,EAAE,MAAM,8BAA8B,CAAC;AAClH,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AACvG,OAAO,EAAE,SAAS,IAAI,cAAc,E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eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC5B,CAAC;QACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAAA,CACzB;IAED,SAAS,GAAY;QACpB,OAAO,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC;IAAA,CACjC;CACD","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport {\n\tcreateWriteStream,\n\ttype Dirent,\n\texistsSync,\n\tmkdirSync,\n\treaddirSync,\n\treadFileSync,\n\trmSync,\n\tstatSync,\n\twriteFileSync,\n} from \"node:fs\";\nimport { arch as osArch, platform as osPlatform } from \"node:os\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { Readable, Writable } from \"node:stream\";\nimport { pipeline } from \"node:stream/promises\";\nimport { spawnProcess, spawnProcessSync, waitForChildProcessWithTermination } from \"../../utils/child-process.ts\";\nimport { killProcessTree, trackDetachedChildPid, untrackDetachedChildPid } from \"../../utils/shell.ts\";\nimport { modelsDir as agentModelsDir, runtimesDir as agentRuntimesDir } from \"../agent-paths.ts\";\n\n/**\n * Managed runtime for prism-ml 1-bit GGUF models (Bonsai-27B first). The provider requires their\n * llama.cpp fork (Q1_0_g128 hybrid-attention kernels) — stock llama.cpp/Ollama cannot serve these\n * weights — so this module owns runtime install (prebuilt release download+extract), GGUF\n * downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable\n * seams for fetch/spawn/exists, pi-owned directories under agentDir (runtimes/, models/),\n * detached+tracked child processes, onProgress as best-effort UI feedback, honest error taxonomies\n * instead of silent fallbacks.\n *\n * The fork publishes precompiled per-platform archives on GitHub Releases (no local build): pin a\n * release tag, download the matching asset, extract it, and locate the `llama-server` binary once\n * at install time (release archive layouts vary — bin/ vs build/bin/ at the top level — so this is\n * a deterministic one-time scan, not a fallback). The found relative path and backend (cpu/cuda)\n * are persisted to a manifest so later `detect()`/`serve()` calls never need to re-scan.\n */\n\n/**\n * Pinned Prism ML llama.cpp fork release — verified via\n * `GET https://api.github.com/repos/PrismML-Eng/llama.cpp/releases/tags/prism-b9594-38c66ad` on\n * 2026-07-18 (release exists, all assets referenced below are present on it). Bump here (and\n * re-verify with the same API call) when the provider ships new kernels.\n */\nexport const PRISM_LLAMACPP_PINNED_RELEASE = \"prism-b9594-38c66ad\";\n\nexport const PRISM_LLAMACPP_RELEASES_BASE_URL = \"https://github.com/PrismML-Eng/llama.cpp/releases/download\";\n\nexport interface PrismModelDescriptor {\n\trepo: string;\n\tfile: string;\n\tmmprojFile?: string;\n\tdisplayName: string;\n}\n\n/** Curated first-model descriptor; the `/models add` wiring task consumes this. */\nexport const BONSAI_27B: PrismModelDescriptor = {\n\trepo: \"prism-ml/Bonsai-27B-gguf\",\n\tfile: \"Bonsai-27B-Q1_0.gguf\",\n\tmmprojFile: \"Bonsai-27B-mmproj-Q8_0.gguf\",\n\tdisplayName: \"Bonsai-27B (1-bit Q1_0 + vision)\",\n};\n\nexport type PrismBackend = \"cpu\" | \"cuda\";\nexport type PrismAssetKind = \"tar-gz\" | \"zip\";\n\nexport interface PrismLlamaAsset {\n\tname: string;\n\tkind: PrismAssetKind;\n\tbackend: PrismBackend;\n}\n\n// Verbatim asset names from the pinned release (see PRISM_LLAMACPP_PINNED_RELEASE doc comment) —\n// hardcoded rather than templated from the tag because the provider's naming isn't fully\n// consistent across platforms (Windows CPU assets drop the tag/commit segment entirely; the\n// Windows CUDA asset uses a different build-number segment, \"b1\" not \"b9594\").\nconst LINUX_X64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-x64.tar.gz\";\nconst LINUX_ARM64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-arm64.tar.gz\";\nconst LINUX_X64_CUDA_ASSET = \"llama-prism-b9594-38c66ad-bin-linux-cuda-12.4-x64.tar.gz\";\nconst MACOS_ARM64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-arm64.tar.gz\";\nconst MACOS_X64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-x64.tar.gz\";\nconst WIN_X64_CPU_ASSET = \"llama-bin-win-cpu-x64.zip\";\nconst WIN_ARM64_CPU_ASSET = \"llama-bin-win-cpu-arm64.zip\";\n\n/**\n * Maps a platform/arch/GPU triple to the exact Prism llama.cpp release asset for\n * {@link PRISM_LLAMACPP_PINNED_RELEASE} — verified against the real GitHub release, not guessed.\n * CPU asset by default; the CUDA 12.4 variant is only offered for linux x64 with an NVIDIA GPU\n * present (a 12.8 variant also exists upstream but has no wired caller yet). Windows CUDA needs a\n * companion `cudart-*` archive and is out of scope for v1 — Windows always resolves to the CPU zip\n * regardless of `hasNvidiaGpu`. Pure and exported so it's independently testable.\n */\nexport function resolvePrismLlamaAsset(\n\tplat: string,\n\tarchitecture: string,\n\thasNvidiaGpu: boolean,\n): PrismLlamaAsset | undefined {\n\tif (plat === \"linux\") {\n\t\tif (architecture === \"x64\") {\n\t\t\treturn hasNvidiaGpu\n\t\t\t\t? { name: LINUX_X64_CUDA_ASSET, kind: \"tar-gz\", backend: \"cuda\" }\n\t\t\t\t: { name: LINUX_X64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\t}\n\t\tif (architecture === \"arm64\") return { name: LINUX_ARM64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"darwin\") {\n\t\tif (architecture === \"arm64\") return { name: MACOS_ARM64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\tif (architecture === \"x64\") return { name: MACOS_X64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"win32\") {\n\t\tif (architecture === \"x64\") return { name: WIN_X64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\tif (architecture === \"arm64\") return { name: WIN_ARM64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\treturn undefined;\n}\n\nexport interface PrismDetectResult {\n\truntimeInstalled: boolean;\n\tbinaryPath?: string;\n\trelease?: string;\n}\n\nexport interface PrismDownloadResult {\n\tok: boolean;\n\tpath?: string;\n\tskipped?: boolean;\n\terror?: string;\n}\n\nexport type PrismServeResult = { ok: true; baseUrl: string } | { ok: false; error: string };\n\ninterface PrismInstallManifest {\n\trelease: string;\n\tbinaryRelPath: string;\n\tbackend: PrismBackend;\n}\n\ntype PrismSpawnOptions = { detached?: boolean; stdio?: \"ignore\"; env?: NodeJS.ProcessEnv };\ntype PrismSpawnFn = (\n\tcommand: string,\n\targs: string[],\n\toptions: PrismSpawnOptions,\n) => Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\">;\ntype PrismExtractArchiveFn = (\n\tinput: Readable,\n\tdestDir: string,\n\tkind: PrismAssetKind,\n) => Promise<{ ok: boolean; error?: string }>;\n\nexport interface PrismLlamaCppDeps {\n\tfetchFn?: typeof fetch;\n\tspawnFn?: PrismSpawnFn;\n\texistsFn?: (path: string) => boolean;\n\tsleepFn?: (ms: number) => Promise<void>;\n\t/** Whether a named command exists on PATH (nvidia-smi, tar — for zip extraction on Windows). */\n\thasCommand?: (command: string) => boolean;\n\t/** Decided once at install time and persisted as `backend` — serve() reads the persisted value\n\t * so `-ngl 99` reflects what was actually installed, not the host's current GPU state. */\n\thasNvidiaGpu?: () => boolean;\n\tplatform?: () => string;\n\tarch?: () => string;\n\t/** Runs the extraction step for a downloaded archive. Injectable so installManaged's\n\t * download->extract->scan orchestration is testable without a real tar/unzip pipeline; defaults\n\t * to the real spawn-based extractor. */\n\textractArchive?: PrismExtractArchiveFn;\n\t/** Health-poll bounds for serve(); default ~120s (240 * 500ms), both overridable for tests. */\n\thealthPollAttempts?: number;\n\thealthPollIntervalMs?: number;\n}\n\nconst EXTRACTION_TIMEOUT_MS = 10 * 60_000;\nconst COMMAND_KILL_GRACE_MS = 2_000;\nconst DEFAULT_HEALTH_POLL_ATTEMPTS = 240;\nconst DEFAULT_HEALTH_POLL_INTERVAL_MS = 500;\nconst HEALTH_CHECK_TIMEOUT_MS = 2_000;\nconst HEAD_REQUEST_TIMEOUT_MS = 10_000;\n\nfunction parseContentLength(header: string | null): number | undefined {\n\tif (!header) return undefined;\n\tconst value = Number(header);\n\treturn Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\n/**\n * `spawnProcess(..., { stdio: [\"pipe\", ...] })` always yields a non-null `stdin` at runtime, but\n * child-process.ts's typed overload only narrows `.stdin` for the capture-only stdio shape used\n * elsewhere. This makes the true invariant explicit and fails loudly instead of silently\n * misbehaving if it's ever violated by a future stdio change.\n */\nfunction requireStdin(proc: ChildProcess, label: string): Writable {\n\tif (!proc.stdin) throw new Error(`${label}: no stdin pipe`);\n\treturn proc.stdin;\n}\n\nexport class PrismLlamaCppRuntime {\n\tprivate readonly _agentDir: string;\n\tprivate readonly _fetch: typeof fetch;\n\tprivate readonly _spawn: PrismSpawnFn;\n\tprivate readonly _exists: (path: string) => boolean;\n\tprivate readonly _sleep: (ms: number) => Promise<void>;\n\tprivate readonly _hasCommand: (command: string) => boolean;\n\tprivate readonly _hasNvidiaGpu: () => boolean;\n\tprivate readonly _platform: () => string;\n\tprivate readonly _arch: () => string;\n\tprivate readonly _extractArchiveOverride: PrismExtractArchiveFn | undefined;\n\tprivate readonly _healthPollAttempts: number;\n\tprivate readonly _healthPollIntervalMs: number;\n\tprivate _child: Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\"> | undefined;\n\n\tconstructor(args: { agentDir: string; deps?: PrismLlamaCppDeps }) {\n\t\tthis._agentDir = args.agentDir;\n\t\tthis._fetch = args.deps?.fetchFn ?? fetch;\n\t\tthis._spawn =\n\t\t\targs.deps?.spawnFn ?? ((command, argv, options) => spawn(command, argv, { ...options, stdio: \"ignore\" }));\n\t\tthis._exists = args.deps?.existsFn ?? existsSync;\n\t\tthis._sleep = args.deps?.sleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));\n\t\tthis._hasCommand =\n\t\t\targs.deps?.hasCommand ??\n\t\t\t((command) =>\n\t\t\t\tspawnProcessSync(command, [\"--version\"], { encoding: \"utf8\", timeout: 5_000 }).error === undefined);\n\t\tthis._hasNvidiaGpu = args.deps?.hasNvidiaGpu ?? (() => this._hasCommand(\"nvidia-smi\"));\n\t\tthis._platform = args.deps?.platform ?? osPlatform;\n\t\tthis._arch = args.deps?.arch ?? osArch;\n\t\tthis._extractArchiveOverride = args.deps?.extractArchive;\n\t\tthis._healthPollAttempts = args.deps?.healthPollAttempts ?? DEFAULT_HEALTH_POLL_ATTEMPTS;\n\t\tthis._healthPollIntervalMs = args.deps?.healthPollIntervalMs ?? DEFAULT_HEALTH_POLL_INTERVAL_MS;\n\t}\n\n\truntimeDir(): string {\n\t\treturn agentRuntimesDir(\"prism-llamacpp\", this._agentDir);\n\t}\n\n\tmodelsDir(): string {\n\t\treturn agentModelsDir(\"llamacpp\", this._agentDir);\n\t}\n\n\tprivate _binaryName(): string {\n\t\treturn this._platform() === \"win32\" ? \"llama-server.exe\" : \"llama-server\";\n\t}\n\n\tprivate _manifestPath(): string {\n\t\treturn join(this.runtimeDir(), \"install.json\");\n\t}\n\n\tprivate _readManifest(): PrismInstallManifest | undefined {\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(readFileSync(this._manifestPath(), \"utf8\")) as {\n\t\t\t\trelease?: unknown;\n\t\t\t\tbinaryRelPath?: unknown;\n\t\t\t\tbackend?: unknown;\n\t\t\t};\n\t\t\tif (\n\t\t\t\ttypeof parsed.release === \"string\" &&\n\t\t\t\ttypeof parsed.binaryRelPath === \"string\" &&\n\t\t\t\t(parsed.backend === \"cpu\" || parsed.backend === \"cuda\")\n\t\t\t) {\n\t\t\t\treturn { release: parsed.release, binaryRelPath: parsed.binaryRelPath, backend: parsed.backend };\n\t\t\t}\n\t\t\treturn undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _writeManifest(manifest: PrismInstallManifest): void {\n\t\twriteFileSync(this._manifestPath(), JSON.stringify(manifest, null, \"\\t\"));\n\t}\n\n\tasync detect(): Promise<PrismDetectResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { runtimeInstalled: false };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tconst runtimeInstalled = this._exists(binaryPath);\n\t\treturn {\n\t\t\truntimeInstalled,\n\t\t\tbinaryPath: runtimeInstalled ? binaryPath : undefined,\n\t\t\trelease: manifest.release,\n\t\t};\n\t}\n\n\t/** One-time recursive scan for the `llama-server` binary inside a freshly extracted release\n\t * archive — layouts vary (bin/ vs build/bin/ at the top level) across platforms/backends, so\n\t * this locates it deterministically instead of assuming a fixed path, then the result is\n\t * persisted so later calls never need to re-scan. */\n\tprivate _findBinaryRelPath(root: string): string | undefined {\n\t\tconst targetName = this._binaryName();\n\t\tconst stack: string[] = [root];\n\t\twhile (stack.length > 0) {\n\t\t\tconst dir = stack.pop();\n\t\t\tif (dir === undefined) continue;\n\t\t\tlet entries: Dirent[];\n\t\t\ttry {\n\t\t\t\tentries = readdirSync(dir, { withFileTypes: true });\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const entry of entries) {\n\t\t\t\tconst full = join(dir, entry.name);\n\t\t\t\tif (entry.isDirectory()) stack.push(full);\n\t\t\t\telse if (entry.isFile() && entry.name === targetName) return relative(root, full);\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Download the pinned release asset for this host and extract it (consent-gated by the caller,\n\t * same contract as OllamaRuntime#installManaged — this method only does the mechanical\n\t * download+extract+locate and reports the outcome honestly). No compiler toolchain required:\n\t * the fork ships prebuilt binaries.\n\t */\n\tasync installManaged(onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\tconst asset = resolvePrismLlamaAsset(this._platform(), this._arch(), this._hasNvidiaGpu());\n\t\tif (!asset) return { ok: false, error: \"unsupported-platform\" };\n\n\t\tonProgress?.(`Downloading ${asset.name}…`);\n\t\tconst downloadUrl = `${PRISM_LLAMACPP_RELEASES_BASE_URL}/${PRISM_LLAMACPP_PINNED_RELEASE}/${asset.name}`;\n\t\tlet response: Response;\n\t\ttry {\n\t\t\tresponse = await this._fetch(downloadUrl);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t\tif (!response.ok || !response.body) {\n\t\t\treturn { ok: false, error: `download-fail: HTTP ${response.status}` };\n\t\t}\n\n\t\tconst destDir = this.runtimeDir();\n\t\tmkdirSync(destDir, { recursive: true });\n\n\t\tonProgress?.(`Extracting ${asset.name}…`);\n\t\tconst extract = this._extractArchiveOverride ?? ((input, dest, kind) => this._extractArchive(input, dest, kind));\n\t\tconst extracted = await extract(response.body as unknown as Readable, destDir, asset.kind);\n\t\tif (!extracted.ok) return extracted;\n\n\t\tonProgress?.(\"Locating llama-server binary…\");\n\t\tconst binaryRelPath = this._findBinaryRelPath(destDir);\n\t\tif (!binaryRelPath) {\n\t\t\treturn { ok: false, error: \"binary-missing: no llama-server binary found in the extracted archive\" };\n\t\t}\n\n\t\tthis._writeManifest({ release: PRISM_LLAMACPP_PINNED_RELEASE, binaryRelPath, backend: asset.backend });\n\t\tonProgress?.(\"Prism llama.cpp runtime installed.\");\n\t\treturn { ok: true };\n\t}\n\n\tprivate async _extractArchive(\n\t\tinput: Readable,\n\t\tdestDir: string,\n\t\tkind: PrismAssetKind,\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\treturn kind === \"zip\" ? await this._extractZip(input, destDir) : await this._extractTarGz(input, destDir);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `extract-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t}\n\n\tprivate async _extractTarGz(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\tconst tarProc = spawnProcess(\"tar\", [\"-xzf\", \"-\", \"-C\", destDir], {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: [\"pipe\", \"ignore\", \"ignore\"],\n\t\t});\n\t\tconst terminationController = new AbortController();\n\t\tconst processWait = waitForChildProcessWithTermination(tarProc, {\n\t\t\tsignal: terminationController.signal,\n\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t});\n\t\ttry {\n\t\t\tawait pipeline(input, requireStdin(tarProc, \"tar\"));\n\t\t} catch (error) {\n\t\t\tterminationController.abort();\n\t\t\tawait processWait.catch(() => {});\n\t\t\tthrow error;\n\t\t}\n\t\tconst terminal = await processWait;\n\t\tif (terminal.reason === \"timeout\" || terminal.code !== 0) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\terror:\n\t\t\t\t\tterminal.reason === \"timeout\"\n\t\t\t\t\t\t? `extract-fail: tar extraction timed out after ${EXTRACTION_TIMEOUT_MS}ms`\n\t\t\t\t\t\t: `extract-fail: tar exited with code ${terminal.code ?? \"unknown\"}`,\n\t\t\t};\n\t\t}\n\t\treturn { ok: true };\n\t}\n\n\tprivate async _extractZip(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\t// Zip's central directory needs seekable file access — buffer to a temp file first.\n\t\tconst zipPath = join(destDir, \"..\", `prism-llamacpp-download-${process.pid}-${Date.now()}.zip`);\n\t\tawait pipeline(input, createWriteStream(zipPath));\n\t\ttry {\n\t\t\tconst extractCommand = this._platform() === \"win32\" && this._hasCommand(\"tar\") ? \"tar\" : \"unzip\";\n\t\t\tconst args = extractCommand === \"tar\" ? [\"-xf\", zipPath, \"-C\", destDir] : [\"-q\", zipPath, \"-d\", destDir];\n\t\t\tconst proc = spawnProcess(extractCommand, args, { detached: process.platform !== \"win32\", stdio: \"ignore\" });\n\t\t\tconst terminal = await waitForChildProcessWithTermination(proc, {\n\t\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t\t});\n\t\t\tif (terminal.reason === \"timeout\" || terminal.code !== 0) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\terror:\n\t\t\t\t\t\tterminal.reason === \"timeout\"\n\t\t\t\t\t\t\t? `extract-fail: ${extractCommand} timed out after ${EXTRACTION_TIMEOUT_MS}ms`\n\t\t\t\t\t\t\t: `extract-fail: ${extractCommand} exited with code ${terminal.code ?? \"unknown\"}`,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { ok: true };\n\t\t} finally {\n\t\t\ttry {\n\t\t\t\trmSync(zipPath, { force: true });\n\t\t\t} catch {\n\t\t\t\t// best-effort cleanup\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async _remoteContentLength(url: string): Promise<number | undefined> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(url, {\n\t\t\t\tmethod: \"HEAD\",\n\t\t\t\tsignal: AbortSignal.timeout(HEAD_REQUEST_TIMEOUT_MS),\n\t\t\t});\n\t\t\treturn response.ok ? parseContentLength(response.headers.get(\"content-length\")) : undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _fileSizeBytes(path: string): number | undefined {\n\t\ttry {\n\t\t\treturn statSync(path).size;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _cleanupPartial(path: string): void {\n\t\ttry {\n\t\t\trmSync(path, { force: true });\n\t\t} catch {\n\t\t\t// best-effort cleanup\n\t\t}\n\t}\n\n\t/**\n\t * Wait until a write stream's underlying fd is actually closed. `pipeline()` destroys the\n\t * stream on error but its promise can settle before the fd close completes (Windows in\n\t * particular keeps the file handle open a tick longer) — unlinking before that races with the\n\t * OS and can leave the \"partial\" file behind or fail the delete outright. Resolves immediately\n\t * if the stream already reports `closed`.\n\t */\n\tprivate _waitForStreamClosed(stream: Writable & { closed?: boolean }): Promise<void> {\n\t\tif (stream.closed) return Promise.resolve();\n\t\treturn new Promise((resolve) => {\n\t\t\tstream.once(\"close\", () => resolve());\n\t\t\tstream.destroy();\n\t\t});\n\t}\n\n\t/**\n\t * Stream a GGUF (or mmproj) file from Hugging Face into pi's owned models dir. Skips a re-download\n\t * when the local file already matches the remote size; verifies size when the response reports\n\t * `content-length` and deletes the partial file on any failure or mismatch — never leaves a\n\t * corrupt/truncated weight file behind for a later load to silently misread.\n\t */\n\tasync downloadModel(\n\t\targs: { repo: string; file: string },\n\t\tonProgress?: (status: string) => void,\n\t): Promise<PrismDownloadResult> {\n\t\tconst destPath = join(this.modelsDir(), args.repo, args.file);\n\t\tconst url = `https://huggingface.co/${args.repo}/resolve/main/${args.file}`;\n\t\tmkdirSync(dirname(destPath), { recursive: true });\n\n\t\tif (this._exists(destPath)) {\n\t\t\tconst remoteSize = await this._remoteContentLength(url);\n\t\t\tif (remoteSize !== undefined && remoteSize === this._fileSizeBytes(destPath)) {\n\t\t\t\tonProgress?.(`${args.file} already downloaded (${remoteSize} bytes) — skipping.`);\n\t\t\t\treturn { ok: true, path: destPath, skipped: true };\n\t\t\t}\n\t\t}\n\n\t\tonProgress?.(`Downloading ${args.file} from ${args.repo}…`);\n\t\tlet response: Response;\n\t\ttry {\n\t\t\tresponse = await this._fetch(url);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t\tif (!response.ok || !response.body) {\n\t\t\treturn { ok: false, error: `download-fail: HTTP ${response.status}` };\n\t\t}\n\n\t\tconst expectedBytes = parseContentLength(response.headers.get(\"content-length\"));\n\t\tconst writeStream = createWriteStream(destPath);\n\t\ttry {\n\t\t\tawait pipeline(response.body as unknown as Readable, writeStream);\n\t\t} catch (error) {\n\t\t\tawait this._waitForStreamClosed(writeStream);\n\t\t\tthis._cleanupPartial(destPath);\n\t\t\treturn { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\n\t\tconst actualBytes = this._fileSizeBytes(destPath);\n\t\tif (expectedBytes !== undefined && actualBytes !== expectedBytes) {\n\t\t\tthis._cleanupPartial(destPath);\n\t\t\treturn { ok: false, error: `size-mismatch: expected ${expectedBytes} bytes, got ${actualBytes ?? 0}` };\n\t\t}\n\n\t\tonProgress?.(`${args.file} downloaded (${actualBytes ?? 0} bytes).`);\n\t\treturn { ok: true, path: destPath };\n\t}\n\n\tprivate async _healthUp(baseUrl: string): Promise<boolean> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${baseUrl}/health`, {\n\t\t\t\tsignal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS),\n\t\t\t});\n\t\t\treturn response.ok;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Spawn `llama-server` detached+tracked (killed on parent shutdown even if pi crashes without\n\t * calling stop()) and poll `/health` until ready. `-ngl 99` is only added when the installed\n\t * asset's persisted `backend` is `\"cuda\"`, not from the host's current GPU state.\n\t */\n\tasync serve(args: {\n\t\tmodelPath: string;\n\t\tmmprojPath?: string;\n\t\tport: number;\n\t\tnumCtx: number;\n\t}): Promise<PrismServeResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { ok: false, error: \"binary-missing\" };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tif (!this._exists(binaryPath)) return { ok: false, error: \"binary-missing\" };\n\n\t\tconst baseUrl = `http://127.0.0.1:${args.port}`;\n\t\tconst argv = [\n\t\t\t\"-m\",\n\t\t\targs.modelPath,\n\t\t\t...(args.mmprojPath ? [\"--mmproj\", args.mmprojPath] : []),\n\t\t\t\"--host\",\n\t\t\t\"127.0.0.1\",\n\t\t\t\"--port\",\n\t\t\tString(args.port),\n\t\t\t\"-c\",\n\t\t\tString(args.numCtx),\n\t\t\t...(manifest.backend === \"cuda\" ? [\"-ngl\", \"99\"] : []),\n\t\t];\n\n\t\tconst child = this._spawn(binaryPath, argv, {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: \"ignore\",\n\t\t\tenv: process.env,\n\t\t});\n\t\tif (child.pid) trackDetachedChildPid(child.pid);\n\t\tchild.unref?.();\n\t\tchild.on(\"exit\", () => {\n\t\t\tthis._child = undefined;\n\t\t});\n\t\tthis._child = child;\n\n\t\tfor (let attempt = 0; attempt < this._healthPollAttempts; attempt++) {\n\t\t\tif (await this._healthUp(baseUrl)) return { ok: true, baseUrl };\n\t\t\tawait this._sleep(this._healthPollIntervalMs);\n\t\t}\n\t\tthis.stop();\n\t\treturn { ok: false, error: \"health-timeout\" };\n\t}\n\n\tstop(): { stopped: boolean } {\n\t\tconst child = this._child;\n\t\tif (!child) return { stopped: false };\n\t\tthis._child = undefined;\n\t\tif (child.pid) {\n\t\t\tuntrackDetachedChildPid(child.pid);\n\t\t\tkillProcessTree(child.pid);\n\t\t}\n\t\treturn { stopped: true };\n\t}\n\n\tisRunning(): boolean {\n\t\treturn this._child !== undefined;\n\t}\n}\n"]}
|