@octopi-agent/engine 0.70.7 → 0.86.38
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 +201 -201
- package/NOTICE +7 -7
- package/README.md +64 -26
- package/dist/config-schema/agent.d.ts +89 -1
- package/dist/config-schema/agent.js +69 -0
- package/dist/config-schema/context.d.ts +23 -4
- package/dist/config-schema/context.js +28 -1
- package/dist/config-schema/engine.d.ts +201 -9
- package/dist/config-schema/engine.js +31 -2
- package/dist/config-schema/governance.d.ts +95 -0
- package/dist/config-schema/governance.js +91 -0
- package/dist/config-schema/models.d.ts +1 -1
- package/dist/config-schema/observability.d.ts +6 -0
- package/dist/config-schema/observability.js +16 -0
- package/dist/config-schema/run.d.ts +10 -0
- package/dist/config-schema/run.js +18 -0
- package/dist/config-schema/shared.d.ts +10 -0
- package/dist/config-schema/shared.js +10 -0
- package/dist/config-schema/substrate.d.ts +1 -1
- package/dist/config-schema/subsystems.d.ts +1 -1
- package/dist/config-schema/subsystems.js +2 -1
- package/dist/config.d.ts +178 -7
- package/dist/config.js +113 -3
- package/dist/harness/activation/compiler.js +3 -0
- package/dist/harness/activation/runtime.d.ts +7 -0
- package/dist/harness/activation/runtime.js +60 -2
- package/dist/harness/activation/sources/agent-signal.d.ts +11 -0
- package/dist/harness/activation/sources/agent-signal.js +33 -0
- package/dist/harness/activation/types.d.ts +6 -1
- package/dist/harness/agent/builder.d.ts +47 -3
- package/dist/harness/agent/builder.js +180 -54
- package/dist/harness/agent/config-bridge.d.ts +33 -25
- package/dist/harness/agent/config-bridge.js +81 -291
- package/dist/harness/agent/index.d.ts +3 -1
- package/dist/harness/agent/index.js +2 -1
- package/dist/harness/agent/tool-purpose.d.ts +38 -0
- package/dist/harness/agent/tool-purpose.js +118 -0
- package/dist/harness/agent/wire-agent-builder.d.ts +133 -0
- package/dist/harness/agent/wire-agent-builder.js +363 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +4 -1
- package/dist/harness/collaboration/autonomous-subsystem/runtime.js +14 -8
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.d.ts +12 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.js +19 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/session-lifecycle-bridge.d.ts +6 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/session-lifecycle-bridge.js +24 -7
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.d.ts +5 -2
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +11 -3
- package/dist/harness/collaboration/autonomous-subsystem/think/index.d.ts +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/think/index.js +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/think/llm-port.d.ts +7 -4
- package/dist/harness/collaboration/autonomous-subsystem/think/llm-port.js +8 -9
- package/dist/harness/collaboration/autonomous-subsystem/think/model-resolver.d.ts +36 -10
- package/dist/harness/collaboration/autonomous-subsystem/think/model-resolver.js +60 -7
- package/dist/harness/collaboration/delegation/child-run.d.ts +144 -0
- package/dist/harness/collaboration/delegation/child-run.js +241 -0
- package/dist/harness/collaboration/delegation/index.d.ts +26 -0
- package/dist/harness/collaboration/delegation/index.js +25 -0
- package/dist/harness/collaboration/delegation/notifier.d.ts +46 -0
- package/dist/harness/collaboration/delegation/notifier.js +84 -0
- package/dist/harness/collaboration/delegation/registry-types.d.ts +200 -0
- package/dist/harness/collaboration/delegation/registry-types.js +61 -0
- package/dist/harness/collaboration/delegation/registry.d.ts +110 -0
- package/dist/harness/collaboration/delegation/registry.js +220 -0
- package/dist/harness/collaboration/delegation/service.d.ts +249 -0
- package/dist/harness/collaboration/delegation/service.js +499 -0
- package/dist/harness/collaboration/delegation/store.d.ts +155 -0
- package/dist/harness/collaboration/delegation/store.js +271 -0
- package/dist/harness/collaboration/delegation/transport.d.ts +73 -0
- package/dist/harness/collaboration/delegation/transport.js +61 -0
- package/dist/harness/collaboration/delegation/types.d.ts +231 -0
- package/dist/harness/collaboration/delegation/types.js +70 -0
- package/dist/harness/collaboration/delegation/wiring.d.ts +111 -0
- package/dist/harness/collaboration/delegation/wiring.js +145 -0
- package/dist/harness/collaboration/orchestration/index.d.ts +11 -13
- package/dist/harness/collaboration/orchestration/index.js +11 -11
- package/dist/harness/context/assembler.d.ts +2 -2
- package/dist/harness/context/assembler.js +12 -7
- package/dist/harness/context/capabilities/compact/engine.js +80 -38
- package/dist/harness/context/capabilities/compact/types.d.ts +12 -1
- package/dist/harness/context/capabilities/summary/registry.js +10 -0
- package/dist/harness/context/constitution/default-agents.md +230 -228
- package/dist/harness/context/default-context-engine.d.ts +101 -23
- package/dist/harness/context/default-context-engine.js +483 -172
- package/dist/harness/context/hybrid-compressor.d.ts +3 -3
- package/dist/harness/context/hybrid-compressor.js +18 -23
- package/dist/harness/context/index.d.ts +5 -1
- package/dist/harness/context/index.js +2 -0
- package/dist/harness/context/layer-snapshot.d.ts +4 -0
- package/dist/harness/context/layer-snapshot.js +30 -18
- package/dist/harness/context/layer-types.d.ts +49 -1
- package/dist/harness/context/layer-types.js +72 -4
- package/dist/harness/context/layers.d.ts +5 -2
- package/dist/harness/context/layers.js +11 -5
- package/dist/harness/context/llm-summarizer.d.ts +15 -1
- package/dist/harness/context/llm-summarizer.js +107 -56
- package/dist/harness/context/message-selector.js +6 -2
- package/dist/harness/context/root-instructions.d.ts +72 -0
- package/dist/harness/context/root-instructions.js +227 -0
- package/dist/harness/context/runtime-datetime.d.ts +34 -7
- package/dist/harness/context/runtime-datetime.js +66 -10
- package/dist/harness/context/smart-router.d.ts +1 -1
- package/dist/harness/context/smart-router.js +1 -1
- package/dist/harness/context/system-prompt-assembler.d.ts +30 -0
- package/dist/harness/context/system-prompt-assembler.js +67 -6
- package/dist/harness/context/token-constants.d.ts +2 -2
- package/dist/harness/context/token-constants.js +2 -2
- package/dist/harness/context/token-estimate-fns.d.ts +1 -3
- package/dist/harness/context/token-estimate-fns.js +2 -4
- package/dist/harness/context/token-estimator.d.ts +1 -1
- package/dist/harness/context/token-estimator.js +14 -6
- package/dist/harness/context/tool-result-aging.d.ts +140 -0
- package/dist/harness/context/tool-result-aging.js +331 -0
- package/dist/harness/context/tool-result-body.d.ts +10 -0
- package/dist/harness/context/tool-result-body.js +27 -0
- package/dist/harness/context/turn-envelope.d.ts +123 -0
- package/dist/harness/context/turn-envelope.js +188 -0
- package/dist/harness/context/types.d.ts +35 -47
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +248 -0
- package/dist/harness/extension/execution-environment/effect-policy.js +334 -0
- package/dist/harness/extension/execution-environment/index.d.ts +18 -2
- package/dist/harness/extension/execution-environment/index.js +11 -2
- package/dist/harness/extension/execution-environment/memory-undo.d.ts +101 -0
- package/dist/harness/extension/execution-environment/memory-undo.js +192 -0
- package/dist/harness/extension/execution-environment/process-isolation.d.ts +84 -0
- package/dist/harness/extension/execution-environment/process-isolation.js +315 -0
- package/dist/harness/extension/execution-environment/resource-limits.d.ts +36 -0
- package/dist/harness/extension/execution-environment/resource-limits.js +84 -0
- package/dist/harness/extension/execution-environment/sandbox.d.ts +125 -4
- package/dist/harness/extension/execution-environment/sandbox.js +493 -51
- package/dist/harness/extension/execution-environment/shell-journal.d.ts +100 -0
- package/dist/harness/extension/execution-environment/shell-journal.js +299 -0
- package/dist/harness/extension/execution-environment/tool-capabilities.d.ts +45 -0
- package/dist/harness/extension/execution-environment/tool-capabilities.js +126 -0
- package/dist/harness/extension/execution-environment/types.d.ts +67 -39
- package/dist/harness/extension/execution-environment/types.js +2 -2
- package/dist/harness/extension/execution-environment/undo-log.d.ts +203 -0
- package/dist/harness/extension/execution-environment/undo-log.js +627 -0
- package/dist/harness/extension/execution-environment/undo-merge.d.ts +40 -0
- package/dist/harness/extension/execution-environment/undo-merge.js +167 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/index.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.d.ts +9 -0
- package/dist/harness/extension/plugin-ecosystem/mcp/bridge.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/api.d.ts +1 -6
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +3 -8
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.d.ts +0 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.js +0 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.d.ts +2 -10
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.js +2 -9
- package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +1 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/index.js +1 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +2 -4
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +4 -21
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +3 -9
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +4 -8
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +1 -4
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +1 -3
- package/dist/harness/extension/plugin-ecosystem/skills/manager.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/skills/manager.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.d.ts +45 -0
- package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.js +136 -0
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +18 -3
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +277 -81
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.d.ts +31 -0
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +291 -0
- package/dist/harness/extension/plugin-ecosystem/tools/document-tools.js +10 -2
- package/dist/harness/extension/plugin-ecosystem/tools/env-info.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/tools/eol.d.ts +35 -0
- package/dist/harness/extension/plugin-ecosystem/tools/eol.js +43 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +40 -9
- package/dist/harness/extension/plugin-ecosystem/tools/file-ops.d.ts +33 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-ops.js +376 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.d.ts +12 -2
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +239 -61
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.d.ts +27 -0
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +554 -0
- package/dist/harness/extension/plugin-ecosystem/tools/http.d.ts +10 -6
- package/dist/harness/extension/plugin-ecosystem/tools/http.js +41 -47
- package/dist/harness/extension/plugin-ecosystem/tools/index.d.ts +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/index.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +4 -0
- package/dist/harness/extension/plugin-ecosystem/tools/memory.js +25 -1
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.d.ts +13 -0
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +391 -0
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +51 -2
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +151 -12
- package/dist/harness/extension/plugin-ecosystem/tools/session-history.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.d.ts +10 -1
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +35 -19
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +8 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +5 -0
- package/dist/harness/extension/plugin-ecosystem/tools/web-search.js +1 -0
- package/dist/harness/governance/credentials/index.d.ts +1 -1
- package/dist/harness/governance/credentials/store.d.ts +16 -4
- package/dist/harness/governance/credentials/store.js +33 -6
- package/dist/harness/governance/credentials/types.d.ts +11 -0
- package/dist/harness/governance/human-in-the-loop/decision-cache.d.ts +105 -0
- package/dist/harness/governance/human-in-the-loop/decision-cache.js +184 -0
- package/dist/harness/governance/human-in-the-loop/index.d.ts +5 -5
- package/dist/harness/governance/human-in-the-loop/index.js +3 -3
- package/dist/harness/governance/human-in-the-loop/types.d.ts +7 -62
- package/dist/harness/governance/human-in-the-loop/types.js +6 -2
- package/dist/harness/governance/security/capability-enforcer.d.ts +9 -1
- package/dist/harness/governance/security/capability-enforcer.js +31 -8
- package/dist/harness/governance/security/degradation.js +5 -7
- package/dist/harness/governance/security/risk-evaluator.d.ts +1 -1
- package/dist/harness/governance/security/risk-evaluator.js +4 -82
- package/dist/harness/governance/security/temp-path.d.ts +41 -0
- package/dist/harness/governance/security/temp-path.js +117 -0
- package/dist/harness/governance/session-acl/index.d.ts +1 -0
- package/dist/harness/governance/session-acl/index.js +1 -0
- package/dist/harness/governance/session-acl/rights.d.ts +12 -0
- package/dist/harness/governance/session-acl/rights.js +89 -7
- package/dist/harness/governance/session-acl/seed-roles.js +9 -1
- package/dist/harness/governance/session-acl/service.js +21 -29
- package/dist/harness/governance/session-acl/tool-rights.d.ts +81 -0
- package/dist/harness/governance/session-acl/tool-rights.js +238 -0
- package/dist/harness/governance/session-acl/types.d.ts +22 -0
- package/dist/harness/index.d.ts +14 -12
- package/dist/harness/index.js +9 -6
- package/dist/harness/knowledge/file-index-store.js +11 -11
- package/dist/harness/knowledge/grounding.d.ts +2 -1
- package/dist/harness/knowledge/grounding.js +6 -3
- package/dist/harness/knowledge/index.d.ts +2 -2
- package/dist/harness/knowledge/index.js +1 -1
- package/dist/harness/knowledge/ingest.js +4 -1
- package/dist/harness/knowledge/job-control-state.js +4 -4
- package/dist/harness/knowledge/job-control.js +11 -11
- package/dist/harness/knowledge/membership-store.js +11 -11
- package/dist/harness/knowledge/network-guard.d.ts +61 -4
- package/dist/harness/knowledge/network-guard.js +390 -113
- package/dist/harness/knowledge/query-service.js +3 -3
- package/dist/harness/knowledge/writer-service.d.ts +2 -0
- package/dist/harness/knowledge/writer-service.js +14 -13
- package/dist/harness/memory/backfill-trigger.d.ts +2 -2
- package/dist/harness/memory/backfill-trigger.js +4 -4
- package/dist/harness/memory/index.d.ts +1 -0
- package/dist/harness/memory/index.js +1 -0
- package/dist/harness/memory/sqlite/agent-db.js +2 -0
- package/dist/harness/memory/sqlite/memory-store.d.ts +1 -0
- package/dist/harness/memory/sqlite/memory-store.js +14 -4
- package/dist/harness/memory/sqlite/wisdom-store.js +9 -9
- package/dist/harness/memory/store.d.ts +1 -0
- package/dist/harness/memory/store.js +3 -0
- package/dist/harness/memory/time-format.d.ts +28 -0
- package/dist/harness/memory/time-format.js +52 -0
- package/dist/harness/memory/types.d.ts +12 -0
- package/dist/harness/observability/observer/hub.js +23 -1
- package/dist/harness/observability/observer/index.d.ts +2 -2
- package/dist/harness/observability/observer/index.js +1 -1
- package/dist/harness/observability/observer/types.d.ts +57 -0
- package/dist/harness/observability/observer/types.js +118 -9
- package/dist/harness/run/agent/agent.d.ts +18 -0
- package/dist/harness/run/agent/agent.js +26 -0
- package/dist/harness/run/concurrency/tool-loop-detection-core.d.ts +1 -1
- package/dist/harness/run/concurrency/tool-loop-detection-core.js +1 -1
- package/dist/harness/run/reliability/harness-events.d.ts +4 -0
- package/dist/harness/run/reliability/run-agent.js +110 -18
- package/dist/harness/run/reliability/run-metrics-collector.d.ts +28 -2
- package/dist/harness/run/reliability/run-metrics-collector.js +92 -2
- package/dist/harness/run/run-guard/default-run-guard.d.ts +0 -5
- package/dist/harness/run/run-guard/default-run-guard.js +104 -44
- package/dist/harness/run/run-scope.d.ts +52 -0
- package/dist/harness/run/runner.d.ts +101 -16
- package/dist/harness/run/runner.js +556 -158
- package/dist/harness/session/attachments/llm-intent.js +17 -17
- package/dist/harness/session/history/port.js +1 -0
- package/dist/harness/session/in-memory-store.d.ts +1 -0
- package/dist/harness/session/in-memory-store.js +22 -1
- package/dist/harness/session/schedules/index.d.ts +10 -0
- package/dist/harness/session/schedules/index.js +10 -0
- package/dist/harness/session/schedules/render.d.ts +13 -0
- package/dist/harness/session/schedules/render.js +63 -0
- package/dist/harness/session/schedules/service.d.ts +179 -0
- package/dist/harness/session/schedules/service.js +695 -0
- package/dist/harness/session/schedules/store.d.ts +119 -0
- package/dist/harness/session/schedules/store.js +323 -0
- package/dist/harness/session/schedules/tools.d.ts +39 -0
- package/dist/harness/session/schedules/tools.js +442 -0
- package/dist/harness/session/schedules/types.d.ts +104 -0
- package/dist/harness/session/schedules/types.js +97 -0
- package/dist/harness/session/tasks/tools.js +22 -0
- package/dist/harness/session/types.d.ts +35 -0
- package/dist/harness/shared/events/agent-event-map.d.ts +13 -0
- package/dist/harness/shared/types/agent-definition.d.ts +23 -0
- package/dist/index.d.ts +23 -9
- package/dist/index.js +10 -2
- package/dist/integration/providers/anthropic.d.ts +10 -0
- package/dist/integration/providers/anthropic.js +44 -1
- package/dist/integration/providers/openai.d.ts +10 -0
- package/dist/integration/providers/openai.js +58 -2
- package/dist/integration/providers/wire-dump-diff.d.ts +191 -0
- package/dist/integration/providers/wire-dump-diff.js +351 -0
- package/dist/integration/providers/wire-dump.d.ts +178 -0
- package/dist/integration/providers/wire-dump.js +396 -0
- package/dist/integration/storage/jsonl.d.ts +11 -0
- package/dist/integration/storage/jsonl.js +148 -39
- package/dist/integration/storage/session-index.js +63 -8
- package/dist/subsystems/cognition-steward/conceptualize/SUBSYSTEM.md +43 -43
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +50 -50
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +6 -6
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +54 -54
- package/dist/subsystems/memory-steward/backfill/handler.js +9 -0
- package/dist/subsystems/wisdom-steward/evaluate/config.yaml +50 -50
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +42 -42
- package/dist/subsystems/wisdom-steward/formation/config.yaml +54 -54
- package/dist/subsystems/wisdom-steward/govern/config.yaml +55 -55
- package/package.json +115 -115
- package/dist/harness/collaboration/multi-agent/agent-registry-types.d.ts +0 -144
- package/dist/harness/collaboration/multi-agent/agent-registry-types.js +0 -25
- package/dist/harness/collaboration/multi-agent/index.d.ts +0 -17
- package/dist/harness/collaboration/multi-agent/index.js +0 -15
- package/dist/harness/collaboration/multi-agent/process.d.ts +0 -167
- package/dist/harness/collaboration/multi-agent/process.js +0 -256
- package/dist/harness/collaboration/multi-agent/registry.d.ts +0 -36
- package/dist/harness/collaboration/multi-agent/registry.js +0 -137
- package/dist/harness/collaboration/multi-agent/swarm.d.ts +0 -135
- package/dist/harness/collaboration/multi-agent/swarm.js +0 -317
- package/dist/harness/collaboration/multi-agent/types.d.ts +0 -66
- package/dist/harness/collaboration/multi-agent/types.js +0 -15
- package/dist/harness/collaboration/orchestration/async-task-store.d.ts +0 -107
- package/dist/harness/collaboration/orchestration/async-task-store.js +0 -12
- package/dist/harness/collaboration/orchestration/async-task.d.ts +0 -156
- package/dist/harness/collaboration/orchestration/async-task.js +0 -359
- package/dist/harness/collaboration/orchestration/planner/hybrid-planner.d.ts +0 -47
- package/dist/harness/collaboration/orchestration/planner/hybrid-planner.js +0 -61
- package/dist/harness/collaboration/orchestration/planner/index.d.ts +0 -9
- package/dist/harness/collaboration/orchestration/planner/index.js +0 -6
- package/dist/harness/collaboration/orchestration/planner/llm-planner.d.ts +0 -54
- package/dist/harness/collaboration/orchestration/planner/llm-planner.js +0 -167
- package/dist/harness/collaboration/orchestration/planner/rule-planner.d.ts +0 -85
- package/dist/harness/collaboration/orchestration/planner/rule-planner.js +0 -175
- package/dist/harness/collaboration/orchestration/quality/classifier.d.ts +0 -21
- package/dist/harness/collaboration/orchestration/quality/classifier.js +0 -235
- package/dist/harness/collaboration/orchestration/quality/gate.d.ts +0 -29
- package/dist/harness/collaboration/orchestration/quality/gate.js +0 -409
- package/dist/harness/collaboration/orchestration/quality/index.d.ts +0 -9
- package/dist/harness/collaboration/orchestration/quality/index.js +0 -8
- package/dist/harness/collaboration/orchestration/quality/types.d.ts +0 -159
- package/dist/harness/collaboration/orchestration/quality/types.js +0 -33
- package/dist/harness/collaboration/orchestration/scheduler/index.d.ts +0 -5
- package/dist/harness/collaboration/orchestration/scheduler/index.js +0 -4
- package/dist/harness/collaboration/orchestration/scheduler/task-scheduler.d.ts +0 -143
- package/dist/harness/collaboration/orchestration/scheduler/task-scheduler.js +0 -297
- package/dist/harness/collaboration/orchestration/strategy/classifier.d.ts +0 -12
- package/dist/harness/collaboration/orchestration/strategy/classifier.js +0 -81
- package/dist/harness/collaboration/orchestration/strategy/index.d.ts +0 -3
- package/dist/harness/collaboration/orchestration/strategy/index.js +0 -2
- package/dist/harness/collaboration/orchestration/strategy/router.d.ts +0 -28
- package/dist/harness/collaboration/orchestration/strategy/router.js +0 -105
- package/dist/harness/collaboration/orchestration/strategy/types.d.ts +0 -49
- package/dist/harness/collaboration/orchestration/strategy/types.js +0 -4
- package/dist/harness/collaboration/orchestration/workflow/engine.d.ts +0 -86
- package/dist/harness/collaboration/orchestration/workflow/engine.js +0 -298
- package/dist/harness/collaboration/orchestration/workflow/index.d.ts +0 -9
- package/dist/harness/collaboration/orchestration/workflow/index.js +0 -7
- package/dist/harness/collaboration/orchestration/workflow/types.d.ts +0 -143
- package/dist/harness/collaboration/orchestration/workflow/types.js +0 -7
- package/dist/harness/governance/human-in-the-loop/approval-manager.d.ts +0 -32
- package/dist/harness/governance/human-in-the-loop/approval-manager.js +0 -93
- /package/dist/harness/collaboration/{multi-agent → delegation}/message-channel-types.d.ts +0 -0
- /package/dist/harness/collaboration/{multi-agent → delegation}/message-channel-types.js +0 -0
|
@@ -0,0 +1,695 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SessionScheduleService — 会话日程唯一写入口
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* - schedules.db 读写与状态机校验
|
|
6
|
+
* - 薄 timer 臂(own setTimeout;禁止 import orchestration TaskScheduler)
|
|
7
|
+
* - 到点先记账再 dispatch Trigger(at-most-once)
|
|
8
|
+
*
|
|
9
|
+
* 见 arch/session-schedules.md。
|
|
10
|
+
*/
|
|
11
|
+
import { randomUUID } from 'node:crypto';
|
|
12
|
+
import { intervalNext, nextFireTime, parseCron } from '@octopi-agent/core/primitives/cron.js';
|
|
13
|
+
import { assertTransition, isScheduleActiveish, parseDurationMs, } from './types.js';
|
|
14
|
+
import { SessionScheduleStore, buildScheduleRecord, } from './store.js';
|
|
15
|
+
/** Node setTimeout 最大 delay(2^31-1);超过会钳成 ~1ms 立刻点火 */
|
|
16
|
+
export const MAX_TIMEOUT_MS = 2_147_483_647;
|
|
17
|
+
function now() {
|
|
18
|
+
return Date.now();
|
|
19
|
+
}
|
|
20
|
+
function unrefTimer(timer) {
|
|
21
|
+
if (typeof timer === 'object' && timer !== null && 'unref' in timer) {
|
|
22
|
+
timer.unref?.();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 解析绝对时间
|
|
27
|
+
*
|
|
28
|
+
* @param at - ISO 字符串或毫秒时间戳;较小的数字按 Unix 秒解释
|
|
29
|
+
*/
|
|
30
|
+
export function parseFireAt(at) {
|
|
31
|
+
if (typeof at === 'number') {
|
|
32
|
+
if (!Number.isFinite(at) || at <= 0) {
|
|
33
|
+
throw new Error(`Invalid at timestamp: ${at}`);
|
|
34
|
+
}
|
|
35
|
+
// Unix 秒(~1.7e9)vs 毫秒(~1.7e12):小于 1e11 视为秒
|
|
36
|
+
const ms = at < 1e11 ? Math.floor(at * 1000) : Math.floor(at);
|
|
37
|
+
return ms;
|
|
38
|
+
}
|
|
39
|
+
const raw = String(at).trim();
|
|
40
|
+
// 纯数字字符串:与 number 相同规则
|
|
41
|
+
if (/^\d+$/.test(raw)) {
|
|
42
|
+
return parseFireAt(Number(raw));
|
|
43
|
+
}
|
|
44
|
+
const t = Date.parse(raw);
|
|
45
|
+
if (Number.isNaN(t)) {
|
|
46
|
+
throw new Error(`Invalid at time: ${at}. Use ISO 8601 or epoch ms`);
|
|
47
|
+
}
|
|
48
|
+
return t;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* SessionScheduleService
|
|
52
|
+
*/
|
|
53
|
+
export class SessionScheduleService {
|
|
54
|
+
options;
|
|
55
|
+
timers = new Map();
|
|
56
|
+
firing = new Set();
|
|
57
|
+
opened = false;
|
|
58
|
+
wakeup;
|
|
59
|
+
notify;
|
|
60
|
+
store;
|
|
61
|
+
tickTimer;
|
|
62
|
+
/** 滑动窗口:近 1h wakeup 点火时间戳 */
|
|
63
|
+
wakeStamps = [];
|
|
64
|
+
/** coalesceKey → 上次 dispatch 时间 */
|
|
65
|
+
coalesceSeen = new Map();
|
|
66
|
+
constructor(options = {}) {
|
|
67
|
+
this.options = options;
|
|
68
|
+
this.wakeup = options.wakeup;
|
|
69
|
+
this.notify = options.notify;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 打开/建库并重臂 active 作业。幂等。
|
|
73
|
+
*/
|
|
74
|
+
async open() {
|
|
75
|
+
if (this.opened)
|
|
76
|
+
return;
|
|
77
|
+
this.store = await SessionScheduleStore.create(this.options);
|
|
78
|
+
this.opened = true;
|
|
79
|
+
this.rearmAll();
|
|
80
|
+
this.startTick();
|
|
81
|
+
}
|
|
82
|
+
/** 兜底扫描:防 timer 丢失(P1) */
|
|
83
|
+
startTick() {
|
|
84
|
+
const interval = this.options.tickIntervalMs ?? 60_000;
|
|
85
|
+
if (interval <= 0)
|
|
86
|
+
return;
|
|
87
|
+
this.tickTimer = setInterval(() => {
|
|
88
|
+
if (!this.opened)
|
|
89
|
+
return;
|
|
90
|
+
try {
|
|
91
|
+
const store = this.requireStore();
|
|
92
|
+
const at = now();
|
|
93
|
+
for (const s of store.listActiveDue()) {
|
|
94
|
+
if (s.nextFireAt <= at && !this.timers.has(s.id) && !this.firing.has(s.id)) {
|
|
95
|
+
this.arm(s);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// 兜底 tick 失败不中断主路径
|
|
101
|
+
}
|
|
102
|
+
}, interval);
|
|
103
|
+
unrefTimer(this.tickTimer);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 注入点火宿主(gateway 绑 AgentRuntime.dispatch)
|
|
107
|
+
*
|
|
108
|
+
* @param fn - 非阻塞 dispatch
|
|
109
|
+
*/
|
|
110
|
+
setWakeup(fn) {
|
|
111
|
+
this.wakeup = fn;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 注入 notify 宿主(只写会话消息)
|
|
115
|
+
*
|
|
116
|
+
* @param fn - 写入回调
|
|
117
|
+
*/
|
|
118
|
+
setNotify(fn) {
|
|
119
|
+
this.notify = fn;
|
|
120
|
+
}
|
|
121
|
+
/** 拆光 timer 并关库(gateway.stop) */
|
|
122
|
+
dispose() {
|
|
123
|
+
if (this.tickTimer) {
|
|
124
|
+
clearInterval(this.tickTimer);
|
|
125
|
+
this.tickTimer = undefined;
|
|
126
|
+
}
|
|
127
|
+
for (const t of this.timers.values())
|
|
128
|
+
clearTimeout(t);
|
|
129
|
+
this.timers.clear();
|
|
130
|
+
this.firing.clear();
|
|
131
|
+
this.wakeStamps = [];
|
|
132
|
+
this.coalesceSeen.clear();
|
|
133
|
+
this.opened = false;
|
|
134
|
+
this.store?.close();
|
|
135
|
+
this.store = undefined;
|
|
136
|
+
}
|
|
137
|
+
requireStore() {
|
|
138
|
+
if (!this.store) {
|
|
139
|
+
throw new Error('SessionScheduleService not opened; call open() first');
|
|
140
|
+
}
|
|
141
|
+
return this.store;
|
|
142
|
+
}
|
|
143
|
+
emitEvent(type, schedule, extra) {
|
|
144
|
+
const events = this.options.events;
|
|
145
|
+
if (!events)
|
|
146
|
+
return;
|
|
147
|
+
const event = {
|
|
148
|
+
type,
|
|
149
|
+
timestamp: now(),
|
|
150
|
+
agentId: schedule.agentId,
|
|
151
|
+
sessionId: schedule.sessionId,
|
|
152
|
+
data: { schedule: { ...schedule }, ...extra },
|
|
153
|
+
};
|
|
154
|
+
events.emit(event);
|
|
155
|
+
}
|
|
156
|
+
// ── 时间规格 ──
|
|
157
|
+
resolveSpec(spec) {
|
|
158
|
+
const provided = [spec.delay, spec.at, spec.every, spec.cron].filter((v) => v !== undefined && v !== null && String(v).trim() !== '');
|
|
159
|
+
if (provided.length !== 1) {
|
|
160
|
+
throw new Error('schedule time requires exactly one of: delay, at, every, cron');
|
|
161
|
+
}
|
|
162
|
+
const from = now();
|
|
163
|
+
if (spec.delay !== undefined && String(spec.delay).trim() !== '') {
|
|
164
|
+
const ms = parseDurationMs(spec.delay);
|
|
165
|
+
if (ms <= 0)
|
|
166
|
+
throw new Error('delay must be > 0');
|
|
167
|
+
const fireAt = from + ms;
|
|
168
|
+
return { kind: 'once', fireAt, nextFireAt: fireAt };
|
|
169
|
+
}
|
|
170
|
+
if (spec.at !== undefined && String(spec.at).trim() !== '') {
|
|
171
|
+
const fireAt = parseFireAt(spec.at);
|
|
172
|
+
// 必须在未来:过去的 at 会变成 setTimeout(0) 立刻点火
|
|
173
|
+
if (fireAt <= from) {
|
|
174
|
+
throw new Error(`at time must be in the future (got ${new Date(fireAt).toISOString()}, now ${new Date(from).toISOString()})`);
|
|
175
|
+
}
|
|
176
|
+
return { kind: 'once', fireAt, nextFireAt: fireAt };
|
|
177
|
+
}
|
|
178
|
+
if (spec.every !== undefined && String(spec.every).trim() !== '') {
|
|
179
|
+
const intervalMs = parseDurationMs(spec.every);
|
|
180
|
+
if (intervalMs < 1000) {
|
|
181
|
+
throw new Error('every interval must be >= 1s');
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
kind: 'interval',
|
|
185
|
+
intervalMs,
|
|
186
|
+
nextFireAt: intervalNext(from, intervalMs),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const cron = String(spec.cron ?? '').trim();
|
|
190
|
+
const parsed = parseCron(cron);
|
|
191
|
+
if (!parsed.ok) {
|
|
192
|
+
throw new Error(`Invalid cron: ${parsed.error}`);
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
kind: 'cron',
|
|
196
|
+
cron,
|
|
197
|
+
nextFireAt: nextFireTime(parsed.spec, from),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
computeNextFireAt(schedule, from) {
|
|
201
|
+
if (schedule.kind === 'interval' && schedule.intervalMs) {
|
|
202
|
+
return intervalNext(from, schedule.intervalMs);
|
|
203
|
+
}
|
|
204
|
+
if (schedule.kind === 'cron' && schedule.cron) {
|
|
205
|
+
const parsed = parseCron(schedule.cron);
|
|
206
|
+
if (!parsed.ok) {
|
|
207
|
+
throw new Error(`Invalid cron on schedule ${schedule.id}: ${parsed.error}`);
|
|
208
|
+
}
|
|
209
|
+
return nextFireTime(parsed.spec, from);
|
|
210
|
+
}
|
|
211
|
+
// once 不再点火
|
|
212
|
+
return Number.POSITIVE_INFINITY;
|
|
213
|
+
}
|
|
214
|
+
// ── 查询 ──
|
|
215
|
+
/**
|
|
216
|
+
* 列出作业
|
|
217
|
+
*
|
|
218
|
+
* @param sessionId - 会话 id
|
|
219
|
+
* @param filter - status / activeOnly
|
|
220
|
+
*/
|
|
221
|
+
list(sessionId, filter) {
|
|
222
|
+
if (!this.store)
|
|
223
|
+
return [];
|
|
224
|
+
const store = this.store;
|
|
225
|
+
const statuses = filter?.activeOnly
|
|
226
|
+
? ['active', 'paused']
|
|
227
|
+
: filter?.status
|
|
228
|
+
? [filter.status]
|
|
229
|
+
: undefined;
|
|
230
|
+
return store.list({ sessionId, ...(statuses ? { statuses } : {}) });
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 按 id 取作业
|
|
234
|
+
*
|
|
235
|
+
* @param id - ScheduleId
|
|
236
|
+
*/
|
|
237
|
+
get(id) {
|
|
238
|
+
return this.requireStore().get(id);
|
|
239
|
+
}
|
|
240
|
+
// ── 变更 ──
|
|
241
|
+
/**
|
|
242
|
+
* 登记作业并 arm
|
|
243
|
+
*
|
|
244
|
+
* @param input - 创建入参 + 时间四选一
|
|
245
|
+
*/
|
|
246
|
+
async set(input) {
|
|
247
|
+
const store = this.requireStore();
|
|
248
|
+
const resolved = this.resolveSpec(input);
|
|
249
|
+
const maxOpen = this.options.maxOpenPerSession ?? 50;
|
|
250
|
+
if (maxOpen > 0 && store.countOpen(input.sessionId) >= maxOpen) {
|
|
251
|
+
throw new Error(`Too many open schedules for session ${input.sessionId} (max ${maxOpen}); cancel some first`);
|
|
252
|
+
}
|
|
253
|
+
const id = `sch_${randomUUID().replace(/-/g, '').slice(0, 20)}`;
|
|
254
|
+
const record = buildScheduleRecord(id, {
|
|
255
|
+
sessionId: input.sessionId,
|
|
256
|
+
agentId: input.agentId,
|
|
257
|
+
kind: resolved.kind,
|
|
258
|
+
...(resolved.fireAt !== undefined ? { fireAt: resolved.fireAt } : {}),
|
|
259
|
+
...(resolved.intervalMs !== undefined ? { intervalMs: resolved.intervalMs } : {}),
|
|
260
|
+
...(resolved.cron !== undefined ? { cron: resolved.cron } : {}),
|
|
261
|
+
...(input.maxRuns !== undefined ? { maxRuns: input.maxRuns } : {}),
|
|
262
|
+
payload: input.payload,
|
|
263
|
+
...(input.coalesceKey !== undefined ? { coalesceKey: input.coalesceKey } : {}),
|
|
264
|
+
catchUp: input.catchUp ?? 'skip',
|
|
265
|
+
...(input.createdByRunId ? { createdByRunId: input.createdByRunId } : {}),
|
|
266
|
+
}, resolved.nextFireAt, now());
|
|
267
|
+
if (record.maxRuns !== undefined && record.maxRuns < 1) {
|
|
268
|
+
throw new Error('max_runs must be >= 1');
|
|
269
|
+
}
|
|
270
|
+
store.insert(record);
|
|
271
|
+
this.arm(record);
|
|
272
|
+
this.emitEvent('session.schedule.created', record);
|
|
273
|
+
return { ...record };
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* 取消作业
|
|
277
|
+
*
|
|
278
|
+
* @param id - ScheduleId
|
|
279
|
+
*/
|
|
280
|
+
cancel(id, _reason) {
|
|
281
|
+
const store = this.requireStore();
|
|
282
|
+
const schedule = store.get(id);
|
|
283
|
+
if (!schedule) {
|
|
284
|
+
throw new Error(`Schedule not found: ${id}`);
|
|
285
|
+
}
|
|
286
|
+
if (schedule.status === 'cancelled') {
|
|
287
|
+
return { ...schedule };
|
|
288
|
+
}
|
|
289
|
+
if (schedule.status === 'completed' || schedule.status === 'failed') {
|
|
290
|
+
throw new Error(`Schedule already settled (${schedule.status}): ${id}`);
|
|
291
|
+
}
|
|
292
|
+
assertTransition(schedule.status, 'cancelled', id);
|
|
293
|
+
this.disarm(id);
|
|
294
|
+
const updatedAt = now();
|
|
295
|
+
store.update(id, { status: 'cancelled', updatedAt });
|
|
296
|
+
const next = { ...schedule, status: 'cancelled', updatedAt };
|
|
297
|
+
this.emitEvent('session.schedule.cancelled', next);
|
|
298
|
+
return next;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* 暂停
|
|
302
|
+
*
|
|
303
|
+
* @param id - ScheduleId
|
|
304
|
+
*/
|
|
305
|
+
pause(id) {
|
|
306
|
+
const store = this.requireStore();
|
|
307
|
+
const schedule = store.get(id);
|
|
308
|
+
if (!schedule) {
|
|
309
|
+
throw new Error(`Schedule not found: ${id}`);
|
|
310
|
+
}
|
|
311
|
+
if (schedule.status === 'paused')
|
|
312
|
+
return { ...schedule };
|
|
313
|
+
assertTransition(schedule.status, 'paused', id);
|
|
314
|
+
this.disarm(id);
|
|
315
|
+
const updatedAt = now();
|
|
316
|
+
store.update(id, { status: 'paused', updatedAt });
|
|
317
|
+
const next = { ...schedule, status: 'paused', updatedAt };
|
|
318
|
+
this.emitEvent('session.schedule.paused', next);
|
|
319
|
+
return next;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* 恢复(re-arm 时套用 catchUp)
|
|
323
|
+
*
|
|
324
|
+
* @param id - ScheduleId
|
|
325
|
+
*/
|
|
326
|
+
resume(id) {
|
|
327
|
+
const store = this.requireStore();
|
|
328
|
+
const schedule = store.get(id);
|
|
329
|
+
if (!schedule) {
|
|
330
|
+
throw new Error(`Schedule not found: ${id}`);
|
|
331
|
+
}
|
|
332
|
+
if (schedule.status === 'active')
|
|
333
|
+
return { ...schedule };
|
|
334
|
+
assertTransition(schedule.status, 'active', id);
|
|
335
|
+
const at = now();
|
|
336
|
+
let nextFireAt = schedule.nextFireAt;
|
|
337
|
+
if (nextFireAt < at) {
|
|
338
|
+
if (schedule.catchUp === 'fire_once') {
|
|
339
|
+
// 补一枪:保持 nextFireAt 为过去,arm 立即 fire
|
|
340
|
+
nextFireAt = at;
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
nextFireAt = this.computeNextFireAt(schedule, at);
|
|
344
|
+
if (!Number.isFinite(nextFireAt)) {
|
|
345
|
+
// once 已过期且 skip:直接完成
|
|
346
|
+
store.update(id, { status: 'completed', updatedAt: at });
|
|
347
|
+
const done = { ...schedule, status: 'completed', updatedAt: at };
|
|
348
|
+
this.emitEvent('session.schedule.completed', done);
|
|
349
|
+
return done;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
store.update(id, { status: 'active', nextFireAt, updatedAt: at });
|
|
354
|
+
const next = {
|
|
355
|
+
...schedule,
|
|
356
|
+
status: 'active',
|
|
357
|
+
nextFireAt,
|
|
358
|
+
updatedAt: at,
|
|
359
|
+
};
|
|
360
|
+
this.arm(next);
|
|
361
|
+
this.emitEvent('session.schedule.resumed', next);
|
|
362
|
+
return next;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* 修改作业(prompt / title / max_runs / 时间 / catch_up)
|
|
366
|
+
* 仅 active|paused;改时间后重算 nextFireAt 并 re-arm。
|
|
367
|
+
*
|
|
368
|
+
* @param id - ScheduleId
|
|
369
|
+
* @param patch - 可选字段
|
|
370
|
+
*/
|
|
371
|
+
update(id, patch) {
|
|
372
|
+
const store = this.requireStore();
|
|
373
|
+
const schedule = store.get(id);
|
|
374
|
+
if (!schedule) {
|
|
375
|
+
throw new Error(`Schedule not found: ${id}`);
|
|
376
|
+
}
|
|
377
|
+
if (schedule.status !== 'active' && schedule.status !== 'paused') {
|
|
378
|
+
throw new Error(`Cannot update schedule in status ${schedule.status}: ${id}`);
|
|
379
|
+
}
|
|
380
|
+
const at = now();
|
|
381
|
+
const payload = { ...schedule.payload };
|
|
382
|
+
if (patch.prompt !== undefined) {
|
|
383
|
+
const text = String(patch.prompt).trim();
|
|
384
|
+
if (!text)
|
|
385
|
+
throw new Error('prompt must not be empty');
|
|
386
|
+
payload.text = text;
|
|
387
|
+
}
|
|
388
|
+
if (patch.title !== undefined) {
|
|
389
|
+
const title = String(patch.title).trim();
|
|
390
|
+
if (title)
|
|
391
|
+
payload.title = title;
|
|
392
|
+
else
|
|
393
|
+
delete payload.title;
|
|
394
|
+
}
|
|
395
|
+
const updatePatch = {
|
|
396
|
+
payload,
|
|
397
|
+
updatedAt: at,
|
|
398
|
+
};
|
|
399
|
+
if (patch.maxRuns !== undefined) {
|
|
400
|
+
if (patch.maxRuns !== null && patch.maxRuns < 1) {
|
|
401
|
+
throw new Error('max_runs must be >= 1');
|
|
402
|
+
}
|
|
403
|
+
updatePatch.maxRuns = patch.maxRuns;
|
|
404
|
+
}
|
|
405
|
+
if (patch.catchUp !== undefined) {
|
|
406
|
+
updatePatch.catchUp = patch.catchUp;
|
|
407
|
+
}
|
|
408
|
+
let next = {
|
|
409
|
+
...schedule,
|
|
410
|
+
payload,
|
|
411
|
+
updatedAt: at,
|
|
412
|
+
...(patch.maxRuns !== undefined
|
|
413
|
+
? { maxRuns: patch.maxRuns ?? undefined }
|
|
414
|
+
: {}),
|
|
415
|
+
...(patch.catchUp !== undefined ? { catchUp: patch.catchUp } : {}),
|
|
416
|
+
};
|
|
417
|
+
if (patch.time) {
|
|
418
|
+
const resolved = this.resolveSpec(patch.time);
|
|
419
|
+
updatePatch.kind = resolved.kind;
|
|
420
|
+
updatePatch.fireAt = resolved.fireAt ?? null;
|
|
421
|
+
updatePatch.intervalMs = resolved.intervalMs ?? null;
|
|
422
|
+
updatePatch.cron = resolved.cron ?? null;
|
|
423
|
+
updatePatch.nextFireAt = resolved.nextFireAt;
|
|
424
|
+
next = {
|
|
425
|
+
...next,
|
|
426
|
+
kind: resolved.kind,
|
|
427
|
+
nextFireAt: resolved.nextFireAt,
|
|
428
|
+
...(resolved.fireAt !== undefined ? { fireAt: resolved.fireAt } : { fireAt: undefined }),
|
|
429
|
+
...(resolved.intervalMs !== undefined
|
|
430
|
+
? { intervalMs: resolved.intervalMs }
|
|
431
|
+
: { intervalMs: undefined }),
|
|
432
|
+
...(resolved.cron !== undefined ? { cron: resolved.cron } : { cron: undefined }),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
store.update(id, updatePatch);
|
|
436
|
+
if (next.status === 'active') {
|
|
437
|
+
this.arm(next);
|
|
438
|
+
}
|
|
439
|
+
this.emitEvent('session.schedule.updated', next);
|
|
440
|
+
return { ...next };
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* 会话删除清理
|
|
444
|
+
*
|
|
445
|
+
* @param sessionId - 会话 id
|
|
446
|
+
*/
|
|
447
|
+
purgeSession(sessionId) {
|
|
448
|
+
const store = this.requireStore();
|
|
449
|
+
for (const s of store.list({ sessionId })) {
|
|
450
|
+
this.disarm(s.id);
|
|
451
|
+
}
|
|
452
|
+
return store.deleteBySession(sessionId);
|
|
453
|
+
}
|
|
454
|
+
// ── timer 臂 ──
|
|
455
|
+
disarm(id) {
|
|
456
|
+
const t = this.timers.get(id);
|
|
457
|
+
if (t) {
|
|
458
|
+
clearTimeout(t);
|
|
459
|
+
this.timers.delete(id);
|
|
460
|
+
}
|
|
461
|
+
this.firing.delete(id);
|
|
462
|
+
}
|
|
463
|
+
arm(schedule) {
|
|
464
|
+
if (schedule.status !== 'active')
|
|
465
|
+
return;
|
|
466
|
+
this.disarm(schedule.id);
|
|
467
|
+
const fireAt = schedule.nextFireAt;
|
|
468
|
+
const delay = Math.max(0, fireAt - now());
|
|
469
|
+
// 超过 2^31-1 时 Node 会把 delay 钳成 1ms → 假「立刻点火」;必须分段 arm
|
|
470
|
+
const slice = Math.min(delay, MAX_TIMEOUT_MS);
|
|
471
|
+
const timer = setTimeout(() => {
|
|
472
|
+
this.timers.delete(schedule.id);
|
|
473
|
+
if (now() < fireAt) {
|
|
474
|
+
// 到点前的 sleeper 片段:继续排下一段
|
|
475
|
+
this.arm(schedule);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
void this.onDue(schedule.id).catch((err) => {
|
|
479
|
+
// setTimeout 路径禁止冒泡 unhandledRejection;错误已写 lastError
|
|
480
|
+
console.error(`[SessionScheduleService] onDue failed for ${schedule.id}:`, err instanceof Error ? err.message : err);
|
|
481
|
+
});
|
|
482
|
+
}, slice);
|
|
483
|
+
unrefTimer(timer);
|
|
484
|
+
this.timers.set(schedule.id, timer);
|
|
485
|
+
}
|
|
486
|
+
rearmAll() {
|
|
487
|
+
const store = this.requireStore();
|
|
488
|
+
const at = now();
|
|
489
|
+
for (const schedule of store.listActiveDue()) {
|
|
490
|
+
if (schedule.nextFireAt < at) {
|
|
491
|
+
if (schedule.catchUp === 'fire_once') {
|
|
492
|
+
// 保持过去时间戳,arm(0) 立即补一枪
|
|
493
|
+
this.arm(schedule);
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const next = this.computeNextFireAt(schedule, at);
|
|
497
|
+
if (!Number.isFinite(next)) {
|
|
498
|
+
store.update(schedule.id, {
|
|
499
|
+
status: 'completed',
|
|
500
|
+
updatedAt: at,
|
|
501
|
+
});
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
store.update(schedule.id, { nextFireAt: next, updatedAt: at });
|
|
505
|
+
this.arm({ ...schedule, nextFireAt: next });
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
this.arm(schedule);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
/** 测试/手动触发(穿冷却语义:仅 active) */
|
|
512
|
+
async runNow(id) {
|
|
513
|
+
await this.onDue(id);
|
|
514
|
+
}
|
|
515
|
+
async onDue(id) {
|
|
516
|
+
const store = this.requireStore();
|
|
517
|
+
if (this.firing.has(id))
|
|
518
|
+
return;
|
|
519
|
+
this.firing.add(id);
|
|
520
|
+
let rearm = null;
|
|
521
|
+
try {
|
|
522
|
+
const schedule = store.get(id);
|
|
523
|
+
if (!schedule || schedule.status !== 'active')
|
|
524
|
+
return;
|
|
525
|
+
const at = now();
|
|
526
|
+
const fireId = randomUUID();
|
|
527
|
+
const runCount = schedule.runCount + 1;
|
|
528
|
+
const exhausted = schedule.kind === 'once' ||
|
|
529
|
+
(schedule.maxRuns !== undefined && runCount >= schedule.maxRuns);
|
|
530
|
+
// 3. 先记账(at-most-once);记账/算点失败 = 不可恢复 → failed
|
|
531
|
+
let nextFireAt;
|
|
532
|
+
try {
|
|
533
|
+
nextFireAt = exhausted
|
|
534
|
+
? schedule.nextFireAt
|
|
535
|
+
: this.computeNextFireAt(schedule, at);
|
|
536
|
+
const status = exhausted ? 'completed' : 'active';
|
|
537
|
+
if (exhausted) {
|
|
538
|
+
assertTransition(schedule.status, 'completed', id);
|
|
539
|
+
}
|
|
540
|
+
store.transaction(() => {
|
|
541
|
+
store.update(id, {
|
|
542
|
+
runCount,
|
|
543
|
+
lastFireAt: at,
|
|
544
|
+
lastFireId: fireId,
|
|
545
|
+
updatedAt: at,
|
|
546
|
+
...(exhausted
|
|
547
|
+
? { status: 'completed' }
|
|
548
|
+
: { nextFireAt }),
|
|
549
|
+
});
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
catch (err) {
|
|
553
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
554
|
+
try {
|
|
555
|
+
store.update(id, {
|
|
556
|
+
status: 'failed',
|
|
557
|
+
lastError: message,
|
|
558
|
+
updatedAt: now(),
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
catch {
|
|
562
|
+
// 库已不可写;错误由 arm().catch 日志兜底
|
|
563
|
+
}
|
|
564
|
+
this.emitEvent('session.schedule.failed', {
|
|
565
|
+
...schedule,
|
|
566
|
+
status: 'failed',
|
|
567
|
+
lastError: message,
|
|
568
|
+
});
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const settled = {
|
|
572
|
+
...schedule,
|
|
573
|
+
runCount,
|
|
574
|
+
lastFireAt: at,
|
|
575
|
+
lastFireId: fireId,
|
|
576
|
+
updatedAt: at,
|
|
577
|
+
status: exhausted ? 'completed' : 'active',
|
|
578
|
+
...(exhausted ? {} : { nextFireAt }),
|
|
579
|
+
};
|
|
580
|
+
if (!exhausted && settled.status === 'active') {
|
|
581
|
+
rearm = settled;
|
|
582
|
+
}
|
|
583
|
+
// 4. 再点火(失败记 lastError,不阻断周期 re-arm)
|
|
584
|
+
try {
|
|
585
|
+
const trigger = this.toTrigger(settled, fireId);
|
|
586
|
+
const skipDispatch = this.shouldCoalesce(settled, at) || this.wakeLimitExceeded(settled);
|
|
587
|
+
if (skipDispatch) {
|
|
588
|
+
this.emitEvent('session.schedule.fire_skipped', settled, {
|
|
589
|
+
fireId,
|
|
590
|
+
reason: this.wakeLimitExceeded(settled) ? 'wake_budget' : 'coalesced',
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
else if (settled.payload.delivery === 'wakeup') {
|
|
594
|
+
this.noteWake(at);
|
|
595
|
+
this.noteCoalesce(settled, at);
|
|
596
|
+
this.wakeup?.(trigger, settled);
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
// notify:只落消息,不跑模型(arch §6)
|
|
600
|
+
this.noteCoalesce(settled, at);
|
|
601
|
+
const content = trigger.payload.kind === 'system_note' || trigger.payload.kind === 'user_message'
|
|
602
|
+
? trigger.payload.content
|
|
603
|
+
: settled.payload.text;
|
|
604
|
+
void Promise.resolve(this.notify?.({ schedule: settled, content })).catch((err) => {
|
|
605
|
+
store.update(id, {
|
|
606
|
+
lastError: err instanceof Error ? err.message : String(err),
|
|
607
|
+
updatedAt: now(),
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
this.emitEvent('session.schedule.fired', settled, { fireId, triggerId: trigger.id });
|
|
612
|
+
}
|
|
613
|
+
catch (err) {
|
|
614
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
615
|
+
store.update(id, { lastError: message, updatedAt: now() });
|
|
616
|
+
this.emitEvent('session.schedule.fire_error', {
|
|
617
|
+
...settled,
|
|
618
|
+
lastError: message,
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
finally {
|
|
623
|
+
this.firing.delete(id);
|
|
624
|
+
// 周期作业必须在此 re-arm;throw 会跳过外层 arm
|
|
625
|
+
if (rearm) {
|
|
626
|
+
try {
|
|
627
|
+
this.arm(rearm);
|
|
628
|
+
}
|
|
629
|
+
catch {
|
|
630
|
+
// arm 失败留给 rearmAll/重启兜底
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
shouldCoalesce(schedule, at) {
|
|
636
|
+
const key = schedule.coalesceKey;
|
|
637
|
+
if (!key)
|
|
638
|
+
return false;
|
|
639
|
+
const last = this.coalesceSeen.get(key);
|
|
640
|
+
const window = this.options.coalesceWindowMs ?? 500;
|
|
641
|
+
return last !== undefined && at - last < window;
|
|
642
|
+
}
|
|
643
|
+
noteCoalesce(schedule, at) {
|
|
644
|
+
if (schedule.coalesceKey) {
|
|
645
|
+
this.coalesceSeen.set(schedule.coalesceKey, at);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
wakeLimitExceeded(schedule) {
|
|
649
|
+
if (schedule.payload.delivery !== 'wakeup')
|
|
650
|
+
return false;
|
|
651
|
+
const max = this.options.maxWakesPerHour ?? 120;
|
|
652
|
+
if (max <= 0)
|
|
653
|
+
return false;
|
|
654
|
+
return this.wakeStamps.length >= max;
|
|
655
|
+
}
|
|
656
|
+
noteWake(at) {
|
|
657
|
+
const hourAgo = at - 3_600_000;
|
|
658
|
+
this.wakeStamps = this.wakeStamps.filter((t) => t > hourAgo);
|
|
659
|
+
this.wakeStamps.push(at);
|
|
660
|
+
}
|
|
661
|
+
toTrigger(schedule, fireId) {
|
|
662
|
+
const occurrence = schedule.runCount;
|
|
663
|
+
const title = schedule.payload.title
|
|
664
|
+
? ` title="${schedule.payload.title}"`
|
|
665
|
+
: '';
|
|
666
|
+
const content = [
|
|
667
|
+
`<schedule_wake id="${schedule.id}"${title} occurrence="${occurrence}">`,
|
|
668
|
+
'这是你此前安排的定时唤醒。请执行下列事项;若仍需周期执行请保持 schedule 不变,已结束请调用 schedule_cancel。',
|
|
669
|
+
'',
|
|
670
|
+
'事项:',
|
|
671
|
+
schedule.payload.text,
|
|
672
|
+
'</schedule_wake>',
|
|
673
|
+
].join('\n');
|
|
674
|
+
return {
|
|
675
|
+
id: `trg_${fireId.slice(0, 12)}`,
|
|
676
|
+
type: 'schedule',
|
|
677
|
+
agentId: schedule.agentId,
|
|
678
|
+
sessionId: schedule.sessionId,
|
|
679
|
+
timestamp: now(),
|
|
680
|
+
payload: {
|
|
681
|
+
kind: 'system_note',
|
|
682
|
+
content,
|
|
683
|
+
},
|
|
684
|
+
...(schedule.coalesceKey ? { coalesceKey: schedule.coalesceKey } : {}),
|
|
685
|
+
metadata: {
|
|
686
|
+
source: 'session_schedule',
|
|
687
|
+
reason: `schedule:${schedule.id}`,
|
|
688
|
+
},
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
/** 未闭合判断(list/注入用) */
|
|
693
|
+
export function scheduleIsOpen(status) {
|
|
694
|
+
return isScheduleActiveish(status);
|
|
695
|
+
}
|