@otium-team/negotium 0.0.0-stage → 0.23.13
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/LICENSE +202 -0
- package/README.md +329 -2
- package/bin/codex-sdk-window-hide.mjs +40 -0
- package/bin/negotium.mjs +22 -0
- package/dist/agent-helpers.js +23090 -0
- package/dist/agent-helpers.js.map +171 -0
- package/dist/background-bash.js +535 -0
- package/dist/background-bash.js.map +16 -0
- package/dist/browser-runtime.js +2334 -0
- package/dist/browser-runtime.js.map +35 -0
- package/dist/canonical-mcp-bridge.js +13 -0
- package/dist/canonical-mcp-bridge.js.map +9 -0
- package/dist/chunk-22kg3fsw.js +79 -0
- package/dist/chunk-22kg3fsw.js.map +9 -0
- package/dist/chunk-d4j77adx.js +336 -0
- package/dist/chunk-d4j77adx.js.map +13 -0
- package/dist/chunk-mk9x18jn.js +172 -0
- package/dist/chunk-mk9x18jn.js.map +12 -0
- package/dist/chunk-qd0vtfm2.js +3923 -0
- package/dist/chunk-qd0vtfm2.js.map +47 -0
- package/dist/chunk-wf7ny8eq.js +206 -0
- package/dist/chunk-wf7ny8eq.js.map +10 -0
- package/dist/chunk-ww8a4ej4.js +54 -0
- package/dist/chunk-ww8a4ej4.js.map +10 -0
- package/dist/cron.js +1 -0
- package/dist/hosted-agent.js +5982 -0
- package/dist/hosted-agent.js.map +62 -0
- package/dist/main.js +60229 -0
- package/dist/main.js.map +340 -0
- package/dist/mcp-catalog.js +39 -0
- package/dist/mcp-catalog.js.map +10 -0
- package/dist/mcp-factories.js +25767 -0
- package/dist/mcp-factories.js.map +181 -0
- package/dist/mcp-servers.js +50 -0
- package/dist/mcp-servers.js.map +10 -0
- package/dist/media.js +570 -0
- package/dist/media.js.map +15 -0
- package/dist/outbox.js +354 -0
- package/dist/outbox.js.map +15 -0
- package/dist/platform-runtime.js +73 -0
- package/dist/platform-runtime.js.map +12 -0
- package/dist/prompts.js +892 -0
- package/dist/prompts.js.map +15 -0
- package/dist/query-runtime.js +1071 -0
- package/dist/query-runtime.js.map +26 -0
- package/dist/registry.js +335 -0
- package/dist/registry.js.map +14 -0
- package/dist/rollout.js +57 -0
- package/dist/rollout.js.map +10 -0
- package/dist/runtime/cron/background-sessions.ts +119 -0
- package/dist/runtime/cron/context.ts +123 -0
- package/dist/runtime/cron/host.ts +67 -0
- package/dist/runtime/cron/index.ts +95 -0
- package/dist/runtime/cron/mcp-server.ts +556 -0
- package/dist/runtime/cron/module.ts +174 -0
- package/dist/runtime/cron/package.json +6 -0
- package/dist/runtime/cron/schedule.ts +211 -0
- package/dist/runtime/cron/scheduler.ts +444 -0
- package/dist/runtime/cron/scripts.ts +203 -0
- package/dist/runtime/cron/store.ts +1163 -0
- package/dist/runtime/cron/summarize.ts +115 -0
- package/dist/runtime/package.json +6 -0
- package/dist/runtime/scripts/faster-whisper-wrapper.py +87 -0
- package/dist/runtime/src/agents/api-topic-agent-switch.ts +179 -0
- package/dist/runtime/src/agents/archiver.ts +677 -0
- package/dist/runtime/src/agents/auth-check.ts +156 -0
- package/dist/runtime/src/agents/claude-notices.ts +130 -0
- package/dist/runtime/src/agents/claude-provider.ts +941 -0
- package/dist/runtime/src/agents/claude-registry.ts +117 -0
- package/dist/runtime/src/agents/claude-runtime-inspection.ts +43 -0
- package/dist/runtime/src/agents/codex-app-server.ts +138 -0
- package/dist/runtime/src/agents/codex-native-multi-agent.ts +360 -0
- package/dist/runtime/src/agents/codex-provider.ts +1193 -0
- package/dist/runtime/src/agents/codex-registry.ts +116 -0
- package/dist/runtime/src/agents/codex-tier-resolver.ts +74 -0
- package/dist/runtime/src/agents/codex-tree-kill.ts +54 -0
- package/dist/runtime/src/agents/codex-tree-manager.ts +230 -0
- package/dist/runtime/src/agents/codex-vault-hook-bridge.ts +240 -0
- package/dist/runtime/src/agents/codex-vault-hook.mjs +50 -0
- package/dist/runtime/src/agents/codex-windows-wrapper.ts +174 -0
- package/dist/runtime/src/agents/compaction-support.ts +75 -0
- package/dist/runtime/src/agents/compaction-tool-projection.ts +125 -0
- package/dist/runtime/src/agents/contracts.ts +75 -0
- package/dist/runtime/src/agents/deep-map.ts +13 -0
- package/dist/runtime/src/agents/execution-host.ts +118 -0
- package/dist/runtime/src/agents/explicit-agent-switch.ts +106 -0
- package/dist/runtime/src/agents/fixtures/claude-attachments.jsonl +2 -0
- package/dist/runtime/src/agents/fixtures/codex-shell.jsonl +5 -0
- package/dist/runtime/src/agents/fork.ts +100 -0
- package/dist/runtime/src/agents/hosted-agent.ts +78 -0
- package/dist/runtime/src/agents/idle-archiver.ts +225 -0
- package/dist/runtime/src/agents/idle-compact.ts +225 -0
- package/dist/runtime/src/agents/index.ts +225 -0
- package/dist/runtime/src/agents/maestro-provider.ts +246 -0
- package/dist/runtime/src/agents/maestro-registry.ts +189 -0
- package/dist/runtime/src/agents/mcp-tools/ask-user.ts +653 -0
- package/dist/runtime/src/agents/mcp-tools/common.ts +23 -0
- package/dist/runtime/src/agents/mcp-tools/inline-assets.ts +203 -0
- package/dist/runtime/src/agents/mcp-tools/publish-html.ts +183 -0
- package/dist/runtime/src/agents/mcp-tools/request-vault.ts +684 -0
- package/dist/runtime/src/agents/mcp-tools/self-config.ts +353 -0
- package/dist/runtime/src/agents/mcp-tools/spawn-subagent.ts +1349 -0
- package/dist/runtime/src/agents/mcp-tools/visual-compat.ts +18 -0
- package/dist/runtime/src/agents/mcp-tools/visuals.ts +60 -0
- package/dist/runtime/src/agents/memory-archive-policy.ts +54 -0
- package/dist/runtime/src/agents/model-catalog.ts +439 -0
- package/dist/runtime/src/agents/public-helpers.ts +149 -0
- package/dist/runtime/src/agents/registry.ts +35 -0
- package/dist/runtime/src/agents/rollout/claude.ts +336 -0
- package/dist/runtime/src/agents/rollout/codex.ts +910 -0
- package/dist/runtime/src/agents/rollout/index.ts +26 -0
- package/dist/runtime/src/agents/rollout/shared.ts +293 -0
- package/dist/runtime/src/agents/self-config-core.ts +704 -0
- package/dist/runtime/src/agents/task-events.ts +59 -0
- package/dist/runtime/src/agents/tool-format.ts +613 -0
- package/dist/runtime/src/agents/topic-agent-switch.ts +189 -0
- package/dist/runtime/src/agents/topic-cleanup.ts +285 -0
- package/dist/runtime/src/agents/vault-tool-policy.ts +36 -0
- package/dist/runtime/src/application/execute-external-user-turn.ts +23 -0
- package/dist/runtime/src/application/submit-runtime-gateway-turn.ts +484 -0
- package/dist/runtime/src/application/submit-user-message.ts +60 -0
- package/dist/runtime/src/application/switch-topic-effort.ts +48 -0
- package/dist/runtime/src/application/switch-topic-model.ts +79 -0
- package/dist/runtime/src/application/topic-service.ts +190 -0
- package/dist/runtime/src/application/turn-follow-ups.ts +291 -0
- package/dist/runtime/src/application/vault-command.ts +170 -0
- package/dist/runtime/src/bus.ts +409 -0
- package/dist/runtime/src/config-public.ts +2 -0
- package/dist/runtime/src/cron-host.ts +33 -0
- package/dist/runtime/src/cron-mcp-host.ts +5 -0
- package/dist/runtime/src/identifiers.ts +49 -0
- package/dist/runtime/src/index.ts +573 -0
- package/dist/runtime/src/mcp/agent-health-server.ts +8 -0
- package/dist/runtime/src/mcp/canonical-bridge-config.ts +84 -0
- package/dist/runtime/src/mcp/canonical-proxy-server.ts +308 -0
- package/dist/runtime/src/mcp/compaction-log-server.ts +25 -0
- package/dist/runtime/src/mcp/decision-server.ts +21 -0
- package/dist/runtime/src/mcp/dm-ipc.ts +60 -0
- package/dist/runtime/src/mcp/factories/agent-health.ts +394 -0
- package/dist/runtime/src/mcp/factories/compaction-log.ts +135 -0
- package/dist/runtime/src/mcp/factories/decision.ts +186 -0
- package/dist/runtime/src/mcp/factories/index.ts +73 -0
- package/dist/runtime/src/mcp/factories/session-comm.ts +184 -0
- package/dist/runtime/src/mcp/factories/session-history.ts +103 -0
- package/dist/runtime/src/mcp/factories/stdio-protection.ts +24 -0
- package/dist/runtime/src/mcp/factories/system-health.ts +259 -0
- package/dist/runtime/src/mcp/factories/task.ts +185 -0
- package/dist/runtime/src/mcp/factories/token-stats.ts +145 -0
- package/dist/runtime/src/mcp/factories/vault.ts +32 -0
- package/dist/runtime/src/mcp/mcp-helpers.ts +40 -0
- package/dist/runtime/src/mcp/peer-bridge.ts +119 -0
- package/dist/runtime/src/mcp/runtime-spec.ts +473 -0
- package/dist/runtime/src/mcp/session-comm/actor-policy.ts +160 -0
- package/dist/runtime/src/mcp/session-comm/bridge-ipc-config.ts +27 -0
- package/dist/runtime/src/mcp/session-comm/context.ts +113 -0
- package/dist/runtime/src/mcp/session-comm/default-host.ts +723 -0
- package/dist/runtime/src/mcp/session-comm/history-target.ts +89 -0
- package/dist/runtime/src/mcp/session-comm/history-tools.ts +113 -0
- package/dist/runtime/src/mcp/session-comm/hub-remote-session.ts +553 -0
- package/dist/runtime/src/mcp/session-comm/limits.ts +8 -0
- package/dist/runtime/src/mcp/session-comm/peer-forward.ts +251 -0
- package/dist/runtime/src/mcp/session-comm/runtime.ts +128 -0
- package/dist/runtime/src/mcp/session-comm/server.ts +1036 -0
- package/dist/runtime/src/mcp/session-comm/tell-permissions.ts +79 -0
- package/dist/runtime/src/mcp/session-comm/topic-catalog.ts +155 -0
- package/dist/runtime/src/mcp/session-comm/topic-config.ts +137 -0
- package/dist/runtime/src/mcp/session-comm/topics.ts +313 -0
- package/dist/runtime/src/mcp/session-history-limits.ts +36 -0
- package/dist/runtime/src/mcp/session-history-reader.ts +359 -0
- package/dist/runtime/src/mcp/session-history-server.ts +19 -0
- package/dist/runtime/src/mcp/stdio-protect.ts +7 -0
- package/dist/runtime/src/mcp/system-health-server.ts +5 -0
- package/dist/runtime/src/mcp/task-server.ts +17 -0
- package/dist/runtime/src/mcp/token-stats-server.ts +10 -0
- package/dist/runtime/src/mcp/vault-server.ts +12 -0
- package/dist/runtime/src/mcp/wiki-search-index.ts +359 -0
- package/dist/runtime/src/mcp/wiki-server.ts +2386 -0
- package/dist/runtime/src/mcp-runtime-host.ts +81 -0
- package/dist/runtime/src/media/file-events.ts +33 -0
- package/dist/runtime/src/media/text-extractor.ts +334 -0
- package/dist/runtime/src/media/video.ts +92 -0
- package/dist/runtime/src/node-host.ts +211 -0
- package/dist/runtime/src/outbox/coalescing-runner.ts +157 -0
- package/dist/runtime/src/outbox/file-ops.ts +267 -0
- package/dist/runtime/src/outbox/index.ts +26 -0
- package/dist/runtime/src/outbox/utils.ts +74 -0
- package/dist/runtime/src/platform/background-bash/context.ts +9 -0
- package/dist/runtime/src/platform/background-bash/manager.ts +292 -0
- package/dist/runtime/src/platform/background-bash/output-buffer.ts +323 -0
- package/dist/runtime/src/platform/config-helpers.ts +47 -0
- package/dist/runtime/src/platform/config.ts +618 -0
- package/dist/runtime/src/platform/constants.ts +30 -0
- package/dist/runtime/src/platform/debug-users.ts +14 -0
- package/dist/runtime/src/platform/delay.ts +1 -0
- package/dist/runtime/src/platform/error.ts +9 -0
- package/dist/runtime/src/platform/file-utils.ts +35 -0
- package/dist/runtime/src/platform/ipc.ts +55 -0
- package/dist/runtime/src/platform/jsonl.ts +277 -0
- package/dist/runtime/src/platform/lifecycle.ts +176 -0
- package/dist/runtime/src/platform/log-rotation.ts +45 -0
- package/dist/runtime/src/platform/logger.ts +42 -0
- package/dist/runtime/src/platform/maestro-bootstrap-env.ts +3 -0
- package/dist/runtime/src/platform/mcp-catalog-policy.ts +63 -0
- package/dist/runtime/src/platform/mcp-config.ts +1329 -0
- package/dist/runtime/src/platform/memoize-import.ts +26 -0
- package/dist/runtime/src/platform/modules.ts +145 -0
- package/dist/runtime/src/platform/node-plugins.ts +46 -0
- package/dist/runtime/src/platform/paths.ts +62 -0
- package/dist/runtime/src/platform/playwright/browser-processes.ts +300 -0
- package/dist/runtime/src/platform/playwright/capability.ts +5 -0
- package/dist/runtime/src/platform/playwright/headed-launch.ts +61 -0
- package/dist/runtime/src/platform/playwright/manager-utils.ts +128 -0
- package/dist/runtime/src/platform/playwright/manager.ts +1502 -0
- package/dist/runtime/src/platform/playwright/mcp-transport.ts +35 -0
- package/dist/runtime/src/platform/playwright/profile-management.ts +33 -0
- package/dist/runtime/src/platform/playwright/public-runtime.ts +52 -0
- package/dist/runtime/src/platform/playwright/transport-probe.ts +89 -0
- package/dist/runtime/src/platform/playwright/vault-broker.ts +261 -0
- package/dist/runtime/src/platform/public-runtime.ts +18 -0
- package/dist/runtime/src/prompts/agents/wiki-archiver.md +194 -0
- package/dist/runtime/src/prompts/builders.ts +696 -0
- package/dist/runtime/src/prompts/sessions/_shared-tools.md +38 -0
- package/dist/runtime/src/prompts/sessions/channel-system.md +13 -0
- package/dist/runtime/src/prompts/sessions/manager-system.md +34 -0
- package/dist/runtime/src/prompts/sessions/surfaces/cli.md +6 -0
- package/dist/runtime/src/prompts/sessions/surfaces/otium-manager.md +18 -0
- package/dist/runtime/src/prompts/sessions/surfaces/otium.md +6 -0
- package/dist/runtime/src/prompts/sessions/surfaces/telegram.md +5 -0
- package/dist/runtime/src/prompts/sessions/surfaces/terminal.md +6 -0
- package/dist/runtime/src/prompts/sessions/topic-system.md +9 -0
- package/dist/runtime/src/prompts/sessions/visual-design.md +38 -0
- package/dist/runtime/src/query/active-rooms.ts +514 -0
- package/dist/runtime/src/query/index.ts +23 -0
- package/dist/runtime/src/query/public-runtime.ts +28 -0
- package/dist/runtime/src/query/room-query-registry.ts +217 -0
- package/dist/runtime/src/query/session-inbox-cleanup.ts +45 -0
- package/dist/runtime/src/query/session-inbox-path.ts +42 -0
- package/dist/runtime/src/query/state.ts +116 -0
- package/dist/runtime/src/query/types.ts +8 -0
- package/dist/runtime/src/runtime/actor-topic-reach.ts +123 -0
- package/dist/runtime/src/runtime/actor-topic-scope.ts +255 -0
- package/dist/runtime/src/runtime/ask-callbacks.ts +217 -0
- package/dist/runtime/src/runtime/attachments.ts +149 -0
- package/dist/runtime/src/runtime/background-session-policy.ts +1 -0
- package/dist/runtime/src/runtime/background-sessions.ts +288 -0
- package/dist/runtime/src/runtime/bashrs-completions.ts +350 -0
- package/dist/runtime/src/runtime/channel-context.ts +115 -0
- package/dist/runtime/src/runtime/context-warning.ts +131 -0
- package/dist/runtime/src/runtime/cron-sessions.ts +35 -0
- package/dist/runtime/src/runtime/delivery-ack.ts +112 -0
- package/dist/runtime/src/runtime/errors.ts +70 -0
- package/dist/runtime/src/runtime/event-heartbeat.ts +77 -0
- package/dist/runtime/src/runtime/file-hooks.ts +81 -0
- package/dist/runtime/src/runtime/footer.ts +23 -0
- package/dist/runtime/src/runtime/gateway-capabilities.ts +22 -0
- package/dist/runtime/src/runtime/inbox.ts +1307 -0
- package/dist/runtime/src/runtime/playwright-turn-abort.ts +59 -0
- package/dist/runtime/src/runtime/public-helpers.ts +78 -0
- package/dist/runtime/src/runtime/remote-session-grant.ts +221 -0
- package/dist/runtime/src/runtime/remote-session-inbox.ts +943 -0
- package/dist/runtime/src/runtime/remote-session-reply-outbox.ts +168 -0
- package/dist/runtime/src/runtime/task-format.ts +18 -0
- package/dist/runtime/src/runtime/tasks.ts +53 -0
- package/dist/runtime/src/runtime/thread-context.ts +123 -0
- package/dist/runtime/src/runtime/thread-read.ts +67 -0
- package/dist/runtime/src/runtime/topic-config.ts +29 -0
- package/dist/runtime/src/runtime/turn-event-stream.ts +1051 -0
- package/dist/runtime/src/runtime/turn-runner.ts +2311 -0
- package/dist/runtime/src/runtime/turn-session.ts +116 -0
- package/dist/runtime/src/runtime/usage-alert.ts +51 -0
- package/dist/runtime/src/runtime/user-turn-envelope.ts +94 -0
- package/dist/runtime/src/runtime/visual-html.ts +175 -0
- package/dist/runtime/src/runtime/visual-store.ts +480 -0
- package/dist/runtime/src/runtime/visuals.ts +209 -0
- package/dist/runtime/src/runtime-gateway.ts +1045 -0
- package/dist/runtime/src/security/sanitize.ts +27 -0
- package/dist/runtime/src/security/sensitive-path.ts +41 -0
- package/dist/runtime/src/storage/activity-log.ts +167 -0
- package/dist/runtime/src/storage/api-messages.ts +766 -0
- package/dist/runtime/src/storage/api-topic-brief.ts +134 -0
- package/dist/runtime/src/storage/api-topic-config.ts +197 -0
- package/dist/runtime/src/storage/api-topics.ts +1647 -0
- package/dist/runtime/src/storage/app-settings.ts +50 -0
- package/dist/runtime/src/storage/ask-user-gates.ts +359 -0
- package/dist/runtime/src/storage/browser-profiles.ts +186 -0
- package/dist/runtime/src/storage/conversation-migration.ts +180 -0
- package/dist/runtime/src/storage/conversations.ts +377 -0
- package/dist/runtime/src/storage/decisions.ts +231 -0
- package/dist/runtime/src/storage/forum/agent-settings.ts +20 -0
- package/dist/runtime/src/storage/forum/index.ts +4 -0
- package/dist/runtime/src/storage/forum/repository.ts +283 -0
- package/dist/runtime/src/storage/forum/schema.ts +314 -0
- package/dist/runtime/src/storage/forum-db.ts +4 -0
- package/dist/runtime/src/storage/remote-session.ts +1028 -0
- package/dist/runtime/src/storage/runtime-events.ts +209 -0
- package/dist/runtime/src/storage/runtime-follow-ups.ts +212 -0
- package/dist/runtime/src/storage/runtime-gateway-submissions.ts +132 -0
- package/dist/runtime/src/storage/runtime-leases.ts +190 -0
- package/dist/runtime/src/storage/runtime-process-leases.ts +288 -0
- package/dist/runtime/src/storage/runtime-topic-state.ts +181 -0
- package/dist/runtime/src/storage/runtime-turn-requests.ts +846 -0
- package/dist/runtime/src/storage/self-schedules.ts +306 -0
- package/dist/runtime/src/storage/session-asks.ts +468 -0
- package/dist/runtime/src/storage/session-inbox-signal.ts +76 -0
- package/dist/runtime/src/storage/session-inbox.ts +161 -0
- package/dist/runtime/src/storage/sqlite.ts +154 -0
- package/dist/runtime/src/storage/storage-contract.ts +54 -0
- package/dist/runtime/src/storage/storage-host.ts +336 -0
- package/dist/runtime/src/storage/storage-public-host.ts +16 -0
- package/dist/runtime/src/storage/storage-public.ts +66 -0
- package/dist/runtime/src/storage/tasks.ts +172 -0
- package/dist/runtime/src/storage/token-stats.ts +432 -0
- package/dist/runtime/src/storage/topic-archive-state.ts +226 -0
- package/dist/runtime/src/storage/topic-archive.ts +169 -0
- package/dist/runtime/src/storage/topic-host-mcp-grants.ts +54 -0
- package/dist/runtime/src/storage/topic-link-records.ts +1085 -0
- package/dist/runtime/src/storage/topic-settings.ts +67 -0
- package/dist/runtime/src/storage/topic-tool-capabilities.ts +89 -0
- package/dist/runtime/src/storage/topic-transcript.ts +98 -0
- package/dist/runtime/src/storage/vault-crypto-core.ts +65 -0
- package/dist/runtime/src/storage/vault-crypto.ts +31 -0
- package/dist/runtime/src/storage/vault-public.ts +27 -0
- package/dist/runtime/src/storage/vault-request-gates.ts +359 -0
- package/dist/runtime/src/storage/vault.ts +388 -0
- package/dist/runtime/src/storage/wiki-summary-names.ts +52 -0
- package/dist/runtime/src/storage/wiki.ts +90 -0
- package/dist/runtime/src/topics/claim-abort.ts +190 -0
- package/dist/runtime/src/topics/create.ts +192 -0
- package/dist/runtime/src/topics/derive.ts +600 -0
- package/dist/runtime/src/topics/lifecycle.ts +425 -0
- package/dist/runtime/src/topics/links.ts +7 -0
- package/dist/runtime/src/topics/personal-general.ts +91 -0
- package/dist/runtime/src/topics/session.ts +1041 -0
- package/dist/runtime/src/topics/update.ts +287 -0
- package/dist/runtime/src/types/api.ts +591 -0
- package/dist/runtime/src/types.ts +418 -0
- package/dist/runtime/src/version.ts +1 -0
- package/dist/runtime/tsconfig.json +13 -0
- package/dist/runtime-gateway.js +514 -0
- package/dist/runtime-gateway.js.map +10 -0
- package/dist/runtime-helpers.js +1792 -0
- package/dist/runtime-helpers.js.map +40 -0
- package/dist/sqlite.js +66 -0
- package/dist/sqlite.js.map +10 -0
- package/dist/storage.js +5223 -0
- package/dist/storage.js.map +47 -0
- package/dist/types/apps/negotium/src/mcp-servers.d.ts +20 -0
- package/dist/types/packages/core/src/agents/api-topic-agent-switch.d.ts +38 -0
- package/dist/types/packages/core/src/agents/archiver.d.ts +87 -0
- package/dist/types/packages/core/src/agents/auth-check.d.ts +19 -0
- package/dist/types/packages/core/src/agents/claude-notices.d.ts +15 -0
- package/dist/types/packages/core/src/agents/claude-provider.d.ts +25 -0
- package/dist/types/packages/core/src/agents/claude-registry.d.ts +3 -0
- package/dist/types/packages/core/src/agents/claude-runtime-inspection.d.ts +9 -0
- package/dist/types/packages/core/src/agents/codex-app-server.d.ts +13 -0
- package/dist/types/packages/core/src/agents/codex-native-multi-agent.d.ts +20 -0
- package/dist/types/packages/core/src/agents/codex-provider.d.ts +64 -0
- package/dist/types/packages/core/src/agents/codex-registry.d.ts +3 -0
- package/dist/types/packages/core/src/agents/codex-tier-resolver.d.ts +10 -0
- package/dist/types/packages/core/src/agents/codex-tree-kill.d.ts +13 -0
- package/dist/types/packages/core/src/agents/codex-tree-manager.d.ts +28 -0
- package/dist/types/packages/core/src/agents/codex-vault-hook-bridge.d.ts +28 -0
- package/dist/types/packages/core/src/agents/codex-windows-wrapper.d.ts +30 -0
- package/dist/types/packages/core/src/agents/compaction-support.d.ts +5 -0
- package/dist/types/packages/core/src/agents/compaction-tool-projection.d.ts +9 -0
- package/dist/types/packages/core/src/agents/contracts.d.ts +68 -0
- package/dist/types/packages/core/src/agents/deep-map.d.ts +2 -0
- package/dist/types/packages/core/src/agents/execution-host.d.ts +33 -0
- package/dist/types/packages/core/src/agents/explicit-agent-switch.d.ts +26 -0
- package/dist/types/packages/core/src/agents/fork.d.ts +50 -0
- package/dist/types/packages/core/src/agents/hosted-agent.d.ts +15 -0
- package/dist/types/packages/core/src/agents/idle-archiver.d.ts +35 -0
- package/dist/types/packages/core/src/agents/idle-compact.d.ts +37 -0
- package/dist/types/packages/core/src/agents/index.d.ts +8 -0
- package/dist/types/packages/core/src/agents/maestro-provider.d.ts +75 -0
- package/dist/types/packages/core/src/agents/maestro-registry.d.ts +3 -0
- package/dist/types/packages/core/src/agents/mcp-tools/ask-user.d.ts +147 -0
- package/dist/types/packages/core/src/agents/mcp-tools/common.d.ts +14 -0
- package/dist/types/packages/core/src/agents/mcp-tools/inline-assets.d.ts +24 -0
- package/dist/types/packages/core/src/agents/mcp-tools/publish-html.d.ts +18 -0
- package/dist/types/packages/core/src/agents/mcp-tools/request-vault.d.ts +124 -0
- package/dist/types/packages/core/src/agents/mcp-tools/self-config.d.ts +45 -0
- package/dist/types/packages/core/src/agents/mcp-tools/spawn-subagent.d.ts +148 -0
- package/dist/types/packages/core/src/agents/mcp-tools/visual-compat.d.ts +55 -0
- package/dist/types/packages/core/src/agents/mcp-tools/visuals.d.ts +70 -0
- package/dist/types/packages/core/src/agents/memory-archive-policy.d.ts +24 -0
- package/dist/types/packages/core/src/agents/model-catalog.d.ts +132 -0
- package/dist/types/packages/core/src/agents/public-helpers.d.ts +17 -0
- package/dist/types/packages/core/src/agents/registry.d.ts +5 -0
- package/dist/types/packages/core/src/agents/rollout/claude.d.ts +77 -0
- package/dist/types/packages/core/src/agents/rollout/codex.d.ts +119 -0
- package/dist/types/packages/core/src/agents/rollout/index.d.ts +6 -0
- package/dist/types/packages/core/src/agents/rollout/shared.d.ts +56 -0
- package/dist/types/packages/core/src/agents/self-config-core.d.ts +199 -0
- package/dist/types/packages/core/src/agents/task-events.d.ts +19 -0
- package/dist/types/packages/core/src/agents/tool-format.d.ts +25 -0
- package/dist/types/packages/core/src/agents/topic-cleanup.d.ts +83 -0
- package/dist/types/packages/core/src/agents/vault-tool-policy.d.ts +2 -0
- package/dist/types/packages/core/src/bus.d.ts +141 -0
- package/dist/types/packages/core/src/identifiers.d.ts +27 -0
- package/dist/types/packages/core/src/mcp/canonical-bridge-config.d.ts +21 -0
- package/dist/types/packages/core/src/mcp/factories/agent-health.d.ts +5 -0
- package/dist/types/packages/core/src/mcp/factories/compaction-log.d.ts +8 -0
- package/dist/types/packages/core/src/mcp/factories/decision.d.ts +18 -0
- package/dist/types/packages/core/src/mcp/factories/index.d.ts +13 -0
- package/dist/types/packages/core/src/mcp/factories/session-comm.d.ts +38 -0
- package/dist/types/packages/core/src/mcp/factories/session-history.d.ts +11 -0
- package/dist/types/packages/core/src/mcp/factories/stdio-protection.d.ts +10 -0
- package/dist/types/packages/core/src/mcp/factories/system-health.d.ts +37 -0
- package/dist/types/packages/core/src/mcp/factories/task.d.ts +13 -0
- package/dist/types/packages/core/src/mcp/factories/token-stats.d.ts +20 -0
- package/dist/types/packages/core/src/mcp/factories/vault.d.ts +9 -0
- package/dist/types/packages/core/src/mcp/mcp-helpers.d.ts +26 -0
- package/dist/types/packages/core/src/mcp/peer-bridge.d.ts +92 -0
- package/dist/types/packages/core/src/mcp/runtime-spec.d.ts +147 -0
- package/dist/types/packages/core/src/mcp/session-comm/actor-policy.d.ts +120 -0
- package/dist/types/packages/core/src/mcp/session-comm/bridge-ipc-config.d.ts +6 -0
- package/dist/types/packages/core/src/mcp/session-comm/context.d.ts +44 -0
- package/dist/types/packages/core/src/mcp/session-comm/history-tools.d.ts +31 -0
- package/dist/types/packages/core/src/mcp/session-comm/hub-remote-session.d.ts +149 -0
- package/dist/types/packages/core/src/mcp/session-comm/limits.d.ts +8 -0
- package/dist/types/packages/core/src/mcp/session-comm/peer-forward.d.ts +85 -0
- package/dist/types/packages/core/src/mcp/session-comm/tell-permissions.d.ts +29 -0
- package/dist/types/packages/core/src/mcp/session-comm/topic-catalog.d.ts +59 -0
- package/dist/types/packages/core/src/mcp/session-history-limits.d.ts +20 -0
- package/dist/types/packages/core/src/mcp/session-history-reader.d.ts +35 -0
- package/dist/types/packages/core/src/mcp/wiki-search-index.d.ts +59 -0
- package/dist/types/packages/core/src/mcp/wiki-server.d.ts +53 -0
- package/dist/types/packages/core/src/media/file-events.d.ts +14 -0
- package/dist/types/packages/core/src/media/text-extractor.d.ts +34 -0
- package/dist/types/packages/core/src/outbox/coalescing-runner.d.ts +68 -0
- package/dist/types/packages/core/src/outbox/file-ops.d.ts +79 -0
- package/dist/types/packages/core/src/outbox/index.d.ts +3 -0
- package/dist/types/packages/core/src/outbox/utils.d.ts +16 -0
- package/dist/types/packages/core/src/platform/background-bash/context.d.ts +1 -0
- package/dist/types/packages/core/src/platform/background-bash/manager.d.ts +34 -0
- package/dist/types/packages/core/src/platform/config-helpers.d.ts +20 -0
- package/dist/types/packages/core/src/platform/config.d.ts +178 -0
- package/dist/types/packages/core/src/platform/constants.d.ts +20 -0
- package/dist/types/packages/core/src/platform/delay.d.ts +1 -0
- package/dist/types/packages/core/src/platform/error.d.ts +6 -0
- package/dist/types/packages/core/src/platform/file-utils.d.ts +16 -0
- package/dist/types/packages/core/src/platform/ipc.d.ts +32 -0
- package/dist/types/packages/core/src/platform/jsonl.d.ts +46 -0
- package/dist/types/packages/core/src/platform/lifecycle.d.ts +40 -0
- package/dist/types/packages/core/src/platform/logger.d.ts +20 -0
- package/dist/types/packages/core/src/platform/maestro-bootstrap-env.d.ts +0 -0
- package/dist/types/packages/core/src/platform/mcp-catalog-policy.d.ts +73 -0
- package/dist/types/packages/core/src/platform/mcp-config.d.ts +286 -0
- package/dist/types/packages/core/src/platform/memoize-import.d.ts +15 -0
- package/dist/types/packages/core/src/platform/paths.d.ts +18 -0
- package/dist/types/packages/core/src/platform/playwright/browser-processes.d.ts +60 -0
- package/dist/types/packages/core/src/platform/playwright/capability.d.ts +1 -0
- package/dist/types/packages/core/src/platform/playwright/headed-launch.d.ts +13 -0
- package/dist/types/packages/core/src/platform/playwright/manager-utils.d.ts +34 -0
- package/dist/types/packages/core/src/platform/playwright/manager.d.ts +194 -0
- package/dist/types/packages/core/src/platform/playwright/mcp-transport.d.ts +4 -0
- package/dist/types/packages/core/src/platform/playwright/profile-management.d.ts +8 -0
- package/dist/types/packages/core/src/platform/playwright/public-runtime.d.ts +6 -0
- package/dist/types/packages/core/src/platform/playwright/transport-probe.d.ts +13 -0
- package/dist/types/packages/core/src/platform/playwright/vault-broker.d.ts +6 -0
- package/dist/types/packages/core/src/platform/public-runtime.d.ts +3 -0
- package/dist/types/packages/core/src/prompts/builders.d.ts +78 -0
- package/dist/types/packages/core/src/query/active-rooms.d.ts +240 -0
- package/dist/types/packages/core/src/query/public-runtime.d.ts +4 -0
- package/dist/types/packages/core/src/query/room-query-registry.d.ts +71 -0
- package/dist/types/packages/core/src/query/session-inbox-cleanup.d.ts +5 -0
- package/dist/types/packages/core/src/query/session-inbox-path.d.ts +8 -0
- package/dist/types/packages/core/src/query/state.d.ts +25 -0
- package/dist/types/packages/core/src/query/types.d.ts +6 -0
- package/dist/types/packages/core/src/runtime/actor-topic-reach.d.ts +63 -0
- package/dist/types/packages/core/src/runtime/actor-topic-scope.d.ts +127 -0
- package/dist/types/packages/core/src/runtime/ask-callbacks.d.ts +53 -0
- package/dist/types/packages/core/src/runtime/attachments.d.ts +36 -0
- package/dist/types/packages/core/src/runtime/background-session-policy.d.ts +1 -0
- package/dist/types/packages/core/src/runtime/background-sessions.d.ts +27 -0
- package/dist/types/packages/core/src/runtime/bashrs-completions.d.ts +50 -0
- package/dist/types/packages/core/src/runtime/channel-context.d.ts +13 -0
- package/dist/types/packages/core/src/runtime/context-warning.d.ts +46 -0
- package/dist/types/packages/core/src/runtime/cron-sessions.d.ts +9 -0
- package/dist/types/packages/core/src/runtime/errors.d.ts +6 -0
- package/dist/types/packages/core/src/runtime/event-heartbeat.d.ts +17 -0
- package/dist/types/packages/core/src/runtime/file-hooks.d.ts +38 -0
- package/dist/types/packages/core/src/runtime/playwright-turn-abort.d.ts +12 -0
- package/dist/types/packages/core/src/runtime/public-helpers.d.ts +17 -0
- package/dist/types/packages/core/src/runtime/remote-session-grant.d.ts +91 -0
- package/dist/types/packages/core/src/runtime/remote-session-inbox.d.ts +206 -0
- package/dist/types/packages/core/src/runtime/remote-session-reply-outbox.d.ts +37 -0
- package/dist/types/packages/core/src/runtime/task-format.d.ts +4 -0
- package/dist/types/packages/core/src/runtime/tasks.d.ts +3 -0
- package/dist/types/packages/core/src/runtime/thread-context.d.ts +62 -0
- package/dist/types/packages/core/src/runtime/topic-config.d.ts +24 -0
- package/dist/types/packages/core/src/runtime/turn-event-stream.d.ts +47 -0
- package/dist/types/packages/core/src/runtime/turn-runner.d.ts +284 -0
- package/dist/types/packages/core/src/runtime/turn-session.d.ts +49 -0
- package/dist/types/packages/core/src/runtime/usage-alert.d.ts +11 -0
- package/dist/types/packages/core/src/runtime/user-turn-envelope.d.ts +37 -0
- package/dist/types/packages/core/src/runtime/visual-html.d.ts +13 -0
- package/dist/types/packages/core/src/runtime/visual-store.d.ts +71 -0
- package/dist/types/packages/core/src/runtime/visuals.d.ts +33 -0
- package/dist/types/packages/core/src/runtime-gateway.d.ts +282 -0
- package/dist/types/packages/core/src/security/sanitize.d.ts +16 -0
- package/dist/types/packages/core/src/security/sensitive-path.d.ts +1 -0
- package/dist/types/packages/core/src/storage/activity-log.d.ts +25 -0
- package/dist/types/packages/core/src/storage/api-messages.d.ts +156 -0
- package/dist/types/packages/core/src/storage/api-topic-brief.d.ts +39 -0
- package/dist/types/packages/core/src/storage/api-topic-config.d.ts +23 -0
- package/dist/types/packages/core/src/storage/api-topics.d.ts +274 -0
- package/dist/types/packages/core/src/storage/app-settings.d.ts +4 -0
- package/dist/types/packages/core/src/storage/ask-user-gates.d.ts +65 -0
- package/dist/types/packages/core/src/storage/browser-profiles.d.ts +31 -0
- package/dist/types/packages/core/src/storage/conversations.d.ts +149 -0
- package/dist/types/packages/core/src/storage/decisions.d.ts +45 -0
- package/dist/types/packages/core/src/storage/forum/agent-settings.d.ts +3 -0
- package/dist/types/packages/core/src/storage/forum/index.d.ts +4 -0
- package/dist/types/packages/core/src/storage/forum/repository.d.ts +42 -0
- package/dist/types/packages/core/src/storage/forum/schema.d.ts +38 -0
- package/dist/types/packages/core/src/storage/forum-db.d.ts +2 -0
- package/dist/types/packages/core/src/storage/remote-session.d.ts +318 -0
- package/dist/types/packages/core/src/storage/runtime-events.d.ts +33 -0
- package/dist/types/packages/core/src/storage/runtime-leases.d.ts +27 -0
- package/dist/types/packages/core/src/storage/runtime-process-leases.d.ts +47 -0
- package/dist/types/packages/core/src/storage/runtime-topic-state.d.ts +39 -0
- package/dist/types/packages/core/src/storage/runtime-turn-requests.d.ts +163 -0
- package/dist/types/packages/core/src/storage/self-schedules.d.ts +54 -0
- package/dist/types/packages/core/src/storage/session-asks.d.ts +78 -0
- package/dist/types/packages/core/src/storage/session-inbox-signal.d.ts +35 -0
- package/dist/types/packages/core/src/storage/session-inbox.d.ts +36 -0
- package/dist/types/packages/core/src/storage/sqlite.d.ts +20 -0
- package/dist/types/packages/core/src/storage/storage-contract.d.ts +46 -0
- package/dist/types/packages/core/src/storage/storage-host.d.ts +41 -0
- package/dist/types/packages/core/src/storage/storage-public-host.d.ts +7 -0
- package/dist/types/packages/core/src/storage/storage-public.d.ts +45 -0
- package/dist/types/packages/core/src/storage/tasks.d.ts +49 -0
- package/dist/types/packages/core/src/storage/token-stats.d.ts +69 -0
- package/dist/types/packages/core/src/storage/topic-archive-state.d.ts +33 -0
- package/dist/types/packages/core/src/storage/topic-archive.d.ts +42 -0
- package/dist/types/packages/core/src/storage/topic-host-mcp-grants.d.ts +5 -0
- package/dist/types/packages/core/src/storage/topic-link-records.d.ts +281 -0
- package/dist/types/packages/core/src/storage/topic-settings.d.ts +19 -0
- package/dist/types/packages/core/src/storage/topic-tool-capabilities.d.ts +37 -0
- package/dist/types/packages/core/src/storage/topic-transcript.d.ts +38 -0
- package/dist/types/packages/core/src/storage/vault-crypto-core.d.ts +6 -0
- package/dist/types/packages/core/src/storage/vault-crypto.d.ts +12 -0
- package/dist/types/packages/core/src/storage/vault-public.d.ts +3 -0
- package/dist/types/packages/core/src/storage/vault-request-gates.d.ts +69 -0
- package/dist/types/packages/core/src/storage/vault.d.ts +49 -0
- package/dist/types/packages/core/src/storage/wiki-summary-names.d.ts +13 -0
- package/dist/types/packages/core/src/storage/wiki.d.ts +15 -0
- package/dist/types/packages/core/src/topics/derive.d.ts +83 -0
- package/dist/types/packages/core/src/topics/lifecycle.d.ts +75 -0
- package/dist/types/packages/core/src/topics/links.d.ts +2 -0
- package/dist/types/packages/core/src/topics/personal-general.d.ts +15 -0
- package/dist/types/packages/core/src/topics/session.d.ts +70 -0
- package/dist/types/packages/core/src/types/api.d.ts +551 -0
- package/dist/types/packages/core/src/types.d.ts +405 -0
- package/dist/types/packages/core/src/version.d.ts +1 -0
- package/dist/types/packages/module-cron/src/background-sessions.d.ts +2 -0
- package/dist/types/packages/module-cron/src/context.d.ts +15 -0
- package/dist/types/packages/module-cron/src/host.d.ts +23 -0
- package/dist/types/packages/module-cron/src/index.d.ts +14 -0
- package/dist/types/packages/module-cron/src/module.d.ts +13 -0
- package/dist/types/packages/module-cron/src/schedule.d.ts +23 -0
- package/dist/types/packages/module-cron/src/scheduler.d.ts +46 -0
- package/dist/types/packages/module-cron/src/scripts.d.ts +29 -0
- package/dist/types/packages/module-cron/src/store.d.ts +152 -0
- package/dist/types/packages/module-cron/src/summarize.d.ts +4 -0
- package/dist/vault.js +848 -0
- package/dist/vault.js.map +18 -0
- package/install-bash-rs.mjs +90 -0
- package/install-browser-rs.mjs +83 -0
- package/package.json +171 -4
|
@@ -0,0 +1,846 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { intersectActorTopicScopes } from "#runtime/actor-topic-scope";
|
|
3
|
+
import { mergeRemoteSessionGrants } from "#runtime/remote-session-grant";
|
|
4
|
+
import {
|
|
5
|
+
flattenUserTurnAttachments,
|
|
6
|
+
legacyUserTurnEnvelope,
|
|
7
|
+
renderUserTurnBatch,
|
|
8
|
+
type UserTurnEnvelope,
|
|
9
|
+
} from "#runtime/user-turn-envelope";
|
|
10
|
+
import { db } from "#storage/forum-db";
|
|
11
|
+
import { TURN_LEASE_STALE_MS } from "#storage/runtime-leases";
|
|
12
|
+
import { getRuntimeTopicEpoch, TOPIC_MAINTENANCE_STALE_MS } from "#storage/runtime-topic-state";
|
|
13
|
+
import type { StorageDatabase } from "#storage/storage-contract";
|
|
14
|
+
import { registerStorageSchemaInitializer } from "#storage/storage-host";
|
|
15
|
+
import type {
|
|
16
|
+
ActorTopicScope,
|
|
17
|
+
AgentKind,
|
|
18
|
+
EffortLevel,
|
|
19
|
+
PeerRuntimeBridgeContext,
|
|
20
|
+
RemoteSessionGrant,
|
|
21
|
+
} from "#types";
|
|
22
|
+
|
|
23
|
+
const REQUEST_CLAIM_STALE_MS = TURN_LEASE_STALE_MS;
|
|
24
|
+
|
|
25
|
+
/** Serializable execution details that must survive a cross-process handoff. */
|
|
26
|
+
export interface RuntimeUserTurnExecution {
|
|
27
|
+
runtimeEpoch?: number;
|
|
28
|
+
sourceRequestId?: string;
|
|
29
|
+
agentOverride?: AgentKind;
|
|
30
|
+
modelOverride?: string;
|
|
31
|
+
effortOverride?: EffortLevel;
|
|
32
|
+
sessionId?: string | null;
|
|
33
|
+
sessionIdSpecified?: boolean;
|
|
34
|
+
sessionScope?: "topic" | "isolated";
|
|
35
|
+
cwd?: string;
|
|
36
|
+
sessionName?: string;
|
|
37
|
+
sessionType?: "dm" | "forum" | "ephemeral" | "manager" | "cron";
|
|
38
|
+
visualTools?: boolean;
|
|
39
|
+
fileDeliveryTools?: boolean;
|
|
40
|
+
silent?: boolean;
|
|
41
|
+
bridgeSessionFromHistory?: boolean;
|
|
42
|
+
peerBridge?: PeerRuntimeBridgeContext;
|
|
43
|
+
from?: string;
|
|
44
|
+
/** Vault namespace for this turn, independent from the local execution principal. */
|
|
45
|
+
vaultUserId?: string;
|
|
46
|
+
/** Product actor identity, independent from the canonical execution principal. */
|
|
47
|
+
actorUserId?: string;
|
|
48
|
+
/** Host-asserted rooms the actor may reach; kept with the row so retries keep it. */
|
|
49
|
+
actorTopicScope?: ActorTopicScope;
|
|
50
|
+
/** Hub-issued per-turn remote session-comm grant; kept with the row like the assertion. */
|
|
51
|
+
remoteSession?: RemoteSessionGrant;
|
|
52
|
+
/** Newly accepted user texts not yet recorded in the unified conversation log. */
|
|
53
|
+
conversationPrompts?: string[];
|
|
54
|
+
/** Number of leading userMessages already present in the unified conversation log. */
|
|
55
|
+
loggedUserMessageCount?: number;
|
|
56
|
+
/** Native session whose provider output proves it accepted this request's user turn. */
|
|
57
|
+
providerSessionId?: string;
|
|
58
|
+
/** Request ids whose ordered messages were folded into this replacement. */
|
|
59
|
+
supersededRequestIds?: string[];
|
|
60
|
+
/**
|
|
61
|
+
* The message was also delivered into a running turn mid-way. Such a row is
|
|
62
|
+
* never folded into another: when that turn reads the message, exactly this
|
|
63
|
+
* row is dropped, and a batch holding it would answer it a second time.
|
|
64
|
+
*/
|
|
65
|
+
deliveredMidTurn?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* A held follow-up that went out (`runtime-follow-ups`): it is answered as a
|
|
68
|
+
* turn of its own, one per turn, so it is never folded into another request
|
|
69
|
+
* and never absorbs a later one.
|
|
70
|
+
*/
|
|
71
|
+
ownTurn?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Slack-style thread this turn belongs to, when it was asked in one. Carried
|
|
74
|
+
* on the request rather than the message because it decides both where the
|
|
75
|
+
* answer goes and which pending requests may merge with it (S-13).
|
|
76
|
+
*/
|
|
77
|
+
threadRootId?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface RuntimeUserTurnRequest {
|
|
81
|
+
requestId: string;
|
|
82
|
+
topicId: string;
|
|
83
|
+
userId: string;
|
|
84
|
+
prompt: string;
|
|
85
|
+
userMessages: UserTurnEnvelope[];
|
|
86
|
+
attachments?: string[];
|
|
87
|
+
allowAutoContinue: boolean;
|
|
88
|
+
execution?: RuntimeUserTurnExecution;
|
|
89
|
+
topicEpoch: number;
|
|
90
|
+
createdAt: number;
|
|
91
|
+
status: "pending" | "running";
|
|
92
|
+
claimedBy?: string;
|
|
93
|
+
claimedAt?: number;
|
|
94
|
+
runningQueryId?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface RuntimeUserTurnRequestRow {
|
|
98
|
+
request_id: string;
|
|
99
|
+
topic_id: string;
|
|
100
|
+
user_id: string;
|
|
101
|
+
prompt: string;
|
|
102
|
+
user_messages_json: string | null;
|
|
103
|
+
attachments_json: string | null;
|
|
104
|
+
allow_auto_continue: number;
|
|
105
|
+
execution_json: string | null;
|
|
106
|
+
topic_epoch: number | bigint;
|
|
107
|
+
created_at: number | bigint;
|
|
108
|
+
status: string;
|
|
109
|
+
claimed_by: string | null;
|
|
110
|
+
claimed_at: number | bigint | null;
|
|
111
|
+
running_query_id: string | null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function createRuntimeUserTurnRequestsTable(database: StorageDatabase): void {
|
|
115
|
+
database.exec(`
|
|
116
|
+
CREATE TABLE IF NOT EXISTS runtime_user_turn_requests (
|
|
117
|
+
request_id TEXT PRIMARY KEY,
|
|
118
|
+
topic_id TEXT NOT NULL,
|
|
119
|
+
user_id TEXT NOT NULL,
|
|
120
|
+
prompt TEXT NOT NULL,
|
|
121
|
+
user_messages_json TEXT,
|
|
122
|
+
attachments_json TEXT,
|
|
123
|
+
allow_auto_continue INTEGER NOT NULL DEFAULT 1 CHECK (allow_auto_continue IN (0, 1)),
|
|
124
|
+
execution_json TEXT,
|
|
125
|
+
topic_epoch INTEGER NOT NULL DEFAULT 0,
|
|
126
|
+
created_at INTEGER NOT NULL,
|
|
127
|
+
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'running')),
|
|
128
|
+
claimed_by TEXT,
|
|
129
|
+
claimed_at INTEGER,
|
|
130
|
+
running_query_id TEXT
|
|
131
|
+
)
|
|
132
|
+
`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Apply additive upgrades and the legacy topic-primary-key rebuild to one database. */
|
|
136
|
+
export function ensureRuntimeUserTurnRequestsSchema(database: StorageDatabase): void {
|
|
137
|
+
createRuntimeUserTurnRequestsTable(database);
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
database.exec("ALTER TABLE runtime_user_turn_requests ADD COLUMN execution_json TEXT");
|
|
141
|
+
} catch {
|
|
142
|
+
// Existing standalone database already has the additive handoff column.
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
database.exec("ALTER TABLE runtime_user_turn_requests ADD COLUMN user_messages_json TEXT");
|
|
146
|
+
} catch {
|
|
147
|
+
// Existing standalone database already has the additive envelope column.
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
database.exec(
|
|
151
|
+
"ALTER TABLE runtime_user_turn_requests ADD COLUMN topic_epoch INTEGER NOT NULL DEFAULT 0",
|
|
152
|
+
);
|
|
153
|
+
} catch {
|
|
154
|
+
// Existing standalone database already has the additive epoch column.
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const legacyTopicPrimaryKey = database
|
|
158
|
+
.query<{ name: string; pk: number }, []>("PRAGMA table_info(runtime_user_turn_requests)")
|
|
159
|
+
.all()
|
|
160
|
+
.some((column) => column.name === "topic_id" && column.pk === 1);
|
|
161
|
+
if (legacyTopicPrimaryKey) {
|
|
162
|
+
database.transaction(() => {
|
|
163
|
+
database.exec(
|
|
164
|
+
"ALTER TABLE runtime_user_turn_requests RENAME TO runtime_user_turn_requests_legacy",
|
|
165
|
+
);
|
|
166
|
+
createRuntimeUserTurnRequestsTable(database);
|
|
167
|
+
database.exec(`
|
|
168
|
+
INSERT INTO runtime_user_turn_requests (
|
|
169
|
+
request_id, topic_id, user_id, prompt, user_messages_json, attachments_json,
|
|
170
|
+
allow_auto_continue, execution_json, topic_epoch, created_at,
|
|
171
|
+
status, claimed_by, claimed_at, running_query_id
|
|
172
|
+
)
|
|
173
|
+
SELECT request_id, topic_id, user_id, prompt, NULL, attachments_json,
|
|
174
|
+
allow_auto_continue, execution_json, topic_epoch, created_at,
|
|
175
|
+
status, claimed_by, claimed_at, running_query_id
|
|
176
|
+
FROM runtime_user_turn_requests_legacy
|
|
177
|
+
`);
|
|
178
|
+
database.exec("DROP TABLE runtime_user_turn_requests_legacy");
|
|
179
|
+
})();
|
|
180
|
+
}
|
|
181
|
+
database.exec(
|
|
182
|
+
"CREATE INDEX IF NOT EXISTS idx_runtime_user_turn_requests_ready ON runtime_user_turn_requests(status, created_at)",
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Registered rather than run at import time. Calling this with `db` on module
|
|
187
|
+
// evaluation resolves the storage connection immediately, which for an
|
|
188
|
+
// embedding host happens before `configureStorageHost()` — Negotium then opens
|
|
189
|
+
// and caches its own database in the default state directory, and every later
|
|
190
|
+
// caller silently keeps using it instead of the host's.
|
|
191
|
+
registerStorageSchemaInitializer((database) =>
|
|
192
|
+
ensureRuntimeUserTurnRequestsSchema(database as unknown as StorageDatabase),
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Reject a stored envelope whose reply context is not the shape the renderer
|
|
197
|
+
* assumes.
|
|
198
|
+
*
|
|
199
|
+
* These rows are written by typed callers, but they are durable JSON that
|
|
200
|
+
* outlives the code that wrote them, and the renderer calls `label.trim()`. A
|
|
201
|
+
* validator that claims to check the envelope has to actually cover its fields,
|
|
202
|
+
* or a hand-edited or downgraded row crashes the turn instead of degrading.
|
|
203
|
+
*/
|
|
204
|
+
function isValidReplyContext(value: UserTurnEnvelope["replyTo"]): boolean {
|
|
205
|
+
if (value === undefined) return true;
|
|
206
|
+
if (!value || typeof value !== "object") return false;
|
|
207
|
+
const optionalText = (field: unknown) => field === undefined || typeof field === "string";
|
|
208
|
+
return (
|
|
209
|
+
(value.kind === "thread" || value.kind === "quote") &&
|
|
210
|
+
optionalText(value.rootId) &&
|
|
211
|
+
optionalText(value.label) &&
|
|
212
|
+
optionalText(value.text)
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function rowToRequest(row: RuntimeUserTurnRequestRow): RuntimeUserTurnRequest {
|
|
217
|
+
let attachments: string[] | undefined;
|
|
218
|
+
if (row.attachments_json) {
|
|
219
|
+
try {
|
|
220
|
+
const parsed = JSON.parse(row.attachments_json) as unknown;
|
|
221
|
+
if (Array.isArray(parsed) && parsed.every((item) => typeof item === "string")) {
|
|
222
|
+
attachments = parsed;
|
|
223
|
+
}
|
|
224
|
+
} catch {
|
|
225
|
+
attachments = undefined;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
let userMessages: UserTurnEnvelope[] | undefined;
|
|
229
|
+
if (row.user_messages_json) {
|
|
230
|
+
try {
|
|
231
|
+
const parsed = JSON.parse(row.user_messages_json) as unknown;
|
|
232
|
+
if (
|
|
233
|
+
Array.isArray(parsed) &&
|
|
234
|
+
parsed.length > 0 &&
|
|
235
|
+
parsed.every(
|
|
236
|
+
(item) =>
|
|
237
|
+
item &&
|
|
238
|
+
typeof item === "object" &&
|
|
239
|
+
typeof (item as UserTurnEnvelope).prompt === "string" &&
|
|
240
|
+
((item as UserTurnEnvelope).attachments === undefined ||
|
|
241
|
+
(Array.isArray((item as UserTurnEnvelope).attachments) &&
|
|
242
|
+
(item as UserTurnEnvelope).attachments?.every(
|
|
243
|
+
(attachment) => typeof attachment === "string",
|
|
244
|
+
))) &&
|
|
245
|
+
((item as UserTurnEnvelope).actorUserId === undefined ||
|
|
246
|
+
typeof (item as UserTurnEnvelope).actorUserId === "string") &&
|
|
247
|
+
((item as UserTurnEnvelope).actorLabel === undefined ||
|
|
248
|
+
typeof (item as UserTurnEnvelope).actorLabel === "string") &&
|
|
249
|
+
isValidReplyContext((item as UserTurnEnvelope).replyTo),
|
|
250
|
+
)
|
|
251
|
+
) {
|
|
252
|
+
userMessages = parsed as UserTurnEnvelope[];
|
|
253
|
+
}
|
|
254
|
+
} catch {
|
|
255
|
+
userMessages = undefined;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// Pre-envelope rows cannot associate a batched attachment list with individual
|
|
259
|
+
// messages. Preserve their historic provider input as one submission instead.
|
|
260
|
+
userMessages ??= [legacyUserTurnEnvelope(row.prompt, attachments)];
|
|
261
|
+
let execution: RuntimeUserTurnExecution | undefined;
|
|
262
|
+
if (row.execution_json) {
|
|
263
|
+
try {
|
|
264
|
+
const parsed = JSON.parse(row.execution_json) as unknown;
|
|
265
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
266
|
+
execution = parsed as RuntimeUserTurnExecution;
|
|
267
|
+
}
|
|
268
|
+
} catch {
|
|
269
|
+
execution = undefined;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
requestId: row.request_id,
|
|
274
|
+
topicId: row.topic_id,
|
|
275
|
+
userId: row.user_id,
|
|
276
|
+
prompt: row.prompt,
|
|
277
|
+
userMessages,
|
|
278
|
+
attachments,
|
|
279
|
+
allowAutoContinue: row.allow_auto_continue !== 0,
|
|
280
|
+
execution,
|
|
281
|
+
topicEpoch: Number(row.topic_epoch),
|
|
282
|
+
createdAt: Number(row.created_at),
|
|
283
|
+
status: row.status === "running" ? "running" : "pending",
|
|
284
|
+
claimedBy: row.claimed_by ?? undefined,
|
|
285
|
+
claimedAt: row.claimed_at === null ? undefined : Number(row.claimed_at),
|
|
286
|
+
runningQueryId: row.running_query_id ?? undefined,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function enqueueRuntimeUserTurnRequest(input: {
|
|
291
|
+
topicId: string;
|
|
292
|
+
userId: string;
|
|
293
|
+
prompt: string;
|
|
294
|
+
userMessages?: UserTurnEnvelope[];
|
|
295
|
+
attachments?: string[];
|
|
296
|
+
allowAutoContinue: boolean;
|
|
297
|
+
requestId?: string;
|
|
298
|
+
execution?: RuntimeUserTurnExecution;
|
|
299
|
+
topicEpoch?: number;
|
|
300
|
+
/** Existing channel behavior supersedes queued work; gateways opt into FIFO. */
|
|
301
|
+
supersedeExisting?: boolean;
|
|
302
|
+
}): string {
|
|
303
|
+
const requestId = input.requestId ?? randomUUID();
|
|
304
|
+
const now = Date.now();
|
|
305
|
+
const topicEpoch = input.topicEpoch ?? getRuntimeTopicEpoch(input.topicId);
|
|
306
|
+
db.transaction(() => {
|
|
307
|
+
if (input.supersedeExisting !== false) {
|
|
308
|
+
db.query("DELETE FROM runtime_user_turn_requests WHERE topic_id = ?").run(input.topicId);
|
|
309
|
+
}
|
|
310
|
+
db.query(
|
|
311
|
+
`INSERT INTO runtime_user_turn_requests
|
|
312
|
+
(request_id, topic_id, user_id, prompt, user_messages_json, attachments_json,
|
|
313
|
+
allow_auto_continue, execution_json, topic_epoch, created_at,
|
|
314
|
+
status, claimed_by, claimed_at, running_query_id)
|
|
315
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', NULL, NULL, NULL)
|
|
316
|
+
ON CONFLICT(request_id) DO NOTHING`,
|
|
317
|
+
).run(
|
|
318
|
+
requestId,
|
|
319
|
+
input.topicId,
|
|
320
|
+
input.userId,
|
|
321
|
+
input.prompt,
|
|
322
|
+
input.userMessages?.length ? JSON.stringify(input.userMessages) : null,
|
|
323
|
+
input.attachments?.length ? JSON.stringify(input.attachments) : null,
|
|
324
|
+
input.allowAutoContinue ? 1 : 0,
|
|
325
|
+
input.execution ? JSON.stringify(input.execution) : null,
|
|
326
|
+
topicEpoch,
|
|
327
|
+
now,
|
|
328
|
+
);
|
|
329
|
+
})();
|
|
330
|
+
return requestId;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Who a request speaks for. `null` when no actor was recorded (a turn the node
|
|
335
|
+
* started for itself, or a row older than the field): two such requests are
|
|
336
|
+
* one conversation, but an unrecorded actor never matches a recorded one — the
|
|
337
|
+
* conservative reading, since the alternative is folding a stranger's message
|
|
338
|
+
* into a turn whose author is unknown.
|
|
339
|
+
*/
|
|
340
|
+
function requestActor(actorUserId: string | undefined): string | null {
|
|
341
|
+
return actorUserId ?? null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function loggedMessageCount(request: RuntimeUserTurnRequest): number {
|
|
345
|
+
const explicit = request.execution?.loggedUserMessageCount;
|
|
346
|
+
if (typeof explicit === "number" && Number.isInteger(explicit)) {
|
|
347
|
+
return Math.min(Math.max(0, explicit), request.userMessages.length);
|
|
348
|
+
}
|
|
349
|
+
const legacyPendingPrompts = request.execution?.conversationPrompts;
|
|
350
|
+
if (legacyPendingPrompts) {
|
|
351
|
+
return Math.max(0, request.userMessages.length - legacyPendingPrompts.length);
|
|
352
|
+
}
|
|
353
|
+
return 0;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Atomically fold every accepted request for a topic into one replacement.
|
|
358
|
+
* BEGIN IMMEDIATE serializes cross-process readers before they observe and
|
|
359
|
+
* replace the current rows, preventing the classic read/merge/delete race.
|
|
360
|
+
*/
|
|
361
|
+
export function mergeRuntimeUserTurnRequest(input: {
|
|
362
|
+
topicId: string;
|
|
363
|
+
userId: string;
|
|
364
|
+
userMessages: UserTurnEnvelope[];
|
|
365
|
+
allowAutoContinue: boolean;
|
|
366
|
+
requestId: string;
|
|
367
|
+
execution: RuntimeUserTurnExecution;
|
|
368
|
+
topicEpoch: number;
|
|
369
|
+
alreadyIncludedRequestIds?: string[];
|
|
370
|
+
/** Requests already committed to the provider's native session; keep their lineage, not prompts. */
|
|
371
|
+
omitRequestIds?: string[];
|
|
372
|
+
/**
|
|
373
|
+
* Requests that must stay standing and never be folded into this one, even
|
|
374
|
+
* for the same actor and thread. Used to queue a message behind the turn a
|
|
375
|
+
* worker is running instead of steering (aborting and replacing) it.
|
|
376
|
+
*/
|
|
377
|
+
keepRequestIds?: string[];
|
|
378
|
+
/**
|
|
379
|
+
* Fold rows whose message was delivered into the running turn as well. Only
|
|
380
|
+
* an explicit steer does: the running turn is stopped, so whatever it was
|
|
381
|
+
* handed must ride the turn that replaces it.
|
|
382
|
+
*/
|
|
383
|
+
includeDeliveredMidTurn?: boolean;
|
|
384
|
+
}): { requestId: string; supersededRequestIds: string[] } {
|
|
385
|
+
const now = Date.now();
|
|
386
|
+
return db
|
|
387
|
+
.transaction(() => {
|
|
388
|
+
const rows = db
|
|
389
|
+
.query<RuntimeUserTurnRequestRow, [string]>(
|
|
390
|
+
"SELECT * FROM runtime_user_turn_requests WHERE topic_id = ? ORDER BY created_at ASC, rowid ASC",
|
|
391
|
+
)
|
|
392
|
+
.all(input.topicId);
|
|
393
|
+
// Merging exists to fold consecutive utterances of one conversation into
|
|
394
|
+
// a single turn. A different thread is by definition a different
|
|
395
|
+
// conversation, and a merged batch spanning two of them has no correct
|
|
396
|
+
// place to answer — so requests are only ever folded within one thread,
|
|
397
|
+
// and a pending request from another thread is left standing to run on
|
|
398
|
+
// its own (S-13).
|
|
399
|
+
const thread = input.execution.threadRootId;
|
|
400
|
+
const kept = new Set(input.keepRequestIds ?? []);
|
|
401
|
+
const sameThread = rows
|
|
402
|
+
.map(rowToRequest)
|
|
403
|
+
.filter(
|
|
404
|
+
(request) =>
|
|
405
|
+
request.execution?.threadRootId === thread &&
|
|
406
|
+
!kept.has(request.requestId) &&
|
|
407
|
+
request.execution?.ownTurn !== true &&
|
|
408
|
+
(input.includeDeliveredMidTurn === true ||
|
|
409
|
+
request.execution?.deliveredMidTurn !== true),
|
|
410
|
+
);
|
|
411
|
+
// Nor is a different person the same conversation for this purpose. A
|
|
412
|
+
// merged batch runs with one actor's identity and one room assertion,
|
|
413
|
+
// so folding B's follow-up onto A's pending question would run A's
|
|
414
|
+
// words under B's authority (or B's under A's). Only the most recent
|
|
415
|
+
// unbroken run of the *same* actor's requests is folded; a request from
|
|
416
|
+
// anyone else ends that run and keeps its own row, in arrival order, so
|
|
417
|
+
// each turn carries exactly the actor and assertion it was sent with.
|
|
418
|
+
const actor = requestActor(input.execution.actorUserId);
|
|
419
|
+
let firstMergeable = sameThread.length;
|
|
420
|
+
while (
|
|
421
|
+
firstMergeable > 0 &&
|
|
422
|
+
requestActor(sameThread[firstMergeable - 1]!.execution?.actorUserId) === actor
|
|
423
|
+
) {
|
|
424
|
+
firstMergeable -= 1;
|
|
425
|
+
}
|
|
426
|
+
const previous = sameThread.slice(firstMergeable);
|
|
427
|
+
const omittedRequestIds = new Set([
|
|
428
|
+
...(input.omitRequestIds ?? []),
|
|
429
|
+
...previous
|
|
430
|
+
.filter((request) => Boolean(request.execution?.providerSessionId))
|
|
431
|
+
.map((request) => request.requestId),
|
|
432
|
+
]);
|
|
433
|
+
const carried = previous.filter((request) => !omittedRequestIds.has(request.requestId));
|
|
434
|
+
const alreadyIncludedRequestIds = new Set(input.alreadyIncludedRequestIds ?? []);
|
|
435
|
+
const alreadyIncludedMessages = carried
|
|
436
|
+
.filter((request) => alreadyIncludedRequestIds.has(request.requestId))
|
|
437
|
+
.flatMap((request) => request.userMessages);
|
|
438
|
+
const includedPrefixMatches = alreadyIncludedMessages.every((message, index) => {
|
|
439
|
+
const candidate = input.userMessages[index];
|
|
440
|
+
return (
|
|
441
|
+
candidate?.prompt === message.prompt &&
|
|
442
|
+
candidate.actorUserId === message.actorUserId &&
|
|
443
|
+
candidate.actorLabel === message.actorLabel &&
|
|
444
|
+
// The reply context is part of the rendered prompt, so two envelopes
|
|
445
|
+
// that differ only there are different submissions. Comparing without
|
|
446
|
+
// it would treat a thread reply as already included in the batch and
|
|
447
|
+
// silently drop it.
|
|
448
|
+
JSON.stringify(candidate.replyTo ?? null) === JSON.stringify(message.replyTo ?? null) &&
|
|
449
|
+
JSON.stringify(candidate.attachments ?? []) === JSON.stringify(message.attachments ?? [])
|
|
450
|
+
);
|
|
451
|
+
});
|
|
452
|
+
const alreadyIncludedMessageCount = includedPrefixMatches
|
|
453
|
+
? alreadyIncludedMessages.length
|
|
454
|
+
: 0;
|
|
455
|
+
const userMessages = [
|
|
456
|
+
...carried.flatMap((request) => request.userMessages),
|
|
457
|
+
...input.userMessages.slice(alreadyIncludedMessageCount),
|
|
458
|
+
];
|
|
459
|
+
const incomingLoggedCount = Math.min(
|
|
460
|
+
Math.max(0, input.execution.loggedUserMessageCount ?? 0),
|
|
461
|
+
input.userMessages.length,
|
|
462
|
+
);
|
|
463
|
+
const loggedUserMessageCount =
|
|
464
|
+
carried.reduce((count, request) => count + loggedMessageCount(request), 0) +
|
|
465
|
+
Math.max(0, incomingLoggedCount - alreadyIncludedMessageCount);
|
|
466
|
+
// Same actor, possibly different assertions (the hub's roster moved
|
|
467
|
+
// between two messages): the merged turn gets the intersection, and no
|
|
468
|
+
// assertion at all if any folded request lacked one. Narrowing is the
|
|
469
|
+
// only safe direction for a batch that speaks with one voice.
|
|
470
|
+
const actorTopicScope = intersectActorTopicScopes([
|
|
471
|
+
...previous.map((request) => request.execution?.actorTopicScope),
|
|
472
|
+
input.execution.actorTopicScope,
|
|
473
|
+
]);
|
|
474
|
+
// Same rule for the remote-session grant: the batch keeps one only when
|
|
475
|
+
// every folded request carried one (for the same hub), the latest-expiring.
|
|
476
|
+
const remoteSession = mergeRemoteSessionGrants([
|
|
477
|
+
...previous.map((request) => request.execution?.remoteSession),
|
|
478
|
+
input.execution.remoteSession,
|
|
479
|
+
]);
|
|
480
|
+
const execution: RuntimeUserTurnExecution = {
|
|
481
|
+
...input.execution,
|
|
482
|
+
actorTopicScope,
|
|
483
|
+
remoteSession,
|
|
484
|
+
loggedUserMessageCount,
|
|
485
|
+
supersededRequestIds: [
|
|
486
|
+
...new Set(
|
|
487
|
+
previous.flatMap((request) => [
|
|
488
|
+
request.requestId,
|
|
489
|
+
...(request.execution?.supersededRequestIds ?? []),
|
|
490
|
+
]),
|
|
491
|
+
),
|
|
492
|
+
],
|
|
493
|
+
conversationPrompts: userMessages
|
|
494
|
+
.slice(loggedUserMessageCount)
|
|
495
|
+
.map((message) => message.prompt),
|
|
496
|
+
};
|
|
497
|
+
const sessionBase = carried.find(
|
|
498
|
+
(request) => request.execution?.sessionIdSpecified,
|
|
499
|
+
)?.execution;
|
|
500
|
+
if (sessionBase?.sessionIdSpecified) {
|
|
501
|
+
execution.sessionId = sessionBase.sessionId;
|
|
502
|
+
execution.sessionIdSpecified = true;
|
|
503
|
+
}
|
|
504
|
+
const attachments = flattenUserTurnAttachments(userMessages);
|
|
505
|
+
|
|
506
|
+
// Delete only what this replacement actually absorbed. Clearing the whole
|
|
507
|
+
// topic would drop another thread's pending question on the floor — it
|
|
508
|
+
// was never folded in, so nothing would ever answer it (S-13).
|
|
509
|
+
const absorbed = previous.map((request) => request.requestId);
|
|
510
|
+
if (absorbed.length > 0) {
|
|
511
|
+
db.query(
|
|
512
|
+
`DELETE FROM runtime_user_turn_requests
|
|
513
|
+
WHERE topic_id = ? AND request_id IN (${absorbed.map(() => "?").join(",")})`,
|
|
514
|
+
).run(input.topicId, ...absorbed);
|
|
515
|
+
}
|
|
516
|
+
db.query(
|
|
517
|
+
`INSERT INTO runtime_user_turn_requests
|
|
518
|
+
(request_id, topic_id, user_id, prompt, user_messages_json, attachments_json,
|
|
519
|
+
allow_auto_continue, execution_json, topic_epoch, created_at,
|
|
520
|
+
status, claimed_by, claimed_at, running_query_id)
|
|
521
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', NULL, NULL, NULL)`,
|
|
522
|
+
).run(
|
|
523
|
+
input.requestId,
|
|
524
|
+
input.topicId,
|
|
525
|
+
input.userId,
|
|
526
|
+
renderUserTurnBatch(userMessages),
|
|
527
|
+
JSON.stringify(userMessages),
|
|
528
|
+
attachments?.length ? JSON.stringify(attachments) : null,
|
|
529
|
+
input.allowAutoContinue ? 1 : 0,
|
|
530
|
+
JSON.stringify(execution),
|
|
531
|
+
input.topicEpoch,
|
|
532
|
+
now,
|
|
533
|
+
);
|
|
534
|
+
return {
|
|
535
|
+
requestId: input.requestId,
|
|
536
|
+
supersededRequestIds: previous.map((request) => request.requestId),
|
|
537
|
+
};
|
|
538
|
+
})
|
|
539
|
+
.immediate();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Record that provider output proves the native session contains this durable
|
|
544
|
+
* turn. A session-id event alone is insufficient: some providers publish it
|
|
545
|
+
* before appending the user prompt. This marker lets a different runtime
|
|
546
|
+
* process omit only a demonstrably committed prompt when it merges a steering
|
|
547
|
+
* message after preemption.
|
|
548
|
+
*
|
|
549
|
+
* Match only the exact request id. A late session event from a superseded turn
|
|
550
|
+
* must not mark its replacement (whose prompts have not reached the provider).
|
|
551
|
+
*/
|
|
552
|
+
export function markRuntimeUserTurnProviderSessionObserved(
|
|
553
|
+
topicId: string,
|
|
554
|
+
requestId: string,
|
|
555
|
+
sessionId: string,
|
|
556
|
+
): boolean {
|
|
557
|
+
return db
|
|
558
|
+
.transaction(() => {
|
|
559
|
+
const row = db
|
|
560
|
+
.query<RuntimeUserTurnRequestRow, [string, string]>(
|
|
561
|
+
"SELECT * FROM runtime_user_turn_requests WHERE topic_id = ? AND request_id = ?",
|
|
562
|
+
)
|
|
563
|
+
.get(topicId, requestId);
|
|
564
|
+
if (!row) return false;
|
|
565
|
+
const request = rowToRequest(row);
|
|
566
|
+
const execution: RuntimeUserTurnExecution = {
|
|
567
|
+
...request.execution,
|
|
568
|
+
providerSessionId: sessionId,
|
|
569
|
+
};
|
|
570
|
+
const result = db
|
|
571
|
+
.query(
|
|
572
|
+
`UPDATE runtime_user_turn_requests
|
|
573
|
+
SET execution_json = ?
|
|
574
|
+
WHERE topic_id = ? AND request_id = ?`,
|
|
575
|
+
)
|
|
576
|
+
.run(JSON.stringify(execution), topicId, requestId);
|
|
577
|
+
return result.changes === 1;
|
|
578
|
+
})
|
|
579
|
+
.immediate();
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
export function markRuntimeUserTurnMessagesLogged(
|
|
583
|
+
topicId: string,
|
|
584
|
+
requestId: string,
|
|
585
|
+
ownerId: string,
|
|
586
|
+
loggedUserMessages: readonly UserTurnEnvelope[],
|
|
587
|
+
): boolean {
|
|
588
|
+
if (loggedUserMessages.length === 0) return false;
|
|
589
|
+
return db
|
|
590
|
+
.transaction(() => {
|
|
591
|
+
const requests = db
|
|
592
|
+
.query<RuntimeUserTurnRequestRow, [string]>(
|
|
593
|
+
"SELECT * FROM runtime_user_turn_requests WHERE topic_id = ? ORDER BY created_at ASC, rowid ASC",
|
|
594
|
+
)
|
|
595
|
+
.all(topicId)
|
|
596
|
+
.map(rowToRequest);
|
|
597
|
+
const hasLoggedPrefix = (request: RuntimeUserTurnRequest): boolean =>
|
|
598
|
+
loggedUserMessages.length <= request.userMessages.length &&
|
|
599
|
+
loggedUserMessages.every((message, index) => {
|
|
600
|
+
const candidate = request.userMessages[index];
|
|
601
|
+
return (
|
|
602
|
+
candidate?.prompt === message.prompt &&
|
|
603
|
+
candidate.actorUserId === message.actorUserId &&
|
|
604
|
+
candidate.actorLabel === message.actorLabel &&
|
|
605
|
+
JSON.stringify(candidate.attachments ?? []) ===
|
|
606
|
+
JSON.stringify(message.attachments ?? [])
|
|
607
|
+
);
|
|
608
|
+
});
|
|
609
|
+
const request =
|
|
610
|
+
requests.find(
|
|
611
|
+
(candidate) =>
|
|
612
|
+
candidate.requestId === requestId &&
|
|
613
|
+
candidate.claimedBy === ownerId &&
|
|
614
|
+
hasLoggedPrefix(candidate),
|
|
615
|
+
) ??
|
|
616
|
+
requests.find(
|
|
617
|
+
(candidate) =>
|
|
618
|
+
candidate.execution?.supersededRequestIds?.includes(requestId) &&
|
|
619
|
+
hasLoggedPrefix(candidate),
|
|
620
|
+
);
|
|
621
|
+
if (!request) return false;
|
|
622
|
+
const count = Math.max(loggedMessageCount(request), loggedUserMessages.length);
|
|
623
|
+
const execution: RuntimeUserTurnExecution = {
|
|
624
|
+
...request.execution,
|
|
625
|
+
loggedUserMessageCount: count,
|
|
626
|
+
conversationPrompts: request.userMessages.slice(count).map((message) => message.prompt),
|
|
627
|
+
};
|
|
628
|
+
const result = db
|
|
629
|
+
.query(
|
|
630
|
+
`UPDATE runtime_user_turn_requests
|
|
631
|
+
SET execution_json = ?
|
|
632
|
+
WHERE topic_id = ? AND request_id = ?`,
|
|
633
|
+
)
|
|
634
|
+
.run(JSON.stringify(execution), topicId, request.requestId);
|
|
635
|
+
return result.changes === 1;
|
|
636
|
+
})
|
|
637
|
+
.immediate();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
export function claimNextRuntimeUserTurnRequest(
|
|
641
|
+
ownerId: string,
|
|
642
|
+
now = Date.now(),
|
|
643
|
+
): RuntimeUserTurnRequest | null {
|
|
644
|
+
return db
|
|
645
|
+
.transaction(() => {
|
|
646
|
+
const row = db
|
|
647
|
+
.query<RuntimeUserTurnRequestRow, [number, number, number, number, number]>(
|
|
648
|
+
`SELECT r.*
|
|
649
|
+
FROM runtime_user_turn_requests r
|
|
650
|
+
LEFT JOIN runtime_turn_leases l ON l.topic_id = r.topic_id
|
|
651
|
+
LEFT JOIN runtime_topic_state s ON s.topic_id = r.topic_id
|
|
652
|
+
WHERE (l.topic_id IS NULL OR l.heartbeat_at < ?)
|
|
653
|
+
AND (s.topic_id IS NULL OR s.maintenance = 0 OR s.heartbeat_at < ?)
|
|
654
|
+
AND r.topic_epoch = COALESCE(s.epoch, 0)
|
|
655
|
+
AND NOT EXISTS (
|
|
656
|
+
SELECT 1
|
|
657
|
+
FROM runtime_user_turn_requests active
|
|
658
|
+
WHERE active.topic_id = r.topic_id
|
|
659
|
+
AND active.request_id <> r.request_id
|
|
660
|
+
AND active.claimed_at IS NOT NULL
|
|
661
|
+
AND active.claimed_at >= ?
|
|
662
|
+
)
|
|
663
|
+
AND (
|
|
664
|
+
(r.status = 'pending' AND (r.claimed_at IS NULL OR r.claimed_at < ?))
|
|
665
|
+
OR (r.status = 'running' AND (r.claimed_at IS NULL OR r.claimed_at < ?))
|
|
666
|
+
)
|
|
667
|
+
ORDER BY r.created_at ASC, r.rowid ASC
|
|
668
|
+
LIMIT 1`,
|
|
669
|
+
)
|
|
670
|
+
.get(
|
|
671
|
+
now - TURN_LEASE_STALE_MS,
|
|
672
|
+
now - TOPIC_MAINTENANCE_STALE_MS,
|
|
673
|
+
now - REQUEST_CLAIM_STALE_MS,
|
|
674
|
+
now - REQUEST_CLAIM_STALE_MS,
|
|
675
|
+
now - REQUEST_CLAIM_STALE_MS,
|
|
676
|
+
);
|
|
677
|
+
if (!row) return null;
|
|
678
|
+
const updated = db
|
|
679
|
+
.query(
|
|
680
|
+
`UPDATE runtime_user_turn_requests
|
|
681
|
+
SET claimed_by = ?, claimed_at = ?
|
|
682
|
+
WHERE request_id = ? AND topic_id = ?
|
|
683
|
+
AND (
|
|
684
|
+
(status = 'pending' AND (claimed_at IS NULL OR claimed_at < ?))
|
|
685
|
+
OR (status = 'running' AND (claimed_at IS NULL OR claimed_at < ?))
|
|
686
|
+
)`,
|
|
687
|
+
)
|
|
688
|
+
.run(
|
|
689
|
+
ownerId,
|
|
690
|
+
now,
|
|
691
|
+
row.request_id,
|
|
692
|
+
row.topic_id,
|
|
693
|
+
now - REQUEST_CLAIM_STALE_MS,
|
|
694
|
+
now - REQUEST_CLAIM_STALE_MS,
|
|
695
|
+
);
|
|
696
|
+
if (Number(updated.changes ?? 0) === 0) return null;
|
|
697
|
+
return rowToRequest({ ...row, claimed_by: ownerId, claimed_at: now });
|
|
698
|
+
})
|
|
699
|
+
.immediate();
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
export function markRuntimeUserTurnRunning(
|
|
703
|
+
topicId: string,
|
|
704
|
+
requestId: string,
|
|
705
|
+
ownerId: string,
|
|
706
|
+
queryId: string,
|
|
707
|
+
): boolean {
|
|
708
|
+
const result = db
|
|
709
|
+
.query(
|
|
710
|
+
`UPDATE runtime_user_turn_requests
|
|
711
|
+
SET status = 'running', running_query_id = ?, claimed_at = ?
|
|
712
|
+
WHERE topic_id = ? AND request_id = ? AND claimed_by = ?`,
|
|
713
|
+
)
|
|
714
|
+
.run(queryId, Date.now(), topicId, requestId, ownerId);
|
|
715
|
+
return Number(result.changes ?? 0) > 0;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
export function releaseRuntimeUserTurnClaim(
|
|
719
|
+
topicId: string,
|
|
720
|
+
requestId: string,
|
|
721
|
+
ownerId: string,
|
|
722
|
+
): boolean {
|
|
723
|
+
const result = db
|
|
724
|
+
.query(
|
|
725
|
+
`UPDATE runtime_user_turn_requests
|
|
726
|
+
SET status = 'pending', claimed_by = NULL, claimed_at = NULL, running_query_id = NULL
|
|
727
|
+
WHERE topic_id = ? AND request_id = ? AND claimed_by = ?`,
|
|
728
|
+
)
|
|
729
|
+
.run(topicId, requestId, ownerId);
|
|
730
|
+
return Number(result.changes ?? 0) > 0;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
export function completeRuntimeUserTurnRequest(
|
|
734
|
+
topicId: string,
|
|
735
|
+
requestId: string,
|
|
736
|
+
ownerId: string,
|
|
737
|
+
): boolean {
|
|
738
|
+
const result = db
|
|
739
|
+
.query(
|
|
740
|
+
`DELETE FROM runtime_user_turn_requests
|
|
741
|
+
WHERE topic_id = ? AND request_id = ? AND claimed_by = ?`,
|
|
742
|
+
)
|
|
743
|
+
.run(topicId, requestId, ownerId);
|
|
744
|
+
return Number(result.changes ?? 0) > 0;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Drop a queued request the running turn already read: its message was
|
|
749
|
+
* delivered into that turn mid-way (Codex-style steer), so it must not run
|
|
750
|
+
* again as a turn of its own. Only a still-pending row goes; a claimed one is
|
|
751
|
+
* already some worker's turn.
|
|
752
|
+
*/
|
|
753
|
+
export function dropReadMidTurnRequest(topicId: string, requestId: string): boolean {
|
|
754
|
+
const result = db
|
|
755
|
+
.query(
|
|
756
|
+
`DELETE FROM runtime_user_turn_requests
|
|
757
|
+
WHERE topic_id = ? AND request_id = ? AND status = 'pending' AND claimed_by IS NULL`,
|
|
758
|
+
)
|
|
759
|
+
.run(topicId, requestId);
|
|
760
|
+
return Number(result.changes ?? 0) > 0;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/** Cancel work captured before a reset/delete epoch began. */
|
|
764
|
+
export function cancelRuntimeUserTurnRequestsBeforeEpoch(topicId: string, epoch: number): string[] {
|
|
765
|
+
const rows = db
|
|
766
|
+
.query<{ request_id: string }, [string, number]>(
|
|
767
|
+
"SELECT request_id FROM runtime_user_turn_requests WHERE topic_id = ? AND topic_epoch < ?",
|
|
768
|
+
)
|
|
769
|
+
.all(topicId, epoch);
|
|
770
|
+
db.query("DELETE FROM runtime_user_turn_requests WHERE topic_id = ? AND topic_epoch < ?").run(
|
|
771
|
+
topicId,
|
|
772
|
+
epoch,
|
|
773
|
+
);
|
|
774
|
+
return rows.map((row) => row.request_id);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
export function cancelRuntimeUserTurnRequests(topicId: string): string[] {
|
|
778
|
+
const rows = db
|
|
779
|
+
.query<{ request_id: string }, [string]>(
|
|
780
|
+
"SELECT request_id FROM runtime_user_turn_requests WHERE topic_id = ?",
|
|
781
|
+
)
|
|
782
|
+
.all(topicId);
|
|
783
|
+
db.query("DELETE FROM runtime_user_turn_requests WHERE topic_id = ?").run(topicId);
|
|
784
|
+
return rows.map((row) => row.request_id);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* The request a worker currently holds for `topicId` — claimed or already
|
|
789
|
+
* running — if any. This is the turn a new human message could steer; whether
|
|
790
|
+
* it may is decided by whoever folds the new message (see
|
|
791
|
+
* `submitRuntimeGatewayTurn`).
|
|
792
|
+
*/
|
|
793
|
+
export function getActiveRuntimeUserTurnRequest(topicId: string): RuntimeUserTurnRequest | null {
|
|
794
|
+
const row = db
|
|
795
|
+
.query<RuntimeUserTurnRequestRow, [string, number]>(
|
|
796
|
+
`SELECT * FROM runtime_user_turn_requests
|
|
797
|
+
WHERE topic_id = ?
|
|
798
|
+
AND (status = 'running' OR (claimed_by IS NOT NULL AND claimed_at >= ?))
|
|
799
|
+
ORDER BY created_at ASC, rowid ASC LIMIT 1`,
|
|
800
|
+
)
|
|
801
|
+
.get(topicId, Date.now() - REQUEST_CLAIM_STALE_MS);
|
|
802
|
+
return row ? rowToRequest(row) : null;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* Fold the messages queued behind a running turn into it, so a steer can run
|
|
807
|
+
* them now. Same replacement `mergeRuntimeUserTurnRequest` makes for a steering
|
|
808
|
+
* message, but with no new message: the running request and the queued tail it
|
|
809
|
+
* shares an actor and thread with become one pending batch. Returns the
|
|
810
|
+
* absorbed running request's id, or null when there is nothing to fold (no
|
|
811
|
+
* running turn, nothing queued, or the queue belongs to someone else).
|
|
812
|
+
*/
|
|
813
|
+
export function foldQueuedIntoActiveRuntimeUserTurnRequest(
|
|
814
|
+
topicId: string,
|
|
815
|
+
newRequestId: string,
|
|
816
|
+
): string | null {
|
|
817
|
+
const active = getActiveRuntimeUserTurnRequest(topicId);
|
|
818
|
+
const latest = getRuntimeUserTurnRequest(topicId);
|
|
819
|
+
if (!active || !latest || latest.requestId === active.requestId || !latest.execution) {
|
|
820
|
+
return null;
|
|
821
|
+
}
|
|
822
|
+
if (requestActor(latest.execution.actorUserId) !== requestActor(active.execution?.actorUserId)) {
|
|
823
|
+
return null;
|
|
824
|
+
}
|
|
825
|
+
const { deliveredMidTurn: _delivered, ...execution } = latest.execution;
|
|
826
|
+
const merged = mergeRuntimeUserTurnRequest({
|
|
827
|
+
topicId,
|
|
828
|
+
userId: latest.userId,
|
|
829
|
+
userMessages: [],
|
|
830
|
+
allowAutoContinue: latest.allowAutoContinue,
|
|
831
|
+
requestId: newRequestId,
|
|
832
|
+
execution,
|
|
833
|
+
topicEpoch: latest.topicEpoch,
|
|
834
|
+
includeDeliveredMidTurn: true,
|
|
835
|
+
});
|
|
836
|
+
return merged.supersededRequestIds.includes(active.requestId) ? active.requestId : null;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
export function getRuntimeUserTurnRequest(topicId: string): RuntimeUserTurnRequest | null {
|
|
840
|
+
const row = db
|
|
841
|
+
.query<RuntimeUserTurnRequestRow, [string]>(
|
|
842
|
+
"SELECT * FROM runtime_user_turn_requests WHERE topic_id = ? ORDER BY created_at DESC, rowid DESC LIMIT 1",
|
|
843
|
+
)
|
|
844
|
+
.get(topicId);
|
|
845
|
+
return row ? rowToRequest(row) : null;
|
|
846
|
+
}
|