@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,1163 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
type AgentKind,
|
|
4
|
+
db as defaultDb,
|
|
5
|
+
type EffortLevel,
|
|
6
|
+
FALLBACK_AGENT,
|
|
7
|
+
getRegistry,
|
|
8
|
+
getTopic,
|
|
9
|
+
isAgentKind,
|
|
10
|
+
logger,
|
|
11
|
+
modelOwner,
|
|
12
|
+
} from "../src/cron-host.ts";
|
|
13
|
+
import { computeNextCronRun, normalizeCronTimezone, parseCronExpression } from "#schedule";
|
|
14
|
+
import { validateCronScriptName } from "#scripts";
|
|
15
|
+
|
|
16
|
+
/** Validate persisted overrides against the agent that will execute the job. */
|
|
17
|
+
function validateCronAgentConfig(
|
|
18
|
+
topicId: string,
|
|
19
|
+
agent: AgentKind | undefined,
|
|
20
|
+
model: string | undefined,
|
|
21
|
+
effort: EffortLevel | undefined,
|
|
22
|
+
): void {
|
|
23
|
+
if (agent !== undefined && !isAgentKind(agent)) {
|
|
24
|
+
throw new Error(`agent '${agent}' is not a recognized agent kind`);
|
|
25
|
+
}
|
|
26
|
+
if (!model && !effort) return;
|
|
27
|
+
const topic = getTopic(topicId);
|
|
28
|
+
const effectiveAgent = agent ?? (topic?.agent as AgentKind | undefined) ?? FALLBACK_AGENT;
|
|
29
|
+
const registry = getRegistry(effectiveAgent);
|
|
30
|
+
if (model) {
|
|
31
|
+
// Codex accepts future IDs, so modelOwner must separately reject cross-agent models.
|
|
32
|
+
const owner = modelOwner(model);
|
|
33
|
+
if ((owner && owner !== effectiveAgent) || !registry.validateModel(model)) {
|
|
34
|
+
throw new Error(`model '${model}' is not valid for agent '${effectiveAgent}'`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (effort && !registry.validateEffort(effort)) {
|
|
38
|
+
throw new Error(`effort '${effort}' is not valid for agent '${effectiveAgent}'`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type CronRunStatus =
|
|
43
|
+
| "pending"
|
|
44
|
+
| "running"
|
|
45
|
+
| "succeeded"
|
|
46
|
+
| "failed"
|
|
47
|
+
| "aborted"
|
|
48
|
+
| "skipped"
|
|
49
|
+
| "interrupted";
|
|
50
|
+
|
|
51
|
+
export interface CronJobRecord {
|
|
52
|
+
id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
ownerUserId: string;
|
|
55
|
+
/** Product-side owner when execution uses a shared canonical principal. */
|
|
56
|
+
actorOwnerUserId?: string;
|
|
57
|
+
topicId: string;
|
|
58
|
+
prompt?: string;
|
|
59
|
+
script?: string;
|
|
60
|
+
/** Short human label for a prompt job, generated asynchronously. */
|
|
61
|
+
summary?: string;
|
|
62
|
+
schedule: string;
|
|
63
|
+
timezone?: string;
|
|
64
|
+
enabled: boolean;
|
|
65
|
+
agent?: AgentKind;
|
|
66
|
+
model?: string;
|
|
67
|
+
effort?: EffortLevel;
|
|
68
|
+
/** @deprecated Session ownership is topic-scoped; use getCronTopicSession(). */
|
|
69
|
+
sessionId?: string;
|
|
70
|
+
nextRunAt: string;
|
|
71
|
+
createdAt: string;
|
|
72
|
+
updatedAt: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function cronActorOwnerUserId(job: Pick<CronJobRecord, "ownerUserId" | "actorOwnerUserId">) {
|
|
76
|
+
return job.actorOwnerUserId ?? job.ownerUserId;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface CronJobPatch {
|
|
80
|
+
name?: string;
|
|
81
|
+
topicId?: string;
|
|
82
|
+
prompt?: string | null;
|
|
83
|
+
script?: string | null;
|
|
84
|
+
summary?: string | null;
|
|
85
|
+
schedule?: string;
|
|
86
|
+
timezone?: string | null;
|
|
87
|
+
enabled?: boolean;
|
|
88
|
+
agent?: AgentKind | null;
|
|
89
|
+
model?: string | null;
|
|
90
|
+
effort?: EffortLevel | null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface CronTopicSessionRecord {
|
|
94
|
+
topicId: string;
|
|
95
|
+
agent: AgentKind;
|
|
96
|
+
ownerUserId: string;
|
|
97
|
+
sessionId: string;
|
|
98
|
+
createdAt: string;
|
|
99
|
+
updatedAt: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface CronTopicContextRecord {
|
|
103
|
+
topicId: string;
|
|
104
|
+
successfulRunsSinceRotation: number;
|
|
105
|
+
lastRotatedAt?: string;
|
|
106
|
+
updatedAt: string;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface CronRunRecord {
|
|
110
|
+
id: string;
|
|
111
|
+
jobId: string;
|
|
112
|
+
source: "schedule" | "manual";
|
|
113
|
+
scheduledAt: string;
|
|
114
|
+
startedAt?: string;
|
|
115
|
+
finishedAt?: string;
|
|
116
|
+
status: CronRunStatus;
|
|
117
|
+
queryId?: string;
|
|
118
|
+
durationMs?: number;
|
|
119
|
+
outputPreview?: string;
|
|
120
|
+
error?: string;
|
|
121
|
+
exitCode?: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface JobRow {
|
|
125
|
+
id: string;
|
|
126
|
+
name: string;
|
|
127
|
+
owner_user_id: string;
|
|
128
|
+
actor_owner_user_id: string | null;
|
|
129
|
+
topic_id: string;
|
|
130
|
+
prompt: string;
|
|
131
|
+
script: string | null;
|
|
132
|
+
summary: string | null;
|
|
133
|
+
schedule: string;
|
|
134
|
+
timezone: string | null;
|
|
135
|
+
enabled: number;
|
|
136
|
+
agent: string | null;
|
|
137
|
+
model: string | null;
|
|
138
|
+
effort: string | null;
|
|
139
|
+
session_id: string | null;
|
|
140
|
+
next_run_at: string;
|
|
141
|
+
created_at: string;
|
|
142
|
+
updated_at: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
interface RunRow {
|
|
146
|
+
id: string;
|
|
147
|
+
job_id: string;
|
|
148
|
+
source: "schedule" | "manual";
|
|
149
|
+
scheduled_at: string;
|
|
150
|
+
started_at: string | null;
|
|
151
|
+
finished_at: string | null;
|
|
152
|
+
status: CronRunStatus;
|
|
153
|
+
query_id: string | null;
|
|
154
|
+
duration_ms: number | null;
|
|
155
|
+
output_preview: string | null;
|
|
156
|
+
error: string | null;
|
|
157
|
+
exit_code: number | null;
|
|
158
|
+
topic_id: string | null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interface TopicSessionRow {
|
|
162
|
+
topic_id: string;
|
|
163
|
+
agent: string;
|
|
164
|
+
owner_user_id: string;
|
|
165
|
+
session_id: string;
|
|
166
|
+
created_at: string;
|
|
167
|
+
updated_at: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface TopicContextRow {
|
|
171
|
+
topic_id: string;
|
|
172
|
+
successful_runs_since_rotation: number;
|
|
173
|
+
last_rotated_at: string | null;
|
|
174
|
+
updated_at: string;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface TableColumnRow {
|
|
178
|
+
name: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let schemaReady = false;
|
|
182
|
+
|
|
183
|
+
export interface CronDatabaseStatement {
|
|
184
|
+
get(...params: any[]): unknown;
|
|
185
|
+
all(...params: any[]): unknown[];
|
|
186
|
+
run(...params: any[]): CronDatabaseRunResult;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface CronDatabaseRunResult {
|
|
190
|
+
changes: number | bigint;
|
|
191
|
+
lastInsertRowid?: number | bigint;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface CronDatabaseTransaction<Result> {
|
|
195
|
+
(): Result;
|
|
196
|
+
deferred?: () => Result;
|
|
197
|
+
immediate?: () => Result;
|
|
198
|
+
exclusive?: () => Result;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Structural SQLite surface accepted from Bun, Node, or an embedding host. */
|
|
202
|
+
export interface CronDatabase {
|
|
203
|
+
exec(sql: string): unknown;
|
|
204
|
+
query(sql: string): CronDatabaseStatement;
|
|
205
|
+
transaction<Result>(fn: () => Result): CronDatabaseTransaction<Result>;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let db: CronDatabase = defaultDb;
|
|
209
|
+
|
|
210
|
+
/** Replace the cron persistence handle for an embedding host. */
|
|
211
|
+
export function configureCronDatabase(database: CronDatabase): () => void {
|
|
212
|
+
const previous = db;
|
|
213
|
+
db = database;
|
|
214
|
+
schemaReady = false;
|
|
215
|
+
let disposed = false;
|
|
216
|
+
return () => {
|
|
217
|
+
if (disposed) return;
|
|
218
|
+
disposed = true;
|
|
219
|
+
db = previous;
|
|
220
|
+
schemaReady = false;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function ensureCronSchema(): void {
|
|
225
|
+
if (schemaReady) return;
|
|
226
|
+
db.exec(`
|
|
227
|
+
CREATE TABLE IF NOT EXISTS negotium_cron_jobs (
|
|
228
|
+
id TEXT PRIMARY KEY,
|
|
229
|
+
name TEXT NOT NULL,
|
|
230
|
+
owner_user_id TEXT NOT NULL,
|
|
231
|
+
actor_owner_user_id TEXT,
|
|
232
|
+
topic_id TEXT NOT NULL REFERENCES api_topics(id) ON DELETE CASCADE,
|
|
233
|
+
prompt TEXT NOT NULL,
|
|
234
|
+
script TEXT,
|
|
235
|
+
summary TEXT,
|
|
236
|
+
schedule TEXT NOT NULL,
|
|
237
|
+
timezone TEXT,
|
|
238
|
+
enabled INTEGER NOT NULL DEFAULT 1,
|
|
239
|
+
agent TEXT,
|
|
240
|
+
model TEXT,
|
|
241
|
+
effort TEXT,
|
|
242
|
+
session_id TEXT,
|
|
243
|
+
next_run_at TEXT NOT NULL,
|
|
244
|
+
created_at TEXT NOT NULL,
|
|
245
|
+
updated_at TEXT NOT NULL
|
|
246
|
+
);
|
|
247
|
+
CREATE INDEX IF NOT EXISTS idx_negotium_cron_due
|
|
248
|
+
ON negotium_cron_jobs(enabled, next_run_at);
|
|
249
|
+
CREATE TABLE IF NOT EXISTS negotium_cron_runs (
|
|
250
|
+
id TEXT PRIMARY KEY,
|
|
251
|
+
job_id TEXT NOT NULL REFERENCES negotium_cron_jobs(id) ON DELETE CASCADE,
|
|
252
|
+
source TEXT NOT NULL,
|
|
253
|
+
scheduled_at TEXT NOT NULL,
|
|
254
|
+
started_at TEXT,
|
|
255
|
+
finished_at TEXT,
|
|
256
|
+
status TEXT NOT NULL,
|
|
257
|
+
query_id TEXT,
|
|
258
|
+
duration_ms INTEGER,
|
|
259
|
+
output_preview TEXT,
|
|
260
|
+
error TEXT,
|
|
261
|
+
exit_code INTEGER,
|
|
262
|
+
topic_id TEXT
|
|
263
|
+
);
|
|
264
|
+
CREATE INDEX IF NOT EXISTS idx_negotium_cron_runs_job
|
|
265
|
+
ON negotium_cron_runs(job_id, scheduled_at DESC);
|
|
266
|
+
CREATE TABLE IF NOT EXISTS negotium_cron_requests (
|
|
267
|
+
id TEXT PRIMARY KEY,
|
|
268
|
+
job_id TEXT NOT NULL REFERENCES negotium_cron_jobs(id) ON DELETE CASCADE,
|
|
269
|
+
requested_at TEXT NOT NULL
|
|
270
|
+
);
|
|
271
|
+
CREATE TABLE IF NOT EXISTS negotium_cron_cancellations (
|
|
272
|
+
id TEXT PRIMARY KEY,
|
|
273
|
+
job_id TEXT NOT NULL REFERENCES negotium_cron_jobs(id) ON DELETE CASCADE,
|
|
274
|
+
requested_at TEXT NOT NULL
|
|
275
|
+
);
|
|
276
|
+
CREATE TABLE IF NOT EXISTS negotium_cron_topic_sessions (
|
|
277
|
+
topic_id TEXT NOT NULL,
|
|
278
|
+
agent TEXT NOT NULL,
|
|
279
|
+
owner_user_id TEXT NOT NULL,
|
|
280
|
+
session_id TEXT NOT NULL,
|
|
281
|
+
created_at TEXT NOT NULL,
|
|
282
|
+
updated_at TEXT NOT NULL,
|
|
283
|
+
PRIMARY KEY (topic_id, agent)
|
|
284
|
+
);
|
|
285
|
+
CREATE INDEX IF NOT EXISTS idx_negotium_cron_topic_sessions_owner
|
|
286
|
+
ON negotium_cron_topic_sessions(owner_user_id, topic_id);
|
|
287
|
+
CREATE TABLE IF NOT EXISTS negotium_cron_topic_context (
|
|
288
|
+
topic_id TEXT PRIMARY KEY REFERENCES api_topics(id) ON DELETE CASCADE,
|
|
289
|
+
successful_runs_since_rotation INTEGER NOT NULL DEFAULT 0,
|
|
290
|
+
last_rotated_at TEXT,
|
|
291
|
+
updated_at TEXT NOT NULL
|
|
292
|
+
);
|
|
293
|
+
`);
|
|
294
|
+
const jobColumns = new Set(
|
|
295
|
+
(db.query("PRAGMA table_info(negotium_cron_jobs)").all() as TableColumnRow[]).map(
|
|
296
|
+
(column) => column.name,
|
|
297
|
+
),
|
|
298
|
+
);
|
|
299
|
+
if (!jobColumns.has("script")) {
|
|
300
|
+
db.exec("ALTER TABLE negotium_cron_jobs ADD COLUMN script TEXT");
|
|
301
|
+
}
|
|
302
|
+
if (!jobColumns.has("actor_owner_user_id")) {
|
|
303
|
+
db.exec("ALTER TABLE negotium_cron_jobs ADD COLUMN actor_owner_user_id TEXT");
|
|
304
|
+
}
|
|
305
|
+
db.exec("DROP INDEX IF EXISTS idx_negotium_cron_owner_name");
|
|
306
|
+
db.exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_negotium_cron_actor_name
|
|
307
|
+
ON negotium_cron_jobs(COALESCE(actor_owner_user_id, owner_user_id), name)`);
|
|
308
|
+
if (!jobColumns.has("summary")) {
|
|
309
|
+
db.exec("ALTER TABLE negotium_cron_jobs ADD COLUMN summary TEXT");
|
|
310
|
+
}
|
|
311
|
+
const runColumns = new Set(
|
|
312
|
+
(db.query("PRAGMA table_info(negotium_cron_runs)").all() as TableColumnRow[]).map(
|
|
313
|
+
(column) => column.name,
|
|
314
|
+
),
|
|
315
|
+
);
|
|
316
|
+
if (!runColumns.has("exit_code")) {
|
|
317
|
+
db.exec("ALTER TABLE negotium_cron_runs ADD COLUMN exit_code INTEGER");
|
|
318
|
+
}
|
|
319
|
+
if (!runColumns.has("topic_id")) {
|
|
320
|
+
db.exec("ALTER TABLE negotium_cron_runs ADD COLUMN topic_id TEXT");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// One-time best-effort promotion from the old job-owned session model.
|
|
324
|
+
// Rows are processed oldest-to-newest so the most recently updated job wins
|
|
325
|
+
// when several legacy jobs targeted the same topic and provider.
|
|
326
|
+
const legacySessions = db
|
|
327
|
+
.query(
|
|
328
|
+
`SELECT j.topic_id, COALESCE(j.agent, t.agent) AS agent,
|
|
329
|
+
j.owner_user_id, j.session_id, j.created_at, j.updated_at
|
|
330
|
+
FROM negotium_cron_jobs j
|
|
331
|
+
LEFT JOIN api_topics t ON t.id = j.topic_id
|
|
332
|
+
WHERE j.session_id IS NOT NULL AND COALESCE(j.agent, t.agent) IS NOT NULL
|
|
333
|
+
ORDER BY j.updated_at ASC`,
|
|
334
|
+
)
|
|
335
|
+
.all() as TopicSessionRow[];
|
|
336
|
+
for (const legacy of legacySessions) {
|
|
337
|
+
db.query(
|
|
338
|
+
`INSERT INTO negotium_cron_topic_sessions
|
|
339
|
+
(topic_id,agent,owner_user_id,session_id,created_at,updated_at)
|
|
340
|
+
VALUES (?,?,?,?,?,?)
|
|
341
|
+
ON CONFLICT(topic_id,agent) DO UPDATE SET
|
|
342
|
+
owner_user_id = excluded.owner_user_id,
|
|
343
|
+
session_id = excluded.session_id,
|
|
344
|
+
updated_at = excluded.updated_at`,
|
|
345
|
+
).run(
|
|
346
|
+
legacy.topic_id,
|
|
347
|
+
legacy.agent,
|
|
348
|
+
legacy.owner_user_id,
|
|
349
|
+
legacy.session_id,
|
|
350
|
+
legacy.created_at,
|
|
351
|
+
legacy.updated_at,
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
if (legacySessions.length > 0) {
|
|
355
|
+
db.query("UPDATE negotium_cron_jobs SET session_id = NULL WHERE session_id IS NOT NULL").run();
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Repair rows an older build's validation bypass could have persisted
|
|
359
|
+
// (validateCronAgentConfig used to skip the agent check entirely when
|
|
360
|
+
// neither model nor effort was set). Clearing back to NULL makes the job
|
|
361
|
+
// inherit its topic's current agent again instead of failing dispatch.
|
|
362
|
+
const invalidAgentRows = db
|
|
363
|
+
.query(
|
|
364
|
+
"SELECT id, agent FROM negotium_cron_jobs WHERE agent IS NOT NULL AND agent NOT IN ('claude','codex','maestro')",
|
|
365
|
+
)
|
|
366
|
+
.all() as { id: string; agent: string }[];
|
|
367
|
+
if (invalidAgentRows.length > 0) {
|
|
368
|
+
db.query(
|
|
369
|
+
"UPDATE negotium_cron_jobs SET agent = NULL WHERE agent IS NOT NULL AND agent NOT IN ('claude','codex','maestro')",
|
|
370
|
+
).run();
|
|
371
|
+
logger.warn(
|
|
372
|
+
{
|
|
373
|
+
jobIds: invalidAgentRows.map((row) => row.id),
|
|
374
|
+
invalidAgents: invalidAgentRows.map((row) => row.agent),
|
|
375
|
+
},
|
|
376
|
+
"cron: cleared unrecognized agent values persisted by an older build; jobs now inherit their topic's agent",
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
schemaReady = true;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function toJob(row: JobRow): CronJobRecord {
|
|
384
|
+
// Defense in depth: ensureCronSchema() repairs unrecognized values already
|
|
385
|
+
// on disk, but treat any raw write that bypassed both as absent rather
|
|
386
|
+
// than forwarding a bogus agent string to the scheduler.
|
|
387
|
+
const rowAgent = row.agent && isAgentKind(row.agent) ? row.agent : undefined;
|
|
388
|
+
const topicAgent = rowAgent
|
|
389
|
+
? rowAgent
|
|
390
|
+
: (
|
|
391
|
+
db.query("SELECT agent FROM api_topics WHERE id = ?").get(row.topic_id) as
|
|
392
|
+
| { agent: string | null }
|
|
393
|
+
| undefined
|
|
394
|
+
)?.agent;
|
|
395
|
+
const topicSession = topicAgent
|
|
396
|
+
? (db
|
|
397
|
+
.query(
|
|
398
|
+
"SELECT session_id FROM negotium_cron_topic_sessions WHERE topic_id = ? AND agent = ?",
|
|
399
|
+
)
|
|
400
|
+
.get(row.topic_id, topicAgent) as { session_id: string } | undefined)
|
|
401
|
+
: undefined;
|
|
402
|
+
return {
|
|
403
|
+
id: row.id,
|
|
404
|
+
name: row.name,
|
|
405
|
+
ownerUserId: row.owner_user_id,
|
|
406
|
+
actorOwnerUserId: row.actor_owner_user_id ?? undefined,
|
|
407
|
+
topicId: row.topic_id,
|
|
408
|
+
prompt: row.prompt.trim() || undefined,
|
|
409
|
+
script: row.script?.trim() || undefined,
|
|
410
|
+
summary: row.summary?.trim() || undefined,
|
|
411
|
+
schedule: row.schedule,
|
|
412
|
+
timezone: row.timezone ?? undefined,
|
|
413
|
+
enabled: row.enabled !== 0,
|
|
414
|
+
agent: rowAgent,
|
|
415
|
+
model: row.model ?? undefined,
|
|
416
|
+
effort: (row.effort as EffortLevel | null) ?? undefined,
|
|
417
|
+
sessionId: topicSession?.session_id,
|
|
418
|
+
nextRunAt: row.next_run_at,
|
|
419
|
+
createdAt: row.created_at,
|
|
420
|
+
updatedAt: row.updated_at,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function toTopicSession(row: TopicSessionRow): CronTopicSessionRecord {
|
|
425
|
+
return {
|
|
426
|
+
topicId: row.topic_id,
|
|
427
|
+
agent: row.agent as AgentKind,
|
|
428
|
+
ownerUserId: row.owner_user_id,
|
|
429
|
+
sessionId: row.session_id,
|
|
430
|
+
createdAt: row.created_at,
|
|
431
|
+
updatedAt: row.updated_at,
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function toTopicContext(row: TopicContextRow): CronTopicContextRecord {
|
|
436
|
+
return {
|
|
437
|
+
topicId: row.topic_id,
|
|
438
|
+
successfulRunsSinceRotation: row.successful_runs_since_rotation,
|
|
439
|
+
lastRotatedAt: row.last_rotated_at ?? undefined,
|
|
440
|
+
updatedAt: row.updated_at,
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export function cronTopicSessionName(topicId: string): string {
|
|
445
|
+
return `cron-${topicId}`;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function toRun(row: RunRow): CronRunRecord {
|
|
449
|
+
return {
|
|
450
|
+
id: row.id,
|
|
451
|
+
jobId: row.job_id,
|
|
452
|
+
source: row.source,
|
|
453
|
+
scheduledAt: row.scheduled_at,
|
|
454
|
+
startedAt: row.started_at ?? undefined,
|
|
455
|
+
finishedAt: row.finished_at ?? undefined,
|
|
456
|
+
status: row.status,
|
|
457
|
+
queryId: row.query_id ?? undefined,
|
|
458
|
+
durationMs: row.duration_ms ?? undefined,
|
|
459
|
+
outputPreview: row.output_preview ?? undefined,
|
|
460
|
+
error: row.error ?? undefined,
|
|
461
|
+
exitCode: row.exit_code ?? undefined,
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
export function createCronJob(input: {
|
|
466
|
+
name: string;
|
|
467
|
+
ownerUserId: string;
|
|
468
|
+
actorOwnerUserId?: string;
|
|
469
|
+
topicId: string;
|
|
470
|
+
prompt?: string;
|
|
471
|
+
script?: string;
|
|
472
|
+
schedule: string;
|
|
473
|
+
timezone?: string;
|
|
474
|
+
agent?: AgentKind;
|
|
475
|
+
model?: string;
|
|
476
|
+
effort?: EffortLevel;
|
|
477
|
+
summary?: string;
|
|
478
|
+
now?: Date;
|
|
479
|
+
}): CronJobRecord {
|
|
480
|
+
ensureCronSchema();
|
|
481
|
+
const prompt = input.prompt?.trim() || undefined;
|
|
482
|
+
const script = input.script?.trim() || undefined;
|
|
483
|
+
if (Boolean(prompt) === Boolean(script)) {
|
|
484
|
+
throw new Error("cron job requires exactly one of prompt or script");
|
|
485
|
+
}
|
|
486
|
+
if (script) {
|
|
487
|
+
const valid = validateCronScriptName(script);
|
|
488
|
+
if (!valid.ok) throw new Error(valid.error);
|
|
489
|
+
}
|
|
490
|
+
const otherOwner = db
|
|
491
|
+
.query(
|
|
492
|
+
`SELECT owner_user_id
|
|
493
|
+
FROM negotium_cron_jobs
|
|
494
|
+
WHERE topic_id = ? AND owner_user_id <> ?
|
|
495
|
+
LIMIT 1`,
|
|
496
|
+
)
|
|
497
|
+
.get(input.topicId, input.ownerUserId) as { owner_user_id: string } | undefined;
|
|
498
|
+
if (otherOwner) {
|
|
499
|
+
throw new Error(
|
|
500
|
+
`topic cron context is already owned by ${otherOwner.owner_user_id}; ` +
|
|
501
|
+
"all cron jobs in one topic must share one owner",
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
parseCronExpression(input.schedule);
|
|
505
|
+
const timezone = input.timezone ? normalizeCronTimezone(input.timezone) : undefined;
|
|
506
|
+
if (input.timezone && !timezone) throw new Error(`invalid timezone: ${input.timezone}`);
|
|
507
|
+
validateCronAgentConfig(input.topicId, input.agent, input.model, input.effort);
|
|
508
|
+
const now = input.now ?? new Date();
|
|
509
|
+
const nowIso = now.toISOString();
|
|
510
|
+
const job: CronJobRecord = {
|
|
511
|
+
id: randomUUID(),
|
|
512
|
+
name: input.name,
|
|
513
|
+
ownerUserId: input.ownerUserId,
|
|
514
|
+
actorOwnerUserId: input.actorOwnerUserId?.trim() || undefined,
|
|
515
|
+
topicId: input.topicId,
|
|
516
|
+
prompt,
|
|
517
|
+
script,
|
|
518
|
+
summary: input.summary?.trim() || undefined,
|
|
519
|
+
schedule: input.schedule.trim(),
|
|
520
|
+
timezone,
|
|
521
|
+
enabled: true,
|
|
522
|
+
agent: input.agent,
|
|
523
|
+
model: input.model,
|
|
524
|
+
effort: input.effort,
|
|
525
|
+
nextRunAt: computeNextCronRun(input.schedule, now, timezone).toISOString(),
|
|
526
|
+
createdAt: nowIso,
|
|
527
|
+
updatedAt: nowIso,
|
|
528
|
+
};
|
|
529
|
+
db.query(
|
|
530
|
+
`INSERT INTO negotium_cron_jobs
|
|
531
|
+
(id,name,owner_user_id,actor_owner_user_id,topic_id,prompt,script,summary,schedule,timezone,enabled,agent,model,effort,session_id,next_run_at,created_at,updated_at)
|
|
532
|
+
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
|
|
533
|
+
).run(
|
|
534
|
+
job.id,
|
|
535
|
+
job.name,
|
|
536
|
+
job.ownerUserId,
|
|
537
|
+
job.actorOwnerUserId ?? null,
|
|
538
|
+
job.topicId,
|
|
539
|
+
job.prompt ?? "",
|
|
540
|
+
job.script ?? null,
|
|
541
|
+
job.summary ?? null,
|
|
542
|
+
job.schedule,
|
|
543
|
+
job.timezone ?? null,
|
|
544
|
+
1,
|
|
545
|
+
job.agent ?? null,
|
|
546
|
+
job.model ?? null,
|
|
547
|
+
job.effort ?? null,
|
|
548
|
+
null,
|
|
549
|
+
job.nextRunAt,
|
|
550
|
+
job.createdAt,
|
|
551
|
+
job.updatedAt,
|
|
552
|
+
);
|
|
553
|
+
return job;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export function listCronJobs(ownerUserId?: string): CronJobRecord[] {
|
|
557
|
+
ensureCronSchema();
|
|
558
|
+
const rows = ownerUserId
|
|
559
|
+
? db
|
|
560
|
+
.query("SELECT * FROM negotium_cron_jobs WHERE owner_user_id = ? ORDER BY created_at DESC")
|
|
561
|
+
.all(ownerUserId)
|
|
562
|
+
: db.query("SELECT * FROM negotium_cron_jobs ORDER BY created_at DESC").all();
|
|
563
|
+
return (rows as JobRow[]).map(toJob);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export function listEnabledCronJobs(): CronJobRecord[] {
|
|
567
|
+
ensureCronSchema();
|
|
568
|
+
return (
|
|
569
|
+
db
|
|
570
|
+
.query("SELECT * FROM negotium_cron_jobs WHERE enabled = 1 ORDER BY created_at DESC")
|
|
571
|
+
.all() as JobRow[]
|
|
572
|
+
).map(toJob);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
export function listCronJobsForTopic(topicId: string): CronJobRecord[] {
|
|
576
|
+
ensureCronSchema();
|
|
577
|
+
return (
|
|
578
|
+
db
|
|
579
|
+
.query("SELECT * FROM negotium_cron_jobs WHERE topic_id = ? ORDER BY created_at DESC")
|
|
580
|
+
.all(topicId) as JobRow[]
|
|
581
|
+
).map(toJob);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export function listCronJobsForTopicOwner(topicId: string, ownerUserId: string): CronJobRecord[] {
|
|
585
|
+
ensureCronSchema();
|
|
586
|
+
return (
|
|
587
|
+
db
|
|
588
|
+
.query(
|
|
589
|
+
"SELECT * FROM negotium_cron_jobs WHERE topic_id = ? AND owner_user_id = ? ORDER BY created_at DESC",
|
|
590
|
+
)
|
|
591
|
+
.all(topicId, ownerUserId) as JobRow[]
|
|
592
|
+
).map(toJob);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export function getCronJob(id: string): CronJobRecord | null {
|
|
596
|
+
ensureCronSchema();
|
|
597
|
+
const row = db.query("SELECT * FROM negotium_cron_jobs WHERE id = ?").get(id) as
|
|
598
|
+
| JobRow
|
|
599
|
+
| undefined;
|
|
600
|
+
return row ? toJob(row) : null;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function getCronJobByOwnerAndName(ownerUserId: string, name: string): CronJobRecord | null {
|
|
604
|
+
ensureCronSchema();
|
|
605
|
+
const row = db
|
|
606
|
+
.query(
|
|
607
|
+
"SELECT * FROM negotium_cron_jobs WHERE COALESCE(actor_owner_user_id, owner_user_id) = ? AND name = ?",
|
|
608
|
+
)
|
|
609
|
+
.get(ownerUserId, name) as JobRow | undefined;
|
|
610
|
+
return row ? toJob(row) : null;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
export function listCronJobsForActorOwner(actorOwnerUserId: string): CronJobRecord[] {
|
|
614
|
+
ensureCronSchema();
|
|
615
|
+
return (
|
|
616
|
+
db
|
|
617
|
+
.query(
|
|
618
|
+
"SELECT * FROM negotium_cron_jobs WHERE COALESCE(actor_owner_user_id, owner_user_id) = ? ORDER BY created_at DESC",
|
|
619
|
+
)
|
|
620
|
+
.all(actorOwnerUserId) as JobRow[]
|
|
621
|
+
).map(toJob);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
export function cronJobPatchChangesContext(job: CronJobRecord, patch: CronJobPatch): boolean {
|
|
625
|
+
return (
|
|
626
|
+
(patch.topicId !== undefined && patch.topicId !== job.topicId) ||
|
|
627
|
+
(patch.prompt !== undefined && (patch.prompt?.trim() || undefined) !== job.prompt) ||
|
|
628
|
+
(patch.script !== undefined && (patch.script?.trim() || undefined) !== job.script) ||
|
|
629
|
+
(patch.agent !== undefined && (patch.agent ?? undefined) !== job.agent) ||
|
|
630
|
+
(patch.model !== undefined && (patch.model?.trim() || undefined) !== job.model) ||
|
|
631
|
+
(patch.effort !== undefined && (patch.effort ?? undefined) !== job.effort)
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
export function updateCronJob(
|
|
636
|
+
id: string,
|
|
637
|
+
patch: CronJobPatch,
|
|
638
|
+
now = new Date(),
|
|
639
|
+
): CronJobRecord | null {
|
|
640
|
+
ensureCronSchema();
|
|
641
|
+
const job = getCronJob(id);
|
|
642
|
+
if (!job) return null;
|
|
643
|
+
if (Object.values(patch).every((value) => value === undefined)) return job;
|
|
644
|
+
|
|
645
|
+
const nextName = patch.name !== undefined ? patch.name.trim() : job.name;
|
|
646
|
+
if (!/^[A-Za-z0-9_-]+$/.test(nextName)) {
|
|
647
|
+
throw new Error("name must use only letters, numbers, dashes, and underscores");
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
const nextPrompt = patch.prompt !== undefined ? patch.prompt?.trim() || undefined : job.prompt;
|
|
651
|
+
const nextScript = patch.script !== undefined ? patch.script?.trim() || undefined : job.script;
|
|
652
|
+
if (Boolean(nextPrompt) === Boolean(nextScript)) {
|
|
653
|
+
throw new Error("cron job requires exactly one of prompt or script");
|
|
654
|
+
}
|
|
655
|
+
if (nextScript) {
|
|
656
|
+
const valid = validateCronScriptName(nextScript);
|
|
657
|
+
if (!valid.ok) throw new Error(valid.error);
|
|
658
|
+
}
|
|
659
|
+
const nextSchedule = patch.schedule?.trim() ?? job.schedule;
|
|
660
|
+
parseCronExpression(nextSchedule);
|
|
661
|
+
const rawTimezone =
|
|
662
|
+
patch.timezone !== undefined ? patch.timezone?.trim() || undefined : job.timezone;
|
|
663
|
+
const nextTimezone = rawTimezone ? normalizeCronTimezone(rawTimezone) : undefined;
|
|
664
|
+
if (rawTimezone && !nextTimezone) throw new Error(`invalid timezone: ${rawTimezone}`);
|
|
665
|
+
const nextTopicId = patch.topicId?.trim() || job.topicId;
|
|
666
|
+
const otherOwner = db
|
|
667
|
+
.query(
|
|
668
|
+
`SELECT owner_user_id FROM negotium_cron_jobs
|
|
669
|
+
WHERE topic_id = ? AND owner_user_id <> ? AND id <> ? LIMIT 1`,
|
|
670
|
+
)
|
|
671
|
+
.get(nextTopicId, job.ownerUserId, id) as { owner_user_id: string } | undefined;
|
|
672
|
+
if (otherOwner) {
|
|
673
|
+
throw new Error(
|
|
674
|
+
`topic cron context is already owned by ${otherOwner.owner_user_id}; ` +
|
|
675
|
+
"all cron jobs in one topic must share one owner",
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
const nextAgent = patch.agent !== undefined ? (patch.agent ?? undefined) : job.agent;
|
|
679
|
+
const nextModel = patch.model !== undefined ? patch.model?.trim() || undefined : job.model;
|
|
680
|
+
const nextEffort = patch.effort !== undefined ? (patch.effort ?? undefined) : job.effort;
|
|
681
|
+
validateCronAgentConfig(nextTopicId, nextAgent, nextModel, nextEffort);
|
|
682
|
+
|
|
683
|
+
const enabled = patch.enabled ?? job.enabled;
|
|
684
|
+
const scheduleChanged = nextSchedule !== job.schedule || nextTimezone !== job.timezone;
|
|
685
|
+
const nextRunAt =
|
|
686
|
+
enabled && (scheduleChanged || (!job.enabled && enabled))
|
|
687
|
+
? computeNextCronRun(nextSchedule, now, nextTimezone).toISOString()
|
|
688
|
+
: job.nextRunAt;
|
|
689
|
+
const timestamp = now.toISOString();
|
|
690
|
+
const sessionSensitiveChanged = cronJobPatchChangesContext(job, patch);
|
|
691
|
+
if (sessionSensitiveChanged) {
|
|
692
|
+
const active = db
|
|
693
|
+
.query(
|
|
694
|
+
"SELECT 1 FROM negotium_cron_runs WHERE job_id = ? AND status IN ('pending','running') LIMIT 1",
|
|
695
|
+
)
|
|
696
|
+
.get(id);
|
|
697
|
+
if (active) {
|
|
698
|
+
throw new Error("cannot change Cron context while a run is active; cancel it first");
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
db.transaction(() => {
|
|
703
|
+
db.query(
|
|
704
|
+
`UPDATE negotium_cron_jobs SET
|
|
705
|
+
name = ?, topic_id = ?, prompt = ?, script = ?, summary = ?, schedule = ?, timezone = ?,
|
|
706
|
+
enabled = ?, agent = ?, model = ?, effort = ?, next_run_at = ?, updated_at = ?
|
|
707
|
+
WHERE id = ?`,
|
|
708
|
+
).run(
|
|
709
|
+
nextName,
|
|
710
|
+
nextTopicId,
|
|
711
|
+
nextPrompt ?? "",
|
|
712
|
+
nextScript ?? null,
|
|
713
|
+
patch.summary !== undefined ? patch.summary?.trim() || null : (job.summary ?? null),
|
|
714
|
+
nextSchedule,
|
|
715
|
+
nextTimezone ?? null,
|
|
716
|
+
enabled ? 1 : 0,
|
|
717
|
+
patch.agent !== undefined ? patch.agent : (job.agent ?? null),
|
|
718
|
+
patch.model !== undefined ? patch.model?.trim() || null : (job.model ?? null),
|
|
719
|
+
patch.effort !== undefined ? patch.effort : (job.effort ?? null),
|
|
720
|
+
nextRunAt,
|
|
721
|
+
timestamp,
|
|
722
|
+
id,
|
|
723
|
+
);
|
|
724
|
+
})();
|
|
725
|
+
return getCronJob(id);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
export function updateCronJobSummaryIfPromptMatches(
|
|
729
|
+
id: string,
|
|
730
|
+
expectedPrompt: string,
|
|
731
|
+
summary: string,
|
|
732
|
+
): boolean {
|
|
733
|
+
ensureCronSchema();
|
|
734
|
+
const result = db
|
|
735
|
+
.query("UPDATE negotium_cron_jobs SET summary = ? WHERE id = ? AND prompt = ?")
|
|
736
|
+
.run(summary.trim(), id, expectedPrompt.trim());
|
|
737
|
+
return Number(result.changes) > 0;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
export function setCronJobEnabled(
|
|
741
|
+
id: string,
|
|
742
|
+
enabled: boolean,
|
|
743
|
+
now = new Date(),
|
|
744
|
+
): CronJobRecord | null {
|
|
745
|
+
ensureCronSchema();
|
|
746
|
+
const job = getCronJob(id);
|
|
747
|
+
if (!job) return null;
|
|
748
|
+
const nextRunAt = enabled
|
|
749
|
+
? computeNextCronRun(job.schedule, now, job.timezone).toISOString()
|
|
750
|
+
: job.nextRunAt;
|
|
751
|
+
db.query(
|
|
752
|
+
"UPDATE negotium_cron_jobs SET enabled = ?, next_run_at = ?, updated_at = ? WHERE id = ?",
|
|
753
|
+
).run(enabled ? 1 : 0, nextRunAt, now.toISOString(), id);
|
|
754
|
+
return getCronJob(id);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
export function setCronJobSessionId(id: string, sessionId: string | null): void {
|
|
758
|
+
const job = getCronJob(id);
|
|
759
|
+
if (!job) return;
|
|
760
|
+
const topic = db.query("SELECT agent FROM api_topics WHERE id = ?").get(job.topicId) as
|
|
761
|
+
| { agent: string | null }
|
|
762
|
+
| undefined;
|
|
763
|
+
const agent = job.agent ?? (topic?.agent as AgentKind | null | undefined);
|
|
764
|
+
if (!agent) return;
|
|
765
|
+
if (sessionId) setCronTopicSession(job.topicId, agent, job.ownerUserId, sessionId);
|
|
766
|
+
else resetCronTopicSessions(job.topicId);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
export function getCronTopicSession(
|
|
770
|
+
topicId: string,
|
|
771
|
+
agent: AgentKind,
|
|
772
|
+
): CronTopicSessionRecord | null {
|
|
773
|
+
ensureCronSchema();
|
|
774
|
+
const row = db
|
|
775
|
+
.query("SELECT * FROM negotium_cron_topic_sessions WHERE topic_id = ? AND agent = ?")
|
|
776
|
+
.get(topicId, agent) as TopicSessionRow | undefined;
|
|
777
|
+
return row ? toTopicSession(row) : null;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
export function listCronTopicSessions(topicId: string): CronTopicSessionRecord[] {
|
|
781
|
+
ensureCronSchema();
|
|
782
|
+
return (
|
|
783
|
+
db
|
|
784
|
+
.query("SELECT * FROM negotium_cron_topic_sessions WHERE topic_id = ? ORDER BY agent")
|
|
785
|
+
.all(topicId) as TopicSessionRow[]
|
|
786
|
+
).map(toTopicSession);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
export function setCronTopicSession(
|
|
790
|
+
topicId: string,
|
|
791
|
+
agent: AgentKind,
|
|
792
|
+
ownerUserId: string,
|
|
793
|
+
sessionId: string,
|
|
794
|
+
now = new Date(),
|
|
795
|
+
): CronTopicSessionRecord {
|
|
796
|
+
ensureCronSchema();
|
|
797
|
+
const timestamp = now.toISOString();
|
|
798
|
+
db.query(
|
|
799
|
+
`INSERT INTO negotium_cron_topic_sessions
|
|
800
|
+
(topic_id,agent,owner_user_id,session_id,created_at,updated_at)
|
|
801
|
+
VALUES (?,?,?,?,?,?)
|
|
802
|
+
ON CONFLICT(topic_id,agent) DO UPDATE SET
|
|
803
|
+
owner_user_id = excluded.owner_user_id,
|
|
804
|
+
session_id = excluded.session_id,
|
|
805
|
+
updated_at = excluded.updated_at`,
|
|
806
|
+
).run(topicId, agent, ownerUserId, sessionId, timestamp, timestamp);
|
|
807
|
+
return getCronTopicSession(topicId, agent)!;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
export function setCronTopicSessionIfJobUpdatedAt(
|
|
811
|
+
jobId: string,
|
|
812
|
+
expectedJobUpdatedAt: string,
|
|
813
|
+
topicId: string,
|
|
814
|
+
agent: AgentKind,
|
|
815
|
+
ownerUserId: string,
|
|
816
|
+
sessionId: string,
|
|
817
|
+
now = new Date(),
|
|
818
|
+
): CronTopicSessionRecord | null {
|
|
819
|
+
ensureCronSchema();
|
|
820
|
+
let updated = false;
|
|
821
|
+
db.transaction(() => {
|
|
822
|
+
const current = db
|
|
823
|
+
.query("SELECT topic_id, updated_at FROM negotium_cron_jobs WHERE id = ?")
|
|
824
|
+
.get(jobId) as { topic_id: string; updated_at: string } | undefined;
|
|
825
|
+
if (!current || current.topic_id !== topicId || current.updated_at !== expectedJobUpdatedAt)
|
|
826
|
+
return;
|
|
827
|
+
setCronTopicSession(topicId, agent, ownerUserId, sessionId, now);
|
|
828
|
+
updated = true;
|
|
829
|
+
})();
|
|
830
|
+
return updated ? getCronTopicSession(topicId, agent) : null;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
export function clearCronTopicSession(topicId: string, agent: AgentKind): boolean {
|
|
834
|
+
ensureCronSchema();
|
|
835
|
+
const result = db
|
|
836
|
+
.query("DELETE FROM negotium_cron_topic_sessions WHERE topic_id = ? AND agent = ?")
|
|
837
|
+
.run(topicId, agent);
|
|
838
|
+
return Number(result.changes) > 0;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
export function resetCronTopicSessions(topicId: string): CronTopicSessionRecord[] {
|
|
842
|
+
ensureCronSchema();
|
|
843
|
+
const sessions = listCronTopicSessions(topicId);
|
|
844
|
+
db.query("DELETE FROM negotium_cron_topic_sessions WHERE topic_id = ?").run(topicId);
|
|
845
|
+
return sessions;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
export function getCronTopicContext(topicId: string): CronTopicContextRecord | null {
|
|
849
|
+
ensureCronSchema();
|
|
850
|
+
const row = db
|
|
851
|
+
.query("SELECT * FROM negotium_cron_topic_context WHERE topic_id = ?")
|
|
852
|
+
.get(topicId) as TopicContextRow | undefined;
|
|
853
|
+
return row ? toTopicContext(row) : null;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
function incrementCronTopicSuccess(topicId: string, now: Date): number {
|
|
857
|
+
const timestamp = now.toISOString();
|
|
858
|
+
db.query(
|
|
859
|
+
`INSERT INTO negotium_cron_topic_context
|
|
860
|
+
(topic_id,successful_runs_since_rotation,last_rotated_at,updated_at)
|
|
861
|
+
VALUES (?,1,NULL,?)
|
|
862
|
+
ON CONFLICT(topic_id) DO UPDATE SET
|
|
863
|
+
successful_runs_since_rotation = successful_runs_since_rotation + 1,
|
|
864
|
+
updated_at = excluded.updated_at`,
|
|
865
|
+
).run(topicId, timestamp);
|
|
866
|
+
return getCronTopicContext(topicId)!.successfulRunsSinceRotation;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
export function markCronTopicContextRotated(topicId: string, now = new Date()): void {
|
|
870
|
+
ensureCronSchema();
|
|
871
|
+
const timestamp = now.toISOString();
|
|
872
|
+
db.query(
|
|
873
|
+
`INSERT INTO negotium_cron_topic_context
|
|
874
|
+
(topic_id,successful_runs_since_rotation,last_rotated_at,updated_at)
|
|
875
|
+
VALUES (?,0,?,?)
|
|
876
|
+
ON CONFLICT(topic_id) DO UPDATE SET
|
|
877
|
+
successful_runs_since_rotation = 0,
|
|
878
|
+
last_rotated_at = excluded.last_rotated_at,
|
|
879
|
+
updated_at = excluded.updated_at`,
|
|
880
|
+
).run(topicId, timestamp, timestamp);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
export function resetCronTopicContextState(topicId: string): boolean {
|
|
884
|
+
ensureCronSchema();
|
|
885
|
+
const result = db
|
|
886
|
+
.query("DELETE FROM negotium_cron_topic_context WHERE topic_id = ?")
|
|
887
|
+
.run(topicId);
|
|
888
|
+
return Number(result.changes) > 0;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
export function listOrphanedCronTopicSessions(): CronTopicSessionRecord[] {
|
|
892
|
+
ensureCronSchema();
|
|
893
|
+
return (
|
|
894
|
+
db
|
|
895
|
+
.query(
|
|
896
|
+
`SELECT s.* FROM negotium_cron_topic_sessions s
|
|
897
|
+
LEFT JOIN api_topics t ON t.id = s.topic_id
|
|
898
|
+
WHERE t.id IS NULL
|
|
899
|
+
ORDER BY s.topic_id, s.agent`,
|
|
900
|
+
)
|
|
901
|
+
.all() as TopicSessionRow[]
|
|
902
|
+
).map(toTopicSession);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
export function deleteCronJob(id: string): boolean {
|
|
906
|
+
ensureCronSchema();
|
|
907
|
+
const result = db.query("DELETE FROM negotium_cron_jobs WHERE id = ?").run(id);
|
|
908
|
+
return Number(result.changes) > 0;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
export function requestCronRun(jobId: string, now = new Date()): string {
|
|
912
|
+
ensureCronSchema();
|
|
913
|
+
const id = randomUUID();
|
|
914
|
+
db.query("INSERT INTO negotium_cron_requests (id,job_id,requested_at) VALUES (?,?,?)").run(
|
|
915
|
+
id,
|
|
916
|
+
jobId,
|
|
917
|
+
now.toISOString(),
|
|
918
|
+
);
|
|
919
|
+
return id;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
export function requestCronCancel(jobId: string, now = new Date()): string {
|
|
923
|
+
ensureCronSchema();
|
|
924
|
+
const id = randomUUID();
|
|
925
|
+
db.query("INSERT INTO negotium_cron_cancellations (id,job_id,requested_at) VALUES (?,?,?)").run(
|
|
926
|
+
id,
|
|
927
|
+
jobId,
|
|
928
|
+
now.toISOString(),
|
|
929
|
+
);
|
|
930
|
+
return id;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
export function claimCronCancellations(limit = 100): string[] {
|
|
934
|
+
ensureCronSchema();
|
|
935
|
+
const jobIds: string[] = [];
|
|
936
|
+
db.transaction(() => {
|
|
937
|
+
const rows = db
|
|
938
|
+
.query(
|
|
939
|
+
`SELECT id, job_id FROM negotium_cron_cancellations
|
|
940
|
+
ORDER BY requested_at LIMIT ?`,
|
|
941
|
+
)
|
|
942
|
+
.all(limit) as Array<{ id: string; job_id: string }>;
|
|
943
|
+
for (const row of rows) {
|
|
944
|
+
db.query("DELETE FROM negotium_cron_cancellations WHERE id = ?").run(row.id);
|
|
945
|
+
if (!jobIds.includes(row.job_id)) jobIds.push(row.job_id);
|
|
946
|
+
}
|
|
947
|
+
})();
|
|
948
|
+
return jobIds;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
function insertRun(
|
|
952
|
+
jobId: string,
|
|
953
|
+
topicId: string,
|
|
954
|
+
source: "schedule" | "manual",
|
|
955
|
+
scheduledAt: string,
|
|
956
|
+
): CronRunRecord {
|
|
957
|
+
const run: CronRunRecord = {
|
|
958
|
+
id: randomUUID(),
|
|
959
|
+
jobId,
|
|
960
|
+
source,
|
|
961
|
+
scheduledAt,
|
|
962
|
+
status: "pending",
|
|
963
|
+
};
|
|
964
|
+
db.query(
|
|
965
|
+
"INSERT INTO negotium_cron_runs (id,job_id,topic_id,source,scheduled_at,status) VALUES (?,?,?,?,?,?)",
|
|
966
|
+
).run(run.id, run.jobId, topicId, run.source, run.scheduledAt, run.status);
|
|
967
|
+
return run;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
export function claimCronRuns(
|
|
971
|
+
now = new Date(),
|
|
972
|
+
limit = 20,
|
|
973
|
+
): Array<{ job: CronJobRecord; run: CronRunRecord }> {
|
|
974
|
+
ensureCronSchema();
|
|
975
|
+
const claimed: Array<{ job: CronJobRecord; run: CronRunRecord }> = [];
|
|
976
|
+
db.transaction(() => {
|
|
977
|
+
const requests = db
|
|
978
|
+
.query(
|
|
979
|
+
`SELECT r.id AS request_id, r.requested_at, j.*
|
|
980
|
+
FROM negotium_cron_requests r JOIN negotium_cron_jobs j ON j.id = r.job_id
|
|
981
|
+
ORDER BY r.requested_at LIMIT ?`,
|
|
982
|
+
)
|
|
983
|
+
.all(limit) as Array<JobRow & { request_id: string; requested_at: string }>;
|
|
984
|
+
for (const row of requests) {
|
|
985
|
+
db.query("DELETE FROM negotium_cron_requests WHERE id = ?").run(row.request_id);
|
|
986
|
+
claimed.push({
|
|
987
|
+
job: toJob(row),
|
|
988
|
+
run: insertRun(row.id, row.topic_id, "manual", row.requested_at),
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
const remaining = Math.max(0, limit - claimed.length);
|
|
993
|
+
if (remaining === 0) return;
|
|
994
|
+
const due = db
|
|
995
|
+
.query(
|
|
996
|
+
`SELECT * FROM negotium_cron_jobs
|
|
997
|
+
WHERE enabled = 1 AND next_run_at <= ? ORDER BY next_run_at LIMIT ?`,
|
|
998
|
+
)
|
|
999
|
+
.all(now.toISOString(), remaining) as JobRow[];
|
|
1000
|
+
for (const row of due) {
|
|
1001
|
+
const job = toJob(row);
|
|
1002
|
+
const run = insertRun(job.id, job.topicId, "schedule", job.nextRunAt);
|
|
1003
|
+
const nextRunAt = computeNextCronRun(job.schedule, now, job.timezone).toISOString();
|
|
1004
|
+
db.query("UPDATE negotium_cron_jobs SET next_run_at = ? WHERE id = ?").run(nextRunAt, job.id);
|
|
1005
|
+
claimed.push({ job: { ...job, nextRunAt }, run });
|
|
1006
|
+
}
|
|
1007
|
+
})();
|
|
1008
|
+
return claimed;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
export function markCronRunStarted(runId: string, queryId: string, now = new Date()): void {
|
|
1012
|
+
ensureCronSchema();
|
|
1013
|
+
db.query(
|
|
1014
|
+
`UPDATE negotium_cron_runs
|
|
1015
|
+
SET status = 'running', query_id = ?, started_at = COALESCE(started_at, ?)
|
|
1016
|
+
WHERE id = ?`,
|
|
1017
|
+
).run(queryId, now.toISOString(), runId);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
export function finishCronRun(
|
|
1021
|
+
runId: string,
|
|
1022
|
+
result: {
|
|
1023
|
+
status: Exclude<CronRunStatus, "pending" | "running">;
|
|
1024
|
+
outputPreview?: string;
|
|
1025
|
+
error?: string;
|
|
1026
|
+
exitCode?: number;
|
|
1027
|
+
},
|
|
1028
|
+
now = new Date(),
|
|
1029
|
+
): number | null {
|
|
1030
|
+
ensureCronSchema();
|
|
1031
|
+
const row = db
|
|
1032
|
+
.query(
|
|
1033
|
+
`SELECT r.started_at, r.status, COALESCE(r.topic_id, j.topic_id) AS topic_id
|
|
1034
|
+
FROM negotium_cron_runs r
|
|
1035
|
+
JOIN negotium_cron_jobs j ON j.id = r.job_id
|
|
1036
|
+
WHERE r.id = ?`,
|
|
1037
|
+
)
|
|
1038
|
+
.get(runId) as
|
|
1039
|
+
| { started_at: string | null; status: CronRunStatus; topic_id: string }
|
|
1040
|
+
| undefined;
|
|
1041
|
+
if (!row || (row.status !== "pending" && row.status !== "running")) return null;
|
|
1042
|
+
const durationMs = row?.started_at
|
|
1043
|
+
? Math.max(0, now.getTime() - Date.parse(row.started_at))
|
|
1044
|
+
: null;
|
|
1045
|
+
let successfulRunsSinceRotation: number | null = null;
|
|
1046
|
+
db.transaction(() => {
|
|
1047
|
+
const exitCode =
|
|
1048
|
+
result.exitCode ??
|
|
1049
|
+
({ succeeded: 0, skipped: 0, failed: 1, aborted: 130, interrupted: 137 } as const)[
|
|
1050
|
+
result.status
|
|
1051
|
+
];
|
|
1052
|
+
const updated = db
|
|
1053
|
+
.query(
|
|
1054
|
+
`UPDATE negotium_cron_runs
|
|
1055
|
+
SET status = ?, finished_at = ?, duration_ms = ?, output_preview = ?, error = ?, exit_code = ?
|
|
1056
|
+
WHERE id = ? AND status IN ('pending','running')`,
|
|
1057
|
+
)
|
|
1058
|
+
.run(
|
|
1059
|
+
result.status,
|
|
1060
|
+
now.toISOString(),
|
|
1061
|
+
durationMs,
|
|
1062
|
+
result.outputPreview?.slice(0, 500) ?? null,
|
|
1063
|
+
result.error ?? null,
|
|
1064
|
+
exitCode,
|
|
1065
|
+
runId,
|
|
1066
|
+
);
|
|
1067
|
+
if (Number(updated.changes) > 0 && result.status === "succeeded") {
|
|
1068
|
+
successfulRunsSinceRotation = incrementCronTopicSuccess(row.topic_id, now);
|
|
1069
|
+
}
|
|
1070
|
+
})();
|
|
1071
|
+
return successfulRunsSinceRotation;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
export function listCronRuns(jobId: string, limit = 20): CronRunRecord[] {
|
|
1075
|
+
ensureCronSchema();
|
|
1076
|
+
return (
|
|
1077
|
+
db
|
|
1078
|
+
.query("SELECT * FROM negotium_cron_runs WHERE job_id = ? ORDER BY scheduled_at DESC LIMIT ?")
|
|
1079
|
+
.all(jobId, limit) as RunRow[]
|
|
1080
|
+
).map(toRun);
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
export function getLastCronRun(jobId: string): CronRunRecord | null {
|
|
1084
|
+
return listCronRuns(jobId, 1)[0] ?? null;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
export function countCronRuns(jobId: string): number {
|
|
1088
|
+
ensureCronSchema();
|
|
1089
|
+
const row = db
|
|
1090
|
+
.query("SELECT COUNT(*) AS count FROM negotium_cron_runs WHERE job_id = ?")
|
|
1091
|
+
.get(jobId) as { count: number } | undefined;
|
|
1092
|
+
return row?.count ?? 0;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
export function recoverPendingCronRuns(
|
|
1096
|
+
limit = 100,
|
|
1097
|
+
): Array<{ job: CronJobRecord; run: CronRunRecord }> {
|
|
1098
|
+
ensureCronSchema();
|
|
1099
|
+
const rows = db
|
|
1100
|
+
.query(
|
|
1101
|
+
`SELECT r.id AS run_id, r.job_id AS run_job_id, r.source AS run_source,
|
|
1102
|
+
r.scheduled_at AS run_scheduled_at, r.started_at AS run_started_at,
|
|
1103
|
+
r.finished_at AS run_finished_at, r.status AS run_status,
|
|
1104
|
+
r.query_id AS run_query_id, r.duration_ms AS run_duration_ms,
|
|
1105
|
+
r.output_preview AS run_output_preview, r.error AS run_error,
|
|
1106
|
+
r.exit_code AS run_exit_code,
|
|
1107
|
+
r.topic_id AS run_topic_id,
|
|
1108
|
+
j.*
|
|
1109
|
+
FROM negotium_cron_runs r
|
|
1110
|
+
JOIN negotium_cron_jobs j ON j.id = r.job_id
|
|
1111
|
+
WHERE r.status = 'pending'
|
|
1112
|
+
ORDER BY r.scheduled_at
|
|
1113
|
+
LIMIT ?`,
|
|
1114
|
+
)
|
|
1115
|
+
.all(limit) as Array<
|
|
1116
|
+
JobRow & {
|
|
1117
|
+
run_id: string;
|
|
1118
|
+
run_job_id: string;
|
|
1119
|
+
run_source: "schedule" | "manual";
|
|
1120
|
+
run_scheduled_at: string;
|
|
1121
|
+
run_started_at: string | null;
|
|
1122
|
+
run_finished_at: string | null;
|
|
1123
|
+
run_status: CronRunStatus;
|
|
1124
|
+
run_query_id: string | null;
|
|
1125
|
+
run_duration_ms: number | null;
|
|
1126
|
+
run_output_preview: string | null;
|
|
1127
|
+
run_error: string | null;
|
|
1128
|
+
run_exit_code: number | null;
|
|
1129
|
+
run_topic_id: string | null;
|
|
1130
|
+
}
|
|
1131
|
+
>;
|
|
1132
|
+
return rows.map((row) => ({
|
|
1133
|
+
job: toJob(row),
|
|
1134
|
+
run: toRun({
|
|
1135
|
+
id: row.run_id,
|
|
1136
|
+
job_id: row.run_job_id,
|
|
1137
|
+
source: row.run_source,
|
|
1138
|
+
scheduled_at: row.run_scheduled_at,
|
|
1139
|
+
started_at: row.run_started_at,
|
|
1140
|
+
finished_at: row.run_finished_at,
|
|
1141
|
+
status: row.run_status,
|
|
1142
|
+
query_id: row.run_query_id,
|
|
1143
|
+
duration_ms: row.run_duration_ms,
|
|
1144
|
+
output_preview: row.run_output_preview,
|
|
1145
|
+
error: row.run_error,
|
|
1146
|
+
exit_code: row.run_exit_code,
|
|
1147
|
+
topic_id: row.run_topic_id,
|
|
1148
|
+
}),
|
|
1149
|
+
}));
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
export function finalizeOrphanedCronRuns(now = new Date()): number {
|
|
1153
|
+
ensureCronSchema();
|
|
1154
|
+
const result = db
|
|
1155
|
+
.query(
|
|
1156
|
+
`UPDATE negotium_cron_runs
|
|
1157
|
+
SET status = 'interrupted', finished_at = ?, exit_code = 137,
|
|
1158
|
+
error = 'node restarted after dispatch; final outcome is unknown'
|
|
1159
|
+
WHERE status = 'running'`,
|
|
1160
|
+
)
|
|
1161
|
+
.run(now.toISOString());
|
|
1162
|
+
return Number(result.changes);
|
|
1163
|
+
}
|