@temmo/cli 0.0.0 → 0.19.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/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +16 -0
- package/.codex-plugin/plugin.json +30 -0
- package/.cursor-plugin/plugin.json +22 -0
- package/.mcp.json +18 -0
- package/LICENSE +202 -0
- package/README.md +82 -2
- package/THIRD_PARTY_NOTICES +32 -0
- package/commands/afk.md +17 -0
- package/deploy/build-ui.mjs +65 -0
- package/deploy/claude/README.md +16 -0
- package/deploy/claude/auto-mode.json +31 -0
- package/deploy/postgres/compose.yaml +26 -0
- package/deploy/temmo-runner.service +24 -0
- package/deploy/verify-ui.mjs +15 -0
- package/harness-core/README.md +27 -0
- package/harness-core/bin/harness.mjs +17 -0
- package/harness-core/package.json +10 -0
- package/harness-core/stage.mjs +108 -0
- package/harness-core/stages/commit.md +7 -0
- package/harness-core/stages/pr.md +7 -0
- package/harness-core/stages/push.md +7 -0
- package/hooks/codex-hooks.json +14 -0
- package/hooks/cursor-hooks.json +17 -0
- package/hooks/hooks.json +27 -0
- package/lib/acp/permission.js +72 -0
- package/lib/acp/sessionConfig.js +109 -0
- package/lib/activity.js +244 -0
- package/lib/agent-guard.js +72 -0
- package/lib/agents-route.js +55 -0
- package/lib/agents.js +213 -0
- package/lib/agui.js +255 -0
- package/lib/alerts.js +36 -0
- package/lib/analyze-otlp.js +46 -0
- package/lib/analyze-run-code.js +397 -0
- package/lib/analyze-sources.js +372 -0
- package/lib/approval-card.js +54 -0
- package/lib/approval-cli.js +53 -0
- package/lib/approval-request.js +81 -0
- package/lib/approval-signer.js +48 -0
- package/lib/approvals.js +295 -0
- package/lib/attachment-cache.js +236 -0
- package/lib/attachment.js +63 -0
- package/lib/blocked.js +20 -0
- package/lib/build-review.js +90 -0
- package/lib/builder/cli.js +90 -0
- package/lib/builder/policy.js +176 -0
- package/lib/builder/recover.js +189 -0
- package/lib/builder/run.js +240 -0
- package/lib/builder/settle.js +198 -0
- package/lib/builder/stage.js +240 -0
- package/lib/builder/state.js +73 -0
- package/lib/builder/supervisor.js +289 -0
- package/lib/builder-clean.js +223 -0
- package/lib/builder-code.cjs +67 -0
- package/lib/builder.js +9 -0
- package/lib/bus-postgres.js +281 -0
- package/lib/bus.js +349 -0
- package/lib/catalog/author.js +278 -0
- package/lib/catalog/dot.js +426 -0
- package/lib/catalog/pattern-model.js +193 -0
- package/lib/catalog/patterns.js +324 -0
- package/lib/catalog/projects.js +83 -0
- package/lib/catalog/records.js +2 -0
- package/lib/catalog/roster.js +70 -0
- package/lib/catalog/team-files.js +49 -0
- package/lib/catalog/teams.js +109 -0
- package/lib/claude-socket.js +333 -0
- package/lib/cli.js +741 -0
- package/lib/code-mode.js +45 -0
- package/lib/code-read-policy.yaml +9 -0
- package/lib/code-run.js +461 -0
- package/lib/codex-compact.js +141 -0
- package/lib/compaction.js +21 -0
- package/lib/config.js +424 -0
- package/lib/cursor-trace.js +248 -0
- package/lib/deep-link.js +20 -0
- package/lib/deliver-push.js +120 -0
- package/lib/delivery.js +28 -0
- package/lib/deploy-tree.js +227 -0
- package/lib/durable.js +404 -0
- package/lib/factory-tick.js +32 -0
- package/lib/family.js +63 -0
- package/lib/fetch-origin.js +53 -0
- package/lib/format.js +133 -0
- package/lib/harness-surface-cli.js +153 -0
- package/lib/harness-surface.js +486 -0
- package/lib/harness.js +271 -0
- package/lib/hook.mjs +291 -0
- package/lib/idle.js +40 -0
- package/lib/install-cursor.js +115 -0
- package/lib/install-opencode.js +105 -0
- package/lib/job.js +82 -0
- package/lib/land-cli.js +319 -0
- package/lib/land-member.js +64 -0
- package/lib/land.js +320 -0
- package/lib/lessons.js +223 -0
- package/lib/limit-alert.js +423 -0
- package/lib/limits.js +284 -0
- package/lib/machine-prompt.js +19 -0
- package/lib/merge-sweep.js +149 -0
- package/lib/messaging.js +1002 -0
- package/lib/mission-cli.js +103 -0
- package/lib/mission.js +241 -0
- package/lib/mode.js +215 -0
- package/lib/models.js +114 -0
- package/lib/names.js +86 -0
- package/lib/opencode-api.js +57 -0
- package/lib/opencode-plugin.mjs +313 -0
- package/lib/operator-token.js +78 -0
- package/lib/operator.js +187 -0
- package/lib/pad-activity.js +143 -0
- package/lib/pad-fresh.js +44 -0
- package/lib/pad.js +198 -0
- package/lib/pattern-cli.js +120 -0
- package/lib/pattern-compiler.js +68 -0
- package/lib/pattern-runs.js +795 -0
- package/lib/pattern-stall.js +58 -0
- package/lib/pattern-tick.js +45 -0
- package/lib/pattern-workflow.js +69 -0
- package/lib/pi-extension.mjs +98 -0
- package/lib/pi.js +57 -0
- package/lib/plugin-copy.js +76 -0
- package/lib/pm-review.js +32 -0
- package/lib/pm-workspace.js +87 -0
- package/lib/postgres.js +115 -0
- package/lib/pr-card.js +34 -0
- package/lib/probe.js +295 -0
- package/lib/process.js +67 -0
- package/lib/project-pause.js +23 -0
- package/lib/prune.js +793 -0
- package/lib/questions.js +250 -0
- package/lib/receipts.js +63 -0
- package/lib/reflect.js +962 -0
- package/lib/rename-migration.js +118 -0
- package/lib/run-terminal.js +437 -0
- package/lib/runner-request.js +33 -0
- package/lib/sandbox-app.js +120 -0
- package/lib/sandbox-claims.js +258 -0
- package/lib/sandbox-cli.js +188 -0
- package/lib/sandbox-hosts-cli.js +68 -0
- package/lib/sandbox-hosts.js +94 -0
- package/lib/sandbox-pr.js +102 -0
- package/lib/sandbox-proposal.js +367 -0
- package/lib/sandbox-tools.js +143 -0
- package/lib/sandbox.js +501 -0
- package/lib/schedule-session.js +68 -0
- package/lib/session-activity.js +67 -0
- package/lib/session-events.js +94 -0
- package/lib/sessions/control.js +244 -0
- package/lib/sessions/git-refs.js +265 -0
- package/lib/sessions/launch.js +359 -0
- package/lib/sessions/registry.js +450 -0
- package/lib/sessions/scope.js +111 -0
- package/lib/setup.js +377 -0
- package/lib/signed-approval.js +225 -0
- package/lib/slash.js +167 -0
- package/lib/spawn-cli.js +45 -0
- package/lib/spawn.js +107 -0
- package/lib/start-limit.js +58 -0
- package/lib/state.js +24 -0
- package/lib/stop.js +308 -0
- package/lib/store-read.js +62 -0
- package/lib/store.js +252 -0
- package/lib/subagent-guard.js +240 -0
- package/lib/surfaces/channel-post.js +43 -0
- package/lib/surfaces/instructions.js +15 -0
- package/lib/surfaces/mcp-reload.js +144 -0
- package/lib/surfaces/ops.js +629 -0
- package/lib/surfaces/render-mcp.js +33 -0
- package/lib/surfaces/run-terminal-ops.js +40 -0
- package/lib/surfaces/sandbox-ops.js +36 -0
- package/lib/surfaces/schema.js +7 -0
- package/lib/surfaces/task-view.js +90 -0
- package/lib/surfaces/types.js +4 -0
- package/lib/task-cli.js +274 -0
- package/lib/task-order.js +34 -0
- package/lib/task-order.mjs +2 -0
- package/lib/task-route.js +125 -0
- package/lib/tasks.js +631 -0
- package/lib/team-permission.js +34 -0
- package/lib/team-templates/blitz/team.dot +9 -0
- package/lib/team-templates/factory/team.dot +26 -0
- package/lib/team-templates/feature/team.dot +14 -0
- package/lib/team-templates/fix-and-review/team.dot +12 -0
- package/lib/team-templates/product/team.dot +16 -0
- package/lib/team-templates/release/team.dot +7 -0
- package/lib/team-templates/review/team.dot +6 -0
- package/lib/terminal-draft.js +42 -0
- package/lib/thread-branch.js +82 -0
- package/lib/thread-channel-store.js +181 -0
- package/lib/thread-channels.js +116 -0
- package/lib/thread-listening.js +20 -0
- package/lib/thread-routes.js +91 -0
- package/lib/threads.js +441 -0
- package/lib/tool-api.js +33 -0
- package/lib/transcript.js +98 -0
- package/lib/ui-build-output.js +19 -0
- package/lib/ui-capabilities.js +90 -0
- package/lib/usage.js +182 -0
- package/lib/vendor/mcp-sdk.d.mts +4 -0
- package/lib/vendor/mcp-sdk.mjs +17046 -0
- package/lib/waiter.js +127 -0
- package/lib/workflows.js +546 -0
- package/lib/worktree-setup.js +86 -0
- package/package.json +112 -1
- package/runner/adapters/claude.js +465 -0
- package/runner/adapters/cli-sandbox.js +277 -0
- package/runner/adapters/codex.js +675 -0
- package/runner/adapters/cursor.mjs +937 -0
- package/runner/adapters/fake.js +119 -0
- package/runner/adapters/opencode-sandbox.js +370 -0
- package/runner/adapters/opencode.js +486 -0
- package/runner/adapters/pi-sandbox.js +134 -0
- package/runner/adapters/pi.js +364 -0
- package/runner/client.js +40 -0
- package/runner/contract.js +129 -0
- package/runner/control.js +178 -0
- package/runner/delivery.js +184 -0
- package/runner/factory-tick.mjs +10 -0
- package/runner/lock.js +41 -0
- package/runner/main.mjs +118 -0
- package/runner/pattern-tick.mjs +16 -0
- package/runner/publish.js +113 -0
- package/runner/runner.js +2612 -0
- package/runner/sandbox-run.mjs +239 -0
- package/runner/schedules.js +294 -0
- package/runner/task-wake.js +59 -0
- package/scripts/ai-collab +3 -0
- package/scripts/ai-collab-bus.mjs +4 -0
- package/scripts/ai-collab-code-mcp.mjs +3 -0
- package/scripts/ai-collab-hook +3 -0
- package/scripts/ai-collab-hook.mjs +3 -0
- package/scripts/ai-collab-mcp-tools.mjs +2 -0
- package/scripts/ai-collab-mcp.mjs +3 -0
- package/scripts/ai-collab.mjs +3 -0
- package/scripts/install-cursor.mjs +11 -0
- package/scripts/install-opencode.mjs +10 -0
- package/scripts/temmo +3 -0
- package/scripts/temmo-bus.mjs +151 -0
- package/scripts/temmo-code-mcp.mjs +63 -0
- package/scripts/temmo-hook +3 -0
- package/scripts/temmo-hook.mjs +201 -0
- package/scripts/temmo-mcp-tools.mjs +22 -0
- package/scripts/temmo-mcp.mjs +27 -0
- package/scripts/temmo.mjs +4 -0
- package/skills/antislop/SKILL.md +701 -0
- package/skills/antislop/VERSION +1 -0
- package/skills/antislop-code/SKILL.md +128 -0
- package/skills/antislop-human/LICENSE-web-quality-skills +21 -0
- package/skills/antislop-human/SKILL.md +204 -0
- package/skills/antislop-human/contrast-check.py +168 -0
- package/skills/antislop-human/contrast-mcp.py +125 -0
- package/skills/antislop-human/references/A11Y-PATTERNS.md +249 -0
- package/skills/antislop-human/references/WCAG.md +127 -0
- package/skills/antislop-layoutmobile/SKILL.md +170 -0
- package/skills/antislop-ui/LICENSE-web-quality-skills +21 -0
- package/skills/antislop-ui/SKILL.md +351 -0
- package/skills/antislop-ui/references/CORE-WEB-VITALS.md +181 -0
- package/skills/antislop-ui/references/WEB-BASELINE.md +28 -0
- package/skills/harness-surface/SKILL.md +42 -0
- package/skills/land/SKILL.md +32 -0
- package/skills/memory-file/SKILL.md +39 -0
- package/skills/temmo/SKILL.md +101 -0
- package/skills/temmo/references/role-programs.md +47 -0
- package/skills/ui-sdk/SKILL.md +56 -0
- package/skills/ui-sdk/references/agent-ready.md +136 -0
- package/skills/ui-sdk/references/build-interface.md +187 -0
- package/skills/ui-sdk/references/create-component.md +88 -0
- package/skills/ui-sdk/references/patterns.md +246 -0
- package/skills/ui-sdk/references/styles.md +66 -0
- package/skills/ui-sdk/references/testing.md +172 -0
- package/skills/ui-sdk/references/web-components.md +78 -0
- package/tools/screen-state.mjs +119 -0
- package/tools/smoke-ui.mjs +540 -0
- package/web/app/activity.jsx +251 -0
- package/web/app/agent-mark.tsx +43 -0
- package/web/app/alert-level.jsx +16 -0
- package/web/app/api/[action]/route.js +8 -0
- package/web/app/api/auth/[step]/route.js +12 -0
- package/web/app/api/events/route.js +8 -0
- package/web/app/api/file/[name]/route.js +8 -0
- package/web/app/api/land/route.js +12 -0
- package/web/app/api/lessons/route.js +8 -0
- package/web/app/api/push/key/route.js +8 -0
- package/web/app/api/push/subscribe/route.js +12 -0
- package/web/app/api/push/test/route.js +8 -0
- package/web/app/api/refs/route.js +8 -0
- package/web/app/api/review-diff/route.js +8 -0
- package/web/app/api/schedules/route.js +8 -0
- package/web/app/api/thread-diff/route.js +8 -0
- package/web/app/api/version/route.js +6 -0
- package/web/app/cards.jsx +189 -0
- package/web/app/chat-api.js +82 -0
- package/web/app/chat-composer.jsx +313 -0
- package/web/app/chat-console.jsx +691 -0
- package/web/app/chat-image.tsx +61 -0
- package/web/app/chat-message.jsx +122 -0
- package/web/app/chat-shared.jsx +59 -0
- package/web/app/chat-sidebar.jsx +25 -0
- package/web/app/chat-stream.js +127 -0
- package/web/app/chat.jsx +31 -0
- package/web/app/compactions.jsx +57 -0
- package/web/app/composer-actions.tsx +87 -0
- package/web/app/decisions.jsx +53 -0
- package/web/app/fonts/OFL-hanken-grotesk.txt +93 -0
- package/web/app/fonts/OFL-ibm-plex-mono.txt +93 -0
- package/web/app/fonts/hanken-grotesk-latin-wght-normal.woff2 +0 -0
- package/web/app/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
- package/web/app/fonts/ibm-plex-mono-latin-500-normal.woff2 +0 -0
- package/web/app/globals.css +199 -0
- package/web/app/home.jsx +246 -0
- package/web/app/icon.svg +1 -0
- package/web/app/land-panel.jsx +63 -0
- package/web/app/land-reviews.jsx +20 -0
- package/web/app/layout.jsx +17 -0
- package/web/app/lessons.tsx +76 -0
- package/web/app/logo.tsx +13 -0
- package/web/app/logos.jsx +16 -0
- package/web/app/manifest.js +8 -0
- package/web/app/mode-menu.tsx +46 -0
- package/web/app/new-session.jsx +275 -0
- package/web/app/notify.jsx +180 -0
- package/web/app/outcomes.tsx +94 -0
- package/web/app/page.jsx +9 -0
- package/web/app/pattern-graph.jsx +75 -0
- package/web/app/patterns-page.jsx +293 -0
- package/web/app/phone-nav.tsx +36 -0
- package/web/app/progress-feed.tsx +33 -0
- package/web/app/providers.tsx +27 -0
- package/web/app/quote.jsx +15 -0
- package/web/app/run-card.tsx +200 -0
- package/web/app/run-requests.tsx +148 -0
- package/web/app/scheduled.jsx +105 -0
- package/web/app/session-panel.tsx +174 -0
- package/web/app/session-toolbar.tsx +180 -0
- package/web/app/shadcn.css +638 -0
- package/web/app/shipped.jsx +49 -0
- package/web/app/subagent-runs.jsx +31 -0
- package/web/app/task-graph.jsx +67 -0
- package/web/app/tasks-strip.tsx +36 -0
- package/web/app/tasks.jsx +375 -0
- package/web/app/team-author.jsx +145 -0
- package/web/app/team-editor.jsx +88 -0
- package/web/app/thread-agents.tsx +32 -0
- package/web/app/thread-place.tsx +9 -0
- package/web/app/thread-sessions.tsx +149 -0
- package/web/app/thread-sidebar.tsx +418 -0
- package/web/app/thread-switcher.tsx +27 -0
- package/web/app/tool-diff.jsx +94 -0
- package/web/components/a2ui/controls.jsx +36 -0
- package/web/components/a2ui/option-picker.jsx +23 -0
- package/web/components/a2ui/script.tsx +121 -0
- package/web/components/a2ui/surface.jsx +81 -0
- package/web/components/chat/thread.jsx +50 -0
- package/web/components/script-text.tsx +42 -0
- package/web/components/ui/avatar.jsx +115 -0
- package/web/components/ui/badge.jsx +47 -0
- package/web/components/ui/button.jsx +63 -0
- package/web/components/ui/chat-container.jsx +52 -0
- package/web/components/ui/choice-list.tsx +72 -0
- package/web/components/ui/code-block.jsx +60 -0
- package/web/components/ui/copy-button.tsx +41 -0
- package/web/components/ui/dropdown-menu.jsx +257 -0
- package/web/components/ui/markdown.jsx +84 -0
- package/web/components/ui/message.jsx +28 -0
- package/web/components/ui/popover.jsx +33 -0
- package/web/components/ui/scroll-button.jsx +34 -0
- package/web/components/ui/select.jsx +189 -0
- package/web/components/ui/sheet.jsx +152 -0
- package/web/components/ui/sonner.jsx +10 -0
- package/web/components/ui/tabs.jsx +88 -0
- package/web/components/ui/textarea.jsx +20 -0
- package/web/components/ui/tooltip.jsx +56 -0
- package/web/components.json +24 -0
- package/web/instrumentation.js +8 -0
- package/web/lib/a2ui-catalog.js +34 -0
- package/web/lib/a2ui-pr.js +76 -0
- package/web/lib/a2ui-run-catalog.js +18 -0
- package/web/lib/a2ui-run.js +93 -0
- package/web/lib/a2ui.js +262 -0
- package/web/lib/activity-rows.js +275 -0
- package/web/lib/agent-detail.js +5 -0
- package/web/lib/alert-level.js +44 -0
- package/web/lib/approval-mode.js +22 -0
- package/web/lib/chat-turns.js +174 -0
- package/web/lib/child-subagents.js +16 -0
- package/web/lib/compaction-view.js +48 -0
- package/web/lib/composer-actions.js +40 -0
- package/web/lib/context-view.js +57 -0
- package/web/lib/fuzzy.js +26 -0
- package/web/lib/highlight.js +44 -0
- package/web/lib/lessons-view.js +23 -0
- package/web/lib/mentions.js +16 -0
- package/web/lib/message-routing.js +105 -0
- package/web/lib/new-session.js +85 -0
- package/web/lib/operator-messages.js +28 -0
- package/web/lib/pattern-graph.js +165 -0
- package/web/lib/pattern-view.js +33 -0
- package/web/lib/pending-summary.js +18 -0
- package/web/lib/permission-modes.js +30 -0
- package/web/lib/project-feed.js +107 -0
- package/web/lib/quote.js +7 -0
- package/web/lib/reply-quote.js +31 -0
- package/web/lib/scheduled.js +79 -0
- package/web/lib/selection.js +110 -0
- package/web/lib/send-confirmation.js +22 -0
- package/web/lib/session-dead.js +10 -0
- package/web/lib/session-state.js +132 -0
- package/web/lib/sidebar.js +15 -0
- package/web/lib/slash-commands.js +164 -0
- package/web/lib/start-default.js +35 -0
- package/web/lib/start-mode.js +35 -0
- package/web/lib/stream-views.js +49 -0
- package/web/lib/subagent-state.js +20 -0
- package/web/lib/task-board.js +54 -0
- package/web/lib/task-graph.js +183 -0
- package/web/lib/task-tags.js +41 -0
- package/web/lib/team-editor.js +10 -0
- package/web/lib/thread-groups.js +86 -0
- package/web/lib/threads.js +28 -0
- package/web/lib/tool-diff.js +273 -0
- package/web/lib/ui-events.js +58 -0
- package/web/lib/utils.js +5 -0
- package/web/lib/view-memory.js +25 -0
- package/web/next.config.mjs +37 -0
- package/web/postcss.config.mjs +1 -0
- package/web/proxy.js +25 -0
- package/web/public/sw.js +39 -0
- package/web/server/auth.js +178 -0
- package/web/server/dispatch.js +293 -0
- package/web/server/history.js +56 -0
- package/web/server/hosted.js +58 -0
- package/web/server/hub.js +126 -0
- package/web/server/instance.js +5 -0
- package/web/server/land-reviews.js +64 -0
- package/web/server/push.js +148 -0
- package/web/server/run-terminal.js +79 -0
- package/web/server/schedules.js +38 -0
- package/web/server/shipped.js +51 -0
- package/web/server/task-board.js +71 -0
- package/web/server/threads.js +209 -0
- package/web/server/ui.js +2937 -0
- package/web/tsconfig.base.json +16 -0
- package/web/tsconfig.json +9 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dispatcher in the UI server: pushes direct messages into Codex, OpenCode and Claude sessions.
|
|
3
|
+
* Claude: a direct message goes to the session's messaging socket (lib/claude-socket.ts) when the gate is open, and counts
|
|
4
|
+
* as delivered only once the transcript shows it. Otherwise the waiter and hooks deliver it.
|
|
5
|
+
* Codex: `codex queue --thread <id> --message <text> --resume`, only while the session is idle (lib/idle.ts).
|
|
6
|
+
* OpenCode: POST /api/session/<id>/prompt with delivery steer (urgent) or queue, and resume: true.
|
|
7
|
+
* A pushed message is reserved before the push and marked delivered after it, so the session's hooks and the waiter skip it.
|
|
8
|
+
* A failed push releases the reservation and backs off per (session, message). fyi and broadcast messages are never pushed.
|
|
9
|
+
* A message sent with expectReply is redelivered when the target has not answered (respond or read receipt):
|
|
10
|
+
* after REDELIVER_MS, at most MAX_ATTEMPTS attempts, until TTL_MS after the first one. Cursor stays hook-only.
|
|
11
|
+
*/
|
|
12
|
+
import { readFileSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { broadcastTopic, inboxTopic, receiptsTopic } from "../../lib/config.js";
|
|
15
|
+
import { backoffMs, MAX_ATTEMPTS, nextAction, REDELIVER_MS, TTL_MS } from "../../lib/delivery.js";
|
|
16
|
+
import { PUSHED, busyWithPush, direct, holdRunning, pushSession, readySession, runCommand } from "../../lib/deliver-push.js";
|
|
17
|
+
import { formatInbox } from "../../lib/format.js";
|
|
18
|
+
import { sessionIdleState } from "../../lib/idle.js";
|
|
19
|
+
import { claimInbox, ATTACHMENT_BUDGET_MS, markDelivered, parseEnvelope, releaseDelivery, reserveDelivery, reservedMessage } from "../../lib/messaging.js";
|
|
20
|
+
import { cacheAttachmentsWithin } from "../../lib/attachment-cache.js";
|
|
21
|
+
import { pollCached } from "../../lib/messaging.js";
|
|
22
|
+
import { handOff } from "../../lib/stop.js";
|
|
23
|
+
import { opencodeRequest } from "../../lib/opencode-api.js";
|
|
24
|
+
import { readReceipts } from "../../lib/receipts.js";
|
|
25
|
+
import { listSessions } from "../../lib/sessions/registry.js";
|
|
26
|
+
import { writeState } from "../../lib/state.js";
|
|
27
|
+
export { MAX_ATTEMPTS, REDELIVER_MS, TTL_MS };
|
|
28
|
+
const TICK_MS = 5000;
|
|
29
|
+
const CLAIM_LIMIT = 4;
|
|
30
|
+
const ANSWERS_MAX = 5000;
|
|
31
|
+
// An undelivered message has no record here yet: no start for the TTL, no redelivery schedule.
|
|
32
|
+
const FIRST_PUSH = { firstAt: null, pushes: 0, dueAt: null };
|
|
33
|
+
function ledgerFile(cfg) {
|
|
34
|
+
return join(cfg.stateDir, cfg.project, "dispatch.json");
|
|
35
|
+
}
|
|
36
|
+
function readLedger(cfg) {
|
|
37
|
+
try {
|
|
38
|
+
const s = JSON.parse(readFileSync(ledgerFile(cfg), "utf8"));
|
|
39
|
+
return s.entries && typeof s.entries === "object" ? s.entries : {};
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function writeLedger(cfg, entries) {
|
|
46
|
+
writeState(ledgerFile(cfg), { entries });
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Builds the dispatcher. projects() returns the enabled project configs; run, opencode, idle, claude, claim, now and env
|
|
50
|
+
* are injectable. tick() makes one pass over every project and resolves the delivery counts; start() runs it every TICK_MS;
|
|
51
|
+
* drain() resolves once the background confirmations of unconfirmed Claude pushes have ended. skip() true makes a tick
|
|
52
|
+
* do nothing (the runner delivers while it is up).
|
|
53
|
+
*/
|
|
54
|
+
export function createDispatcher({ projects, run = runCommand, opencode = opencodeRequest, idle = sessionIdleState, now = Date.now, env = process.env, claim = claimInbox, claude = undefined, skip = () => false, } = {}) {
|
|
55
|
+
let timer;
|
|
56
|
+
let running = false;
|
|
57
|
+
const reads = new Map();
|
|
58
|
+
const answers = new Map();
|
|
59
|
+
const cursors = new Map();
|
|
60
|
+
const backoff = new Map();
|
|
61
|
+
// Per (session, id) of an unconfirmed push: whether the whole range written after that push holds the message.
|
|
62
|
+
const rechecks = new Map();
|
|
63
|
+
const watchers = new Set();
|
|
64
|
+
const slot = (session, id) => `${session.id}\n${id}`;
|
|
65
|
+
const cooling = (session, id) => (backoff.get(slot(session, id))?.nextAt ?? 0) > now();
|
|
66
|
+
// Records a failed attempt for each id; the next attempt waits backoffMs, and the first failure starts the TTL.
|
|
67
|
+
function failed(session, ids) {
|
|
68
|
+
for (const id of ids) {
|
|
69
|
+
const prev = backoff.get(slot(session, id));
|
|
70
|
+
const failures = (prev?.failures ?? 0) + 1;
|
|
71
|
+
backoff.set(slot(session, id), { failures, firstAt: prev?.firstAt ?? now(), nextAt: now() + backoffMs(failures) });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Whether a message whose pushes have failed is past the TTL: it is no longer pushed, and stays for the hooks.
|
|
75
|
+
function expired(session, id) {
|
|
76
|
+
const entry = backoff.get(slot(session, id));
|
|
77
|
+
return entry?.firstAt !== undefined && nextAction({ firstAt: entry.firstAt, pushes: 0, dueAt: null }, now(), { acked: false, busy: false, ready: true }) === "expired";
|
|
78
|
+
}
|
|
79
|
+
function forget(session, ids) {
|
|
80
|
+
for (const id of ids) {
|
|
81
|
+
backoff.delete(slot(session, id));
|
|
82
|
+
rechecks.delete(slot(session, id));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Delivers text to one session through its harness's API. Resolves {status}: "delivered", "unsent" or "unconfirmed" (Claude only).
|
|
86
|
+
function push(session, text, { urgent, ids }) {
|
|
87
|
+
return pushSession(session, text, { urgent, ids }, { env, run, opencode, ...(claude ? { claude } : {}) });
|
|
88
|
+
}
|
|
89
|
+
// Keeps the reservation while a late confirmation is watched, then marks the messages delivered or releases them.
|
|
90
|
+
// A release keeps a whole-range check per id, run before the message is pushed again.
|
|
91
|
+
function watchLate(session, scfg, ids, result) {
|
|
92
|
+
const job = result.watch()
|
|
93
|
+
.then((ok) => {
|
|
94
|
+
if (ok)
|
|
95
|
+
return markDelivered(scfg, ids);
|
|
96
|
+
for (const id of ids)
|
|
97
|
+
rechecks.set(slot(session, id), () => result.confirmedSince?.(id) ?? false);
|
|
98
|
+
return releaseDelivery(scfg, ids);
|
|
99
|
+
})
|
|
100
|
+
.catch(() => { })
|
|
101
|
+
.finally(() => watchers.delete(job));
|
|
102
|
+
watchers.add(job);
|
|
103
|
+
}
|
|
104
|
+
// Pushes reserved messages and settles the reservation. Resolves "delivered", "failed", "held" or "pending" (watched).
|
|
105
|
+
async function dispatchIds(session, scfg, text, { urgent, ids, messages }) {
|
|
106
|
+
// A Stop hook that started since the ready check delivers the messages: hand them to it (read every second) and mark
|
|
107
|
+
// them, so the inbox copy is hidden. A failed hand-off releases them for the next tick.
|
|
108
|
+
if (holdRunning(session, scfg)) {
|
|
109
|
+
if (await handOff(scfg, messages).then(() => true, () => false)) {
|
|
110
|
+
await markDelivered(scfg, ids).catch(() => { });
|
|
111
|
+
forget(session, ids);
|
|
112
|
+
return "delivered";
|
|
113
|
+
}
|
|
114
|
+
await releaseDelivery(scfg, ids).catch(() => { });
|
|
115
|
+
return "held";
|
|
116
|
+
}
|
|
117
|
+
const result = await push(session, text, { urgent, ids });
|
|
118
|
+
if (result.status === "delivered") {
|
|
119
|
+
await markDelivered(scfg, ids).catch(() => { });
|
|
120
|
+
forget(session, ids);
|
|
121
|
+
return "delivered";
|
|
122
|
+
}
|
|
123
|
+
failed(session, ids);
|
|
124
|
+
if (result.status === "unconfirmed") {
|
|
125
|
+
watchLate(session, scfg, ids, result);
|
|
126
|
+
return "pending";
|
|
127
|
+
}
|
|
128
|
+
await releaseDelivery(scfg, ids).catch(() => { });
|
|
129
|
+
return "failed";
|
|
130
|
+
}
|
|
131
|
+
// The text a session receives. An attachment is cached first, so the session reads this machine's copy
|
|
132
|
+
// instead of downloading the file from the bus again. The sender-identity paragraph is SessionStart only.
|
|
133
|
+
const textFor = async (scfg, messages) => {
|
|
134
|
+
const shown = await cacheAttachmentsWithin(scfg, messages, { budgetMs: ATTACHMENT_BUDGET_MS });
|
|
135
|
+
return formatInbox(shown, { session: scfg.session, peer: scfg.peer });
|
|
136
|
+
};
|
|
137
|
+
// Claude takes a socket message in any state, when its gate is open and no Stop hook holds it; Codex waits for an idle session; OpenCode takes a queued or steering prompt in any state.
|
|
138
|
+
function ready(session, active, cfg) {
|
|
139
|
+
return readySession(session, active, { env, idle, cfg });
|
|
140
|
+
}
|
|
141
|
+
// Folds new receipts and replies to this ledger's messages into reads and answers.
|
|
142
|
+
async function refreshAcks(cfg, entries) {
|
|
143
|
+
const topics = new Set([receiptsTopic(cfg.project), broadcastTopic(cfg.project)]);
|
|
144
|
+
for (const e of Object.values(entries)) {
|
|
145
|
+
topics.add(inboxTopic(cfg.project, e.address.split("#")[0]));
|
|
146
|
+
if (e.from)
|
|
147
|
+
topics.add(inboxTopic(cfg.project, e.from.split("#")[0]));
|
|
148
|
+
}
|
|
149
|
+
for (const topic of topics) {
|
|
150
|
+
const key = `${cfg.project}\n${topic}`;
|
|
151
|
+
let events;
|
|
152
|
+
try {
|
|
153
|
+
events = await pollCached(cfg, topic, cursors.get(key) || "all");
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (events.some((e) => e.reset))
|
|
159
|
+
process.stderr.write(`temmo: bus replayed ${topic} from the oldest event (unknown cursor)\n`);
|
|
160
|
+
if (events.length)
|
|
161
|
+
cursors.set(key, events.at(-1).id);
|
|
162
|
+
if (topic === receiptsTopic(cfg.project)) {
|
|
163
|
+
readReceipts(events, reads);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
for (const ev of events) {
|
|
167
|
+
const m = parseEnvelope(ev.message);
|
|
168
|
+
if (m?.kind !== "msg" || typeof m.replyTo !== "string" || typeof m.session !== "string")
|
|
169
|
+
continue;
|
|
170
|
+
if (!answers.has(m.replyTo))
|
|
171
|
+
answers.set(m.replyTo, new Set());
|
|
172
|
+
answers.get(m.replyTo).add(`${m.from}#${m.session}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const acked = (id, address) => Boolean(reads.get(id)?.has(address) || answers.get(id)?.has(address));
|
|
177
|
+
async function pushProject(cfg, active) {
|
|
178
|
+
const counts = { delivered: 0, redelivered: 0, dropped: 0 };
|
|
179
|
+
const entries = readLedger(cfg);
|
|
180
|
+
const sessions = listSessions(cfg).filter((s) => PUSHED.has(s.harness) && s.online);
|
|
181
|
+
for (const session of sessions) {
|
|
182
|
+
if (nextAction(FIRST_PUSH, now(), { acked: false, busy: false, ready: await ready(session, active, cfg) }) !== "push")
|
|
183
|
+
continue;
|
|
184
|
+
const scfg = { ...cfg, peer: session.peer, session: session.session };
|
|
185
|
+
// The predicate runs before the cap: fyi, broadcasts and messages in backoff or reserved do not use up the claim.
|
|
186
|
+
// A project-chat @mention is not pushed here. claimInbox and the runner deliver it to the session it names.
|
|
187
|
+
const candidates = await claim(scfg, {
|
|
188
|
+
commit: false, limit: CLAIM_LIMIT, accept: (m) => direct(m) && !cooling(session, m.id) && !expired(session, m.id),
|
|
189
|
+
}).catch(() => []);
|
|
190
|
+
if (!candidates.length)
|
|
191
|
+
continue;
|
|
192
|
+
// Reserve before the push so the waiter's claim cannot take the same messages.
|
|
193
|
+
const owned = new Set(await reserveDelivery(scfg, candidates.map((m) => m.id)));
|
|
194
|
+
const waiting = candidates.filter((m) => owned.has(m.id)).map(m => reservedMessage(scfg, m)).filter((m) => m !== null);
|
|
195
|
+
if (!waiting.length)
|
|
196
|
+
continue;
|
|
197
|
+
// A message released after an unconfirmed push is checked over the whole range first: a late confirmation settles it.
|
|
198
|
+
const late = waiting.filter((m) => rechecks.get(slot(session, m.id))?.());
|
|
199
|
+
if (late.length) {
|
|
200
|
+
await markDelivered(scfg, late.map((m) => m.id)).catch(() => { });
|
|
201
|
+
for (const m of late)
|
|
202
|
+
forget(session, [m.id]);
|
|
203
|
+
counts.delivered += late.length;
|
|
204
|
+
}
|
|
205
|
+
const rest = waiting.filter((m) => !late.includes(m));
|
|
206
|
+
if (!rest.length)
|
|
207
|
+
continue;
|
|
208
|
+
const ids = rest.map((m) => m.id);
|
|
209
|
+
const status = await dispatchIds(session, scfg, await textFor(scfg, rest), { urgent: rest.some((m) => m.urgent), ids, messages: rest });
|
|
210
|
+
if (status !== "delivered")
|
|
211
|
+
continue;
|
|
212
|
+
counts.delivered += rest.length;
|
|
213
|
+
for (const m of rest) {
|
|
214
|
+
if (m.expectReply)
|
|
215
|
+
entries[m.id] = { address: session.id, from: m.from, firstAt: now(), lastAt: now(), attempts: 1, message: m };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (Object.keys(entries).length) {
|
|
219
|
+
await refreshAcks(cfg, entries);
|
|
220
|
+
for (const [id, e] of Object.entries(entries)) {
|
|
221
|
+
const session = sessions.find((s) => s.id === e.address);
|
|
222
|
+
const state = { firstAt: e.firstAt, pushes: e.attempts, dueAt: e.lastAt + REDELIVER_MS };
|
|
223
|
+
// Live busy and ready are probed only once the redelivery is due; before that the policy reads acks and the TTL alone.
|
|
224
|
+
const probed = now() >= state.dueAt
|
|
225
|
+
? { busy: Boolean(session && busyWithPush(session, active)), ready: Boolean(session) && await ready(session, active, cfg) }
|
|
226
|
+
: { busy: false, ready: false };
|
|
227
|
+
let action = nextAction(state, now(), { acked: acked(id, e.address), ...probed });
|
|
228
|
+
// Re-check the answer right before redelivering, so one that arrived during the awaits above gets no copy.
|
|
229
|
+
if (action === "push" && !cooling(session, id)) {
|
|
230
|
+
await refreshAcks(cfg, { [id]: e });
|
|
231
|
+
action = nextAction(state, now(), { acked: acked(id, e.address), ...probed });
|
|
232
|
+
}
|
|
233
|
+
if (action === "answered" || action === "expired" || action === "dropped") {
|
|
234
|
+
delete entries[id];
|
|
235
|
+
counts.dropped += 1;
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (action !== "push" || cooling(session, id))
|
|
239
|
+
continue;
|
|
240
|
+
const scfg = { ...cfg, peer: session.peer, session: session.session };
|
|
241
|
+
const again = { ...e.message, attempt: e.attempts + 1, attempts: MAX_ATTEMPTS };
|
|
242
|
+
// Redelivery reserves an id that is already seen, so the seen list can be trimmed without exposing it.
|
|
243
|
+
const owned = await reserveDelivery(scfg, [id], { allowSeen: true });
|
|
244
|
+
if (owned.length && await dispatchIds(session, scfg, await textFor(scfg, [again]), { urgent: again.urgent === true, ids: [id], messages: [again] }) === "delivered") {
|
|
245
|
+
Object.assign(e, { lastAt: now(), attempts: e.attempts + 1 });
|
|
246
|
+
counts.redelivered += 1;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
for (const id of answers.keys()) {
|
|
251
|
+
if (answers.size <= ANSWERS_MAX)
|
|
252
|
+
break;
|
|
253
|
+
answers.delete(id);
|
|
254
|
+
}
|
|
255
|
+
writeLedger(cfg, entries);
|
|
256
|
+
return counts;
|
|
257
|
+
}
|
|
258
|
+
async function tick() {
|
|
259
|
+
const total = { delivered: 0, redelivered: 0, dropped: 0 };
|
|
260
|
+
if (running || await Promise.resolve().then(skip).catch(() => false))
|
|
261
|
+
return total;
|
|
262
|
+
running = true;
|
|
263
|
+
try {
|
|
264
|
+
const probe = await opencode("GET", "/api/session/active", { env });
|
|
265
|
+
const active = probe.ok ? new Set(Object.keys(probe.body?.data ?? probe.body ?? {})) : null;
|
|
266
|
+
for (const cfg of projects()) {
|
|
267
|
+
if (!cfg.enabled)
|
|
268
|
+
continue;
|
|
269
|
+
const counts = await pushProject(cfg, active).catch(() => null);
|
|
270
|
+
for (const key of Object.keys(total))
|
|
271
|
+
total[key] += counts?.[key] ?? 0;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
finally {
|
|
275
|
+
running = false;
|
|
276
|
+
}
|
|
277
|
+
return total;
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
tick,
|
|
281
|
+
drain() {
|
|
282
|
+
return Promise.allSettled([...watchers]);
|
|
283
|
+
},
|
|
284
|
+
start() {
|
|
285
|
+
if (!timer)
|
|
286
|
+
timer = setInterval(() => void tick(), TICK_MS).unref();
|
|
287
|
+
},
|
|
288
|
+
close() {
|
|
289
|
+
clearInterval(timer);
|
|
290
|
+
timer = undefined;
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { closeSync, mkdirSync, openSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
export function openHistory(cfg) {
|
|
6
|
+
const dir = join(cfg.stateDir, "ui-history");
|
|
7
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
8
|
+
const key = createHash("sha256").update(JSON.stringify([cfg.url, cfg.token])).digest("hex");
|
|
9
|
+
const file = join(dir, key + ".sqlite");
|
|
10
|
+
closeSync(openSync(file, "a", 0o600));
|
|
11
|
+
const db = new DatabaseSync(file);
|
|
12
|
+
db.exec(`
|
|
13
|
+
PRAGMA busy_timeout = 5000;
|
|
14
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
15
|
+
topic TEXT NOT NULL, id TEXT NOT NULL, time REAL NOT NULL, payload TEXT NOT NULL,
|
|
16
|
+
PRIMARY KEY (topic, id)
|
|
17
|
+
);
|
|
18
|
+
CREATE INDEX IF NOT EXISTS events_time ON events (topic, time);
|
|
19
|
+
CREATE TABLE IF NOT EXISTS read_positions (
|
|
20
|
+
conversation TEXT NOT NULL, reader TEXT NOT NULL, sequence INTEGER NOT NULL,
|
|
21
|
+
PRIMARY KEY (conversation, reader)
|
|
22
|
+
);
|
|
23
|
+
`);
|
|
24
|
+
const insert = db.prepare("INSERT OR IGNORE INTO events VALUES (?, ?, ?, ?)");
|
|
25
|
+
const select = db.prepare("SELECT rowid AS sequence, payload FROM events WHERE topic = ? ORDER BY time, rowid");
|
|
26
|
+
const recent = db.prepare("SELECT rowid AS sequence, payload FROM events WHERE topic = ? ORDER BY time DESC, rowid DESC LIMIT ?");
|
|
27
|
+
const position = db.prepare("SELECT sequence FROM read_positions WHERE conversation = ? AND reader = ?");
|
|
28
|
+
const mark = db.prepare(`
|
|
29
|
+
INSERT INTO read_positions VALUES (?, ?, ?)
|
|
30
|
+
ON CONFLICT (conversation, reader) DO UPDATE SET sequence = excluded.sequence
|
|
31
|
+
WHERE excluded.sequence > read_positions.sequence
|
|
32
|
+
`);
|
|
33
|
+
return {
|
|
34
|
+
add(topic, events) {
|
|
35
|
+
if (!events.length)
|
|
36
|
+
return [];
|
|
37
|
+
db.exec("BEGIN");
|
|
38
|
+
try {
|
|
39
|
+
const added = events.filter(event => insert.run(topic, event.id, event.time, JSON.stringify(event)).changes);
|
|
40
|
+
db.exec("COMMIT");
|
|
41
|
+
return added;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
db.exec("ROLLBACK");
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
events(topic, limit) {
|
|
49
|
+
const rows = limit === undefined ? select.all(topic) : recent.all(topic, limit).reverse();
|
|
50
|
+
return rows.map(row => ({ ...JSON.parse(row.payload), sequence: row.sequence }));
|
|
51
|
+
},
|
|
52
|
+
position: (conversation, reader) => position.get(conversation, reader)?.sequence ?? 0,
|
|
53
|
+
mark: (conversation, reader, sequence) => mark.run(conversation, reader, sequence),
|
|
54
|
+
close: () => db.close(),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { isGithubLogin } from "../../lib/config.js";
|
|
3
|
+
function error(status, message) {
|
|
4
|
+
return Response.json({ error: message }, { status, headers: { "cache-control": "no-store" } });
|
|
5
|
+
}
|
|
6
|
+
// Each UI instance sees only one owner's projects, including its implicit default,
|
|
7
|
+
// Home, archives, attachments and actions that broadcast to every visible project.
|
|
8
|
+
export function createHostedUi({ auth, projects, stateDir, origin, createUi }) {
|
|
9
|
+
const groups = new Map();
|
|
10
|
+
const credentials = new Map();
|
|
11
|
+
const names = new Set();
|
|
12
|
+
for (const cfg of projects) {
|
|
13
|
+
if (!isGithubLogin(cfg.owner) || !cfg.token)
|
|
14
|
+
throw new Error("Hosted projects require an owner and a dedicated bus token");
|
|
15
|
+
const owner = cfg.owner.toLowerCase();
|
|
16
|
+
if (credentials.has(cfg.token) && credentials.get(cfg.token) !== owner) {
|
|
17
|
+
throw new Error("Hosted users must not share bus tokens");
|
|
18
|
+
}
|
|
19
|
+
if (names.has(cfg.project))
|
|
20
|
+
throw new Error("Hosted projects require unique topic namespaces");
|
|
21
|
+
names.add(cfg.project);
|
|
22
|
+
credentials.set(cfg.token, owner);
|
|
23
|
+
if (!groups.has(owner))
|
|
24
|
+
groups.set(owner, []);
|
|
25
|
+
groups.get(owner).push({ ...cfg, owner, stateDir: join(stateDir, "hosted", owner) });
|
|
26
|
+
}
|
|
27
|
+
const instances = new Map();
|
|
28
|
+
function call(request, method, ...args) {
|
|
29
|
+
const caller = request && auth.user(request);
|
|
30
|
+
if (!caller)
|
|
31
|
+
return error(401, "Sign in with GitHub");
|
|
32
|
+
const login = caller.login.toLowerCase();
|
|
33
|
+
if (!groups.has(login))
|
|
34
|
+
return error(404, "No project is assigned to your account");
|
|
35
|
+
if (!instances.has(login)) {
|
|
36
|
+
instances.set(login, createUi({ projects: groups.get(login), allowedOrigin: origin, auth, hosted: true }));
|
|
37
|
+
}
|
|
38
|
+
const instance = instances.get(login);
|
|
39
|
+
return method === "file" ? instance.file(args[0], request) : instance[method](request, ...args);
|
|
40
|
+
}
|
|
41
|
+
const unavailable = (request) => auth.user(request)
|
|
42
|
+
? error(404, "Unavailable in hosted mode")
|
|
43
|
+
: error(401, "Sign in with GitHub");
|
|
44
|
+
return {
|
|
45
|
+
auth: auth.route,
|
|
46
|
+
events: (request) => call(request, "events"),
|
|
47
|
+
handle: (request, action) => call(request, "handle", action),
|
|
48
|
+
file: (name, request) => call(request, "file", name),
|
|
49
|
+
push: unavailable,
|
|
50
|
+
schedules: unavailable,
|
|
51
|
+
lessons: unavailable,
|
|
52
|
+
close() {
|
|
53
|
+
for (const instance of instances.values())
|
|
54
|
+
instance.close();
|
|
55
|
+
instances.clear();
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { openHistory } from "./history.js";
|
|
2
|
+
import { pollCached, streamEvents } from "../../lib/messaging.js";
|
|
3
|
+
export const HISTORY_LIMIT = 500;
|
|
4
|
+
const MAX_TOPICS = 40;
|
|
5
|
+
const RESUBSCRIBE_MS = 50;
|
|
6
|
+
const RETRY_MIN_MS = 1000;
|
|
7
|
+
const RETRY_MAX_MS = 60_000;
|
|
8
|
+
const RATE_LIMIT_MS = 30_000;
|
|
9
|
+
// One bus streaming subscription shared by every browser: history is fetched once per topic, then kept live.
|
|
10
|
+
export function createHub(cfg, { keep = () => false } = {}) {
|
|
11
|
+
const history = openHistory(cfg);
|
|
12
|
+
const topics = new Map();
|
|
13
|
+
const listeners = new Set();
|
|
14
|
+
let latestTime = Math.floor(Date.now() / 1000);
|
|
15
|
+
let stream = null;
|
|
16
|
+
let timer;
|
|
17
|
+
let retry = RETRY_MIN_MS;
|
|
18
|
+
let closed = false;
|
|
19
|
+
function add(topic, events, { replay = false } = {}) {
|
|
20
|
+
const entry = topics.get(topic);
|
|
21
|
+
if (closed)
|
|
22
|
+
return [];
|
|
23
|
+
if (!replay)
|
|
24
|
+
events = history.add(topic, events);
|
|
25
|
+
if (!entry?.loaded)
|
|
26
|
+
return [];
|
|
27
|
+
const added = [];
|
|
28
|
+
for (const ev of events) {
|
|
29
|
+
if (entry.ids.has(ev.id))
|
|
30
|
+
continue;
|
|
31
|
+
added.push(ev);
|
|
32
|
+
entry.ids.add(ev.id);
|
|
33
|
+
entry.events.push(ev);
|
|
34
|
+
}
|
|
35
|
+
for (const ev of entry.events.splice(0, Math.max(0, entry.events.length - HISTORY_LIMIT)))
|
|
36
|
+
entry.ids.delete(ev.id);
|
|
37
|
+
if (added.length)
|
|
38
|
+
for (const listener of listeners)
|
|
39
|
+
listener(topic, added);
|
|
40
|
+
return added;
|
|
41
|
+
}
|
|
42
|
+
function evict() {
|
|
43
|
+
const idle = [...topics].filter(([topic, entry]) => entry.loaded && !keep(topic)).sort((a, b) => a[1].used - b[1].used);
|
|
44
|
+
while (topics.size > MAX_TOPICS && idle.length)
|
|
45
|
+
topics.delete(idle.shift()[0]);
|
|
46
|
+
}
|
|
47
|
+
function schedule(ms) {
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
if (!closed)
|
|
50
|
+
timer = setTimeout(connect, ms);
|
|
51
|
+
timer?.unref?.();
|
|
52
|
+
}
|
|
53
|
+
async function connect() {
|
|
54
|
+
stream?.abort();
|
|
55
|
+
const names = [...topics].filter(([, entry]) => entry.loaded).map(([topic]) => topic);
|
|
56
|
+
if (closed || !names.length)
|
|
57
|
+
return;
|
|
58
|
+
const ctrl = new AbortController();
|
|
59
|
+
stream = ctrl;
|
|
60
|
+
let limited = false;
|
|
61
|
+
try {
|
|
62
|
+
for await (const ev of streamEvents(cfg, names, { since: String(latestTime - 1), signal: ctrl.signal, onOpen: () => { retry = RETRY_MIN_MS; } })) {
|
|
63
|
+
add(ev.topic, [ev]);
|
|
64
|
+
latestTime = Math.max(latestTime, ev.time);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
limited = error.ntfyStatus === 429;
|
|
69
|
+
}
|
|
70
|
+
if (stream !== ctrl || closed)
|
|
71
|
+
return;
|
|
72
|
+
schedule(limited ? Math.max(retry, RATE_LIMIT_MS) : retry);
|
|
73
|
+
retry = Math.min(retry * 2, RETRY_MAX_MS);
|
|
74
|
+
}
|
|
75
|
+
// tailBytes: history keeps the newest events when the topic's cache exceeds the poll cap.
|
|
76
|
+
function open(topic, { tailBytes = 0 } = {}) {
|
|
77
|
+
let entry = topics.get(topic);
|
|
78
|
+
if (!entry) {
|
|
79
|
+
const created = { events: [], ids: new Set(), loaded: false, used: 0 };
|
|
80
|
+
entry = created;
|
|
81
|
+
topics.set(topic, created);
|
|
82
|
+
created.ready = (async () => {
|
|
83
|
+
let events;
|
|
84
|
+
try {
|
|
85
|
+
events = await pollCached(cfg, topic, "all", undefined, { tailBytes });
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (closed)
|
|
89
|
+
return;
|
|
90
|
+
if (!history.events(topic, 1).length) {
|
|
91
|
+
topics.delete(topic);
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (closed)
|
|
96
|
+
return;
|
|
97
|
+
if (events)
|
|
98
|
+
history.add(topic, events);
|
|
99
|
+
created.loaded = true;
|
|
100
|
+
add(topic, history.events(topic, HISTORY_LIMIT), { replay: true });
|
|
101
|
+
evict();
|
|
102
|
+
schedule(RESUBSCRIBE_MS);
|
|
103
|
+
})();
|
|
104
|
+
}
|
|
105
|
+
entry.used = Date.now();
|
|
106
|
+
return entry.ready;
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
open,
|
|
110
|
+
record: (event) => add(event.topic, [event]),
|
|
111
|
+
history,
|
|
112
|
+
events: (topic) => topics.get(topic)?.events ?? [],
|
|
113
|
+
listen(listener) {
|
|
114
|
+
listeners.add(listener);
|
|
115
|
+
return () => listeners.delete(listener);
|
|
116
|
+
},
|
|
117
|
+
close() {
|
|
118
|
+
if (closed)
|
|
119
|
+
return;
|
|
120
|
+
closed = true;
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
stream?.abort();
|
|
123
|
+
history.close();
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Node loads the messaging server unbundled, so lib/*.mjs keeps its own module graph and optional imports.
|
|
2
|
+
export async function ui() {
|
|
3
|
+
const { ui: instance } = await import(/* turbopackIgnore: true */ /* webpackIgnore: true */ process.env.TEMMO_UI_SERVER);
|
|
4
|
+
return instance();
|
|
5
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { execRun, requestLand, HOST_CHECKOUT } from "../../lib/land.js";
|
|
2
|
+
const stages = ["pm-merge", "answer-sync"];
|
|
3
|
+
const stage = (id) => stages.findIndex(name => id === name || id.startsWith(name + "-"));
|
|
4
|
+
function rank(id) {
|
|
5
|
+
const index = stage(id);
|
|
6
|
+
return index === -1 ? stages.length : index;
|
|
7
|
+
}
|
|
8
|
+
function authorized(task) {
|
|
9
|
+
const answer = task.question?.answer;
|
|
10
|
+
return task.status === "review" && task.review && !task.review.decision &&
|
|
11
|
+
/^operator(?::.+)?$/.test(answer?.by) && /^merge(?:[ -])when(?:[ -])green$/i.test(answer.text.trim());
|
|
12
|
+
}
|
|
13
|
+
// Opens the review branch's PR into main; never pushes main.
|
|
14
|
+
export async function mergeReview(review, env, { run = execRun, cwd = HOST_CHECKOUT } = {}) {
|
|
15
|
+
try {
|
|
16
|
+
const tip = await run("git", ["rev-parse", "--verify", `refs/heads/${review.branch}^{commit}`], { cwd, encoding: "utf8" });
|
|
17
|
+
if (tip.status !== 0 || tip.stdout.trim() !== review.commit) {
|
|
18
|
+
return { landed: false, detail: "the build branch moved past the submitted commit" };
|
|
19
|
+
}
|
|
20
|
+
const { url } = await requestLand(review.branch, { run, cwd });
|
|
21
|
+
return { landed: false, opened: true, detail: `PR ${url}` };
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
return { landed: false, detail: error.message };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function landReviews(projects, { merge, by, onReceipt = () => { } }) {
|
|
28
|
+
const queue = projects.flatMap(project => project.tasks.list().filter(authorized).map((task) => ({ project, task })));
|
|
29
|
+
queue.sort((a, b) => rank(a.task.id) - rank(b.task.id) || a.task.review.at - b.task.review.at);
|
|
30
|
+
const receipts = [];
|
|
31
|
+
let prerequisite;
|
|
32
|
+
for (const { project, task } of queue) {
|
|
33
|
+
const { id, review } = task;
|
|
34
|
+
const result = { project: project.name, id, landed: false };
|
|
35
|
+
try {
|
|
36
|
+
if (prerequisite)
|
|
37
|
+
throw new Error(`Waiting for ${prerequisite}`);
|
|
38
|
+
const current = project.tasks.get(id);
|
|
39
|
+
if (!authorized(current) || current.review.submission !== review.submission ||
|
|
40
|
+
current.review.commit !== review.commit || current.review.branch !== review.branch) {
|
|
41
|
+
throw new Error("The review or its authorization changed");
|
|
42
|
+
}
|
|
43
|
+
if (review.check.code !== 0 || current.review.check.code !== 0)
|
|
44
|
+
throw new Error("The submitted check is red");
|
|
45
|
+
if (current.blockers.some((blocker) => project.tasks.get(blocker).status !== "done")) {
|
|
46
|
+
throw new Error("The task has unfinished blockers");
|
|
47
|
+
}
|
|
48
|
+
Object.assign(result, await merge(review));
|
|
49
|
+
if (result.landed) {
|
|
50
|
+
await project.tasks.decideReview(id, { ...review, action: "accept", by, landedAnswer: current.question.answer });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
result.detail = result.landed ? `${result.detail}. Task update failed: ${error.message}` : error.message;
|
|
55
|
+
if (result.landed)
|
|
56
|
+
result.updateFailed = true;
|
|
57
|
+
}
|
|
58
|
+
if (stage(id) !== -1 && ((!result.landed && !result.opened) || result.updateFailed))
|
|
59
|
+
prerequisite ??= id;
|
|
60
|
+
receipts.push(result);
|
|
61
|
+
onReceipt(result);
|
|
62
|
+
}
|
|
63
|
+
return receipts;
|
|
64
|
+
}
|