@octopi-agent/engine 0.65.0 → 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 +285 -9
- package/dist/config-schema/engine.js +35 -3
- 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 +87 -1
- package/dist/config-schema/substrate.js +114 -0
- package/dist/config-schema/subsystems.d.ts +1 -1
- package/dist/config-schema/subsystems.js +2 -1
- package/dist/config.d.ts +249 -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 +48 -4
- package/dist/harness/agent/builder.js +206 -55
- package/dist/harness/agent/config-bridge.d.ts +33 -25
- package/dist/harness/agent/config-bridge.js +83 -280
- 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/gate.js +2 -2
- package/dist/harness/context/capabilities/summary/policies.js +5 -5
- package/dist/harness/context/capabilities/summary/registry.js +39 -24
- 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-health.js +3 -2
- 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 +32 -15
- package/dist/harness/context/layers.js +87 -71
- 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 +33 -0
- package/dist/harness/context/system-prompt-assembler.js +78 -7
- 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 +4 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/index.js +3 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.d.ts +56 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.js +281 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +98 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +250 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/stream.d.ts +124 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/stream.js +372 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.d.ts +118 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.js +8 -0
- package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.js +1 -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 +278 -82
- 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.d.ts +11 -1
- package/dist/harness/extension/plugin-ecosystem/tools/memory.js +39 -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 +18 -15
- package/dist/harness/index.js +11 -7
- 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 +8 -8
- package/dist/harness/knowledge/retriever.js +1 -1
- package/dist/harness/knowledge/source-store.d.ts +17 -2
- package/dist/harness/knowledge/source-store.js +6 -5
- 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/cognition-decay.d.ts +36 -0
- package/dist/harness/memory/cognition-decay.js +55 -0
- package/dist/harness/memory/cognition-gates.d.ts +60 -0
- package/dist/harness/memory/cognition-gates.js +191 -0
- package/dist/harness/memory/cognition-trigger.d.ts +30 -0
- package/dist/harness/memory/cognition-trigger.js +41 -0
- package/dist/harness/memory/cognition-types.d.ts +1 -1
- package/dist/harness/memory/cognition.d.ts +40 -8
- package/dist/harness/memory/cognition.js +432 -79
- package/dist/harness/memory/conceptualizer.d.ts +66 -0
- package/dist/harness/memory/conceptualizer.js +246 -0
- package/dist/harness/memory/index.d.ts +17 -2
- package/dist/harness/memory/index.js +12 -1
- package/dist/harness/memory/sqlite/agent-db.d.ts +9 -0
- package/dist/harness/memory/sqlite/agent-db.js +120 -15
- package/dist/harness/memory/sqlite/cognition-store.d.ts +55 -16
- package/dist/harness/memory/sqlite/cognition-store.js +591 -144
- 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.d.ts +33 -14
- package/dist/harness/memory/sqlite/wisdom-store.js +257 -44
- 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 +343 -16
- package/dist/harness/memory/types.js +49 -0
- package/dist/harness/memory/wisdom-formation.d.ts +101 -0
- package/dist/harness/memory/wisdom-formation.js +304 -0
- package/dist/harness/memory/wisdom-gates.d.ts +50 -0
- package/dist/harness/memory/wisdom-gates.js +204 -0
- package/dist/harness/memory/wisdom-policy.d.ts +107 -0
- package/dist/harness/memory/wisdom-policy.js +220 -0
- package/dist/harness/memory/wisdom-types.d.ts +3 -2
- package/dist/harness/memory/wisdom-types.js +2 -2
- package/dist/harness/memory/wisdom.d.ts +27 -0
- package/dist/harness/memory/wisdom.js +181 -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 +27 -0
- package/dist/harness/run/agent/agent.js +45 -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 +116 -20
- 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 +557 -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/index.d.ts +1 -0
- package/dist/harness/session/index.js +1 -0
- 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/title.d.ts +86 -0
- package/dist/harness/session/title.js +268 -0
- package/dist/harness/session/types.d.ts +35 -0
- package/dist/harness/shared/events/agent-event-map.d.ts +21 -0
- package/dist/harness/shared/events/agent-event-map.js +1 -0
- package/dist/harness/shared/types/agent-definition.d.ts +23 -0
- package/dist/index.d.ts +25 -9
- package/dist/index.js +11 -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 -0
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +50 -0
- package/dist/subsystems/cognition-steward/conceptualize/handler.d.ts +9 -0
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +125 -0
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +6 -0
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +54 -0
- package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +12 -0
- package/dist/subsystems/cognition-steward/consolidate/handler.js +93 -0
- package/dist/subsystems/memory-steward/backfill/handler.js +9 -0
- package/dist/subsystems/wisdom-steward/evaluate/config.yaml +50 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.d.ts +18 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.js +91 -0
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +42 -0
- package/dist/subsystems/wisdom-steward/formation/config.yaml +54 -0
- package/dist/subsystems/wisdom-steward/formation/handler.d.ts +18 -0
- package/dist/subsystems/wisdom-steward/formation/handler.js +212 -0
- package/dist/subsystems/wisdom-steward/govern/config.yaml +55 -0
- package/dist/subsystems/wisdom-steward/govern/handler.d.ts +17 -0
- package/dist/subsystems/wisdom-steward/govern/handler.js +68 -0
- 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,188 +1,635 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SqliteConceptGraph — SQLite
|
|
2
|
+
* SqliteConceptGraph — SQLite 认知图谱(生产路径)
|
|
3
|
+
*
|
|
4
|
+
* 契约见 arch/cognition-graph-formation.md:
|
|
5
|
+
* 结构同一性合并、持证边、衰减、扩散激活、merge_candidates、causal_candidate。
|
|
3
6
|
*
|
|
4
7
|
* @module
|
|
5
8
|
*/
|
|
9
|
+
import { counterEvidenceWeaken, DEFAULT_EDGE_DECAY_FLOOR, DEFAULT_RETRIEVE_STATUSES, hebbianStrengthen, nextEdgeStrength, } from '../cognition-decay.js';
|
|
10
|
+
import { evaluateConceptGate, licenseEdge, normalizeForCue } from '../cognition-gates.js';
|
|
6
11
|
import { AgentDatabase } from './agent-db.js';
|
|
7
12
|
import { cosineDistance, parseEmbedding, serializeEmbedding } from './vector-search.js';
|
|
13
|
+
function daysBetween(a, b) {
|
|
14
|
+
return Math.max(0, (b - a) / 86_400_000);
|
|
15
|
+
}
|
|
16
|
+
function normName(name) {
|
|
17
|
+
return name.trim().toLowerCase();
|
|
18
|
+
}
|
|
19
|
+
/** 转义 LIKE 通配符(配合 ESCAPE '\') */
|
|
20
|
+
function escapeLike(s) {
|
|
21
|
+
return s.replace(/[\\%_]/g, (c) => `\\${c}`);
|
|
22
|
+
}
|
|
8
23
|
export class SqliteConceptGraph {
|
|
9
24
|
db;
|
|
10
25
|
embedding;
|
|
11
26
|
mergeThreshold;
|
|
27
|
+
candidateBandHi;
|
|
28
|
+
stopNodes;
|
|
29
|
+
stopEdges;
|
|
30
|
+
decayFloor;
|
|
31
|
+
causesMinIndependentMemories;
|
|
12
32
|
constructor(db, options) {
|
|
13
33
|
this.db = db;
|
|
14
34
|
this.embedding = options?.embeddingProvider ?? null;
|
|
15
|
-
this.mergeThreshold = options?.mergeThreshold ?? 0.
|
|
35
|
+
this.mergeThreshold = options?.mergeThreshold ?? 0.2;
|
|
36
|
+
this.candidateBandHi = options?.candidateBandHi ?? 0.35;
|
|
37
|
+
this.stopNodes = options?.stopNodes ?? 10_000;
|
|
38
|
+
this.stopEdges = options?.stopEdges ?? 40_000;
|
|
39
|
+
this.decayFloor = options?.decayFloor ?? DEFAULT_EDGE_DECAY_FLOOR;
|
|
40
|
+
this.causesMinIndependentMemories = options?.causesMinIndependentMemories ?? 2;
|
|
16
41
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
this.db.raw.prepare('UPDATE concepts SET frequency = frequency + 1, updated_at = ? WHERE id = ?').run(Date.now(), existing.id);
|
|
22
|
-
return existing.id;
|
|
23
|
-
}
|
|
24
|
-
// 2. 语义匹配(需要 embedding)
|
|
25
|
-
if (this.embedding) {
|
|
26
|
-
const vec = await this.embedding.embed(concept.name);
|
|
27
|
-
const similarId = await this.findSimilarConcept(vec);
|
|
28
|
-
if (similarId) {
|
|
29
|
-
// 合并:更新 frequency
|
|
30
|
-
this.db.raw.prepare('UPDATE concepts SET frequency = frequency + 1, updated_at = ? WHERE id = ?').run(Date.now(), similarId);
|
|
31
|
-
return similarId;
|
|
32
|
-
}
|
|
33
|
-
// 新概念(带 embedding)
|
|
34
|
-
const id = AgentDatabase.generateId('cpt');
|
|
35
|
-
const now = Date.now();
|
|
36
|
-
this.db.raw.prepare(`
|
|
37
|
-
INSERT INTO concepts (id, name, description, frequency, memory_ids, properties, embedding, created_at, updated_at)
|
|
38
|
-
VALUES (?, ?, ?, 1, '[]', '[]', ?, ?, ?)
|
|
39
|
-
`).run(id, concept.name, concept.description ?? null, serializeEmbedding(vec), now, now);
|
|
40
|
-
return id;
|
|
41
|
-
}
|
|
42
|
-
// 新概念(无 embedding)
|
|
43
|
-
const id = AgentDatabase.generateId('cpt');
|
|
44
|
-
const now = Date.now();
|
|
45
|
-
this.db.raw.prepare(`
|
|
46
|
-
INSERT INTO concepts (id, name, description, frequency, memory_ids, properties, embedding, created_at, updated_at)
|
|
47
|
-
VALUES (?, ?, ?, 1, '[]', '[]', NULL, ?, ?)
|
|
48
|
-
`).run(id, concept.name, concept.description ?? null, now, now);
|
|
49
|
-
return id;
|
|
42
|
+
get capacityStop() {
|
|
43
|
+
const n = this.db.raw.prepare('SELECT COUNT(*) AS c FROM concepts').get().c;
|
|
44
|
+
const e = this.db.raw.prepare('SELECT COUNT(*) AS c FROM concept_edges').get().c;
|
|
45
|
+
return n >= this.stopNodes || e >= this.stopEdges;
|
|
50
46
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
WITH RECURSIVE graph_walk(node_id, depth) AS (
|
|
66
|
-
SELECT ?, 0
|
|
67
|
-
UNION
|
|
68
|
-
SELECT DISTINCT
|
|
69
|
-
CASE WHEN e.source_id = gw.node_id THEN e.target_id ELSE e.source_id END,
|
|
70
|
-
gw.depth + 1
|
|
71
|
-
FROM graph_walk gw
|
|
72
|
-
JOIN concept_edges e ON (e.source_id = gw.node_id OR e.target_id = gw.node_id)
|
|
73
|
-
WHERE gw.depth < ?
|
|
74
|
-
)
|
|
75
|
-
SELECT DISTINCT c.*, gw.depth
|
|
76
|
-
FROM graph_walk gw
|
|
77
|
-
JOIN concepts c ON c.id = gw.node_id
|
|
78
|
-
ORDER BY gw.depth, c.frequency DESC
|
|
79
|
-
LIMIT 20
|
|
80
|
-
`).all(start.id, depth);
|
|
81
|
-
const nodes = rows.map(r => this.rowToNode(r));
|
|
82
|
-
const nodeIds = new Set(nodes.map(n => n.id));
|
|
83
|
-
// 获取相关的边
|
|
84
|
-
const allEdges = this.db.raw.prepare(`
|
|
85
|
-
SELECT * FROM concept_edges
|
|
86
|
-
WHERE source_id IN (${nodes.map(() => '?').join(',')})
|
|
87
|
-
OR target_id IN (${nodes.map(() => '?').join(',')})
|
|
88
|
-
`).all(...nodeIds, ...nodeIds);
|
|
89
|
-
const edges = allEdges
|
|
90
|
-
.filter((e) => nodeIds.has(e.source_id) && nodeIds.has(e.target_id))
|
|
91
|
-
.map((e) => this.rowToEdge(e));
|
|
92
|
-
return { nodes, edges };
|
|
47
|
+
rowToNode(row) {
|
|
48
|
+
return {
|
|
49
|
+
id: row.id,
|
|
50
|
+
name: row.name,
|
|
51
|
+
kind: row.kind ?? 'construct',
|
|
52
|
+
description: row.description ?? undefined,
|
|
53
|
+
frequency: row.frequency,
|
|
54
|
+
memoryIds: safeJsonArray(row.memory_ids),
|
|
55
|
+
domain: safeJsonArray(row.domain),
|
|
56
|
+
status: row.status ?? 'shadow',
|
|
57
|
+
embedding: row.embedding ? parseEmbedding(row.embedding) : null,
|
|
58
|
+
createdAt: row.created_at,
|
|
59
|
+
updatedAt: row.updated_at,
|
|
60
|
+
};
|
|
93
61
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
62
|
+
rowToEdge(row) {
|
|
63
|
+
let basis = {
|
|
64
|
+
memoryIds: [],
|
|
65
|
+
cue: '',
|
|
66
|
+
evidenceClass: 'cooccur',
|
|
67
|
+
licensedAt: row.created_at,
|
|
68
|
+
};
|
|
69
|
+
try {
|
|
70
|
+
const parsed = JSON.parse(row.basis);
|
|
71
|
+
if (parsed && typeof parsed === 'object') {
|
|
72
|
+
basis = {
|
|
73
|
+
memoryIds: Array.isArray(parsed.memoryIds) ? parsed.memoryIds : [],
|
|
74
|
+
cue: typeof parsed.cue === 'string' ? parsed.cue : '',
|
|
75
|
+
evidenceClass: parsed.evidenceClass ?? 'cooccur',
|
|
76
|
+
licensedAt: typeof parsed.licensedAt === 'number' ? parsed.licensedAt : row.created_at,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// basis 解析失败用默认弱证件
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
id: row.id,
|
|
85
|
+
sourceId: row.source_id,
|
|
86
|
+
targetId: row.target_id,
|
|
87
|
+
relationType: row.relation_type ?? 'related',
|
|
88
|
+
strength: row.strength,
|
|
89
|
+
description: row.description ?? undefined,
|
|
90
|
+
status: row.status ?? 'shadow',
|
|
91
|
+
basis,
|
|
92
|
+
createdAt: row.created_at,
|
|
93
|
+
updatedAt: row.updated_at,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async admitConcept(input) {
|
|
97
|
+
const gate = evaluateConceptGate(input, { capacityAdmitStop: this.capacityStop });
|
|
98
|
+
if (!gate.ok) {
|
|
99
|
+
return { action: 'rejected', reason: gate.reason, message: gate.message };
|
|
100
|
+
}
|
|
101
|
+
const nameKey = normName(input.name);
|
|
102
|
+
const existing = this.db.raw
|
|
103
|
+
.prepare('SELECT * FROM concepts WHERE LOWER(name) = ?')
|
|
104
|
+
.all(nameKey);
|
|
105
|
+
// 同名多义项:指纹兼容则合并,冲突则新建 + merge_candidate
|
|
106
|
+
const conflicts = [];
|
|
107
|
+
for (const row of existing) {
|
|
108
|
+
const node = this.rowToNode(row);
|
|
109
|
+
const conflict = this.fingerprintConflict(node, input);
|
|
110
|
+
if (conflict) {
|
|
111
|
+
conflicts.push({ node, reason: conflict });
|
|
101
112
|
continue;
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
113
|
+
}
|
|
114
|
+
const memIds = new Set([...node.memoryIds, ...(input.memoryIds ?? [])]);
|
|
115
|
+
this.db.raw
|
|
116
|
+
.prepare('UPDATE concepts SET frequency = frequency + 1, memory_ids = ?, description = COALESCE(description, ?), updated_at = ? WHERE id = ?')
|
|
117
|
+
.run(JSON.stringify([...memIds]), input.description ?? null, Date.now(), node.id);
|
|
118
|
+
return { action: 'merged', id: node.id, reason: 'ok' };
|
|
119
|
+
}
|
|
120
|
+
// embedding 近邻
|
|
121
|
+
if (this.embedding) {
|
|
122
|
+
const vec = await this.embedding.embed(input.name);
|
|
123
|
+
const near = await this.findSimilarConcept(vec);
|
|
124
|
+
if (near) {
|
|
125
|
+
const conflict = this.fingerprintConflict(near.node, input);
|
|
126
|
+
if (!conflict && near.distance <= this.mergeThreshold) {
|
|
127
|
+
const memIds = new Set([...near.node.memoryIds, ...(input.memoryIds ?? [])]);
|
|
128
|
+
this.db.raw
|
|
129
|
+
.prepare('UPDATE concepts SET frequency = frequency + 1, memory_ids = ?, updated_at = ? WHERE id = ?')
|
|
130
|
+
.run(JSON.stringify([...memIds]), Date.now(), near.node.id);
|
|
131
|
+
return { action: 'merged', id: near.node.id, reason: 'ok' };
|
|
111
132
|
}
|
|
133
|
+
// 近阈带或指纹冲突 → candidate
|
|
134
|
+
const id = this.insertConcept(input, vec);
|
|
135
|
+
const candidateId = this.insertMergeCandidate(near.node.id, id, conflict ?? 'embedding_band', near.distance, conflict ? [conflict] : []);
|
|
136
|
+
return {
|
|
137
|
+
action: 'merge_candidate',
|
|
138
|
+
id,
|
|
139
|
+
candidateId,
|
|
140
|
+
reason: conflict ? 'fingerprint_conflict' : 'ok',
|
|
141
|
+
};
|
|
112
142
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
for (let j = i + 1; j < conceptIds.length; j++) {
|
|
118
|
-
await this.addEdge({
|
|
119
|
-
sourceId: conceptIds[i],
|
|
120
|
-
targetId: conceptIds[j],
|
|
121
|
-
relationType: 'related',
|
|
122
|
-
strength: 0.5,
|
|
123
|
-
});
|
|
143
|
+
const id = this.insertConcept(input, vec);
|
|
144
|
+
if (conflicts.length) {
|
|
145
|
+
const candidateId = this.insertMergeCandidate(conflicts[0].node.id, id, conflicts[0].reason, null, [conflicts[0].reason]);
|
|
146
|
+
return { action: 'merge_candidate', id, candidateId, reason: 'fingerprint_conflict' };
|
|
124
147
|
}
|
|
148
|
+
return { action: 'created', id, reason: 'ok' };
|
|
149
|
+
}
|
|
150
|
+
const id = this.insertConcept(input, null);
|
|
151
|
+
if (conflicts.length) {
|
|
152
|
+
const candidateId = this.insertMergeCandidate(conflicts[0].node.id, id, conflicts[0].reason, null, [conflicts[0].reason]);
|
|
153
|
+
return { action: 'merge_candidate', id, candidateId, reason: 'fingerprint_conflict' };
|
|
125
154
|
}
|
|
155
|
+
return { action: 'created', id, reason: 'ok' };
|
|
126
156
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
157
|
+
fingerprintConflict(existing, input) {
|
|
158
|
+
if (existing.kind !== input.kind && existing.kind !== 'construct' && input.kind !== 'construct') {
|
|
159
|
+
return `kind:${existing.kind}≠${input.kind}`;
|
|
160
|
+
}
|
|
161
|
+
if (existing.kind !== input.kind)
|
|
162
|
+
return `kind:${existing.kind}≠${input.kind}`;
|
|
163
|
+
const a = new Set(existing.domain);
|
|
164
|
+
const b = new Set(input.domain ?? []);
|
|
165
|
+
if (a.size > 0 && b.size > 0) {
|
|
166
|
+
const overlap = [...b].some((d) => a.has(d));
|
|
167
|
+
if (!overlap)
|
|
168
|
+
return 'domain_disjoint';
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
insertConcept(input, vec) {
|
|
173
|
+
const id = AgentDatabase.generateId('cpt');
|
|
174
|
+
const now = Date.now();
|
|
175
|
+
this.db.raw
|
|
176
|
+
.prepare(`INSERT INTO concepts (id, name, kind, description, frequency, memory_ids, domain, status, embedding, created_at, updated_at)
|
|
177
|
+
VALUES (?, ?, ?, ?, 1, ?, ?, 'shadow', ?, ?, ?)`)
|
|
178
|
+
.run(id, input.name.trim(), input.kind, input.description ?? null, JSON.stringify(input.memoryIds ?? []), JSON.stringify(input.domain ?? []), vec ? serializeEmbedding(vec) : null, now, now);
|
|
179
|
+
return id;
|
|
180
|
+
}
|
|
181
|
+
insertMergeCandidate(leftId, rightId, reason, distance, fingerprintDiff) {
|
|
182
|
+
const id = AgentDatabase.generateId('mrg');
|
|
183
|
+
this.db.raw
|
|
184
|
+
.prepare(`INSERT INTO concept_merge_candidates
|
|
185
|
+
(id, left_id, right_id, reason, distance, fingerprint_diff, status, created_at)
|
|
186
|
+
VALUES (?, ?, ?, ?, ?, ?, 'open', ?)`)
|
|
187
|
+
.run(id, leftId, rightId, reason, distance, JSON.stringify(fingerprintDiff), Date.now());
|
|
188
|
+
return id;
|
|
133
189
|
}
|
|
134
|
-
/**
|
|
135
|
-
* 查找语义相似的概念(用于去重)
|
|
136
|
-
*/
|
|
137
190
|
async findSimilarConcept(embedding) {
|
|
138
|
-
const rows = this.db.raw
|
|
139
|
-
|
|
140
|
-
|
|
191
|
+
const rows = this.db.raw
|
|
192
|
+
.prepare('SELECT * FROM concepts WHERE embedding IS NOT NULL')
|
|
193
|
+
.all();
|
|
194
|
+
let best = null;
|
|
141
195
|
for (const row of rows) {
|
|
142
196
|
const vec = parseEmbedding(row.embedding);
|
|
143
197
|
if (!vec)
|
|
144
198
|
continue;
|
|
145
199
|
const distance = cosineDistance(embedding, vec);
|
|
146
|
-
if (distance <
|
|
147
|
-
|
|
148
|
-
bestId = row.id;
|
|
200
|
+
if (!best || distance < best.distance) {
|
|
201
|
+
best = { node: this.rowToNode(row), distance };
|
|
149
202
|
}
|
|
150
203
|
}
|
|
151
|
-
if (
|
|
152
|
-
return
|
|
153
|
-
}
|
|
204
|
+
if (best && best.distance <= this.candidateBandHi)
|
|
205
|
+
return best;
|
|
154
206
|
return null;
|
|
155
207
|
}
|
|
156
|
-
/**
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
208
|
+
/** 同 pair(无向)已有 memoryIds:边 basis + causal_candidate */
|
|
209
|
+
collectPairMemoryIds(sourceId, targetId) {
|
|
210
|
+
const ids = new Set();
|
|
211
|
+
const edges = this.db.raw
|
|
212
|
+
.prepare(`SELECT basis FROM concept_edges
|
|
213
|
+
WHERE (source_id = ? AND target_id = ?) OR (source_id = ? AND target_id = ?)`)
|
|
214
|
+
.all(sourceId, targetId, targetId, sourceId);
|
|
215
|
+
for (const e of edges) {
|
|
216
|
+
try {
|
|
217
|
+
const b = JSON.parse(e.basis);
|
|
218
|
+
for (const mid of b.memoryIds ?? [])
|
|
219
|
+
ids.add(mid);
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
// basis 解析失败不贡献证据
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const aux = this.db.raw
|
|
226
|
+
.prepare(`SELECT memory_ids FROM concept_edges_aux
|
|
227
|
+
WHERE edge_key IN (?, ?)`)
|
|
228
|
+
.all(`${sourceId}->${targetId}`, `${targetId}->${sourceId}`);
|
|
229
|
+
for (const a of aux) {
|
|
230
|
+
for (const mid of safeJsonArray(a.memory_ids))
|
|
231
|
+
ids.add(mid);
|
|
232
|
+
}
|
|
233
|
+
return [...ids];
|
|
234
|
+
}
|
|
235
|
+
async admitEdge(input) {
|
|
236
|
+
// 持证前累积同 pair 已有 memoryIds(跨命题多证据闭环)
|
|
237
|
+
const priorIds = this.collectPairMemoryIds(input.sourceId, input.targetId);
|
|
238
|
+
const memoryIds = [...new Set([...priorIds, ...(input.basis.memoryIds ?? [])])];
|
|
239
|
+
const license = licenseEdge({
|
|
240
|
+
relationType: input.relationType,
|
|
241
|
+
evidenceClass: input.basis.evidenceClass,
|
|
242
|
+
cue: input.basis.cue,
|
|
243
|
+
evidenceText: input.evidenceText,
|
|
244
|
+
memoryIds,
|
|
245
|
+
causesMinIndependentMemories: this.causesMinIndependentMemories,
|
|
246
|
+
});
|
|
247
|
+
if (license.status === 'rejected') {
|
|
248
|
+
return { action: 'rejected', reason: license.reason, message: license.message };
|
|
249
|
+
}
|
|
250
|
+
if (license.status === 'candidate_only') {
|
|
251
|
+
await this.addCausalCandidate({
|
|
252
|
+
sourceId: input.sourceId,
|
|
253
|
+
targetId: input.targetId,
|
|
254
|
+
memoryIds: input.basis.memoryIds,
|
|
255
|
+
cue: input.basis.cue,
|
|
256
|
+
note: license.message,
|
|
257
|
+
});
|
|
258
|
+
return {
|
|
259
|
+
action: 'candidate_only',
|
|
260
|
+
reason: license.reason,
|
|
261
|
+
message: license.message,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
if (!input.sourceId || !input.targetId) {
|
|
265
|
+
return { action: 'rejected', reason: 'invalid_relation', message: 'endpoint id missing' };
|
|
266
|
+
}
|
|
267
|
+
// 端点存在性
|
|
268
|
+
const src = this.db.raw.prepare('SELECT id FROM concepts WHERE id = ?').get(input.sourceId);
|
|
269
|
+
const tgt = this.db.raw.prepare('SELECT id FROM concepts WHERE id = ?').get(input.targetId);
|
|
270
|
+
if (!src || !tgt) {
|
|
271
|
+
return { action: 'rejected', reason: 'invalid_relation', message: 'endpoint missing' };
|
|
272
|
+
}
|
|
273
|
+
const relationType = license.relationType;
|
|
274
|
+
const status = license.status === 'active' ? 'active' : 'shadow';
|
|
275
|
+
const now = Date.now();
|
|
276
|
+
const basis = {
|
|
277
|
+
memoryIds: [...new Set(input.basis.memoryIds ?? [])],
|
|
278
|
+
cue: input.basis.cue ?? '',
|
|
279
|
+
evidenceClass: input.basis.evidenceClass,
|
|
280
|
+
licensedAt: input.basis.licensedAt ?? now,
|
|
281
|
+
};
|
|
282
|
+
const existing = this.db.raw
|
|
283
|
+
.prepare('SELECT * FROM concept_edges WHERE source_id = ? AND target_id = ? AND relation_type = ?')
|
|
284
|
+
.get(input.sourceId, input.targetId, relationType);
|
|
285
|
+
if (existing) {
|
|
286
|
+
const edge = this.rowToEdge(existing);
|
|
287
|
+
const memIds = new Set([...edge.basis.memoryIds, ...basis.memoryIds]);
|
|
288
|
+
const mergedBasis = { ...edge.basis, memoryIds: [...memIds], cue: basis.cue || edge.basis.cue };
|
|
289
|
+
this.db.raw
|
|
290
|
+
.prepare('UPDATE concept_edges SET strength = MAX(strength, ?), basis = ?, status = CASE WHEN ? = \'active\' THEN \'active\' ELSE status END, updated_at = ? WHERE id = ?')
|
|
291
|
+
.run(input.strength, JSON.stringify(mergedBasis), status, now, existing.id);
|
|
292
|
+
return {
|
|
293
|
+
action: license.status === 'demoted' ? 'demoted' : status,
|
|
294
|
+
edgeId: existing.id,
|
|
295
|
+
relationType,
|
|
296
|
+
reason: license.reason,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
const edgeId = AgentDatabase.generateId('edge');
|
|
300
|
+
this.db.raw
|
|
301
|
+
.prepare(`INSERT INTO concept_edges
|
|
302
|
+
(id, source_id, target_id, relation_type, strength, description, status, basis, created_at, updated_at)
|
|
303
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
304
|
+
.run(edgeId, input.sourceId, input.targetId, relationType, Math.min(1, Math.max(0, input.strength)), input.description ?? null, status, JSON.stringify(basis), now, now);
|
|
305
|
+
return {
|
|
306
|
+
action: license.status === 'demoted' ? 'demoted' : status,
|
|
307
|
+
edgeId,
|
|
308
|
+
relationType,
|
|
309
|
+
reason: license.reason,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async addCausalCandidate(input) {
|
|
313
|
+
const edgeKey = `${input.sourceId}->${input.targetId}`;
|
|
314
|
+
this.db.raw
|
|
315
|
+
.prepare(`INSERT INTO concept_edges_aux (id, edge_key, kind, memory_ids, cue, note, created_at)
|
|
316
|
+
VALUES (?, ?, 'causal_candidate', ?, ?, ?, ?)`)
|
|
317
|
+
.run(AgentDatabase.generateId('causal'), edgeKey, JSON.stringify(input.memoryIds ?? []), normalizeForCue(input.cue), input.note ?? null, Date.now());
|
|
318
|
+
}
|
|
319
|
+
async listMergeCandidates(limit = 50) {
|
|
320
|
+
const rows = this.db.raw
|
|
321
|
+
.prepare(`SELECT * FROM concept_merge_candidates WHERE status = 'open' ORDER BY created_at DESC LIMIT ?`)
|
|
322
|
+
.all(limit);
|
|
323
|
+
return rows.map((r) => ({
|
|
324
|
+
id: r.id,
|
|
325
|
+
leftId: r.left_id,
|
|
326
|
+
rightId: r.right_id,
|
|
327
|
+
reason: r.reason,
|
|
328
|
+
distance: r.distance,
|
|
329
|
+
fingerprintDiff: safeJsonArray(r.fingerprint_diff),
|
|
330
|
+
status: r.status,
|
|
331
|
+
createdAt: r.created_at,
|
|
332
|
+
resolvedAt: r.resolved_at ?? undefined,
|
|
333
|
+
resolvedAction: r.resolved_action ?? undefined,
|
|
334
|
+
}));
|
|
335
|
+
}
|
|
336
|
+
async resolveMerge(id, action) {
|
|
337
|
+
const row = this.db.raw
|
|
338
|
+
.prepare('SELECT * FROM concept_merge_candidates WHERE id = ?')
|
|
339
|
+
.get(id);
|
|
161
340
|
if (!row)
|
|
162
341
|
return;
|
|
163
|
-
const
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
.run(
|
|
342
|
+
const status = action === 'merge' ? 'merged' : action === 'keep_split' ? 'kept_split' : 'dropped';
|
|
343
|
+
if (action !== 'merge') {
|
|
344
|
+
this.db.raw
|
|
345
|
+
.prepare('UPDATE concept_merge_candidates SET status = ?, resolved_at = ?, resolved_action = ? WHERE id = ?')
|
|
346
|
+
.run(status, Date.now(), action, id);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const left = this.db.raw.prepare('SELECT * FROM concepts WHERE id = ?').get(row.left_id);
|
|
350
|
+
let right = this.db.raw.prepare('SELECT * FROM concepts WHERE id = ?').get(row.right_id);
|
|
351
|
+
if (!right && row.right_id.startsWith('pending:')) {
|
|
352
|
+
const pendingName = row.right_id.slice('pending:'.length).toLowerCase();
|
|
353
|
+
const candidates = this.db.raw
|
|
354
|
+
.prepare('SELECT * FROM concepts WHERE LOWER(name) = ?')
|
|
355
|
+
.all(pendingName);
|
|
356
|
+
// 同名多义项:取非 left 的第一个
|
|
357
|
+
right = candidates.find((c) => c.id !== row.left_id);
|
|
358
|
+
}
|
|
359
|
+
if (!left || !right || left.id === right.id) {
|
|
360
|
+
this.db.raw
|
|
361
|
+
.prepare('UPDATE concept_merge_candidates SET status = ?, resolved_at = ?, resolved_action = ? WHERE id = ?')
|
|
362
|
+
.run('dropped', Date.now(), 'drop_missing_endpoint', id);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const db = this.db.raw;
|
|
366
|
+
db.exec('BEGIN');
|
|
367
|
+
try {
|
|
368
|
+
const memIds = new Set([
|
|
369
|
+
...safeJsonArray(left.memory_ids),
|
|
370
|
+
...safeJsonArray(right.memory_ids),
|
|
371
|
+
]);
|
|
372
|
+
db.prepare('UPDATE concepts SET frequency = frequency + ?, memory_ids = ?, updated_at = ? WHERE id = ?').run(right.frequency, JSON.stringify([...memIds]), Date.now(), left.id);
|
|
373
|
+
// 迁边前:丢掉会变自环的边
|
|
374
|
+
db.prepare('DELETE FROM concept_edges WHERE (source_id = ? AND target_id = ?) OR (source_id = ? AND target_id = ?)')
|
|
375
|
+
.run(left.id, right.id, right.id, left.id);
|
|
376
|
+
// 改写端点(UNIQUE 冲突则保留 left 侧已有边,丢弃 right 侧重复)
|
|
377
|
+
db.prepare(`UPDATE OR IGNORE concept_edges SET source_id = ?, updated_at = ? WHERE source_id = ?`).run(left.id, Date.now(), right.id);
|
|
378
|
+
db.prepare(`UPDATE OR IGNORE concept_edges SET target_id = ?, updated_at = ? WHERE target_id = ?`).run(left.id, Date.now(), right.id);
|
|
379
|
+
// OR IGNORE 未更新的仍是 right 端点 → 删除残留
|
|
380
|
+
db.prepare('DELETE FROM concept_edges WHERE source_id = ? OR target_id = ?').run(right.id, right.id);
|
|
381
|
+
// 迁完后自环清理
|
|
382
|
+
db.prepare('DELETE FROM concept_edges WHERE source_id = target_id').run();
|
|
383
|
+
db.prepare('DELETE FROM concepts WHERE id = ?').run(right.id);
|
|
384
|
+
db.prepare('UPDATE concept_merge_candidates SET status = ?, resolved_at = ?, resolved_action = ? WHERE id = ?').run('merged', Date.now(), 'merge', id);
|
|
385
|
+
db.exec('COMMIT');
|
|
386
|
+
}
|
|
387
|
+
catch (err) {
|
|
388
|
+
db.exec('ROLLBACK');
|
|
389
|
+
throw err instanceof Error
|
|
390
|
+
? err
|
|
391
|
+
: new Error(`resolveMerge failed: ${String(err)}`);
|
|
168
392
|
}
|
|
169
393
|
}
|
|
170
|
-
|
|
394
|
+
async reinforce(input) {
|
|
395
|
+
const e = Math.min(1, Math.max(0, input.evidenceStrength ?? 0.5));
|
|
396
|
+
const now = Date.now();
|
|
397
|
+
for (const id of input.nodeIds ?? []) {
|
|
398
|
+
this.db.raw
|
|
399
|
+
.prepare('UPDATE concepts SET frequency = frequency + 1, updated_at = ? WHERE id = ?')
|
|
400
|
+
.run(now, id);
|
|
401
|
+
}
|
|
402
|
+
for (const id of input.edgeIds ?? []) {
|
|
403
|
+
const row = this.db.raw.prepare('SELECT strength FROM concept_edges WHERE id = ?').get(id);
|
|
404
|
+
if (!row)
|
|
405
|
+
continue;
|
|
406
|
+
const next = hebbianStrengthen(row.strength, e);
|
|
407
|
+
this.db.raw
|
|
408
|
+
.prepare('UPDATE concept_edges SET strength = ?, updated_at = ? WHERE id = ?')
|
|
409
|
+
.run(next, now, id);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
async counterEvidence(input) {
|
|
413
|
+
const row = this.db.raw
|
|
414
|
+
.prepare('SELECT * FROM concept_edges WHERE id = ?')
|
|
415
|
+
.get(input.edgeId);
|
|
416
|
+
if (!row)
|
|
417
|
+
return;
|
|
418
|
+
const edge = this.rowToEdge(row);
|
|
419
|
+
let strength = counterEvidenceWeaken(edge.strength);
|
|
420
|
+
let relationType = edge.relationType;
|
|
421
|
+
if (input.markOpposes)
|
|
422
|
+
relationType = 'opposes';
|
|
423
|
+
let memIds = edge.basis.memoryIds;
|
|
424
|
+
if (input.memoryId) {
|
|
425
|
+
memIds = memIds.filter((m) => m !== input.memoryId);
|
|
426
|
+
}
|
|
427
|
+
this.db.raw
|
|
428
|
+
.prepare('UPDATE concept_edges SET strength = ?, relation_type = ?, basis = ?, updated_at = ? WHERE id = ?')
|
|
429
|
+
.run(strength, relationType, JSON.stringify({ ...edge.basis, memoryIds: memIds }), Date.now(), input.edgeId);
|
|
430
|
+
}
|
|
431
|
+
async promote(ids, to) {
|
|
432
|
+
const now = Date.now();
|
|
433
|
+
for (const id of ids) {
|
|
434
|
+
this.db.raw
|
|
435
|
+
.prepare('UPDATE concepts SET status = ?, updated_at = ? WHERE id = ?')
|
|
436
|
+
.run(to, now, id);
|
|
437
|
+
this.db.raw
|
|
438
|
+
.prepare('UPDATE concept_edges SET status = ?, updated_at = ? WHERE id = ?')
|
|
439
|
+
.run(to, now, id);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
async demote(ids, to) {
|
|
443
|
+
const now = Date.now();
|
|
444
|
+
for (const id of ids) {
|
|
445
|
+
this.db.raw
|
|
446
|
+
.prepare('UPDATE concepts SET status = ?, updated_at = ? WHERE id = ?')
|
|
447
|
+
.run(to, now, id);
|
|
448
|
+
this.db.raw
|
|
449
|
+
.prepare('UPDATE concept_edges SET status = ?, updated_at = ? WHERE id = ?')
|
|
450
|
+
.run(to, now, id);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
async applyDecay(now = Date.now()) {
|
|
454
|
+
const edges = this.db.raw.prepare('SELECT * FROM concept_edges').all();
|
|
455
|
+
let decayedEdges = 0;
|
|
456
|
+
for (const row of edges) {
|
|
457
|
+
const days = daysBetween(row.updated_at, now);
|
|
458
|
+
if (days < 1)
|
|
459
|
+
continue;
|
|
460
|
+
const next = nextEdgeStrength(row.strength, row.relation_type, days);
|
|
461
|
+
if (next < row.strength - 1e-6) {
|
|
462
|
+
this.db.raw
|
|
463
|
+
.prepare('UPDATE concept_edges SET strength = ?, updated_at = ? WHERE id = ?')
|
|
464
|
+
.run(next, now, row.id);
|
|
465
|
+
decayedEdges += 1;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
// GC 无支撑且无有效边的节点
|
|
469
|
+
const liveEdges = this.db.raw
|
|
470
|
+
.prepare(`SELECT source_id, target_id FROM concept_edges WHERE strength > ?`)
|
|
471
|
+
.all(this.decayFloor);
|
|
472
|
+
const connected = new Set();
|
|
473
|
+
for (const e of liveEdges) {
|
|
474
|
+
connected.add(e.source_id);
|
|
475
|
+
connected.add(e.target_id);
|
|
476
|
+
}
|
|
477
|
+
const nodes = this.db.raw.prepare('SELECT id, memory_ids FROM concepts').all();
|
|
478
|
+
let gcNodes = 0;
|
|
479
|
+
for (const n of nodes) {
|
|
480
|
+
if (!connected.has(n.id) && safeJsonArray(n.memory_ids).length === 0) {
|
|
481
|
+
this.db.raw.prepare('DELETE FROM concept_edges WHERE source_id = ? OR target_id = ?').run(n.id, n.id);
|
|
482
|
+
this.db.raw.prepare('DELETE FROM concepts WHERE id = ?').run(n.id);
|
|
483
|
+
gcNodes += 1;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
const gcEdges = this.db.raw
|
|
487
|
+
.prepare(`DELETE FROM concept_edges WHERE source_id NOT IN (SELECT id FROM concepts)
|
|
488
|
+
OR target_id NOT IN (SELECT id FROM concepts)`)
|
|
489
|
+
.run().changes;
|
|
490
|
+
return { decayedEdges, gcNodes, gcEdges };
|
|
491
|
+
}
|
|
492
|
+
async spreadingActivate(seeds, options) {
|
|
493
|
+
const depth = options?.depth ?? 2;
|
|
494
|
+
const delta = options?.delta ?? 0.55;
|
|
495
|
+
const tau = options?.tau ?? 0.08;
|
|
496
|
+
const limit = options?.limit ?? 40;
|
|
497
|
+
const statuses = options?.statuses ?? DEFAULT_RETRIEVE_STATUSES;
|
|
498
|
+
const empty = { nodes: [], edges: [], activation: {}, seeds: [] };
|
|
499
|
+
if (!seeds.length || statuses.length === 0)
|
|
500
|
+
return empty;
|
|
501
|
+
const statusPlace = statuses.map(() => '?').join(',');
|
|
502
|
+
const statusSet = new Set(statuses);
|
|
503
|
+
const seedMap = new Map();
|
|
504
|
+
for (const seed of seeds) {
|
|
505
|
+
const q = `%${escapeLike(seed.toLowerCase())}%`;
|
|
506
|
+
const rows = this.db.raw
|
|
507
|
+
.prepare(`SELECT * FROM concepts WHERE status IN (${statusPlace})
|
|
508
|
+
AND (LOWER(name) LIKE ? ESCAPE '\\' OR LOWER(description) LIKE ? ESCAPE '\\')`)
|
|
509
|
+
.all(...statuses, q, q);
|
|
510
|
+
for (const r of rows)
|
|
511
|
+
seedMap.set(r.id, this.rowToNode(r));
|
|
512
|
+
}
|
|
513
|
+
const seedIds = [...seedMap.keys()];
|
|
514
|
+
if (!seedIds.length)
|
|
515
|
+
return empty;
|
|
516
|
+
const activation = new Map();
|
|
517
|
+
for (const id of seedIds)
|
|
518
|
+
activation.set(id, 1);
|
|
519
|
+
// 邻接:两端都必须在检索 status 集合内(禁止扩散进 shadow)
|
|
520
|
+
const allEdges = this.db.raw
|
|
521
|
+
.prepare(`SELECT e.* FROM concept_edges e
|
|
522
|
+
JOIN concepts s ON s.id = e.source_id
|
|
523
|
+
JOIN concepts t ON t.id = e.target_id
|
|
524
|
+
WHERE s.status IN (${statusPlace}) AND t.status IN (${statusPlace})`)
|
|
525
|
+
.all(...statuses, ...statuses);
|
|
526
|
+
const adj = new Map();
|
|
527
|
+
for (const row of allEdges) {
|
|
528
|
+
const e = this.rowToEdge(row);
|
|
529
|
+
const src = this.db.raw.prepare('SELECT status FROM concepts WHERE id = ?').get(e.sourceId);
|
|
530
|
+
const tgt = this.db.raw.prepare('SELECT status FROM concepts WHERE id = ?').get(e.targetId);
|
|
531
|
+
if (!src || !tgt || !statusSet.has(src.status) || !statusSet.has(tgt.status)) {
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (!adj.has(e.sourceId))
|
|
535
|
+
adj.set(e.sourceId, []);
|
|
536
|
+
if (!adj.has(e.targetId))
|
|
537
|
+
adj.set(e.targetId, []);
|
|
538
|
+
adj.get(e.sourceId).push({ to: e.targetId, w: e.strength });
|
|
539
|
+
adj.get(e.targetId).push({ to: e.sourceId, w: e.strength });
|
|
540
|
+
}
|
|
541
|
+
let frontier = [...seedIds];
|
|
542
|
+
for (let d = 1; d <= depth; d++) {
|
|
543
|
+
const next = [];
|
|
544
|
+
for (const id of frontier) {
|
|
545
|
+
const a = activation.get(id) ?? 0;
|
|
546
|
+
for (const { to, w } of adj.get(id) ?? []) {
|
|
547
|
+
const v = a * w * Math.pow(delta, d);
|
|
548
|
+
const prev = activation.get(to) ?? 0;
|
|
549
|
+
if (v > prev && v >= tau) {
|
|
550
|
+
activation.set(to, Math.max(prev, v));
|
|
551
|
+
next.push(to);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
frontier = next;
|
|
556
|
+
if (frontier.length === 0)
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
const kept = [...activation.entries()]
|
|
560
|
+
.filter(([, a]) => a >= tau)
|
|
561
|
+
.sort((a, b) => b[1] - a[1])
|
|
562
|
+
.slice(0, limit);
|
|
563
|
+
const nodeIds = new Set(kept.map(([id]) => id));
|
|
564
|
+
if (!nodeIds.size)
|
|
565
|
+
return empty;
|
|
566
|
+
const nodes = [];
|
|
567
|
+
for (const id of nodeIds) {
|
|
568
|
+
const row = this.db.raw.prepare('SELECT * FROM concepts WHERE id = ?').get(id);
|
|
569
|
+
if (row)
|
|
570
|
+
nodes.push(this.rowToNode(row));
|
|
571
|
+
}
|
|
572
|
+
const idList = [...nodeIds];
|
|
573
|
+
const placeholders = idList.map(() => '?').join(',');
|
|
574
|
+
const edgeRows = this.db.raw
|
|
575
|
+
.prepare(`SELECT * FROM concept_edges WHERE source_id IN (${placeholders})
|
|
576
|
+
AND target_id IN (${placeholders})`)
|
|
577
|
+
.all(...idList, ...idList);
|
|
171
578
|
return {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
memoryIds: JSON.parse(row.memory_ids),
|
|
579
|
+
nodes,
|
|
580
|
+
edges: edgeRows.map((r) => this.rowToEdge(r)),
|
|
581
|
+
activation: Object.fromEntries(kept),
|
|
582
|
+
seeds: seedIds,
|
|
177
583
|
};
|
|
178
584
|
}
|
|
179
|
-
|
|
585
|
+
async getFullGraph() {
|
|
586
|
+
const nodes = this.db.raw.prepare('SELECT * FROM concepts').all().map((r) => this.rowToNode(r));
|
|
587
|
+
const edges = this.db.raw.prepare('SELECT * FROM concept_edges').all().map((r) => this.rowToEdge(r));
|
|
588
|
+
return { nodes, edges };
|
|
589
|
+
}
|
|
590
|
+
async stats() {
|
|
591
|
+
const byStatus = {
|
|
592
|
+
shadow: 0,
|
|
593
|
+
active: 0,
|
|
594
|
+
strengthened: 0,
|
|
595
|
+
};
|
|
596
|
+
const byKind = {
|
|
597
|
+
entity: 0,
|
|
598
|
+
construct: 0,
|
|
599
|
+
method: 0,
|
|
600
|
+
problem: 0,
|
|
601
|
+
constraint: 0,
|
|
602
|
+
};
|
|
603
|
+
for (const row of this.db.raw.prepare('SELECT status, kind FROM concepts').all()) {
|
|
604
|
+
const s = row.status in byStatus ? row.status : 'shadow';
|
|
605
|
+
byStatus[s] += 1;
|
|
606
|
+
if (row.kind in byKind)
|
|
607
|
+
byKind[row.kind] += 1;
|
|
608
|
+
}
|
|
609
|
+
const nodes = this.db.raw.prepare('SELECT COUNT(*) AS c FROM concepts').get().c;
|
|
610
|
+
const edges = this.db.raw.prepare('SELECT COUNT(*) AS c FROM concept_edges').get().c;
|
|
611
|
+
const openMerge = this.db.raw
|
|
612
|
+
.prepare(`SELECT COUNT(*) AS c FROM concept_merge_candidates WHERE status = 'open'`)
|
|
613
|
+
.get().c;
|
|
614
|
+
const causal = this.db.raw
|
|
615
|
+
.prepare(`SELECT COUNT(*) AS c FROM concept_edges_aux WHERE kind = 'causal_candidate'`)
|
|
616
|
+
.get().c;
|
|
180
617
|
return {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
618
|
+
nodes,
|
|
619
|
+
edges,
|
|
620
|
+
byStatus,
|
|
621
|
+
byKind,
|
|
622
|
+
openMergeCandidates: openMerge,
|
|
623
|
+
causalCandidates: causal,
|
|
186
624
|
};
|
|
187
625
|
}
|
|
188
626
|
}
|
|
627
|
+
function safeJsonArray(raw) {
|
|
628
|
+
try {
|
|
629
|
+
const v = JSON.parse(raw ?? '[]');
|
|
630
|
+
return Array.isArray(v) ? v.filter((x) => typeof x === 'string') : [];
|
|
631
|
+
}
|
|
632
|
+
catch {
|
|
633
|
+
return [];
|
|
634
|
+
}
|
|
635
|
+
}
|