@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
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Output Error Classifier - 异常分类器
|
|
3
|
-
*
|
|
4
|
-
* 根据质量检测结果和上下文信息,分类异常类型并推荐恢复策略
|
|
5
|
-
*/
|
|
6
|
-
// ================================================================
|
|
7
|
-
// 辅助函数
|
|
8
|
-
// ================================================================
|
|
9
|
-
/**
|
|
10
|
-
* 验证质量检测结果
|
|
11
|
-
*/
|
|
12
|
-
function validateQualityResult(result) {
|
|
13
|
-
if (typeof result !== 'object' || result === null) {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
const r = result;
|
|
17
|
-
return (typeof r.isAnomalous === 'boolean' &&
|
|
18
|
-
typeof r.qualityScore === 'number' &&
|
|
19
|
-
Array.isArray(r.anomalyHints) &&
|
|
20
|
-
typeof r.details === 'object' && r.details !== null);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* 验证分类上下文
|
|
24
|
-
*/
|
|
25
|
-
function validateContext(context) {
|
|
26
|
-
if (typeof context !== 'object' || context === null) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
const c = context;
|
|
30
|
-
if (typeof c.iterationCount !== 'number' || c.iterationCount < 0) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
if (!Array.isArray(c.previousErrors)) {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* 创建默认分类结果
|
|
40
|
-
*/
|
|
41
|
-
function createDefaultClassification() {
|
|
42
|
-
return {
|
|
43
|
-
type: 'unknown',
|
|
44
|
-
confidence: 0,
|
|
45
|
-
severity: 'minor',
|
|
46
|
-
recommendedStrategy: 'retry',
|
|
47
|
-
evidence: ['Invalid input'],
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
// ================================================================
|
|
51
|
-
// 核心分类函数
|
|
52
|
-
// ================================================================
|
|
53
|
-
/**
|
|
54
|
-
* 判断异常严重程度
|
|
55
|
-
*
|
|
56
|
-
* 基于质量评分和异常提示数量
|
|
57
|
-
*/
|
|
58
|
-
function determineSeverity(qualityScore, anomalyHints) {
|
|
59
|
-
// 评分极低或多个崩溃提示 → severe
|
|
60
|
-
if (qualityScore < 0.3 || anomalyHints.filter(h => h.type === 'model_collapse').length >= 2) {
|
|
61
|
-
return 'severe';
|
|
62
|
-
}
|
|
63
|
-
// 评分中等或有崩溃提示 → moderate
|
|
64
|
-
if (qualityScore < 0.5 || anomalyHints.some(h => h.type === 'model_collapse')) {
|
|
65
|
-
return 'moderate';
|
|
66
|
-
}
|
|
67
|
-
// 其他 → minor
|
|
68
|
-
return 'minor';
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* 根据异常提示确定主要异常类型
|
|
72
|
-
*/
|
|
73
|
-
function determineAnomalyType(anomalyHints, finishReason) {
|
|
74
|
-
// finish_reason 为 length → truncation
|
|
75
|
-
if (finishReason === 'length') {
|
|
76
|
-
return 'truncation';
|
|
77
|
-
}
|
|
78
|
-
// 按置信度排序异常提示
|
|
79
|
-
const sortedHints = [...anomalyHints].sort((a, b) => b.confidence - a.confidence);
|
|
80
|
-
// 最高置信度的提示类型
|
|
81
|
-
if (sortedHints.length > 0) {
|
|
82
|
-
const topHint = sortedHints[0];
|
|
83
|
-
if (topHint && topHint.confidence >= 0.5) {
|
|
84
|
-
return topHint.type;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
// 默认未知
|
|
88
|
-
return 'unknown';
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* 推荐恢复策略
|
|
92
|
-
*
|
|
93
|
-
* 基于异常类型和严重程度
|
|
94
|
-
*/
|
|
95
|
-
function recommendStrategy(type, severity, config, previousErrors) {
|
|
96
|
-
// 获取策略优先级配置
|
|
97
|
-
const priorityList = config.strategyPriority[type] ?? ['retry', 'abort'];
|
|
98
|
-
// 如果之前已经多次失败,直接 abort 或 degrade
|
|
99
|
-
const sameTypeErrors = previousErrors.filter(e => e.type === type);
|
|
100
|
-
if (sameTypeErrors.length >= config.maxRetries) {
|
|
101
|
-
return severity === 'severe' ? 'abort' : 'degrade';
|
|
102
|
-
}
|
|
103
|
-
// 根据严重程度调整策略
|
|
104
|
-
if (severity === 'severe') {
|
|
105
|
-
// 严重崩溃优先 fallback 或 abort
|
|
106
|
-
if (priorityList.includes('fallback') && config.fallbackModels.length > 0) {
|
|
107
|
-
return 'fallback';
|
|
108
|
-
}
|
|
109
|
-
return 'abort';
|
|
110
|
-
}
|
|
111
|
-
// moderate 崩溃:如果有 fallback 模型,优先 fallback
|
|
112
|
-
if (severity === 'moderate' &&
|
|
113
|
-
priorityList.includes('fallback') &&
|
|
114
|
-
config.fallbackModels.length > 0) {
|
|
115
|
-
return 'fallback';
|
|
116
|
-
}
|
|
117
|
-
// 其他情况按优先级选择
|
|
118
|
-
return priorityList[0] ?? 'retry';
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* 收集分类依据
|
|
122
|
-
*/
|
|
123
|
-
function collectEvidence(qualityResult, context) {
|
|
124
|
-
const evidence = [];
|
|
125
|
-
// 质量评分
|
|
126
|
-
evidence.push(`Quality score: ${qualityResult.qualityScore.toFixed(2)}`);
|
|
127
|
-
// 检测详情
|
|
128
|
-
if (qualityResult.details.syntaxFragmentDensity > 0) {
|
|
129
|
-
evidence.push(`Syntax fragment density: ${qualityResult.details.syntaxFragmentDensity.toFixed(2)}`);
|
|
130
|
-
}
|
|
131
|
-
if (qualityResult.details.entropy > 0) {
|
|
132
|
-
evidence.push(`Entropy: ${qualityResult.details.entropy.toFixed(2)}`);
|
|
133
|
-
}
|
|
134
|
-
if (qualityResult.details.repetitionRatio > 0) {
|
|
135
|
-
evidence.push(`Repetition ratio: ${qualityResult.details.repetitionRatio.toFixed(2)}`);
|
|
136
|
-
}
|
|
137
|
-
// 异常提示
|
|
138
|
-
for (const hint of qualityResult.anomalyHints) {
|
|
139
|
-
if (hint.evidence) {
|
|
140
|
-
evidence.push(`[${hint.type}] ${hint.evidence.slice(0, 50)}`);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
// finish_reason
|
|
144
|
-
if (context.finishReason) {
|
|
145
|
-
evidence.push(`Finish reason: ${context.finishReason}`);
|
|
146
|
-
}
|
|
147
|
-
// 工具调用
|
|
148
|
-
if (context.toolCalls && context.toolCalls.length > 0) {
|
|
149
|
-
evidence.push(`Tool calls: ${context.toolCalls.length}`);
|
|
150
|
-
}
|
|
151
|
-
// 迭代次数
|
|
152
|
-
evidence.push(`Iteration: ${context.iterationCount}`);
|
|
153
|
-
// 之前的错误
|
|
154
|
-
if (context.previousErrors.length > 0) {
|
|
155
|
-
evidence.push(`Previous errors: ${context.previousErrors.length}`);
|
|
156
|
-
}
|
|
157
|
-
return evidence;
|
|
158
|
-
}
|
|
159
|
-
// ================================================================
|
|
160
|
-
// ErrorClassifier 实现
|
|
161
|
-
// ================================================================
|
|
162
|
-
/**
|
|
163
|
-
* Output 异常分类器
|
|
164
|
-
*/
|
|
165
|
-
export class OutputErrorClassifier {
|
|
166
|
-
config;
|
|
167
|
-
constructor(config) {
|
|
168
|
-
this.config = config ?? {
|
|
169
|
-
maxRetries: 2,
|
|
170
|
-
fallbackModels: [],
|
|
171
|
-
strategyPriority: {
|
|
172
|
-
model_collapse: ['fallback', 'retry', 'abort'],
|
|
173
|
-
truncation: ['retry', 'abort'],
|
|
174
|
-
repetition_loop: ['retry', 'fallback', 'degrade'],
|
|
175
|
-
format_error: ['retry', 'abort'],
|
|
176
|
-
},
|
|
177
|
-
degradeConfig: {
|
|
178
|
-
disableTools: true,
|
|
179
|
-
maxTokens: 500,
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* 分类异常
|
|
185
|
-
*/
|
|
186
|
-
classify(qualityResult, context) {
|
|
187
|
-
// 输入验证
|
|
188
|
-
if (!validateQualityResult(qualityResult)) {
|
|
189
|
-
return createDefaultClassification();
|
|
190
|
-
}
|
|
191
|
-
if (!validateContext(context)) {
|
|
192
|
-
return createDefaultClassification();
|
|
193
|
-
}
|
|
194
|
-
// 如果不是异常,返回默认分类
|
|
195
|
-
if (!qualityResult.isAnomalous) {
|
|
196
|
-
return {
|
|
197
|
-
type: 'unknown',
|
|
198
|
-
confidence: 0,
|
|
199
|
-
severity: 'minor',
|
|
200
|
-
recommendedStrategy: 'retry',
|
|
201
|
-
evidence: collectEvidence(qualityResult, context),
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
// 确定异常类型
|
|
205
|
-
const type = determineAnomalyType(qualityResult.anomalyHints, context.finishReason);
|
|
206
|
-
// 确定严重程度
|
|
207
|
-
const severity = determineSeverity(qualityResult.qualityScore, qualityResult.anomalyHints);
|
|
208
|
-
// 计算置信度(基于异常提示和评分)
|
|
209
|
-
let confidence = 1 - qualityResult.qualityScore;
|
|
210
|
-
if (qualityResult.anomalyHints.length > 0) {
|
|
211
|
-
const maxHintConfidence = Math.max(...qualityResult.anomalyHints.map(h => h.confidence));
|
|
212
|
-
confidence = Math.max(confidence, maxHintConfidence);
|
|
213
|
-
}
|
|
214
|
-
// 推荐恢复策略
|
|
215
|
-
const recommendedStrategy = recommendStrategy(type, severity, this.config, context.previousErrors);
|
|
216
|
-
// 收集依据
|
|
217
|
-
const evidence = collectEvidence(qualityResult, context);
|
|
218
|
-
return {
|
|
219
|
-
type,
|
|
220
|
-
confidence: Math.min(1, confidence),
|
|
221
|
-
severity,
|
|
222
|
-
recommendedStrategy,
|
|
223
|
-
evidence,
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
// ================================================================
|
|
228
|
-
// 工厂函数
|
|
229
|
-
// ================================================================
|
|
230
|
-
/**
|
|
231
|
-
* 创建 Output 异常分类器
|
|
232
|
-
*/
|
|
233
|
-
export function createOutputErrorClassifier(config) {
|
|
234
|
-
return new OutputErrorClassifier(config);
|
|
235
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Output Quality Gate - 输出质量检测
|
|
3
|
-
*
|
|
4
|
-
* 检测 LLM 输出的质量问题:
|
|
5
|
-
* - 模型崩溃(token 序列混乱)
|
|
6
|
-
* - 截断
|
|
7
|
-
* - 重复循环
|
|
8
|
-
* - 格式错误
|
|
9
|
-
*/
|
|
10
|
-
import type { QualityCheckResult } from './types.js';
|
|
11
|
-
/**
|
|
12
|
-
* 输出质量检测器
|
|
13
|
-
*/
|
|
14
|
-
export declare class OutputQualityGate {
|
|
15
|
-
/**
|
|
16
|
-
* 检测文本输出质量
|
|
17
|
-
*/
|
|
18
|
-
checkTextOutput(text: unknown, config: unknown): QualityCheckResult;
|
|
19
|
-
/**
|
|
20
|
-
* 检测流式 chunk 质量(增量检测)
|
|
21
|
-
*
|
|
22
|
-
* 当累积文本较长时开始检测,避免误判正常输出
|
|
23
|
-
*/
|
|
24
|
-
checkStreamChunk(chunk: unknown, accumulated: unknown, config: unknown): QualityCheckResult;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* 创建输出质量检测器
|
|
28
|
-
*/
|
|
29
|
-
export declare function createOutputQualityGate(): OutputQualityGate;
|
|
@@ -1,409 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Output Quality Gate - 输出质量检测
|
|
3
|
-
*
|
|
4
|
-
* 检测 LLM 输出的质量问题:
|
|
5
|
-
* - 模型崩溃(token 序列混乱)
|
|
6
|
-
* - 截断
|
|
7
|
-
* - 重复循环
|
|
8
|
-
* - 格式错误
|
|
9
|
-
*/
|
|
10
|
-
// ================================================================
|
|
11
|
-
// 辅助函数
|
|
12
|
-
// ================================================================
|
|
13
|
-
/**
|
|
14
|
-
* 检测文本语言
|
|
15
|
-
*/
|
|
16
|
-
function detectLanguage(text) {
|
|
17
|
-
const chineseChars = text.match(/[\u4e00-\u9fa5]/g)?.length ?? 0;
|
|
18
|
-
return chineseChars / text.length > 0.3 ? 'zh' : 'en';
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 简单哈希函数(用于 Rabin-Karp)
|
|
22
|
-
*/
|
|
23
|
-
function simpleHash(str) {
|
|
24
|
-
let hash = 0;
|
|
25
|
-
for (let i = 0; i < str.length; i++) {
|
|
26
|
-
hash = ((hash << 5) - hash) + str.charCodeAt(i);
|
|
27
|
-
hash |= 0;
|
|
28
|
-
}
|
|
29
|
-
return hash;
|
|
30
|
-
}
|
|
31
|
-
// ================================================================
|
|
32
|
-
// 语法碎片检测模式(重构:只检测真正的碎片)
|
|
33
|
-
// ================================================================
|
|
34
|
-
/**
|
|
35
|
-
* 语法碎片检测模式
|
|
36
|
-
*
|
|
37
|
-
* 检测不完整的代码块特征(真正的碎片,不是完整语法)
|
|
38
|
-
*/
|
|
39
|
-
const SYNTAX_FRAGMENT_PATTERNS = [
|
|
40
|
-
// 悬空的 if(没有闭合括号或代码块)
|
|
41
|
-
/if\s*\([^)]*$/gm,
|
|
42
|
-
// 悬空的箭头函数(没有函数体)
|
|
43
|
-
/=>\s*\{[^}]*$/gm,
|
|
44
|
-
/=>\s*$/gm,
|
|
45
|
-
// 不完整的函数声明(没有闭合)
|
|
46
|
-
/function\s+\w*\s*\([^)]*\)\s*\{[^}]*$/gm,
|
|
47
|
-
// 不完整的模板字符串(没有闭合反引号)
|
|
48
|
-
/`[^`]*$/gm,
|
|
49
|
-
// 不完整的对象/数组字面量
|
|
50
|
-
/\{[^}]*$/gm,
|
|
51
|
-
/\[[^\]]*$/gm,
|
|
52
|
-
// 不完整的字符串(没有闭合引号)
|
|
53
|
-
/"[^"]*$/gm,
|
|
54
|
-
/'[^']*$/gm,
|
|
55
|
-
// 不完整的注释
|
|
56
|
-
/\/\*[^*]*\*([^/]|$)/gm,
|
|
57
|
-
/\/\/[^\n]*$/gm,
|
|
58
|
-
// 异常的括号组合(不匹配)
|
|
59
|
-
/\([^)]*\)/g, // 用于计数,实际检查在函数中
|
|
60
|
-
];
|
|
61
|
-
/**
|
|
62
|
-
* 崩溃特征模式
|
|
63
|
-
*
|
|
64
|
-
* 检测典型的崩溃输出特征
|
|
65
|
-
*/
|
|
66
|
-
const COLLAPSE_PATTERNS = [
|
|
67
|
-
// 中英混杂的代码片段(真正的崩溃特征)
|
|
68
|
-
/[\u4e00-\u9fa5]+\s*\{[\u4e00-\u9fa5]+\s*:/gi,
|
|
69
|
-
/[\u4e00-\u9fa5]+\s*=>[\u4e00-\u9fa5]+/gi,
|
|
70
|
-
// 无意义的字符组合
|
|
71
|
-
/\w+\s*\?\s*\w+\s*\|\|/gi, // 异常的条件表达式
|
|
72
|
-
/\w+\s*\|\|\s*\w+\s*::/gi, // 异常的类型表达式
|
|
73
|
-
// 异常的标记组合
|
|
74
|
-
/```.*?```.*?```.*?```/gi, // 多个不完整代码块
|
|
75
|
-
];
|
|
76
|
-
// ================================================================
|
|
77
|
-
// 核心检测函数
|
|
78
|
-
// ================================================================
|
|
79
|
-
/**
|
|
80
|
-
* 计算字符熵值(语言感知版本)
|
|
81
|
-
*
|
|
82
|
-
* 过低的熵值表示输出异常(字符分布过于集中)
|
|
83
|
-
*/
|
|
84
|
-
function calculateEntropy(text) {
|
|
85
|
-
if (text.length === 0)
|
|
86
|
-
return 0;
|
|
87
|
-
const charFreq = new Map();
|
|
88
|
-
for (const char of text) {
|
|
89
|
-
charFreq.set(char, (charFreq.get(char) ?? 0) + 1);
|
|
90
|
-
}
|
|
91
|
-
let entropy = 0;
|
|
92
|
-
const len = text.length;
|
|
93
|
-
for (const [, freq] of charFreq) {
|
|
94
|
-
const p = freq / len;
|
|
95
|
-
entropy -= p * Math.log2(p);
|
|
96
|
-
}
|
|
97
|
-
return entropy;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* 计算相对熵值(考虑语言特性)
|
|
101
|
-
*
|
|
102
|
-
* 返回 0-1 之间的值,越低越异常
|
|
103
|
-
*/
|
|
104
|
-
function calculateRelativeEntropy(text) {
|
|
105
|
-
if (text.length === 0)
|
|
106
|
-
return 1;
|
|
107
|
-
const entropy = calculateEntropy(text);
|
|
108
|
-
const lang = detectLanguage(text);
|
|
109
|
-
// 中文文本熵值通常在 3-4 之间,英文在 4-5 之间
|
|
110
|
-
const expectedEntropy = lang === 'zh' ? 3.5 : 4.5;
|
|
111
|
-
// 相对熵 = 实际熵 / 期望熵
|
|
112
|
-
return Math.min(1, entropy / expectedEntropy);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* 检测重复片段(优化版本 - Rabin-Karp 算法)
|
|
116
|
-
*
|
|
117
|
-
* 检测文本中的重复模式
|
|
118
|
-
*/
|
|
119
|
-
function detectRepetition(text) {
|
|
120
|
-
if (text.length < 50)
|
|
121
|
-
return 0;
|
|
122
|
-
// 对于超长文本,采样检测
|
|
123
|
-
const maxSampleSize = 10000;
|
|
124
|
-
const sampleText = text.length > maxSampleSize
|
|
125
|
-
? text.slice(0, maxSampleSize / 2) + text.slice(text.length - maxSampleSize / 2)
|
|
126
|
-
: text;
|
|
127
|
-
const windowSize = 20;
|
|
128
|
-
const seen = new Map();
|
|
129
|
-
let repeatCount = 0;
|
|
130
|
-
// 使用 Rabin-Karp 算法检测重复
|
|
131
|
-
for (let i = 0; i <= sampleText.length - windowSize; i++) {
|
|
132
|
-
const hash = simpleHash(sampleText.slice(i, i + windowSize));
|
|
133
|
-
const lastSeen = seen.get(hash);
|
|
134
|
-
if (lastSeen !== undefined && i - lastSeen < windowSize * 2) {
|
|
135
|
-
repeatCount++;
|
|
136
|
-
}
|
|
137
|
-
seen.set(hash, i);
|
|
138
|
-
}
|
|
139
|
-
// 检测模式重复(相似的句子结构)
|
|
140
|
-
const sentences = sampleText.split(/[。!?.\n]/).filter(s => s.trim().length > 10);
|
|
141
|
-
if (sentences.length > 3) {
|
|
142
|
-
const uniqueSentences = new Set(sentences.map(s => s.trim().slice(0, 30)));
|
|
143
|
-
const repetitionRatio = 1 - (uniqueSentences.size / sentences.length);
|
|
144
|
-
return Math.max(repeatCount * 0.1, repetitionRatio);
|
|
145
|
-
}
|
|
146
|
-
return Math.min(1, repeatCount * 0.1);
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* 移除 Markdown 代码块
|
|
150
|
-
*
|
|
151
|
-
* 代码块中的语法结构不应该被误判为碎片
|
|
152
|
-
*/
|
|
153
|
-
function removeCodeBlocks(text) {
|
|
154
|
-
// 移除围栏代码块 (```...```)
|
|
155
|
-
let result = text.replace(/```[\s\S]*?```/g, '');
|
|
156
|
-
// 移除行内代码 (`...`)
|
|
157
|
-
result = result.replace(/`[^`]+`/g, '');
|
|
158
|
-
return result;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* 计算语法碎片密度
|
|
162
|
-
*
|
|
163
|
-
* 每100字符的语法碎片数量
|
|
164
|
-
*/
|
|
165
|
-
function calculateSyntaxFragmentDensity(text) {
|
|
166
|
-
if (text.length === 0)
|
|
167
|
-
return 0;
|
|
168
|
-
// 移除代码块(代码块中的语法不应被检测)
|
|
169
|
-
const cleanedText = removeCodeBlocks(text);
|
|
170
|
-
// 对于超长文本,采样检测
|
|
171
|
-
const maxSampleSize = 5000;
|
|
172
|
-
const sampleText = cleanedText.length > maxSampleSize
|
|
173
|
-
? cleanedText.slice(0, maxSampleSize / 2) + cleanedText.slice(cleanedText.length - maxSampleSize / 2)
|
|
174
|
-
: cleanedText;
|
|
175
|
-
// 如果清理后的文本太短,返回 0
|
|
176
|
-
if (sampleText.length < 10)
|
|
177
|
-
return 0;
|
|
178
|
-
let fragmentCount = 0;
|
|
179
|
-
// 检测悬空的语法结构
|
|
180
|
-
const danglingPatterns = [
|
|
181
|
-
/if\s*\([^)]*$/gm, // 悬空的 if
|
|
182
|
-
/=>\s*\{[^}]*$/gm, // 悬空的箭头函数
|
|
183
|
-
/=>\s*$/gm, // 悬空的 =>
|
|
184
|
-
/function\s+\w*\s*\([^)]*\)\s*\{[^}]*$/gm, // 不完整的函数
|
|
185
|
-
/`[^`]*$/gm, // 不完整的模板字符串
|
|
186
|
-
/"[^"]*$/gm, // 不完整的字符串
|
|
187
|
-
/'[^']*$/gm, // 不完整的字符串
|
|
188
|
-
];
|
|
189
|
-
for (const pattern of danglingPatterns) {
|
|
190
|
-
const matches = sampleText.match(pattern) ?? [];
|
|
191
|
-
fragmentCount += matches.length;
|
|
192
|
-
}
|
|
193
|
-
// 检测括号不匹配
|
|
194
|
-
const openBraces = (sampleText.match(/\{/g) ?? []).length;
|
|
195
|
-
const closeBraces = (sampleText.match(/\}/g) ?? []).length;
|
|
196
|
-
const openParens = (sampleText.match(/\(/g) ?? []).length;
|
|
197
|
-
const closeParens = (sampleText.match(/\)/g) ?? []).length;
|
|
198
|
-
const openBrackets = (sampleText.match(/\[/g) ?? []).length;
|
|
199
|
-
const closeBrackets = (sampleText.match(/\]/g) ?? []).length;
|
|
200
|
-
// 括号不匹配也视为碎片
|
|
201
|
-
const braceMismatch = Math.abs(openBraces - closeBraces);
|
|
202
|
-
const parenMismatch = Math.abs(openParens - closeParens);
|
|
203
|
-
const bracketMismatch = Math.abs(openBrackets - closeBrackets);
|
|
204
|
-
fragmentCount += braceMismatch + parenMismatch + bracketMismatch;
|
|
205
|
-
// 密度 = 碎片数 / (文本长度 / 100)
|
|
206
|
-
return fragmentCount / (sampleText.length / 100);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* 检测崩溃特征
|
|
210
|
-
*
|
|
211
|
-
* 返回崩溃提示列表
|
|
212
|
-
*/
|
|
213
|
-
function detectCollapseFeatures(text) {
|
|
214
|
-
const hints = [];
|
|
215
|
-
// 对于超长文本,采样检测
|
|
216
|
-
const maxSampleSize = 5000;
|
|
217
|
-
const sampleText = text.length > maxSampleSize
|
|
218
|
-
? text.slice(0, maxSampleSize / 2) + text.slice(text.length - maxSampleSize / 2)
|
|
219
|
-
: text;
|
|
220
|
-
for (const pattern of COLLAPSE_PATTERNS) {
|
|
221
|
-
const matches = sampleText.match(pattern) ?? [];
|
|
222
|
-
if (matches.length > 0) {
|
|
223
|
-
hints.push({
|
|
224
|
-
type: 'model_collapse',
|
|
225
|
-
confidence: Math.min(1, matches.length * 0.3),
|
|
226
|
-
evidence: matches[0]?.slice(0, 100) ?? 'unknown',
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return hints;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* 计算平均句子长度
|
|
234
|
-
*/
|
|
235
|
-
function calculateAvgSentenceLength(text) {
|
|
236
|
-
const sentences = text.split(/[。!?.!?\n]/).filter(s => s.trim().length > 0);
|
|
237
|
-
if (sentences.length === 0)
|
|
238
|
-
return text.length;
|
|
239
|
-
const totalLen = sentences.reduce((sum, s) => sum + s.length, 0);
|
|
240
|
-
return totalLen / sentences.length;
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* 综合质量评分计算
|
|
244
|
-
*/
|
|
245
|
-
function calculateQualityScore(details, config) {
|
|
246
|
-
// 基础评分 = 1.0
|
|
247
|
-
let score = 1.0;
|
|
248
|
-
// 语法碎片密度惩罚
|
|
249
|
-
if (details.syntaxFragmentDensity > config.syntaxFragmentThreshold) {
|
|
250
|
-
const penalty = (details.syntaxFragmentDensity - config.syntaxFragmentThreshold) * 0.5;
|
|
251
|
-
score -= Math.min(penalty, 0.4);
|
|
252
|
-
}
|
|
253
|
-
// 熵值惩罚(使用相对熵)
|
|
254
|
-
if (details.entropy < config.entropyThreshold) {
|
|
255
|
-
const penalty = (config.entropyThreshold - details.entropy) * 0.1;
|
|
256
|
-
score -= Math.min(penalty, 0.3);
|
|
257
|
-
}
|
|
258
|
-
// 重复惩罚
|
|
259
|
-
score -= details.repetitionRatio * 0.3;
|
|
260
|
-
// 平均句子长度惩罚(异常短表示碎片化)
|
|
261
|
-
if (details.avgSentenceLength < 10 && details.avgSentenceLength > 0) {
|
|
262
|
-
score -= 0.2;
|
|
263
|
-
}
|
|
264
|
-
return Math.max(0, Math.min(1, score));
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* 验证配置参数
|
|
268
|
-
*/
|
|
269
|
-
function validateConfig(config) {
|
|
270
|
-
if (typeof config !== 'object' || config === null) {
|
|
271
|
-
return false;
|
|
272
|
-
}
|
|
273
|
-
const c = config;
|
|
274
|
-
// 检查必需字段
|
|
275
|
-
if (typeof c.enabled !== 'boolean')
|
|
276
|
-
return false;
|
|
277
|
-
if (typeof c.anomalyThreshold !== 'number')
|
|
278
|
-
return false;
|
|
279
|
-
if (typeof c.syntaxFragmentThreshold !== 'number')
|
|
280
|
-
return false;
|
|
281
|
-
if (typeof c.entropyThreshold !== 'number')
|
|
282
|
-
return false;
|
|
283
|
-
// 检查数值范围
|
|
284
|
-
if (c.anomalyThreshold < 0 || c.anomalyThreshold > 1)
|
|
285
|
-
return false;
|
|
286
|
-
if (c.syntaxFragmentThreshold < 0)
|
|
287
|
-
return false;
|
|
288
|
-
if (c.entropyThreshold < 0)
|
|
289
|
-
return false;
|
|
290
|
-
return true;
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* 创建默认结果
|
|
294
|
-
*/
|
|
295
|
-
function createDefaultResult() {
|
|
296
|
-
return {
|
|
297
|
-
isAnomalous: false,
|
|
298
|
-
qualityScore: 1.0,
|
|
299
|
-
anomalyHints: [],
|
|
300
|
-
details: {
|
|
301
|
-
syntaxFragmentDensity: 0,
|
|
302
|
-
repetitionRatio: 0,
|
|
303
|
-
entropy: 0,
|
|
304
|
-
avgSentenceLength: 0,
|
|
305
|
-
},
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
// ================================================================
|
|
309
|
-
// OutputQualityGate 实现
|
|
310
|
-
// ================================================================
|
|
311
|
-
/**
|
|
312
|
-
* 输出质量检测器
|
|
313
|
-
*/
|
|
314
|
-
export class OutputQualityGate {
|
|
315
|
-
/**
|
|
316
|
-
* 检测文本输出质量
|
|
317
|
-
*/
|
|
318
|
-
checkTextOutput(text, config) {
|
|
319
|
-
// 输入验证
|
|
320
|
-
if (typeof text !== 'string') {
|
|
321
|
-
return createDefaultResult();
|
|
322
|
-
}
|
|
323
|
-
if (!validateConfig(config)) {
|
|
324
|
-
throw new TypeError('Invalid QualityGateConfig: must have enabled (boolean), anomalyThreshold (0-1), syntaxFragmentThreshold (number), entropyThreshold (number)');
|
|
325
|
-
}
|
|
326
|
-
if (!config.enabled) {
|
|
327
|
-
return createDefaultResult();
|
|
328
|
-
}
|
|
329
|
-
// 空字符串返回默认结果
|
|
330
|
-
if (text.length === 0) {
|
|
331
|
-
return createDefaultResult();
|
|
332
|
-
}
|
|
333
|
-
// 计算各项指标
|
|
334
|
-
const syntaxFragmentDensity = calculateSyntaxFragmentDensity(text);
|
|
335
|
-
const entropy = calculateEntropy(text);
|
|
336
|
-
const repetitionRatio = detectRepetition(text);
|
|
337
|
-
const avgSentenceLength = calculateAvgSentenceLength(text);
|
|
338
|
-
const details = {
|
|
339
|
-
syntaxFragmentDensity,
|
|
340
|
-
entropy,
|
|
341
|
-
repetitionRatio,
|
|
342
|
-
avgSentenceLength,
|
|
343
|
-
};
|
|
344
|
-
// 检测崩溃特征
|
|
345
|
-
const collapseHints = detectCollapseFeatures(text);
|
|
346
|
-
// 综合评分
|
|
347
|
-
const qualityScore = calculateQualityScore(details, config);
|
|
348
|
-
// 判断是否异常
|
|
349
|
-
const isAnomalous = qualityScore < config.anomalyThreshold || collapseHints.length > 0;
|
|
350
|
-
// 构建异常提示
|
|
351
|
-
const anomalyHints = [...collapseHints];
|
|
352
|
-
// 根据各项指标添加提示
|
|
353
|
-
if (syntaxFragmentDensity > config.syntaxFragmentThreshold) {
|
|
354
|
-
anomalyHints.push({
|
|
355
|
-
type: 'model_collapse',
|
|
356
|
-
confidence: Math.min(1, syntaxFragmentDensity * 0.5),
|
|
357
|
-
evidence: `High syntax fragment density: ${syntaxFragmentDensity.toFixed(2)}`,
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
if (entropy < config.entropyThreshold) {
|
|
361
|
-
anomalyHints.push({
|
|
362
|
-
type: 'model_collapse',
|
|
363
|
-
confidence: Math.min(1, (config.entropyThreshold - entropy) * 0.3),
|
|
364
|
-
evidence: `Low entropy: ${entropy.toFixed(2)}`,
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
if (repetitionRatio > 0.3) {
|
|
368
|
-
anomalyHints.push({
|
|
369
|
-
type: 'repetition_loop',
|
|
370
|
-
confidence: Math.min(1, repetitionRatio),
|
|
371
|
-
evidence: `High repetition ratio: ${repetitionRatio.toFixed(2)}`,
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
return {
|
|
375
|
-
isAnomalous,
|
|
376
|
-
qualityScore,
|
|
377
|
-
anomalyHints,
|
|
378
|
-
details,
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* 检测流式 chunk 质量(增量检测)
|
|
383
|
-
*
|
|
384
|
-
* 当累积文本较长时开始检测,避免误判正常输出
|
|
385
|
-
*/
|
|
386
|
-
checkStreamChunk(chunk, accumulated, config) {
|
|
387
|
-
// 输入验证
|
|
388
|
-
if (typeof chunk !== 'string' || typeof accumulated !== 'string') {
|
|
389
|
-
return createDefaultResult();
|
|
390
|
-
}
|
|
391
|
-
if (!validateConfig(config)) {
|
|
392
|
-
throw new TypeError('Invalid QualityGateConfig');
|
|
393
|
-
}
|
|
394
|
-
// 累积文本足够长才开始检测(至少500字符)
|
|
395
|
-
if (accumulated.length < 500) {
|
|
396
|
-
return createDefaultResult();
|
|
397
|
-
}
|
|
398
|
-
return this.checkTextOutput(accumulated, config);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
// ================================================================
|
|
402
|
-
// 工厂函数
|
|
403
|
-
// ================================================================
|
|
404
|
-
/**
|
|
405
|
-
* 创建输出质量检测器
|
|
406
|
-
*/
|
|
407
|
-
export function createOutputQualityGate() {
|
|
408
|
-
return new OutputQualityGate();
|
|
409
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Output Quality — 输出质量检测
|
|
3
|
-
*
|
|
4
|
-
* 从旧 loop/ 迁移到 Harness 层。
|
|
5
|
-
* 通过 Agent 的 afterToolCall 回调槽集成。
|
|
6
|
-
*/
|
|
7
|
-
export { OutputQualityGate, createOutputQualityGate } from './gate.js';
|
|
8
|
-
export { OutputErrorClassifier, createOutputErrorClassifier } from './classifier.js';
|
|
9
|
-
export type { QualityCheckResult, QualityGateConfig, AnomalyHint, QualityCheckDetails, AnomalyType, AnomalySeverity, RecoveryStrategyType, ErrorClassification, RecoveryConfig, RecoveryResult, } from './types.js';
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Output Quality — 输出质量检测
|
|
3
|
-
*
|
|
4
|
-
* 从旧 loop/ 迁移到 Harness 层。
|
|
5
|
-
* 通过 Agent 的 afterToolCall 回调槽集成。
|
|
6
|
-
*/
|
|
7
|
-
export { OutputQualityGate, createOutputQualityGate } from './gate.js';
|
|
8
|
-
export { OutputErrorClassifier, createOutputErrorClassifier } from './classifier.js';
|