@otium-team/negotium 0.0.0-stage → 0.23.11
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 +23903 -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 +2336 -0
- package/dist/browser-runtime.js.map +35 -0
- package/dist/canonical-mcp-bridge.js +14 -0
- package/dist/canonical-mcp-bridge.js.map +9 -0
- package/dist/chunk-0fenxbe6.js +173 -0
- package/dist/chunk-0fenxbe6.js.map +12 -0
- package/dist/chunk-2mx7fq49.js +6 -0
- package/dist/chunk-2mx7fq49.js.map +9 -0
- package/dist/chunk-ad7kxsaq.js +202 -0
- package/dist/chunk-ad7kxsaq.js.map +9 -0
- package/dist/chunk-da9ad838.js +3927 -0
- package/dist/chunk-da9ad838.js.map +47 -0
- package/dist/chunk-f7p0h1gs.js +54 -0
- package/dist/chunk-f7p0h1gs.js.map +10 -0
- package/dist/chunk-jtv8yzy1.js +206 -0
- package/dist/chunk-jtv8yzy1.js.map +10 -0
- package/dist/chunk-mxxwx5mg.js +375 -0
- package/dist/chunk-mxxwx5mg.js.map +13 -0
- package/dist/cron.js +1 -0
- package/dist/hosted-agent.js +5919 -0
- package/dist/hosted-agent.js.map +62 -0
- package/dist/main.js +60536 -0
- package/dist/main.js.map +338 -0
- package/dist/mcp-catalog.js +39 -0
- package/dist/mcp-catalog.js.map +10 -0
- package/dist/mcp-factories.js +26572 -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 +1073 -0
- package/dist/query-runtime.js.map +26 -0
- package/dist/registry.js +336 -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 +866 -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 +420 -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 +186 -0
- package/dist/runtime/src/application/vault-command.ts +170 -0
- package/dist/runtime/src/bus.ts +403 -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 +200 -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 +508 -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 +2284 -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 +848 -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-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 +805 -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 +582 -0
- package/dist/runtime/src/types.ts +395 -0
- package/dist/runtime/src/version.ts +1 -0
- package/dist/runtime/tsconfig.json +13 -0
- package/dist/runtime-gateway.js +453 -0
- package/dist/runtime-gateway.js.map +10 -0
- package/dist/runtime-helpers.js +1794 -0
- package/dist/runtime-helpers.js.map +40 -0
- package/dist/sqlite.js +68 -0
- package/dist/sqlite.js.map +10 -0
- package/dist/storage.js +5194 -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 +138 -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 +234 -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 +224 -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 +138 -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 +546 -0
- package/dist/types/packages/core/src/types.d.ts +384 -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 +850 -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,288 @@
|
|
|
1
|
+
import { listActiveMemoryArchiverSessions } from "#agents/archiver";
|
|
2
|
+
import { summarizeDisplayText } from "#agents/tool-format";
|
|
3
|
+
import { getTopic } from "#storage/api-topics";
|
|
4
|
+
import { listRecentRuntimeEventsForTopic } from "#storage/runtime-events";
|
|
5
|
+
import { listRuntimeTurnLeases } from "#storage/runtime-leases";
|
|
6
|
+
import type { BackgroundSessionDto } from "#types/api";
|
|
7
|
+
import { COMPLETED_BACKGROUND_SESSION_RETENTION_MS } from "./background-session-policy";
|
|
8
|
+
|
|
9
|
+
export type BackgroundSessionProvider = (
|
|
10
|
+
userId: string,
|
|
11
|
+
allUsers?: boolean,
|
|
12
|
+
) => BackgroundSessionDto[];
|
|
13
|
+
|
|
14
|
+
const providers = new Set<BackgroundSessionProvider>();
|
|
15
|
+
interface TransientBackgroundSessionRecord extends BackgroundSessionDto {
|
|
16
|
+
userId: string;
|
|
17
|
+
expiresAt?: number;
|
|
18
|
+
expiryTimer?: ReturnType<typeof setTimeout>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const transientSessions = new Map<string, TransientBackgroundSessionRecord>();
|
|
22
|
+
|
|
23
|
+
export interface TransientBackgroundSessionHandle {
|
|
24
|
+
id: string;
|
|
25
|
+
update(status: string, step?: string): void;
|
|
26
|
+
setOutput(output: string): void;
|
|
27
|
+
finish(status?: string, step?: string): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function beginTransientBackgroundSession(
|
|
31
|
+
userId: string,
|
|
32
|
+
session: Omit<BackgroundSessionDto, "id" | "startedAt" | "steps"> & {
|
|
33
|
+
id: string;
|
|
34
|
+
steps?: string[];
|
|
35
|
+
retentionMs?: number;
|
|
36
|
+
},
|
|
37
|
+
): TransientBackgroundSessionHandle {
|
|
38
|
+
const { retentionMs = COMPLETED_BACKGROUND_SESSION_RETENTION_MS, ...sessionFields } = session;
|
|
39
|
+
const record = {
|
|
40
|
+
...sessionFields,
|
|
41
|
+
userId,
|
|
42
|
+
startedAt: new Date().toISOString(),
|
|
43
|
+
active: true,
|
|
44
|
+
steps: [...(session.steps ?? [])],
|
|
45
|
+
};
|
|
46
|
+
const previous = transientSessions.get(record.id);
|
|
47
|
+
if (previous?.expiryTimer) clearTimeout(previous.expiryTimer);
|
|
48
|
+
transientSessions.set(record.id, record);
|
|
49
|
+
let finished = false;
|
|
50
|
+
return {
|
|
51
|
+
id: record.id,
|
|
52
|
+
update(status, step) {
|
|
53
|
+
if (finished) return;
|
|
54
|
+
const current = transientSessions.get(record.id);
|
|
55
|
+
if (current !== record) return;
|
|
56
|
+
const nextStatus = text(status, 160);
|
|
57
|
+
if (nextStatus) current.status = nextStatus;
|
|
58
|
+
const nextStep = text(step);
|
|
59
|
+
if (nextStep && current.steps.at(-1) !== nextStep) {
|
|
60
|
+
current.steps.push(nextStep);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
setOutput(output) {
|
|
64
|
+
if (finished) return;
|
|
65
|
+
const current = transientSessions.get(record.id);
|
|
66
|
+
if (current !== record) return;
|
|
67
|
+
current.output = output;
|
|
68
|
+
},
|
|
69
|
+
finish(status, step) {
|
|
70
|
+
if (finished) return;
|
|
71
|
+
this.update(status ?? record.status, step);
|
|
72
|
+
finished = true;
|
|
73
|
+
const current = transientSessions.get(record.id);
|
|
74
|
+
if (current !== record) return;
|
|
75
|
+
current.active = false;
|
|
76
|
+
current.expiresAt = Date.now() + Math.max(0, retentionMs);
|
|
77
|
+
current.expiryTimer = setTimeout(
|
|
78
|
+
() => {
|
|
79
|
+
if (transientSessions.get(record.id) === record) transientSessions.delete(record.id);
|
|
80
|
+
},
|
|
81
|
+
Math.max(0, retentionMs),
|
|
82
|
+
);
|
|
83
|
+
current.expiryTimer.unref?.();
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function registerBackgroundSessionProvider(provider: BackgroundSessionProvider): () => void {
|
|
89
|
+
providers.add(provider);
|
|
90
|
+
return () => providers.delete(provider);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function text(value: unknown, maxLen?: number): string {
|
|
94
|
+
if (typeof value !== "string") return "";
|
|
95
|
+
const normalized = value.replace(/\s+/g, " ").trim();
|
|
96
|
+
return maxLen === undefined ? normalized : normalized.slice(0, maxLen);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function byteSize(value: unknown): string {
|
|
100
|
+
const bytes = Buffer.byteLength(typeof value === "string" ? value : "");
|
|
101
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
102
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
103
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function compactPath(value: unknown): string {
|
|
107
|
+
if (typeof value !== "string" || !value.trim()) return "";
|
|
108
|
+
const normalized = value.trim().replaceAll("\\", "/");
|
|
109
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
110
|
+
return parts.length > 2 ? `…/${parts.slice(-2).join("/")}` : normalized;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function compactToolLabel(value: string): string {
|
|
114
|
+
const match = value.match(/^([^()]+)\((.*)\)$/);
|
|
115
|
+
return match ? `${match[1]} · ${match[2]}` : value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Build a richer single-line tool step, mirroring the detail level of
|
|
120
|
+
* `toolTimelineText` in the terminal adapter.
|
|
121
|
+
*/
|
|
122
|
+
function detailedToolStep(payload: Record<string, unknown>): string {
|
|
123
|
+
const name = String(payload.name ?? "tool");
|
|
124
|
+
const shortName = name.split("__").at(-1)?.toLowerCase() ?? name.toLowerCase();
|
|
125
|
+
const input =
|
|
126
|
+
payload.input && typeof payload.input === "object"
|
|
127
|
+
? (payload.input as Record<string, unknown>)
|
|
128
|
+
: {};
|
|
129
|
+
const path = compactPath(input.file_path ?? input.path ?? input.file_id);
|
|
130
|
+
const label = compactToolLabel(String(payload.label ?? name));
|
|
131
|
+
|
|
132
|
+
if (shortName === "ask_session" || shortName === "tell_session") {
|
|
133
|
+
const target = typeof input.to === "string" && input.to.trim() ? input.to.trim() : "session";
|
|
134
|
+
const msg = typeof input.message === "string" ? summarizeDisplayText(text(input.message)) : "";
|
|
135
|
+
const action = shortName === "ask_session" ? "Ask" : "Tell";
|
|
136
|
+
return `${action} ${target}${msg ? ` — ${msg}` : ""}`;
|
|
137
|
+
}
|
|
138
|
+
if (shortName === "edit" || shortName === "write" || shortName === "delete") {
|
|
139
|
+
const action = shortName === "edit" ? "Edit" : shortName === "write" ? "Write" : "Delete";
|
|
140
|
+
if (path) {
|
|
141
|
+
const before = typeof input.before === "string" ? input.before : "";
|
|
142
|
+
const after = typeof input.after === "string" ? input.after : "";
|
|
143
|
+
if (before || after) {
|
|
144
|
+
const added = after.split("\n").filter(Boolean).length;
|
|
145
|
+
const removed = before.split("\n").filter(Boolean).length;
|
|
146
|
+
return `${action} ${path} (+${added} -${removed})`;
|
|
147
|
+
}
|
|
148
|
+
return `${action} ${path}`;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (shortName === "read" || shortName === "view") {
|
|
152
|
+
const action = shortName === "read" ? "Read" : "View";
|
|
153
|
+
if (path) return `${action} ${path}`;
|
|
154
|
+
return `${action} file`;
|
|
155
|
+
}
|
|
156
|
+
if (shortName === "grep" || shortName === "glob") {
|
|
157
|
+
const pattern =
|
|
158
|
+
typeof input.pattern === "string" ? summarizeDisplayText(text(input.pattern)) : "";
|
|
159
|
+
const action = shortName === "grep" ? "Grep" : "Glob";
|
|
160
|
+
if (pattern) return `${action} '${pattern}'${path ? ` in ${path}` : ""}`;
|
|
161
|
+
if (path) return `${action} ${path}`;
|
|
162
|
+
}
|
|
163
|
+
if (shortName === "bash") {
|
|
164
|
+
const cmd = typeof input.command === "string" ? summarizeDisplayText(text(input.command)) : "";
|
|
165
|
+
return cmd ? `Bash: ${cmd}` : "Bash";
|
|
166
|
+
}
|
|
167
|
+
if (shortName === "webfetch" || shortName === "url_fetch") {
|
|
168
|
+
const url = typeof input.url === "string" ? summarizeDisplayText(text(input.url)) : "";
|
|
169
|
+
return url ? `Fetch ${url}` : "WebFetch";
|
|
170
|
+
}
|
|
171
|
+
return summarizeDisplayText(label);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function backgroundSessionProgress(
|
|
175
|
+
topicId: string,
|
|
176
|
+
queryId: string,
|
|
177
|
+
): { status: string; steps: string[] } {
|
|
178
|
+
let status = "Running";
|
|
179
|
+
const steps: string[] = [];
|
|
180
|
+
const toolNames = new Map<string, string>();
|
|
181
|
+
for (const event of listRecentRuntimeEventsForTopic(topicId, 2_000)) {
|
|
182
|
+
if (event.type !== "ai-status") continue;
|
|
183
|
+
const payload = event.payload as Record<string, unknown>;
|
|
184
|
+
if (payload.queryId !== queryId) continue;
|
|
185
|
+
const kind = payload.kind;
|
|
186
|
+
let step = "";
|
|
187
|
+
if (kind === "ai_active") step = "Scheduled turn started";
|
|
188
|
+
else if (kind === "reasoning") step = `Reasoning: ${text(payload.content)}`;
|
|
189
|
+
else if (kind === "tool_call") {
|
|
190
|
+
const toolUseId = text(payload.toolUseId);
|
|
191
|
+
const toolName = text(payload.name) || "Tool";
|
|
192
|
+
if (toolUseId) toolNames.set(toolUseId, toolName.split("__").at(-1) ?? toolName);
|
|
193
|
+
step = detailedToolStep(payload);
|
|
194
|
+
} else if (kind === "tool_status") step = text(payload.content);
|
|
195
|
+
else if (kind === "tool_output") {
|
|
196
|
+
const toolName = toolNames.get(text(payload.toolUseId)) ?? "Tool";
|
|
197
|
+
step = payload.isError
|
|
198
|
+
? `${toolName} failed: ${summarizeDisplayText(text(payload.content)) || "unknown error"}`
|
|
199
|
+
: `${toolName} result · ${byteSize(payload.content)}`;
|
|
200
|
+
} else if (kind === "ai_done") {
|
|
201
|
+
const usage =
|
|
202
|
+
payload.usage && typeof payload.usage === "object"
|
|
203
|
+
? (payload.usage as Record<string, unknown>)
|
|
204
|
+
: undefined;
|
|
205
|
+
const input = typeof usage?.input === "number" ? usage.input.toLocaleString() : "";
|
|
206
|
+
const output = typeof usage?.output === "number" ? usage.output.toLocaleString() : "";
|
|
207
|
+
step =
|
|
208
|
+
input || output
|
|
209
|
+
? `Scheduled turn completed · ${input || "0"} in / ${output || "0"} out`
|
|
210
|
+
: "Scheduled turn completed";
|
|
211
|
+
status = "Completed";
|
|
212
|
+
} else if (kind === "ai_error") {
|
|
213
|
+
step = `Scheduled turn failed: ${text(payload.error) || "unknown error"}`;
|
|
214
|
+
status = "Failed";
|
|
215
|
+
} else if (kind === "ai_aborted") {
|
|
216
|
+
const reason = text(payload.reason);
|
|
217
|
+
step = `Scheduled turn stopped${reason ? ` · ${reason}` : ""}`;
|
|
218
|
+
status = "Stopped";
|
|
219
|
+
}
|
|
220
|
+
if (step && steps.at(-1) !== step) steps.push(step);
|
|
221
|
+
if (step && kind !== "ai_done" && kind !== "ai_error" && kind !== "ai_aborted") status = step;
|
|
222
|
+
}
|
|
223
|
+
return { status, steps };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* `allUsers` widens every section — memory archiver, compact/transient, and
|
|
228
|
+
* cron — past the caller's own participation. Meant for single-owner
|
|
229
|
+
* surfaces (terminal, Telegram) where "my view" and "everything on this
|
|
230
|
+
* node" are the same person anyway, so idle background upkeep (and compact,
|
|
231
|
+
* manual or idle-triggered) triggered under a different local userId is
|
|
232
|
+
* still visible.
|
|
233
|
+
*/
|
|
234
|
+
export function listBackgroundSessionsForUser(
|
|
235
|
+
userId: string,
|
|
236
|
+
allUsers = false,
|
|
237
|
+
): BackgroundSessionDto[] {
|
|
238
|
+
const memory = listActiveMemoryArchiverSessions(userId, allUsers);
|
|
239
|
+
const now = Date.now();
|
|
240
|
+
for (const [id, session] of transientSessions) {
|
|
241
|
+
if (session.expiresAt !== undefined && session.expiresAt <= now) {
|
|
242
|
+
if (session.expiryTimer) clearTimeout(session.expiryTimer);
|
|
243
|
+
transientSessions.delete(id);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const transient = [...transientSessions.values()]
|
|
247
|
+
.filter((session) => allUsers || session.userId === userId)
|
|
248
|
+
.map(({ userId: _userId, expiresAt: _expiresAt, expiryTimer: _expiryTimer, ...session }) => ({
|
|
249
|
+
...session,
|
|
250
|
+
steps: [...session.steps],
|
|
251
|
+
}));
|
|
252
|
+
const provided = [...providers].flatMap((provider) => provider(userId, allUsers));
|
|
253
|
+
const providedCronTopicIds = new Set(
|
|
254
|
+
provided
|
|
255
|
+
.filter((session) => session.kind === "cron")
|
|
256
|
+
.map((session) => session.topicId)
|
|
257
|
+
.filter((topicId): topicId is string => Boolean(topicId)),
|
|
258
|
+
);
|
|
259
|
+
const cron = listRuntimeTurnLeases()
|
|
260
|
+
.filter((lease) => lease.origin.startsWith("cron:"))
|
|
261
|
+
.flatMap((lease): BackgroundSessionDto[] => {
|
|
262
|
+
const topic = getTopic(lease.topicId);
|
|
263
|
+
const isParticipant =
|
|
264
|
+
allUsers || topic?.participants.some((participant) => participant.userId === userId);
|
|
265
|
+
if (!topic || !isParticipant || providedCronTopicIds.has(lease.topicId)) {
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
const progress = backgroundSessionProgress(lease.topicId, lease.queryId);
|
|
269
|
+
return [
|
|
270
|
+
{
|
|
271
|
+
id: `cron:${lease.queryId}`,
|
|
272
|
+
kind: "cron",
|
|
273
|
+
title: topic.title,
|
|
274
|
+
topicId: topic.id,
|
|
275
|
+
startedAt: new Date(lease.startedAt).toISOString(),
|
|
276
|
+
status: lease.abortRequested ? "Stopping" : progress.status,
|
|
277
|
+
active: true,
|
|
278
|
+
agent: topic.agent,
|
|
279
|
+
model: topic.effectiveModel ?? topic.defaultModel,
|
|
280
|
+
effort: topic.effectiveEffort ?? topic.defaultEffort,
|
|
281
|
+
steps: progress.steps,
|
|
282
|
+
},
|
|
283
|
+
];
|
|
284
|
+
});
|
|
285
|
+
return [...memory, ...transient, ...provided, ...cron].sort((left, right) =>
|
|
286
|
+
left.startedAt.localeCompare(right.startedAt),
|
|
287
|
+
);
|
|
288
|
+
}
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bash-rs completion watcher — turns `result.json` files written by the Rust
|
|
3
|
+
* `bash-rs` background-bash daemon into session-inbox `tell`s. bash-rs is the
|
|
4
|
+
* only backend; the retired TypeScript server called `injectMessage()` itself.
|
|
5
|
+
*
|
|
6
|
+
* Why a watcher instead of bash-rs calling back into negotium: bash-rs is a
|
|
7
|
+
* standalone project (github.com/maestrojeong/bash-rs-mcp, sibling of
|
|
8
|
+
* browser-rs-mcp) that knows nothing about topics/session-inbox/turns — it
|
|
9
|
+
* only ever appends `{bash_id}/meta.json` (on spawn) and `{bash_id}/result.json`
|
|
10
|
+
* (on completion) under `BASHRS_SPILL_ROOT`, and never reads or deletes
|
|
11
|
+
* either. Any consumer, negotium or otherwise, watches that directory the
|
|
12
|
+
* same way `runtime/inbox.ts` already watches the session-inbox itself:
|
|
13
|
+
* `fs.watch` + a fallback poll, at-least-once, dedup by a stable id.
|
|
14
|
+
*
|
|
15
|
+
* `owner` on the wire is `${userId}\0${topicId}` — see
|
|
16
|
+
* `platform/mcp-config.ts::backgroundBashTransport` for what negotium sends
|
|
17
|
+
* as `X-Background-Bash-User`/`X-Background-Bash-Topic`, and bash-rs's own
|
|
18
|
+
* `security.rs::resolve_identity` for the matching NUL-join on the other end.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
closeSync,
|
|
23
|
+
existsSync,
|
|
24
|
+
openSync,
|
|
25
|
+
readdirSync,
|
|
26
|
+
readFileSync,
|
|
27
|
+
readSync,
|
|
28
|
+
renameSync,
|
|
29
|
+
rmSync,
|
|
30
|
+
statSync,
|
|
31
|
+
} from "node:fs";
|
|
32
|
+
import { join } from "node:path";
|
|
33
|
+
import { debouncedFlush, FALLBACK_INTERVAL_MS, watchDir } from "#outbox/utils";
|
|
34
|
+
import { BASHRS_SPILL_ROOT } from "#platform/config";
|
|
35
|
+
import { logger } from "#platform/logger";
|
|
36
|
+
import {
|
|
37
|
+
acquireRuntimeProcessLease,
|
|
38
|
+
PROCESS_LEASE_HEARTBEAT_MS,
|
|
39
|
+
type RuntimeProcessLeaseHandle,
|
|
40
|
+
} from "#storage/runtime-process-leases";
|
|
41
|
+
import { enqueueSessionInbox } from "#storage/session-inbox";
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Where a finished background job's turn is delivered.
|
|
45
|
+
*
|
|
46
|
+
* The default appends to negotium's own session inbox. An embedding host that
|
|
47
|
+
* owns a different inbox installs its own sink here — otium, for one, resolves
|
|
48
|
+
* `RUN_DIR` to its own state dir and runs its own inbox worker, so writing to
|
|
49
|
+
* negotium's path would drop the turn on the floor rather than deliver it.
|
|
50
|
+
* Mirrors the `setFileHooks()` seam in `runtime/file-hooks.ts`.
|
|
51
|
+
*
|
|
52
|
+
* A sink must throw to signal failure: the caller leaves `result.json` in
|
|
53
|
+
* place so the next sweep retries, which is what keeps delivery at-least-once.
|
|
54
|
+
*/
|
|
55
|
+
export interface BashrsCompletion {
|
|
56
|
+
userId: string;
|
|
57
|
+
topicId: string;
|
|
58
|
+
/** Stable per-job id; use it to collapse a retried delivery into one turn. */
|
|
59
|
+
bashId: string;
|
|
60
|
+
message: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type BashrsCompletionSink = (completion: BashrsCompletion) => void;
|
|
64
|
+
|
|
65
|
+
const defaultSink: BashrsCompletionSink = ({ userId, topicId, bashId, message }) => {
|
|
66
|
+
enqueueSessionInbox({
|
|
67
|
+
userId,
|
|
68
|
+
topicId,
|
|
69
|
+
id: `bashrs:${bashId}`,
|
|
70
|
+
entry: {
|
|
71
|
+
type: "tell",
|
|
72
|
+
from: "__bg_bash__",
|
|
73
|
+
message,
|
|
74
|
+
depth: 0,
|
|
75
|
+
requestId: bashId,
|
|
76
|
+
timestamp: new Date().toISOString(),
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
let completionSink: BashrsCompletionSink = defaultSink;
|
|
82
|
+
|
|
83
|
+
/** Install a host's delivery sink, or pass null to restore the default. */
|
|
84
|
+
export function setBashrsCompletionSink(sink: BashrsCompletionSink | null): void {
|
|
85
|
+
completionSink = sink ?? defaultSink;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const PROCESS_ROLE = "worker:bashrs-completions";
|
|
89
|
+
/** How long a delivered job's spill dir (logs + result.json.injected) is kept
|
|
90
|
+
* around before being swept — mirrors the TS server's own
|
|
91
|
+
* `COMPLETED_RETENTION_MS`, for the same reason: a reader might still be
|
|
92
|
+
* mid-way through fetching a spill path out of the injected message. */
|
|
93
|
+
const RETENTION_MS = 15 * 60_000;
|
|
94
|
+
/** Read at most this many trailing bytes of stdout.log/stderr.log into the
|
|
95
|
+
* injected message — full output stays on disk at the path we report. */
|
|
96
|
+
const TAIL_BYTES = 8 * 1024;
|
|
97
|
+
|
|
98
|
+
interface JobResult {
|
|
99
|
+
bash_id: string;
|
|
100
|
+
owner: string;
|
|
101
|
+
exit_code: number | null;
|
|
102
|
+
finished_at_ms: number;
|
|
103
|
+
matched_line: string | null;
|
|
104
|
+
/**
|
|
105
|
+
* Which arm ended a watch. Absent for a plain background run, and absent
|
|
106
|
+
* from results written by bash-rs before v0.1.4 — those fall back to the
|
|
107
|
+
* generic completion notice.
|
|
108
|
+
*/
|
|
109
|
+
watch_outcome?: "matched" | "timeout" | "exited";
|
|
110
|
+
unknown: boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isJobResult(value: unknown): value is JobResult {
|
|
114
|
+
if (!value || typeof value !== "object") return false;
|
|
115
|
+
const v = value as Record<string, unknown>;
|
|
116
|
+
return typeof v.bash_id === "string" && typeof v.owner === "string";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function readTail(filePath: string): { text: string; truncated: boolean } {
|
|
120
|
+
try {
|
|
121
|
+
const { size } = statSync(filePath);
|
|
122
|
+
const fd = openSync(filePath, "r");
|
|
123
|
+
try {
|
|
124
|
+
const start = Math.max(0, size - TAIL_BYTES);
|
|
125
|
+
const buf = Buffer.alloc(size - start);
|
|
126
|
+
readSync(fd, buf, 0, buf.length, start);
|
|
127
|
+
return { text: buf.toString("utf8"), truncated: start > 0 };
|
|
128
|
+
} finally {
|
|
129
|
+
closeSync(fd);
|
|
130
|
+
}
|
|
131
|
+
} catch {
|
|
132
|
+
return { text: "", truncated: false };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function buildMessage(dir: string, result: JobResult): string {
|
|
137
|
+
const stdout = readTail(join(dir, "stdout.log"));
|
|
138
|
+
const stderr = readTail(join(dir, "stderr.log"));
|
|
139
|
+
const parts: string[] = [];
|
|
140
|
+
if (stdout.text.trim() || stdout.truncated) {
|
|
141
|
+
parts.push(
|
|
142
|
+
`stdout${stdout.truncated ? ` (truncated, full output: ${join(dir, "stdout.log")})` : ""}:\n${stdout.text.trim()}`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
if (stderr.text.trim() || stderr.truncated) {
|
|
146
|
+
parts.push(
|
|
147
|
+
`stderr${stderr.truncated ? ` (truncated, full output: ${join(dir, "stderr.log")})` : ""}:\n${stderr.text.trim()}`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
const header = watchHeader(result) ?? `[background_bash ${result.bash_id} finished]`;
|
|
151
|
+
const exitLine = result.unknown
|
|
152
|
+
? "exit code: unknown (bash-rs restarted while this job was running)"
|
|
153
|
+
: `exit code: ${result.exit_code ?? "unknown"}`;
|
|
154
|
+
return `${header}\n${exitLine}\n${parts.join("\n") || "(no output)"}`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* A watch promises exactly one turn, and which outcome produced it is the
|
|
159
|
+
* whole point of the notice: "timed out" and "exited before matching" mean
|
|
160
|
+
* opposite things about whether the condition is still coming.
|
|
161
|
+
*/
|
|
162
|
+
function watchHeader(result: JobResult): string | null {
|
|
163
|
+
if (result.matched_line) {
|
|
164
|
+
return `[background_bash_watch ${result.bash_id} matched]\nmatched line: ${result.matched_line.slice(0, 500)}`;
|
|
165
|
+
}
|
|
166
|
+
if (result.watch_outcome === "timeout") {
|
|
167
|
+
return `[background_bash_watch ${result.bash_id} timed out without a match]`;
|
|
168
|
+
}
|
|
169
|
+
if (result.watch_outcome === "exited") {
|
|
170
|
+
return `[background_bash_watch ${result.bash_id} exited before matching]`;
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function parseOwner(owner: string): { userId: string; topicId: string } | null {
|
|
176
|
+
const nul = owner.indexOf("\0");
|
|
177
|
+
if (nul < 0) return null;
|
|
178
|
+
const userId = owner.slice(0, nul);
|
|
179
|
+
const topicId = owner.slice(nul + 1);
|
|
180
|
+
if (!userId || !topicId) return null;
|
|
181
|
+
return { userId, topicId };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function injectedMarker(dir: string): string {
|
|
185
|
+
return join(dir, "result.json.injected");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** One sweep: deliver any new `result.json`, then reap old delivered jobs. */
|
|
189
|
+
export async function flushBashrsCompletions(): Promise<void> {
|
|
190
|
+
let entries: string[];
|
|
191
|
+
try {
|
|
192
|
+
entries = readdirSync(BASHRS_SPILL_ROOT);
|
|
193
|
+
} catch {
|
|
194
|
+
return; // Nothing spawned yet.
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const now = Date.now();
|
|
198
|
+
for (const bashId of entries) {
|
|
199
|
+
const dir = join(BASHRS_SPILL_ROOT, bashId);
|
|
200
|
+
const resultPath = join(dir, "result.json");
|
|
201
|
+
const marker = injectedMarker(dir);
|
|
202
|
+
|
|
203
|
+
// Already delivered: sweep once the retention window has passed.
|
|
204
|
+
try {
|
|
205
|
+
const markerStat = statSync(marker);
|
|
206
|
+
if (now - markerStat.mtimeMs > RETENTION_MS) {
|
|
207
|
+
rmSync(dir, { recursive: true, force: true });
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
} catch {
|
|
211
|
+
// No marker yet — fall through to try delivering it.
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let raw: string;
|
|
215
|
+
try {
|
|
216
|
+
raw = readFileSync(resultPath, "utf8");
|
|
217
|
+
} catch {
|
|
218
|
+
continue; // Job still running, or not a bash-rs dir at all.
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
let result: unknown;
|
|
222
|
+
try {
|
|
223
|
+
result = JSON.parse(raw);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
logger.warn({ err, dir }, "bashrs-completions: corrupt result.json, skipping");
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (!isJobResult(result)) {
|
|
229
|
+
logger.warn({ dir }, "bashrs-completions: result.json missing bash_id/owner, skipping");
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const parsed = parseOwner(result.owner);
|
|
234
|
+
if (!parsed) {
|
|
235
|
+
// Not negotium's convention (e.g. clawgram's bare-topic owner, or a
|
|
236
|
+
// standalone curl user) — nothing for this watcher to do with it.
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Another consumer of the same spill dir (a node and an embedding host can
|
|
241
|
+
// both hold leadership, each under its own lease store) may have finished
|
|
242
|
+
// this job between our read and now. Nothing left to deliver.
|
|
243
|
+
if (!existsSync(resultPath)) continue;
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
// The stable bash_id is carried through so at-least-once delivery on
|
|
247
|
+
// either side — this watcher crashing before the rename, or the sink's
|
|
248
|
+
// own retry semantics — collapses to one turn rather than several.
|
|
249
|
+
completionSink({
|
|
250
|
+
userId: parsed.userId,
|
|
251
|
+
topicId: parsed.topicId,
|
|
252
|
+
bashId: result.bash_id,
|
|
253
|
+
message: buildMessage(dir, result),
|
|
254
|
+
});
|
|
255
|
+
try {
|
|
256
|
+
renameSync(resultPath, marker);
|
|
257
|
+
} catch (renameErr) {
|
|
258
|
+
// ENOENT here means a concurrent consumer already marked the job
|
|
259
|
+
// delivered (the sink collapses the duplicate by bash_id). That is the
|
|
260
|
+
// success state, not a failure to retry.
|
|
261
|
+
if ((renameErr as NodeJS.ErrnoException).code !== "ENOENT") throw renameErr;
|
|
262
|
+
logger.debug(
|
|
263
|
+
{ bashId: result.bash_id },
|
|
264
|
+
"bashrs-completions: already delivered by another consumer",
|
|
265
|
+
);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
logger.info(
|
|
269
|
+
{ bashId: result.bash_id, userId: parsed.userId, topicId: parsed.topicId },
|
|
270
|
+
"bashrs-completions: delivered",
|
|
271
|
+
);
|
|
272
|
+
} catch (err) {
|
|
273
|
+
// Leave result.json in place — the next sweep retries. At-least-once,
|
|
274
|
+
// same as every other outbox in this codebase.
|
|
275
|
+
logger.warn({ err, dir }, "bashrs-completions: delivery failed, will retry");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
let watcher: ReturnType<typeof watchDir> | null = null;
|
|
281
|
+
let fallbackTimer: ReturnType<typeof setInterval> | null = null;
|
|
282
|
+
let debouncedTrigger: (() => void) | null = null;
|
|
283
|
+
let leadershipTimer: ReturnType<typeof setInterval> | null = null;
|
|
284
|
+
let workerLease: RuntimeProcessLeaseHandle | null = null;
|
|
285
|
+
let workerStarted = false;
|
|
286
|
+
|
|
287
|
+
function stopLeaderResources(): void {
|
|
288
|
+
if (watcher) {
|
|
289
|
+
watcher.close();
|
|
290
|
+
watcher = null;
|
|
291
|
+
}
|
|
292
|
+
if (fallbackTimer) {
|
|
293
|
+
clearInterval(fallbackTimer);
|
|
294
|
+
fallbackTimer = null;
|
|
295
|
+
}
|
|
296
|
+
debouncedTrigger = null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function tryBecomeLeader(): void {
|
|
300
|
+
if (!workerStarted || workerLease) return;
|
|
301
|
+
const acquired = acquireRuntimeProcessLease(PROCESS_ROLE, {
|
|
302
|
+
onLost: () => {
|
|
303
|
+
if (workerLease !== acquired) return;
|
|
304
|
+
workerLease = null;
|
|
305
|
+
stopLeaderResources();
|
|
306
|
+
logger.warn("bashrs-completions: worker leadership lost");
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
if (!acquired) return;
|
|
310
|
+
|
|
311
|
+
workerLease = acquired;
|
|
312
|
+
debouncedTrigger = debouncedFlush(flushBashrsCompletions, "bashrs-completions", 200);
|
|
313
|
+
watcher = watchDir(BASHRS_SPILL_ROOT, () => debouncedTrigger?.());
|
|
314
|
+
fallbackTimer = setInterval(() => debouncedTrigger?.(), FALLBACK_INTERVAL_MS);
|
|
315
|
+
fallbackTimer.unref?.();
|
|
316
|
+
|
|
317
|
+
logger.info(
|
|
318
|
+
{ dir: BASHRS_SPILL_ROOT, role: PROCESS_ROLE },
|
|
319
|
+
"bashrs-completions: worker leadership acquired",
|
|
320
|
+
);
|
|
321
|
+
void flushBashrsCompletions();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Join the bashrs-completions worker election — same one-leader-per-role
|
|
326
|
+
* lease as `startSessionInboxWorker`, so exactly one runtime process watches
|
|
327
|
+
* `BASHRS_SPILL_ROOT` regardless of how many are up. Returns a cleanup
|
|
328
|
+
* function for graceful shutdown.
|
|
329
|
+
*/
|
|
330
|
+
export function startBashrsCompletionsWorker(): () => void {
|
|
331
|
+
if (workerStarted) return () => {};
|
|
332
|
+
workerStarted = true;
|
|
333
|
+
|
|
334
|
+
tryBecomeLeader();
|
|
335
|
+
leadershipTimer = setInterval(tryBecomeLeader, PROCESS_LEASE_HEARTBEAT_MS);
|
|
336
|
+
leadershipTimer.unref?.();
|
|
337
|
+
|
|
338
|
+
return () => {
|
|
339
|
+
if (!workerStarted) return;
|
|
340
|
+
workerStarted = false;
|
|
341
|
+
if (leadershipTimer) {
|
|
342
|
+
clearInterval(leadershipTimer);
|
|
343
|
+
leadershipTimer = null;
|
|
344
|
+
}
|
|
345
|
+
stopLeaderResources();
|
|
346
|
+
workerLease?.stop();
|
|
347
|
+
workerLease = null;
|
|
348
|
+
logger.info("bashrs-completions: worker stopped");
|
|
349
|
+
};
|
|
350
|
+
}
|