gentle-pi 4.8.0 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -0
- package/assets/agents/gentle-ai-explore.md +1 -1
- package/assets/agents/gentle-ai-verify.md +1 -1
- package/assets/agents/gentle-ai-worker.md +10 -9
- package/assets/design-canvas/canvas-boards.js +22 -2
- package/assets/design-canvas/canvas-boot.js +3 -1
- package/assets/design-canvas/canvas-i18n-es.js +5 -0
- package/assets/design-canvas/canvas-menu.js +13 -7
- package/assets/design-canvas/canvas-messages.js +1 -1
- package/assets/design-canvas/canvas-rail.js +5 -2
- package/assets/design-canvas/canvas-store.js +3 -0
- package/assets/design-canvas/canvas-theme.js +69 -0
- package/assets/design-canvas/canvas.css +27 -2
- package/assets/design-canvas/index.html +3 -0
- package/assets/design-canvas/inspector.js +28 -5
- package/assets/design-canvas/theme-early.js +10 -0
- package/assets/migrations/agent-body-history.json +6 -0
- package/assets/orchestrator-delegation.md +1 -5
- package/assets/orchestrator-full-delegation-card.md +9 -13
- package/assets/orchestrator-full-delegation.md +2 -2
- package/assets/orchestrator-memory.md +2 -2
- package/assets/orchestrator-prompts.md +3 -31
- package/assets/orchestrator-tracking.md +6 -6
- package/assets/orchestrator-verification.md +4 -4
- package/assets/orchestrator-writer.md +12 -12
- package/assets/orchestrator.md +2 -2
- package/bin/gentle-shell.mjs +77 -1
- package/contracts/review-provider-contract-mirror/provider-contract.lock.json +7 -7
- package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/manifest.json +3 -3
- package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/orchestration/pi.md +1 -1
- package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/routing/pi.md +10 -10
- package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/generated/provider-capabilities.baseline.json +3 -3
- package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/generated/provider-roles.baseline.json +1 -1
- package/docs/assets/features/gentle-tower.png +0 -0
- package/docs/auto-permission-answers.md +1 -1
- package/docs/bundled-install.md +1 -1
- package/docs/delegated-verification.md +6 -4
- package/docs/desktop-feed.md +303 -0
- package/docs/desktop-rpc.md +703 -25
- package/docs/fast-mode.md +2 -2
- package/docs/gentle-shell-desktop.md +465 -0
- package/docs/gentle-shell.md +65 -3
- package/docs/images/desktop/chat.png +0 -0
- package/docs/images/desktop/palette.png +0 -0
- package/docs/images/desktop/todo.png +0 -0
- package/docs/install-wizard.md +1 -1
- package/docs/odd-preparation-continuity.md +1 -1
- package/docs/readme-reference.md +44 -15
- package/docs/work-continuity.md +10 -7
- package/extensions/ask-user-choice.ts +36 -18
- package/extensions/ask-user-question.ts +47 -8
- package/extensions/gentle-agents.ts +206 -37
- package/extensions/gentle-ai.ts +234 -24
- package/extensions/gentle-desktop-feed.ts +778 -0
- package/extensions/gentle-desktop-rpc.ts +145 -12
- package/extensions/gentle-herdr-desktop-status.ts +275 -0
- package/extensions/gentle-shell.ts +69 -7
- package/extensions/gentle-stats.ts +30 -1
- package/extensions/gentle-tower.ts +305 -0
- package/extensions/skill-registry.ts +13 -3
- package/herdr/RECIPE.md +157 -0
- package/herdr/keys.json +14 -0
- package/herdr/layouts/gentle-shell.json +202 -0
- package/herdr/mods/gentle-agents/icons/kind-herdr.svg +1 -0
- package/herdr/mods/gentle-agents/icons/kind-remote.svg +1 -0
- package/herdr/mods/gentle-agents/icons/kind-tui.svg +1 -0
- package/herdr/mods/gentle-agents/main.js +190 -0
- package/herdr/mods/gentle-agents/mod.json +1 -0
- package/herdr/mods/gentle-agents/snapshot.js +469 -0
- package/herdr/mods/gentle-agents/tokens.js +273 -0
- package/herdr/mods/gentle-chat/MINIMAL.md +69 -0
- package/herdr/mods/gentle-chat/actions.js +180 -0
- package/herdr/mods/gentle-chat/feed.js +321 -0
- package/herdr/mods/gentle-chat/icons/branch.svg +1 -0
- package/herdr/mods/gentle-chat/icons/chat.svg +1 -0
- package/herdr/mods/gentle-chat/icons/check.svg +1 -0
- package/herdr/mods/gentle-chat/icons/chev.svg +1 -0
- package/herdr/mods/gentle-chat/icons/clip.svg +1 -0
- package/herdr/mods/gentle-chat/icons/down.svg +1 -0
- package/herdr/mods/gentle-chat/icons/folder.svg +1 -0
- package/herdr/mods/gentle-chat/icons/kind-herdr.svg +1 -0
- package/herdr/mods/gentle-chat/icons/kind-remote.svg +1 -0
- package/herdr/mods/gentle-chat/icons/kind-tui.svg +1 -0
- package/herdr/mods/gentle-chat/icons/mark.svg +1 -0
- package/herdr/mods/gentle-chat/icons/send.svg +1 -0
- package/herdr/mods/gentle-chat/icons/shield.svg +1 -0
- package/herdr/mods/gentle-chat/icons/stop.svg +1 -0
- package/herdr/mods/gentle-chat/icons/term.svg +1 -0
- package/herdr/mods/gentle-chat/icons/user.svg +1 -0
- package/herdr/mods/gentle-chat/icons/x.svg +1 -0
- package/herdr/mods/gentle-chat/main.js +968 -0
- package/herdr/mods/gentle-chat/mod.json +1 -0
- package/herdr/mods/gentle-chat/session.js +212 -0
- package/herdr/mods/gentle-chat/snapshot.js +469 -0
- package/herdr/mods/gentle-chat/tokens.js +137 -0
- package/herdr/mods/gentle-chat/ui.js +883 -0
- package/herdr/mods/gentle-confirm/main.js +60 -0
- package/herdr/mods/gentle-confirm/mod.json +1 -0
- package/herdr/mods/gentle-inspector/actions.js +180 -0
- package/herdr/mods/gentle-inspector/icons/bolt.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/bot.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/check.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/db.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/diff.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/jobs.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/plug.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/review.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/stats.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/stop.svg +1 -0
- package/herdr/mods/gentle-inspector/icons/todo.svg +1 -0
- package/herdr/mods/gentle-inspector/main.js +500 -0
- package/herdr/mods/gentle-inspector/mod.json +1 -0
- package/herdr/mods/gentle-inspector/snapshot.js +469 -0
- package/herdr/mods/gentle-inspector/tokens.js +273 -0
- package/herdr/mods/gentle-palette/actions.js +180 -0
- package/herdr/mods/gentle-palette/icons/bolt.svg +1 -0
- package/herdr/mods/gentle-palette/icons/bot.svg +1 -0
- package/herdr/mods/gentle-palette/icons/chat.svg +1 -0
- package/herdr/mods/gentle-palette/icons/check.svg +1 -0
- package/herdr/mods/gentle-palette/icons/cmd.svg +1 -0
- package/herdr/mods/gentle-palette/icons/diff.svg +1 -0
- package/herdr/mods/gentle-palette/icons/jobs.svg +1 -0
- package/herdr/mods/gentle-palette/icons/plus.svg +1 -0
- package/herdr/mods/gentle-palette/icons/review.svg +1 -0
- package/herdr/mods/gentle-palette/icons/search.svg +1 -0
- package/herdr/mods/gentle-palette/icons/shield.svg +1 -0
- package/herdr/mods/gentle-palette/icons/stats.svg +1 -0
- package/herdr/mods/gentle-palette/icons/stop.svg +1 -0
- package/herdr/mods/gentle-palette/icons/term.svg +1 -0
- package/herdr/mods/gentle-palette/icons/todo.svg +1 -0
- package/herdr/mods/gentle-palette/main.js +333 -0
- package/herdr/mods/gentle-palette/mod.json +1 -0
- package/herdr/mods/gentle-palette/snapshot.js +469 -0
- package/herdr/mods/gentle-palette/tokens.js +273 -0
- package/herdr/mods/gentle-pane/icons/chat.svg +1 -0
- package/herdr/mods/gentle-pane/icons/close.svg +1 -0
- package/herdr/mods/gentle-pane/icons/kind-herdr.svg +1 -0
- package/herdr/mods/gentle-pane/icons/kind-remote.svg +1 -0
- package/herdr/mods/gentle-pane/icons/kind-tui.svg +1 -0
- package/herdr/mods/gentle-pane/icons/split-down.svg +1 -0
- package/herdr/mods/gentle-pane/icons/split-right.svg +1 -0
- package/herdr/mods/gentle-pane/icons/term.svg +1 -0
- package/herdr/mods/gentle-pane/icons/unzoom.svg +1 -0
- package/herdr/mods/gentle-pane/icons/zoom.svg +1 -0
- package/herdr/mods/gentle-pane/main.js +91 -0
- package/herdr/mods/gentle-pane/mod.json +1 -0
- package/herdr/mods/gentle-pane/snapshot.js +469 -0
- package/herdr/mods/gentle-pane/tokens.js +273 -0
- package/herdr/mods/gentle-sidebar/actions.js +180 -0
- package/herdr/mods/gentle-sidebar/icons/gear.svg +1 -0
- package/herdr/mods/gentle-sidebar/icons/mark.svg +1 -0
- package/herdr/mods/gentle-sidebar/icons/refresh.svg +1 -0
- package/herdr/mods/gentle-sidebar/icons/server-plus.svg +1 -0
- package/herdr/mods/gentle-sidebar/main.js +171 -0
- package/herdr/mods/gentle-sidebar/mod.json +1 -0
- package/herdr/mods/gentle-sidebar/snapshot.js +469 -0
- package/herdr/mods/gentle-sidebar/tokens.js +273 -0
- package/herdr/mods/gentle-spaces/icons/kind-herdr.svg +1 -0
- package/herdr/mods/gentle-spaces/icons/kind-remote.svg +1 -0
- package/herdr/mods/gentle-spaces/icons/kind-tui.svg +1 -0
- package/herdr/mods/gentle-spaces/icons/monitor.svg +1 -0
- package/herdr/mods/gentle-spaces/icons/plus.svg +1 -0
- package/herdr/mods/gentle-spaces/icons/server.svg +1 -0
- package/herdr/mods/gentle-spaces/machines.js +31 -0
- package/herdr/mods/gentle-spaces/main.js +299 -0
- package/herdr/mods/gentle-spaces/mod.json +1 -0
- package/herdr/mods/gentle-spaces/snapshot.js +469 -0
- package/herdr/mods/gentle-spaces/tokens.js +273 -0
- package/herdr/mods/gentle-status/actions.js +180 -0
- package/herdr/mods/gentle-status/main.js +192 -0
- package/herdr/mods/gentle-status/mod.json +1 -0
- package/herdr/mods/gentle-status/snapshot.js +469 -0
- package/herdr/mods/gentle-status/tokens.js +273 -0
- package/herdr/mods/gentle-tabs/icons/close.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/kind-herdr.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/kind-remote.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/kind-tui.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/plus.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/win-close.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/win-max.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/win-min.svg +1 -0
- package/herdr/mods/gentle-tabs/icons/win-restore.svg +1 -0
- package/herdr/mods/gentle-tabs/main.js +171 -0
- package/herdr/mods/gentle-tabs/mod.json +1 -0
- package/herdr/mods/gentle-tabs/snapshot.js +469 -0
- package/herdr/mods/gentle-tabs/tokens.js +273 -0
- package/herdr/mods/gentle-tokens/main.js +20 -0
- package/herdr/mods/gentle-tokens/mod.json +1 -0
- package/herdr/mods/gentle-tokens/snapshot.js +469 -0
- package/herdr/mods/gentle-tokens/tokens.js +273 -0
- package/herdr/preview.png +0 -0
- package/herdr/themes/gentle-shell-dark.json +33 -0
- package/herdr/themes/gentle-shell-light.json +34 -0
- package/lib/agents-protocol.ts +2 -0
- package/lib/agents-rpc-publisher.ts +39 -12
- package/lib/agents-runner.ts +1 -0
- package/lib/auto-permission-answers.ts +2 -1
- package/lib/background-jobs.ts +22 -2
- package/lib/bounded-writer-admission.ts +1 -1
- package/lib/change-diff.ts +104 -0
- package/lib/commit-slice-signal.ts +1 -1
- package/lib/design-canvas-assets.ts +2 -0
- package/lib/desktop-agents-service.ts +31 -10
- package/lib/desktop-changes-service.ts +171 -0
- package/lib/desktop-feed-actions.ts +804 -0
- package/lib/desktop-feed-answers.ts +129 -0
- package/lib/desktop-feed-sources.ts +184 -0
- package/lib/desktop-feed-stats.ts +98 -0
- package/lib/desktop-feed-store.ts +124 -0
- package/lib/desktop-feed.ts +788 -0
- package/lib/desktop-jobs-service.ts +34 -7
- package/lib/desktop-review-service.ts +187 -0
- package/lib/desktop-rpc-agents.ts +112 -3
- package/lib/desktop-rpc-changes.ts +223 -0
- package/lib/desktop-rpc-config-watch.ts +285 -0
- package/lib/desktop-rpc-config.ts +162 -8
- package/lib/desktop-rpc-jobs.ts +118 -9
- package/lib/desktop-rpc-permissions.ts +154 -3
- package/lib/desktop-rpc-review.ts +180 -0
- package/lib/desktop-rpc-session.ts +203 -0
- package/lib/desktop-rpc-sessions.ts +250 -0
- package/lib/desktop-rpc-stats.ts +187 -0
- package/lib/desktop-rpc-updates.ts +143 -0
- package/lib/desktop-rpc.ts +116 -5
- package/lib/desktop-session-service.ts +223 -0
- package/lib/desktop-sessions-service.ts +249 -0
- package/lib/desktop-stats-service.ts +142 -0
- package/lib/desktop-updates-service.ts +268 -0
- package/lib/gentle-shell-desktop.ts +1370 -0
- package/lib/gentle-shell-launcher.ts +45 -3
- package/lib/herdr-desktop-events.ts +40 -0
- package/lib/herdr-desktop-status.ts +229 -0
- package/lib/jobs-sidebar-state.ts +5 -0
- package/lib/native-review-cli.ts +7 -0
- package/lib/openai-fast-tier.ts +1 -0
- package/lib/session-profile-freeze.ts +13 -5
- package/lib/stats-collector.ts +56 -27
- package/lib/tower-actions.ts +100 -0
- package/lib/tower-hub.ts +73 -0
- package/lib/tower-launch.ts +111 -0
- package/lib/tower-open.ts +99 -0
- package/lib/tower-presence.ts +70 -0
- package/lib/tower-server.ts +234 -0
- package/lib/tower-snapshot.ts +298 -0
- package/lib/update-status.ts +238 -0
- package/lib/verifier-settle-reminder.ts +1 -1
- package/package.json +13 -4
- package/runtime/design-canvas-assets.mjs +2 -0
- package/runtime/gentle-shell-desktop.mjs +1371 -0
- package/runtime/gentle-shell-launcher.mjs +44 -2
- package/runtime/native-review-cli.mjs +7 -0
- package/runtime/tower-actions.mjs +101 -0
- package/runtime/tower-hub.mjs +74 -0
- package/runtime/tower-launch.mjs +112 -0
- package/runtime/tower-open.mjs +100 -0
- package/runtime/tower-presence.mjs +71 -0
- package/runtime/tower-server.mjs +234 -0
- package/runtime/tower-snapshot.mjs +299 -0
- package/scripts/build-full-delegation-card.mjs +1 -21
- package/scripts/build-runtime-modules.mjs +9 -0
- package/scripts/gentle-ai-installer.mjs +10 -10
- package/scripts/verify-package-files.mjs +35 -15
- package/skills/provider-defect-report/SKILL.md +61 -0
- package/skills/rdd-defect-workflow/SKILL.md +3 -3
- package/tests/agent-prompt-diet-contract.test.ts +2 -2
- package/tests/append-system-prompt-route.test.ts +16 -5
- package/tests/ask-user-choice.test.ts +78 -21
- package/tests/ask-user-question.test.ts +59 -5
- package/tests/auto-permission-answers.test.ts +12 -1
- package/tests/background-jobs.test.ts +12 -0
- package/tests/bounded-stop-contract.test.ts +139 -26
- package/tests/change-diff.test.ts +75 -0
- package/tests/commit-slice-signal.test.ts +3 -2
- package/tests/decision-routing-contract.test.ts +390 -0
- package/tests/delegated-key-learnings-contract.test.ts +21 -28
- package/tests/delivery-b-rendered-surfaces.test.ts +32 -0
- package/tests/design-canvas-i18n.test.ts +24 -22
- package/tests/design-canvas-inspector.test.ts +67 -0
- package/tests/design-canvas-modules.test.ts +3 -2
- package/tests/design-canvas-rail.test.ts +10 -10
- package/tests/design-canvas-store.test.ts +9 -0
- package/tests/design-canvas-theme.test.ts +198 -0
- package/tests/design-canvas-tokens.test.ts +1 -1
- package/tests/desktop-feed-actions-guards.test.ts +595 -0
- package/tests/desktop-feed-actions.test.ts +761 -0
- package/tests/desktop-feed-live-work.test.ts +209 -0
- package/tests/desktop-feed-performance.test.ts +369 -0
- package/tests/desktop-feed-plan-profiles.test.ts +509 -0
- package/tests/desktop-feed-send-prompt.test.ts +303 -0
- package/tests/desktop-feed-tool-summaries.test.ts +69 -0
- package/tests/desktop-feed.test.ts +615 -0
- package/tests/desktop-rpc-agents.test.ts +428 -2
- package/tests/desktop-rpc-changes.test.ts +608 -0
- package/tests/desktop-rpc-config-watch.test.ts +1159 -0
- package/tests/desktop-rpc-config.test.ts +11 -1
- package/tests/desktop-rpc-design.test.ts +11 -1
- package/tests/desktop-rpc-jobs.test.ts +402 -5
- package/tests/desktop-rpc-permissions.test.ts +11 -1
- package/tests/desktop-rpc-review.test.ts +678 -0
- package/tests/desktop-rpc-session.test.ts +642 -0
- package/tests/desktop-rpc-sessions.test.ts +647 -0
- package/tests/desktop-rpc-stats.test.ts +431 -0
- package/tests/desktop-rpc-updates.test.ts +525 -0
- package/tests/desktop-rpc-usage.test.ts +11 -1
- package/tests/desktop-rpc.test.ts +43 -2
- package/tests/fixtures/primary-session-prompt/gentleman.golden.md +11 -13
- package/tests/fixtures/session-profile-v1/replay.ts +4 -1
- package/tests/full-delegation-closure.test.ts +20 -1
- package/tests/gentle-agents.test.ts +519 -4
- package/tests/gentle-ai-binary.test.ts +1 -1
- package/tests/gentle-ai-installer.test.ts +52 -52
- package/tests/gentle-ai.test.ts +75 -16
- package/tests/gentle-fast.test.ts +15 -0
- package/tests/gentle-herdr-desktop-status.test.ts +595 -0
- package/tests/gentle-shell-desktop-bin.test.ts +125 -0
- package/tests/gentle-shell-desktop.test.ts +927 -0
- package/tests/gentle-shell-launcher-tower.test.ts +104 -0
- package/tests/gentle-shell.test.ts +3 -0
- package/tests/gentle-tower.test.ts +578 -0
- package/tests/herdr-desktop-status.test.ts +285 -0
- package/tests/independent-recovery-delivery.test.ts +16 -4
- package/tests/installer-server.test.ts +2 -2
- package/tests/lazy-module-contradictions-contract.test.ts +27 -4
- package/tests/maintainer/desktop-rpc.maintest.ts +279 -4
- package/tests/native-review-capability-contract.test.ts +9 -1
- package/tests/no-self-pause-contract.test.ts +23 -13
- package/tests/odd-routing-canonical-ratchet.test.ts +10 -0
- package/tests/odd-routing-contract.test.ts +19 -3
- package/tests/orchestrator-budget.test.ts +3 -1
- package/tests/orchestrator-prompt-budget.test.ts +2 -1
- package/tests/orchestrator-prompt-diet-contract.test.ts +31 -3
- package/tests/package-manifest.test.ts +10 -10
- package/tests/pi-routing-measured-values.test.ts +24 -0
- package/tests/primary-session-prompt.test.ts +25 -0
- package/tests/profile-fast-tier.test.ts +66 -1
- package/tests/provider-defect-handoff.test.ts +118 -100
- package/tests/review-contract-prompt.test.ts +2 -1
- package/tests/review-due-public-boundary.test.ts +2 -1
- package/tests/sensitive-path-tools.test.ts +138 -4
- package/tests/session-profile-extension.test.ts +310 -16
- package/tests/session-profile-freeze.test.ts +26 -0
- package/tests/session-profile-v1-compat.test.ts +20 -0
- package/tests/skill-collision-prefixes.test.ts +1 -0
- package/tests/skill-contradictions-contract.test.ts +5 -3
- package/tests/skill-registry.test.ts +54 -0
- package/tests/stats-collector.test.ts +109 -2
- package/tests/support/design-canvas-dom.ts +17 -0
- package/tests/task-size-routing-contract.test.ts +7 -3
- package/tests/tower-actions.test.ts +247 -0
- package/tests/tower-artifacts.test.ts +114 -0
- package/tests/tower-open.test.ts +97 -0
- package/tests/tower-presence.test.ts +175 -0
- package/tests/tower-server.test.ts +122 -0
- package/tests/tower-snapshot.test.ts +236 -0
- package/tests/tower-web-echo.test.ts +125 -0
- package/tests/tower-web-hover.test.ts +251 -0
- package/tests/verification-scoped-stop-contract.test.ts +32 -0
- package/tests/verification-tiers-contract.test.ts +5 -5
- package/tests/verifier-settle-reminder.test.ts +2 -2
- package/tests/verify-first-launch-contract.test.ts +2 -2
- package/tests/work-continuity-contract.test.ts +83 -17
- package/tests/writer-rejection-next-step.test.ts +55 -0
- package/tests/writer-surface-authority-contract.test.ts +158 -0
- /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/README.md +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/schemas/lens.schema.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/schemas/refuter.schema.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/schemas/targeted-validator.schema.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/vectors/lens.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/vectors/refuter.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.5.0 → v1.6.0}/bundle/vectors/targeted-validator.json +0 -0
package/lib/tower-hub.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Gentle Tower hub: one private JSON file per live session, written atomically.
|
|
2
|
+
// Directory 0700, files 0600, write to a temp file then rename (repo convention).
|
|
3
|
+
|
|
4
|
+
import { chmodSync, lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { isTowerSnapshot, TOWER_SNAPSHOT_MAX_BYTES, type TowerSnapshot } from "./tower-snapshot.ts";
|
|
8
|
+
|
|
9
|
+
export { TOWER_SNAPSHOT_MAX_BYTES };
|
|
10
|
+
|
|
11
|
+
/** `GENTLE_TOWER_HOME` overrides the hub for tests and isolated runs. */
|
|
12
|
+
export function towerHubDir(env: NodeJS.ProcessEnv = process.env, home: string = homedir()): string {
|
|
13
|
+
return env.GENTLE_TOWER_HOME || join(home, ".gentle-shell", "tower", "sessions");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function safeId(sessionId: string): string {
|
|
17
|
+
if (!/^[A-Za-z0-9._-]{1,128}$/.test(sessionId) || sessionId.startsWith(".")) throw new Error(`Gentle Tower: unsafe session id ${JSON.stringify(sessionId)}`);
|
|
18
|
+
return sessionId;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function privateDir(dir: string): void {
|
|
22
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
23
|
+
const st = lstatSync(dir);
|
|
24
|
+
if (!st.isDirectory() || st.isSymbolicLink()) throw new Error(`Gentle Tower: hub is not a directory: ${dir}`);
|
|
25
|
+
if ((st.mode & 0o077) !== 0) chmodSync(dir, 0o700);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function writeTowerSnapshot(dir: string, snapshot: TowerSnapshot): void {
|
|
29
|
+
const id = safeId(snapshot.sessionId);
|
|
30
|
+
const body = JSON.stringify(snapshot);
|
|
31
|
+
if (Buffer.byteLength(body) > TOWER_SNAPSHOT_MAX_BYTES) throw new Error(`Gentle Tower: snapshot too large (${Buffer.byteLength(body)} bytes)`);
|
|
32
|
+
privateDir(dir);
|
|
33
|
+
const target = join(dir, `${id}.json`);
|
|
34
|
+
const temp = join(dir, `.${id}.${process.pid}.${Date.now()}.tmp`);
|
|
35
|
+
try {
|
|
36
|
+
writeFileSync(temp, body, { mode: 0o600, flag: "wx" });
|
|
37
|
+
renameSync(temp, target);
|
|
38
|
+
} finally {
|
|
39
|
+
rmSync(temp, { force: true });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function removeTowerSnapshot(dir: string, sessionId: string): void {
|
|
44
|
+
rmSync(join(dir, `${safeId(sessionId)}.json`), { force: true });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function alive(pid: number | undefined): boolean {
|
|
48
|
+
if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0) return true;
|
|
49
|
+
try { process.kill(pid, 0); return true; } catch (e) { return (e as NodeJS.ErrnoException).code === "EPERM"; }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Snapshots whose publishing process is still running (one killed without shutdown left a frozen file). */
|
|
53
|
+
export function readLiveTowerSnapshots(dir: string): TowerSnapshot[] {
|
|
54
|
+
return readTowerSnapshots(dir).filter((s) => alive(s.pid));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Every valid snapshot in the hub; unreadable, oversized or foreign files are skipped. */
|
|
58
|
+
export function readTowerSnapshots(dir: string): TowerSnapshot[] {
|
|
59
|
+
let names: string[];
|
|
60
|
+
try { names = readdirSync(dir); } catch { return []; }
|
|
61
|
+
const out: TowerSnapshot[] = [];
|
|
62
|
+
for (const name of names.sort()) {
|
|
63
|
+
if (!name.endsWith(".json") || name.startsWith(".")) continue;
|
|
64
|
+
try {
|
|
65
|
+
const path = join(dir, name);
|
|
66
|
+
const st = lstatSync(path);
|
|
67
|
+
if (!st.isFile() || st.size > TOWER_SNAPSHOT_MAX_BYTES) continue;
|
|
68
|
+
const value = JSON.parse(readFileSync(path, "utf8"));
|
|
69
|
+
if (isTowerSnapshot(value) && `${value.sessionId}.json` === name) out.push(value);
|
|
70
|
+
} catch { /* skip */ }
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Gentle Tower: open a new floor = start a new Gentle Shell session in a new
|
|
2
|
+
// terminal window, in a project already in the tower. Fixed command, every
|
|
3
|
+
// value quoted for the shell that reads it (sh on macOS/Linux, cmd on
|
|
4
|
+
// Windows); the first message is one argument and never reaches a command line.
|
|
5
|
+
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { chmodSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
/** POSIX single-quote: safe for any byte except NUL. */
|
|
12
|
+
export function shellQuote(value: string): string {
|
|
13
|
+
if (value.includes("\0")) throw new Error("Gentle Tower: NUL in a shell argument");
|
|
14
|
+
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface LaunchSpec { cwd: string; node: string; launcher: string; prompt?: string }
|
|
18
|
+
|
|
19
|
+
export function buildLaunchScript(spec: LaunchSpec): string {
|
|
20
|
+
// `--` for gentle-shell, then `--` for pi: a message starting with "-" is never an option.
|
|
21
|
+
const args = [spec.node, spec.launcher, ...(spec.prompt ? ["--", "--", spec.prompt] : [])].map(shellQuote).join(" ");
|
|
22
|
+
// The script removes itself first: it runs once and leaves no copy of the message behind.
|
|
23
|
+
return `#!/bin/sh\nrm -f "$0"\ncd ${shellQuote(spec.cwd)} && exec ${args}\n`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** cmd reads at most 8191 characters per line. */
|
|
27
|
+
const CMD_LINE_MAX = 8191;
|
|
28
|
+
|
|
29
|
+
function cmdSafe(value: string, what: string, quotes: boolean): string {
|
|
30
|
+
// A line break ends the command; other control characters have no safe spelling in a batch file.
|
|
31
|
+
if (/[\0-\x08\n-\x1f\x7f]/.test(value)) throw new Error(`Gentle Tower: the ${what} has a line break or control character, which cmd cannot pass safely`);
|
|
32
|
+
if (!quotes && value.includes('"')) throw new Error(`Gentle Tower: the ${what} has a double quote, which cmd cannot pass safely`);
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* One argument for a batch-file line, read first by cmd and then by the C runtime of the program.
|
|
38
|
+
* Inside double quotes cmd keeps `& | < > ^ ( )` literal; `%` is the one character a batch file
|
|
39
|
+
* still expands there, so it is written `%%`. A quote is written `""`: cmd stays inside the quotes and
|
|
40
|
+
* the C runtime reads one literal `"`; backslashes before a quote or at the end are doubled for it.
|
|
41
|
+
*/
|
|
42
|
+
function cmdArg(value: string): string {
|
|
43
|
+
const escaped = value.replace(/(\\*)"/g, '$1$1""').replace(/(\\+)$/, "$1$1");
|
|
44
|
+
return `"${escaped.replace(/%/g, "%%")}"`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The Windows launcher (a `.cmd` file, CRLF). Delayed expansion is turned off so `!` stays literal,
|
|
49
|
+
* the code page is UTF-8 so the message keeps its accents, and the script deletes itself at the end:
|
|
50
|
+
* a running batch file cannot remove itself before its last line.
|
|
51
|
+
*/
|
|
52
|
+
export function buildWindowsLaunchScript(spec: LaunchSpec): string {
|
|
53
|
+
const cwd = cmdSafe(spec.cwd, "project folder", false);
|
|
54
|
+
const node = cmdSafe(spec.node, "Node path", false);
|
|
55
|
+
const launcher = cmdSafe(spec.launcher, "launcher path", false);
|
|
56
|
+
const prompt = spec.prompt === undefined ? undefined : cmdSafe(spec.prompt, "first message", true);
|
|
57
|
+
// `--` for gentle-shell, then `--` for pi: a message starting with "-" is never an option.
|
|
58
|
+
const args = [node, launcher, ...(prompt ? ["--", "--", prompt] : [])].map(cmdArg).join(" ");
|
|
59
|
+
const run = `cd /d "${cwd.replace(/%/g, "%%")}" && ${args}`;
|
|
60
|
+
if (run.length > CMD_LINE_MAX) throw new Error(`Gentle Tower: the command is longer than cmd's ${CMD_LINE_MAX}-character line limit`);
|
|
61
|
+
return ["@echo off", "setlocal DisableDelayedExpansion", "chcp 65001 >nul", run, `(goto) 2>nul & del "%~f0"`, ""].join("\r\n");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface LaunchDeps { tmpRoot?: string; platform?: NodeJS.Platform; spawn?: typeof spawn }
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Opens the script in a new terminal window: macOS Terminal, the Linux default terminal
|
|
68
|
+
* (x-terminal-emulator), or on Windows Windows Terminal, else a cmd window. Never throws: every failure is a rejection.
|
|
69
|
+
*/
|
|
70
|
+
export async function launchInTerminal(spec: LaunchSpec, deps: LaunchDeps = {}): Promise<void> {
|
|
71
|
+
const platform = deps.platform ?? process.platform;
|
|
72
|
+
const run = deps.spawn ?? spawn;
|
|
73
|
+
if (platform === "win32") {
|
|
74
|
+
const body = buildWindowsLaunchScript(spec);
|
|
75
|
+
const dir = mkdtempSync(join(deps.tmpRoot ?? tmpdir(), "gentle-tower-floor-"));
|
|
76
|
+
writeFileSync(join(dir, "new-floor.cmd"), body, { mode: 0o700, flag: "wx" });
|
|
77
|
+
// Every starter runs in the script's folder and names it by a fixed relative name, so no
|
|
78
|
+
// user text and no path ever reaches a command line that cmd parses.
|
|
79
|
+
const starter = (cmd: string, args: string[], verbatim: boolean) => new Promise<void>((resolve, reject) => {
|
|
80
|
+
const child = run(cmd, args, { stdio: "ignore", cwd: dir, windowsVerbatimArguments: verbatim });
|
|
81
|
+
child.once("error", reject);
|
|
82
|
+
// wt.exe and `cmd /c start` hand the window off and exit: their exit code is the answer.
|
|
83
|
+
child.once("exit", (code) => (code === 0 ? resolve() : reject(new Error(`Gentle Tower: ${cmd} exited with ${code}`))));
|
|
84
|
+
});
|
|
85
|
+
// Windows Terminal splits its own arguments on `;`, so a folder with one goes straight to cmd.
|
|
86
|
+
if (!dir.includes(";")) {
|
|
87
|
+
try {
|
|
88
|
+
return await starter("wt.exe", ["-d", dir, "cmd", "/d", "/c", ".\\new-floor.cmd"], false);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
// Only a missing Windows Terminal falls back; one that started and failed is not retried in a second window.
|
|
91
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Verbatim: the empty window title `""` must reach `start` as written.
|
|
95
|
+
return starter("cmd.exe", ["/d", "/c", "start", '""', "cmd", "/d", "/k", ".\\new-floor.cmd"], true);
|
|
96
|
+
}
|
|
97
|
+
const dir = mkdtempSync(join(deps.tmpRoot ?? tmpdir(), "gentle-tower-floor-"));
|
|
98
|
+
chmodSync(dir, 0o700);
|
|
99
|
+
const script = join(dir, "new-floor.command");
|
|
100
|
+
writeFileSync(script, buildLaunchScript(spec), { mode: 0o700, flag: "wx" });
|
|
101
|
+
const mac = platform === "darwin";
|
|
102
|
+
const [cmd, args] = mac ? ["open", ["-a", "Terminal", script]] : ["x-terminal-emulator", ["-e", script]];
|
|
103
|
+
await new Promise<void>((resolve, reject) => {
|
|
104
|
+
const child = run(cmd, args as string[], { stdio: "ignore", detached: !mac });
|
|
105
|
+
child.once("error", reject);
|
|
106
|
+
// `open` hands the script to Terminal and exits: its exit code is the answer.
|
|
107
|
+
// The Linux terminal is the window itself and keeps running, so starting it is enough.
|
|
108
|
+
if (mac) child.once("exit", (code) => (code === 0 ? resolve() : reject(new Error(`Gentle Tower: open exited with ${code}`))));
|
|
109
|
+
else child.once("spawn", () => { child.unref(); resolve(); });
|
|
110
|
+
});
|
|
111
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Gentle Tower startup shared by `/gentle:tower` (inside a session) and
|
|
2
|
+
// `gentle-shell tower` (standalone): same hub, same built web app, same
|
|
3
|
+
// localhost-only server with a fresh token, same "new floor" launcher.
|
|
4
|
+
|
|
5
|
+
import { randomBytes } from "node:crypto";
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { towerHubDir } from "./tower-hub.ts";
|
|
11
|
+
import { launchInTerminal } from "./tower-launch.ts";
|
|
12
|
+
import { defaultPresenceDirs } from "./tower-presence.ts";
|
|
13
|
+
import { startTowerServer, type TowerServer } from "./tower-server.ts";
|
|
14
|
+
|
|
15
|
+
// lib/ and its generated runtime/ copy both sit one level under the package root.
|
|
16
|
+
const PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
17
|
+
export const TOWER_WEB_DIR = join(PACKAGE_ROOT, "web", "tower", "dist");
|
|
18
|
+
const LAUNCHER = join(PACKAGE_ROOT, "bin", "gentle-shell.mjs");
|
|
19
|
+
|
|
20
|
+
export class TowerNotBuiltError extends Error {
|
|
21
|
+
constructor(webDir: string) {
|
|
22
|
+
super(`Gentle Tower: the web app is not built (${webDir}). Run: pnpm build:tower`);
|
|
23
|
+
this.name = "TowerNotBuiltError";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface OpenTowerOptions {
|
|
28
|
+
/** Built web app folder (defaults to the package's web/tower/dist). */
|
|
29
|
+
webDir?: string;
|
|
30
|
+
/** Fixed port; still bound to 127.0.0.1. Random when absent. */
|
|
31
|
+
port?: number;
|
|
32
|
+
/** Peer presence registries (defaults to the user's). */
|
|
33
|
+
presenceDirs?: readonly string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function openTowerServer(env: NodeJS.ProcessEnv, options: OpenTowerOptions = {}): Promise<TowerServer> {
|
|
37
|
+
const webDir = options.webDir ?? TOWER_WEB_DIR;
|
|
38
|
+
if (!existsSync(join(webDir, "index.html"))) return Promise.reject(new TowerNotBuiltError(webDir));
|
|
39
|
+
return startTowerServer({
|
|
40
|
+
hubDir: towerHubDir(env),
|
|
41
|
+
webDir,
|
|
42
|
+
token: randomBytes(24).toString("base64url"),
|
|
43
|
+
engramUrl: env.ENGRAM_URL || "http://127.0.0.1:7437",
|
|
44
|
+
presenceDirs: options.presenceDirs ?? defaultPresenceDirs(),
|
|
45
|
+
...(options.port === undefined ? {} : { port: options.port }),
|
|
46
|
+
onNewFloor: (cwd, prompt) => launchInTerminal({ cwd, node: process.execPath, launcher: LAUNCHER, ...(prompt ? { prompt } : {}) }),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function openInBrowser(url: string): void {
|
|
51
|
+
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
|
|
52
|
+
try {
|
|
53
|
+
const child = spawn(cmd, [url], { stdio: "ignore", detached: true });
|
|
54
|
+
child.on("error", () => { /* the URL is also shown */ });
|
|
55
|
+
child.unref();
|
|
56
|
+
} catch { /* the URL is also shown */ }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function untilSignal(): Promise<void> {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
const done = () => {
|
|
62
|
+
process.off("SIGINT", done);
|
|
63
|
+
process.off("SIGTERM", done);
|
|
64
|
+
resolve();
|
|
65
|
+
};
|
|
66
|
+
process.on("SIGINT", done);
|
|
67
|
+
process.on("SIGTERM", done);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface RunTowerOptions extends OpenTowerOptions {
|
|
72
|
+
env: NodeJS.ProcessEnv;
|
|
73
|
+
open: boolean;
|
|
74
|
+
stdout: (text: string) => void;
|
|
75
|
+
stderr: (text: string) => void;
|
|
76
|
+
openUrl?: (url: string) => void;
|
|
77
|
+
/** Resolves when the tower should stop (defaults to SIGINT/SIGTERM). */
|
|
78
|
+
untilStopped?: (server: TowerServer) => Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** `gentle-shell tower`: serve until stopped; returns the exit code. */
|
|
82
|
+
export async function runTower(options: RunTowerOptions): Promise<number> {
|
|
83
|
+
let server: TowerServer;
|
|
84
|
+
try {
|
|
85
|
+
server = await openTowerServer(options.env, options);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const message = error instanceof TowerNotBuiltError ? error.message : `Gentle Tower could not start: ${error instanceof Error ? error.message : String(error)}`;
|
|
88
|
+
options.stderr(`${message}\n`);
|
|
89
|
+
return 1;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
options.stdout(`Gentle Tower: ${server.url}\nPress Ctrl+C to stop.\n`);
|
|
93
|
+
if (options.open) (options.openUrl ?? openInBrowser)(server.url);
|
|
94
|
+
await (options.untilStopped ?? untilSignal)(server);
|
|
95
|
+
} finally {
|
|
96
|
+
await server.close();
|
|
97
|
+
}
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Gentle Tower: every live Gentle Shell session, from the peer presence registry
|
|
2
|
+
// Gentle Agents already keeps (<home>/gentle-agents/presence). Read-only and
|
|
3
|
+
// bounded. A session without the tower extension becomes a *basic* floor: name,
|
|
4
|
+
// project and its live helpers, no tasks or cost, and it takes no actions.
|
|
5
|
+
|
|
6
|
+
import { lstatSync, readdirSync, readFileSync } from "node:fs";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { join, posix, win32 } from "node:path";
|
|
9
|
+
import { nameSubagents, TOWER_SNAPSHOT_VERSION, type TowerSnapshot } from "./tower-snapshot.ts";
|
|
10
|
+
|
|
11
|
+
const MAX_BYTES = 256 * 1024;
|
|
12
|
+
const LIVE = new Set(["queued", "running", "waiting"]);
|
|
13
|
+
/** A session whose heartbeat is older than this is gone. */
|
|
14
|
+
export const PRESENCE_MAX_AGE_MS = 2 * 60_000;
|
|
15
|
+
|
|
16
|
+
export function defaultPresenceDirs(home: string = homedir()): string[] {
|
|
17
|
+
return [join(home, ".gentle-shell", "agent", "gentle-agents", "presence"), join(home, ".pi", "agent", "gentle-agents", "presence")];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readJson(path: string): Record<string, unknown> | undefined {
|
|
21
|
+
try {
|
|
22
|
+
const st = lstatSync(path);
|
|
23
|
+
if (!st.isFile() || st.size > MAX_BYTES) return undefined;
|
|
24
|
+
const v = JSON.parse(readFileSync(path, "utf8"));
|
|
25
|
+
return v && typeof v === "object" && !Array.isArray(v) ? v : undefined;
|
|
26
|
+
} catch { return undefined; }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Absolute on any platform: `/home/me/app` or `C:\\work\\app` (the registry may come from another OS). */
|
|
30
|
+
const absolute = (p: string) => posix.isAbsolute(p) || /^[A-Za-z]:[\\/]/.test(p);
|
|
31
|
+
const basename = (p: string) => win32.basename(p) || p;
|
|
32
|
+
|
|
33
|
+
export type PresenceFloor = TowerSnapshot & { basic: true; sessionHash: string };
|
|
34
|
+
|
|
35
|
+
export function readPresenceFloors(dirs: readonly string[], now: number, maxAgeMs = PRESENCE_MAX_AGE_MS): PresenceFloor[] {
|
|
36
|
+
const best = new Map<string, PresenceFloor>();
|
|
37
|
+
for (const dir of dirs) {
|
|
38
|
+
let names: string[];
|
|
39
|
+
try { names = readdirSync(dir).filter((n) => n.endsWith(".header.json")); } catch { continue; }
|
|
40
|
+
for (const name of names) {
|
|
41
|
+
const header = readJson(join(dir, name));
|
|
42
|
+
const hash = header?.sessionHash, heartbeat = header?.heartbeat;
|
|
43
|
+
if (typeof hash !== "string" || !/^[0-9a-f]{64}$/.test(hash) || typeof heartbeat !== "number") continue;
|
|
44
|
+
if (header?.unavailable || now - heartbeat > maxAgeMs) continue;
|
|
45
|
+
const meta = (readJson(join(dir, name.replace(/\.header\.json$/, ".discovery.json")))?.metadata ?? {}) as { workspace?: unknown; tasks?: unknown };
|
|
46
|
+
const cwd = typeof meta.workspace === "string" && absolute(meta.workspace) ? meta.workspace : undefined;
|
|
47
|
+
if (!cwd) continue;
|
|
48
|
+
const tasks = (Array.isArray(meta.tasks) ? meta.tasks : []) as Array<{ id?: unknown; agent?: unknown; label?: unknown; status?: unknown }>;
|
|
49
|
+
const live = tasks.filter((t) => !!t && typeof t === "object" && typeof t.id === "string" && typeof t.agent === "string" && LIVE.has(String(t.status)))
|
|
50
|
+
.slice(0, 12).map((t) => ({ id: String(t.id), agent: String(t.agent), label: typeof t.label === "string" ? t.label : undefined, status: String(t.status) }));
|
|
51
|
+
const floor: PresenceFloor = {
|
|
52
|
+
version: TOWER_SNAPSHOT_VERSION,
|
|
53
|
+
sessionId: `p-${hash.slice(0, 32)}`,
|
|
54
|
+
sessionHash: hash,
|
|
55
|
+
name: typeof header.label === "string" && header.label.trim() ? header.label.trim().slice(0, 120) : basename(cwd),
|
|
56
|
+
project: { cwd, name: basename(cwd) },
|
|
57
|
+
model: "",
|
|
58
|
+
context: { percent: null, window: 0 },
|
|
59
|
+
cost: { total: 0, input: 0, output: 0 },
|
|
60
|
+
todos: [],
|
|
61
|
+
subagents: nameSubagents(live).map((a) => ({ id: a.id, role: a.role, name: a.name, status: a.status ?? "running", cost: 0, tokens: 0 })),
|
|
62
|
+
basic: true,
|
|
63
|
+
updatedAt: heartbeat,
|
|
64
|
+
};
|
|
65
|
+
const prev = best.get(hash);
|
|
66
|
+
if (!prev || prev.updatedAt < floor.updatedAt) best.set(hash, floor);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return [...best.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
70
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// Gentle Tower web server: serves the built web app and the hub's floors.
|
|
2
|
+
// Closed by default: 127.0.0.1 only, a random port, a per-launch token on every
|
|
3
|
+
// request, and any Origin other than its own is refused. Read-only for now:
|
|
4
|
+
// actions come later and reuse Gentle Shell's own authority.
|
|
5
|
+
|
|
6
|
+
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
7
|
+
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
|
8
|
+
import { basename, dirname, extname, resolve, sep } from "node:path";
|
|
9
|
+
import { timingSafeEqual } from "node:crypto";
|
|
10
|
+
import { readLiveTowerSnapshots as readTowerSnapshots } from "./tower-hub.ts";
|
|
11
|
+
import { readPresenceFloors } from "./tower-presence.ts";
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
import { parseTowerAction, TOWER_ACTION_MAX_BYTES, writeTowerAction } from "./tower-actions.ts";
|
|
14
|
+
|
|
15
|
+
export interface TowerServerOptions {
|
|
16
|
+
hubDir: string;
|
|
17
|
+
webDir: string;
|
|
18
|
+
token: string;
|
|
19
|
+
port?: number;
|
|
20
|
+
pollMs?: number;
|
|
21
|
+
/** Local Engram HTTP API for the read-only memory window. */
|
|
22
|
+
engramUrl?: string;
|
|
23
|
+
/** Peer presence registries: every live session shows, detailed or basic. */
|
|
24
|
+
presenceDirs?: readonly string[];
|
|
25
|
+
now?: () => number;
|
|
26
|
+
/** Opens a new Gentle Shell session (a floor) in a project already in the tower. */
|
|
27
|
+
onNewFloor?: (cwd: string, prompt?: string) => Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ARTIFACT_TYPES: Record<string, string> = { ".md": "text/plain; charset=utf-8", ".markdown": "text/plain; charset=utf-8", ".txt": "text/plain; charset=utf-8", ".mmd": "text/plain; charset=utf-8", ".mermaid": "text/plain; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp", ".gif": "image/gif" };
|
|
31
|
+
const ARTIFACT_MAX_BYTES = 2 * 1024 * 1024;
|
|
32
|
+
const MEMORY_MAX_BYTES = 512 * 1024;
|
|
33
|
+
|
|
34
|
+
export interface TowerServer {
|
|
35
|
+
host: "127.0.0.1";
|
|
36
|
+
port: number;
|
|
37
|
+
url: string;
|
|
38
|
+
close(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const TYPES: Record<string, string> = {
|
|
42
|
+
".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8",
|
|
43
|
+
".json": "application/json", ".png": "image/png", ".svg": "image/svg+xml", ".woff2": "font/woff2", ".ico": "image/x-icon",
|
|
44
|
+
};
|
|
45
|
+
const SECURITY_HEADERS = {
|
|
46
|
+
"X-Content-Type-Options": "nosniff",
|
|
47
|
+
"Referrer-Policy": "no-referrer",
|
|
48
|
+
"Cache-Control": "no-store",
|
|
49
|
+
"Content-Security-Policy": "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; connect-src 'self'; frame-ancestors 'none'",
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function sameToken(given: string | null | undefined, token: string): boolean {
|
|
53
|
+
if (!given) return false;
|
|
54
|
+
const a = Buffer.from(given), b = Buffer.from(token);
|
|
55
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function startTowerServer(options: TowerServerOptions): Promise<TowerServer> {
|
|
59
|
+
const webRoot = realpathSync(options.webDir);
|
|
60
|
+
const clients = new Set<ServerResponse>();
|
|
61
|
+
let lastBody = "";
|
|
62
|
+
const now = options.now ?? Date.now;
|
|
63
|
+
// Detailed floors (tower extension) win; every other live session joins as a basic floor.
|
|
64
|
+
const LIVE = new Set(["queued", "running", "waiting"]);
|
|
65
|
+
/** A snapshot this much older than the heartbeat has stopped updating. */
|
|
66
|
+
const STALE_SNAPSHOT_MS = 60_000;
|
|
67
|
+
const allSessions = () => {
|
|
68
|
+
// Finished helpers never show, even if a session still runs an older publisher.
|
|
69
|
+
const detailed = readTowerSnapshots(options.hubDir).map((s) => ({ ...s, subagents: s.subagents.filter((a) => LIVE.has(a.status)) }));
|
|
70
|
+
if (!options.presenceDirs?.length) return detailed;
|
|
71
|
+
const presence = new Map(readPresenceFloors(options.presenceDirs, now()).map((f) => [f.sessionHash, f]));
|
|
72
|
+
const known = new Set<string>();
|
|
73
|
+
for (const s of detailed) {
|
|
74
|
+
const hash = createHash("sha256").update(s.sessionId).digest("hex");
|
|
75
|
+
known.add(hash);
|
|
76
|
+
// Only a snapshot that stopped updating gives way to the heartbeat's live helpers;
|
|
77
|
+
// a recent one keeps its own, which carry spend and progress the heartbeat lacks.
|
|
78
|
+
const p = presence.get(hash);
|
|
79
|
+
if (p && p.updatedAt - s.updatedAt > STALE_SNAPSHOT_MS) s.subagents = p.subagents;
|
|
80
|
+
}
|
|
81
|
+
const basic = [...presence.values()].filter((f) => !known.has(f.sessionHash)).map(({ sessionHash: _h, ...f }) => f);
|
|
82
|
+
return [...detailed, ...basic];
|
|
83
|
+
};
|
|
84
|
+
const sessionsBody = () => JSON.stringify({ sessions: allSessions() });
|
|
85
|
+
let port = 0;
|
|
86
|
+
|
|
87
|
+
const MESSAGES: Record<number, string> = { 400: "Bad request", 404: "Not found", 405: "Method not allowed", 413: "Too large", 500: "Server error", 502: "Engram unavailable" };
|
|
88
|
+
const deny = (res: ServerResponse, status = 403) => { if (res.headersSent) return; res.writeHead(status, { "Content-Type": "text/plain", ...SECURITY_HEADERS }); res.end(MESSAGES[status] ?? "Forbidden"); };
|
|
89
|
+
|
|
90
|
+
const serveStatic = (res: ServerResponse, pathname: string) => {
|
|
91
|
+
let decoded: string;
|
|
92
|
+
try { decoded = decodeURIComponent(pathname); } catch { return deny(res, 400); }
|
|
93
|
+
if (decoded.includes("\0")) return deny(res, 400);
|
|
94
|
+
const wanted = resolve(webRoot, "." + (decoded === "/" ? "/index.html" : decoded));
|
|
95
|
+
let real: string;
|
|
96
|
+
try { real = realpathSync(wanted); } catch { return deny(res, 404); }
|
|
97
|
+
if (real !== webRoot && !real.startsWith(webRoot + sep)) return deny(res, 404);
|
|
98
|
+
try {
|
|
99
|
+
if (!statSync(real).isFile()) return deny(res, 404);
|
|
100
|
+
res.writeHead(200, { "Content-Type": TYPES[extname(real)] ?? "application/octet-stream", ...SECURITY_HEADERS });
|
|
101
|
+
res.end(readFileSync(real));
|
|
102
|
+
} catch { deny(res, 404); }
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const receiveAction = (req: IncomingMessage, res: ServerResponse) => {
|
|
106
|
+
const chunks: Buffer[] = [];
|
|
107
|
+
let size = 0, over = false;
|
|
108
|
+
req.on("data", (c: Buffer) => { size += c.length; if (size > TOWER_ACTION_MAX_BYTES) { over = true; } else chunks.push(c); });
|
|
109
|
+
req.on("end", () => {
|
|
110
|
+
if (over) return deny(res, 413);
|
|
111
|
+
let action;
|
|
112
|
+
try { action = parseTowerAction(JSON.parse(Buffer.concat(chunks).toString("utf8"))); } catch { action = undefined; }
|
|
113
|
+
if (!action) return deny(res, 400);
|
|
114
|
+
if (action.type === "new-floor") {
|
|
115
|
+
// Never an arbitrary folder: only a project that already has a floor.
|
|
116
|
+
if (!options.onNewFloor || !allSessions().some((s) => s.project.cwd === action.project)) return deny(res, 403);
|
|
117
|
+
options.onNewFloor(action.project, action.prompt).then(
|
|
118
|
+
() => { res.writeHead(202, { "Content-Type": "application/json", ...SECURITY_HEADERS }); res.end("{}"); },
|
|
119
|
+
() => deny(res, 500));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
// Only a floor that publishes its own snapshot has a session reading its inbox.
|
|
123
|
+
if (!readTowerSnapshots(options.hubDir).some((s) => s.sessionId === action.sessionId)) return deny(res, 404);
|
|
124
|
+
try {
|
|
125
|
+
const id = writeTowerAction(options.hubDir, action);
|
|
126
|
+
res.writeHead(202, { "Content-Type": "application/json", ...SECURITY_HEADERS });
|
|
127
|
+
res.end(JSON.stringify({ id }));
|
|
128
|
+
} catch { deny(res, 500); }
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// Only a file the session itself wrote (it is in its own list), reached
|
|
133
|
+
// directly: a symlink that points elsewhere is refused.
|
|
134
|
+
const serveArtifact = (res: ServerResponse, sessionId: string | null, wanted: string | null) => {
|
|
135
|
+
const session = readTowerSnapshots(options.hubDir).find((x) => x.sessionId === sessionId);
|
|
136
|
+
if (!session || !wanted || !session.artifacts?.some((a) => a.path === wanted)) return deny(res, 404);
|
|
137
|
+
try {
|
|
138
|
+
const real = realpathSync(wanted);
|
|
139
|
+
if (real !== realpathSync(dirname(wanted)) + sep + basename(wanted)) return deny(res, 404);
|
|
140
|
+
const st = statSync(real);
|
|
141
|
+
const type = ARTIFACT_TYPES[extname(real).toLowerCase()];
|
|
142
|
+
if (!st.isFile() || !type || st.size > ARTIFACT_MAX_BYTES) return deny(res, 404);
|
|
143
|
+
res.writeHead(200, { "Content-Type": type, ...SECURITY_HEADERS, "Content-Security-Policy": "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:" });
|
|
144
|
+
res.end(readFileSync(real));
|
|
145
|
+
} catch { deny(res, 404); }
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const proxyMemory = async (res: ServerResponse, kind: string, query: URLSearchParams) => {
|
|
149
|
+
if (!options.engramUrl) return deny(res, 404);
|
|
150
|
+
const project = query.get("project") ?? "";
|
|
151
|
+
const q = (query.get("q") ?? "").slice(0, 200);
|
|
152
|
+
if (!project || project.length > 200 || (kind === "search" && !q)) return deny(res, 400);
|
|
153
|
+
const target = kind === "search"
|
|
154
|
+
? `${options.engramUrl}/search?${new URLSearchParams({ q, project, limit: "40" })}`
|
|
155
|
+
: `${options.engramUrl}/review?${new URLSearchParams({ project, limit: "40" })}`;
|
|
156
|
+
try {
|
|
157
|
+
const r = await fetch(target, { signal: AbortSignal.timeout(4000) });
|
|
158
|
+
const body = await r.text();
|
|
159
|
+
if (!r.ok || body.length > MEMORY_MAX_BYTES) return deny(res, 502);
|
|
160
|
+
JSON.parse(body);
|
|
161
|
+
res.writeHead(200, { "Content-Type": "application/json", ...SECURITY_HEADERS });
|
|
162
|
+
res.end(body);
|
|
163
|
+
} catch { deny(res, 502); }
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const handler = (req: IncomingMessage, res: ServerResponse) => {
|
|
167
|
+
if (req.method !== "GET" && req.method !== "POST") return deny(res, 405);
|
|
168
|
+
const origin = req.headers.origin;
|
|
169
|
+
if (origin && origin !== `http://127.0.0.1:${port}`) return deny(res);
|
|
170
|
+
// A browser always sends Origin on POST; a missing one is not our page.
|
|
171
|
+
if (req.method === "POST" && !origin) return deny(res);
|
|
172
|
+
// The token is the first path segment, so every relative request the app
|
|
173
|
+
// makes (assets, lazy chunks, the API) carries it without cookies, which
|
|
174
|
+
// browsers would share with every other port on 127.0.0.1.
|
|
175
|
+
const raw = (req.url ?? "/").split("?")[0];
|
|
176
|
+
const slash = raw.indexOf("/", 1);
|
|
177
|
+
const head = slash === -1 ? raw.slice(1) : raw.slice(1, slash);
|
|
178
|
+
if (!sameToken(head, options.token)) return deny(res);
|
|
179
|
+
if (slash === -1) { res.writeHead(308, { Location: `/${head}/`, ...SECURITY_HEADERS }); return res.end(); }
|
|
180
|
+
const rest = raw.slice(slash);
|
|
181
|
+
if (rest === "/api/action") {
|
|
182
|
+
if (req.method !== "POST") return deny(res, 405);
|
|
183
|
+
return receiveAction(req, res);
|
|
184
|
+
}
|
|
185
|
+
if (req.method !== "GET") return deny(res, 405);
|
|
186
|
+
const query = new URLSearchParams((req.url ?? "").split("?")[1] ?? "");
|
|
187
|
+
if (rest === "/api/artifact") return serveArtifact(res, query.get("session"), query.get("path"));
|
|
188
|
+
if (rest === "/api/memory/search" || rest === "/api/memory/review") return void proxyMemory(res, rest.slice("/api/memory/".length), query);
|
|
189
|
+
if (rest === "/api/sessions") {
|
|
190
|
+
res.writeHead(200, { "Content-Type": "application/json", ...SECURITY_HEADERS });
|
|
191
|
+
return res.end(sessionsBody());
|
|
192
|
+
}
|
|
193
|
+
if (rest === "/api/events") {
|
|
194
|
+
res.writeHead(200, { "Content-Type": "text/event-stream", Connection: "keep-alive", ...SECURITY_HEADERS });
|
|
195
|
+
res.write(`data: ${sessionsBody()}\n\n`);
|
|
196
|
+
clients.add(res);
|
|
197
|
+
req.on("close", () => clients.delete(res));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
serveStatic(res, rest);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const server = createServer(handler);
|
|
204
|
+
const poll = setInterval(() => {
|
|
205
|
+
if (clients.size === 0) return;
|
|
206
|
+
const body = sessionsBody();
|
|
207
|
+
if (body === lastBody) return;
|
|
208
|
+
lastBody = body;
|
|
209
|
+
for (const c of clients) c.write(`data: ${body}\n\n`);
|
|
210
|
+
}, options.pollMs ?? 700);
|
|
211
|
+
poll.unref();
|
|
212
|
+
|
|
213
|
+
return new Promise((resolveStart, reject) => {
|
|
214
|
+
server.once("error", reject);
|
|
215
|
+
server.listen(options.port ?? 0, "127.0.0.1", () => {
|
|
216
|
+
const addr = server.address();
|
|
217
|
+
port = typeof addr === "object" && addr ? addr.port : 0;
|
|
218
|
+
lastBody = sessionsBody();
|
|
219
|
+
resolveStart({
|
|
220
|
+
host: "127.0.0.1",
|
|
221
|
+
port,
|
|
222
|
+
url: `http://127.0.0.1:${port}/${options.token}/`,
|
|
223
|
+
close: () => new Promise<void>((done) => {
|
|
224
|
+
clearInterval(poll);
|
|
225
|
+
for (const c of clients) c.end();
|
|
226
|
+
clients.clear();
|
|
227
|
+
server.close(() => done());
|
|
228
|
+
server.closeAllConnections?.();
|
|
229
|
+
}),
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|