@octopi-agent/engine 0.102.10 → 0.121.15
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/dist/builtin-model-info.js +28 -7
- package/dist/config-admin/agent-presets.d.ts +32 -0
- package/dist/config-admin/agent-presets.js +133 -0
- package/dist/config-admin/console-access-key.d.ts +46 -0
- package/dist/config-admin/console-access-key.js +47 -0
- package/dist/config-admin/discover.d.ts +33 -4
- package/dist/config-admin/discover.js +101 -6
- package/dist/config-admin/env.d.ts +11 -0
- package/dist/config-admin/env.js +41 -0
- package/dist/config-admin/error-copy.d.ts +106 -0
- package/dist/config-admin/error-copy.js +287 -0
- package/dist/config-admin/i18n.d.ts +21 -0
- package/dist/config-admin/i18n.js +19 -0
- package/dist/config-admin/index.d.ts +8 -2
- package/dist/config-admin/index.js +4 -1
- package/dist/config-admin/model-presets.d.ts +14 -0
- package/dist/config-admin/model-presets.js +595 -70
- package/dist/config-admin/onboarding-apply.d.ts +83 -0
- package/dist/config-admin/onboarding-apply.js +259 -0
- package/dist/config-admin/onboarding.d.ts +160 -0
- package/dist/config-admin/onboarding.js +298 -0
- package/dist/config-admin/password-policy.d.ts +24 -0
- package/dist/config-admin/password-policy.js +31 -0
- package/dist/config-admin/redact.d.ts +42 -9
- package/dist/config-admin/redact.js +69 -5
- package/dist/config-schema/agent.d.ts +5 -1
- package/dist/config-schema/agent.js +20 -2
- package/dist/config-schema/browser.d.ts +62 -0
- package/dist/config-schema/browser.js +89 -0
- package/dist/config-schema/context.d.ts +7 -2
- package/dist/config-schema/context.js +6 -3
- package/dist/config-schema/engine.d.ts +86 -7
- package/dist/config-schema/engine.js +9 -2
- package/dist/config-schema/governance.d.ts +12 -1
- package/dist/config-schema/governance.js +10 -2
- package/dist/config-schema/models.d.ts +12 -8
- package/dist/config-schema/models.js +16 -1
- package/dist/config-schema/run.d.ts +4 -0
- package/dist/config-schema/run.js +10 -0
- package/dist/config-schema/shared.d.ts +1 -0
- package/dist/config-schema/shared.js +4 -2
- package/dist/config-schema/substrate.d.ts +5 -0
- package/dist/config-schema/substrate.js +8 -0
- package/dist/config.d.ts +116 -10
- package/dist/config.js +109 -13
- package/dist/file-mode.d.ts +67 -0
- package/dist/file-mode.js +182 -0
- package/dist/fork-child.d.ts +82 -0
- package/dist/fork-child.js +123 -0
- package/dist/harness/activation/delegated-run.d.ts +42 -0
- package/dist/harness/activation/delegated-run.js +71 -0
- package/dist/harness/activation/dispatcher.js +4 -0
- package/dist/harness/activation/index.d.ts +1 -0
- package/dist/harness/activation/run-backend.d.ts +92 -0
- package/dist/harness/activation/run-backend.js +16 -0
- package/dist/harness/activation/run-protocol.d.ts +392 -0
- package/dist/harness/activation/run-protocol.js +19 -0
- package/dist/harness/activation/runtime.d.ts +33 -0
- package/dist/harness/activation/runtime.js +119 -28
- package/dist/harness/activation/types.d.ts +19 -2
- package/dist/harness/agent/builder.d.ts +12 -0
- package/dist/harness/agent/builder.js +78 -22
- package/dist/harness/agent/config-bridge.d.ts +10 -1
- package/dist/harness/agent/config-bridge.js +15 -3
- package/dist/harness/agent/registered-tool-adapter.d.ts +27 -0
- package/dist/harness/agent/registered-tool-adapter.js +67 -0
- package/dist/harness/agent/wire-agent-builder.d.ts +2 -0
- package/dist/harness/agent/wire-agent-builder.js +9 -2
- package/dist/harness/capabilities/document/backends/plain.js +2 -1
- package/dist/harness/capabilities/document/legacy/factory.js +3 -2
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.d.ts +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.js +66 -36
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.d.ts +6 -2
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.js +27 -7
- package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.js +13 -0
- package/dist/harness/collaboration/autonomous-subsystem/loader.js +1 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +11 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.js +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.d.ts +13 -3
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +48 -39
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +3 -0
- package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +4 -0
- package/dist/harness/collaboration/delegation/child-run.d.ts +9 -0
- package/dist/harness/collaboration/delegation/child-run.js +34 -2
- package/dist/harness/collaboration/delegation/registry-types.d.ts +2 -0
- package/dist/harness/collaboration/delegation/service.d.ts +9 -4
- package/dist/harness/collaboration/delegation/service.js +33 -12
- package/dist/harness/collaboration/delegation/store.d.ts +2 -0
- package/dist/harness/collaboration/delegation/store.js +1 -0
- package/dist/harness/collaboration/delegation/types.d.ts +11 -0
- package/dist/harness/collaboration/delegation/wiring.d.ts +2 -0
- package/dist/harness/collaboration/delegation/wiring.js +1 -0
- package/dist/harness/context/capabilities/compact/engine.d.ts +44 -5
- package/dist/harness/context/capabilities/compact/engine.js +127 -39
- package/dist/harness/context/capabilities/compact/index.d.ts +2 -1
- package/dist/harness/context/capabilities/compact/index.js +1 -1
- package/dist/harness/context/capabilities/compact/types.d.ts +59 -9
- package/dist/harness/context/capabilities/summary/gate.d.ts +19 -2
- package/dist/harness/context/capabilities/summary/gate.js +53 -6
- package/dist/harness/context/capabilities/summary/registry.js +20 -0
- package/dist/harness/context/capabilities/summary/tool-binding.js +16 -12
- package/dist/harness/context/capabilities/summary/tool-bindings.d.ts +13 -0
- package/dist/harness/context/capabilities/summary/tool-bindings.js +22 -0
- package/dist/harness/context/capabilities/summary/types.d.ts +16 -0
- package/dist/harness/context/constitution/default-agents.md +85 -0
- package/dist/harness/context/context-size-ledger.js +8 -1
- package/dist/harness/context/default-context-engine.d.ts +100 -6
- package/dist/harness/context/default-context-engine.js +382 -89
- package/dist/harness/context/index.d.ts +2 -1
- package/dist/harness/context/index.js +1 -0
- package/dist/harness/context/layer-health.d.ts +4 -0
- package/dist/harness/context/layer-health.js +4 -0
- package/dist/harness/context/layer-types.d.ts +57 -0
- package/dist/harness/context/layer-types.js +73 -1
- package/dist/harness/context/layers.d.ts +3 -0
- package/dist/harness/context/layers.js +13 -6
- package/dist/harness/context/prefix-fingerprint.d.ts +46 -0
- package/dist/harness/context/prefix-fingerprint.js +52 -0
- package/dist/harness/context/summarize.js +18 -0
- package/dist/harness/context/system-prompt-assembler.d.ts +2 -0
- package/dist/harness/context/system-prompt-assembler.js +5 -2
- package/dist/harness/context/tool-result-aging.d.ts +104 -16
- package/dist/harness/context/tool-result-aging.js +211 -57
- package/dist/harness/context/types.d.ts +71 -4
- package/dist/harness/context/types.js +25 -1
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +4 -3
- package/dist/harness/extension/execution-environment/effect-policy.js +2 -2
- package/dist/harness/extension/execution-environment/privileged-exec.js +4 -1
- package/dist/harness/extension/execution-environment/workspace.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +27 -1
- package/dist/harness/extension/plugin-ecosystem/channel-types.js +11 -2
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +12 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +25 -0
- package/dist/harness/extension/plugin-ecosystem/commands/builtin.js +28 -1
- 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/plugins/api.d.ts +168 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +156 -15
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.d.ts +95 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.js +170 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.d.ts +178 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.js +314 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.d.ts +40 -5
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +45 -26
- package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +11 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +42 -76
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +30 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +46 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +9 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +19 -14
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +3 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.d.ts +333 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.js +16 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.d.ts +64 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.js +692 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +0 -37
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +108 -4
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +18 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +34 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.d.ts +0 -6
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +57 -4
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +81 -13
- package/dist/harness/extension/plugin-ecosystem/tools/http.js +5 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +13 -7
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +126 -14
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +44 -5
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +10 -24
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +7 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +4 -0
- package/dist/harness/governance/credentials/db.d.ts +9 -0
- package/dist/harness/governance/credentials/db.js +122 -31
- package/dist/harness/governance/credentials/store.d.ts +68 -7
- package/dist/harness/governance/credentials/store.js +177 -64
- package/dist/harness/governance/credentials/types.d.ts +13 -2
- package/dist/harness/governance/index.d.ts +1 -0
- package/dist/harness/governance/index.js +1 -0
- package/dist/harness/governance/registry/db.d.ts +32 -0
- package/dist/harness/governance/registry/db.js +142 -0
- package/dist/harness/governance/registry/index.d.ts +9 -0
- package/dist/harness/governance/registry/index.js +9 -0
- package/dist/harness/governance/registry/service.d.ts +381 -0
- package/dist/harness/governance/registry/service.js +825 -0
- package/dist/harness/governance/registry/types.d.ts +77 -0
- package/dist/harness/governance/registry/types.js +7 -0
- package/dist/harness/governance/security/default-security-guard.d.ts +3 -0
- package/dist/harness/governance/security/default-security-guard.js +12 -1
- package/dist/harness/governance/security/risk-evaluator.d.ts +13 -3
- package/dist/harness/governance/security/risk-evaluator.js +307 -21
- package/dist/harness/governance/security/shell-parser-treesitter.js +12 -4
- package/dist/harness/governance/session-acl/tool-rights.d.ts +1 -0
- package/dist/harness/index.d.ts +5 -3
- package/dist/harness/index.js +2 -2
- package/dist/harness/knowledge/boot-config.d.ts +2 -0
- package/dist/harness/knowledge/boot-config.js +2 -0
- package/dist/harness/knowledge/client.d.ts +53 -7
- package/dist/harness/knowledge/client.js +77 -20
- package/dist/harness/knowledge/db.d.ts +16 -0
- package/dist/harness/knowledge/db.js +107 -41
- package/dist/harness/knowledge/engine-thread.js +5 -7
- package/dist/harness/knowledge/grounding.js +4 -1
- package/dist/harness/knowledge/http-app.d.ts +15 -0
- package/dist/harness/knowledge/http-app.js +82 -21
- package/dist/harness/knowledge/http-bridge.d.ts +15 -0
- package/dist/harness/knowledge/http-bridge.js +19 -0
- package/dist/harness/knowledge/knowledge-serve-child.js +2 -3
- package/dist/harness/knowledge/query-service.d.ts +12 -2
- package/dist/harness/knowledge/query-service.js +75 -41
- package/dist/harness/knowledge/query-worker-client.d.ts +5 -1
- package/dist/harness/knowledge/query-worker-client.js +4 -1
- package/dist/harness/knowledge/query-worker.js +5 -3
- package/dist/harness/knowledge/read-http.d.ts +2 -0
- package/dist/harness/knowledge/read-http.js +9 -2
- package/dist/harness/knowledge/retriever.d.ts +1 -1
- package/dist/harness/knowledge/retriever.js +1 -2
- package/dist/harness/knowledge/serve.d.ts +6 -1
- package/dist/harness/knowledge/serve.js +5 -6
- package/dist/harness/knowledge/source-store.d.ts +101 -5
- package/dist/harness/knowledge/source-store.js +297 -71
- package/dist/harness/knowledge/start-service-process.d.ts +27 -8
- package/dist/harness/knowledge/start-service-process.js +49 -90
- package/dist/harness/knowledge/tenant-gate.d.ts +81 -0
- package/dist/harness/knowledge/tenant-gate.js +167 -0
- package/dist/harness/knowledge/writer-service.d.ts +18 -2
- package/dist/harness/knowledge/writer-service.js +80 -16
- package/dist/harness/knowledge/writer-worker-client.d.ts +9 -2
- package/dist/harness/knowledge/writer-worker-client.js +20 -3
- package/dist/harness/knowledge/writer-worker.js +11 -4
- package/dist/harness/memory/backfill-trigger.d.ts +10 -3
- package/dist/harness/memory/backfill-trigger.js +19 -19
- package/dist/harness/memory/cognition-seeds.d.ts +45 -0
- package/dist/harness/memory/cognition-seeds.js +67 -0
- package/dist/harness/memory/cognition.d.ts +12 -3
- package/dist/harness/memory/cognition.js +55 -35
- package/dist/harness/memory/conceptualizer.d.ts +11 -1
- package/dist/harness/memory/conceptualizer.js +10 -1
- package/dist/harness/memory/context-slice.d.ts +42 -0
- package/dist/harness/memory/context-slice.js +197 -0
- package/dist/harness/memory/health-probe.d.ts +8 -2
- package/dist/harness/memory/health-probe.js +10 -13
- package/dist/harness/memory/index.d.ts +5 -1
- package/dist/harness/memory/index.js +3 -1
- package/dist/harness/memory/sqlite/agent-db.d.ts +1 -0
- package/dist/harness/memory/sqlite/agent-db.js +9 -8
- package/dist/harness/memory/sqlite/cognition-store.d.ts +26 -3
- package/dist/harness/memory/sqlite/cognition-store.js +103 -45
- package/dist/harness/memory/sqlite/memory-store.js +4 -0
- package/dist/harness/memory/sqlite/sqlite-vec.d.ts +24 -1
- package/dist/harness/memory/sqlite/sqlite-vec.js +37 -1
- package/dist/harness/memory/types.d.ts +18 -2
- package/dist/harness/observability/observer/hub.d.ts +10 -0
- package/dist/harness/observability/observer/hub.js +95 -2
- package/dist/harness/run/agent/agent.js +3 -2
- package/dist/harness/run/concurrency/tool-validator.js +4 -3
- package/dist/harness/run/reliability/run-agent.d.ts +22 -0
- package/dist/harness/run/reliability/run-agent.js +79 -10
- package/dist/harness/run/run-host.d.ts +302 -0
- package/dist/harness/run/run-host.js +978 -0
- package/dist/harness/run/run-scope.d.ts +11 -0
- package/dist/harness/run/run-scope.js +19 -1
- package/dist/harness/run/runner.d.ts +149 -22
- package/dist/harness/run/runner.js +352 -35
- package/dist/harness/session/in-memory-store.js +13 -1
- package/dist/harness/session/schedules/service.d.ts +10 -2
- package/dist/harness/session/schedules/service.js +13 -2
- package/dist/harness/session/tasks/tools.js +12 -0
- package/dist/harness/session/types.d.ts +10 -14
- package/dist/harness/shared/events/agent-event-map.d.ts +29 -0
- package/dist/harness/shared/types/agent-definition.d.ts +6 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/integration/agent-runtime/channel-message-source.d.ts +3 -1
- package/dist/integration/agent-runtime/channel-message-source.js +4 -3
- package/dist/integration/browser/camoufox.d.ts +40 -0
- package/dist/integration/browser/camoufox.js +80 -0
- package/dist/integration/browser/index.d.ts +91 -0
- package/dist/integration/browser/index.js +110 -0
- package/dist/integration/browser/install.d.ts +47 -0
- package/dist/integration/browser/install.js +214 -0
- package/dist/integration/browser/playwright.d.ts +224 -0
- package/dist/integration/browser/playwright.js +925 -0
- package/dist/integration/browser/service.d.ts +41 -0
- package/dist/integration/browser/service.js +181 -0
- package/dist/integration/browser/stealth.d.ts +42 -0
- package/dist/integration/browser/stealth.js +104 -0
- package/dist/integration/providers/anthropic-message-normalize.d.ts +20 -0
- package/dist/integration/providers/anthropic-message-normalize.js +48 -0
- package/dist/integration/providers/anthropic.js +27 -2
- package/dist/integration/providers/fetch-timeout.d.ts +17 -0
- package/dist/integration/providers/fetch-timeout.js +24 -0
- package/dist/integration/providers/openai.d.ts +8 -0
- package/dist/integration/providers/openai.js +41 -1
- package/dist/integration/providers/wire-dump-diff.js +2 -1
- package/dist/integration/providers/wire-dump.d.ts +56 -2
- package/dist/integration/providers/wire-dump.js +119 -6
- package/dist/integration/storage/archive-manager.d.ts +14 -2
- package/dist/integration/storage/archive-manager.js +24 -8
- package/dist/integration/storage/atomic-tmp.d.ts +12 -0
- package/dist/integration/storage/atomic-tmp.js +36 -0
- package/dist/integration/storage/jsonl.d.ts +9 -0
- package/dist/integration/storage/jsonl.js +137 -38
- package/dist/integration/storage/session-index.d.ts +5 -0
- package/dist/integration/storage/session-index.js +25 -4
- package/dist/integration/tools/assemble.d.ts +50 -0
- package/dist/integration/tools/assemble.js +137 -0
- package/dist/paths.d.ts +18 -0
- package/dist/paths.js +36 -0
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +3 -1
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +74 -5
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +3 -2
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +11 -2
- package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +6 -3
- package/dist/subsystems/cognition-steward/consolidate/handler.js +266 -28
- package/dist/subsystems/memory-steward/backfill/handler.js +3 -2
- package/dist/subsystems/memory-steward/govern/config.yaml +6 -1
- package/dist/subsystems/wisdom-steward/attribute/config.yaml +5 -1
- package/dist/subsystems/wisdom-steward/formation/config.yaml +3 -0
- package/dist/subsystems/wisdom-steward/govern/config.yaml +6 -1
- package/package.json +5 -3
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
* KnowledgeSourceStore — 源注册 + 可见性 + catalog 投影
|
|
3
3
|
*/
|
|
4
4
|
import { randomUUID, createHash } from 'node:crypto';
|
|
5
|
+
import { PLATFORM_TENANT_ID } from '@octopi-agent/core/types.js';
|
|
5
6
|
import { KnowledgeDatabase } from './db.js';
|
|
7
|
+
import { ownerFunnelAllows, projectGateAllows, projectGateSql, tenantGateAllows, tenantGateSql, tenantOwnGateSql, tenantPrecedenceOrder, } from './tenant-gate.js';
|
|
6
8
|
import { deriveTopicsFromPaths } from './topics.js';
|
|
7
9
|
import { asSourceId } from './types.js';
|
|
8
10
|
const DEFAULT_SYNC = { strategy: 'watch', debounceMs: 2000, enabled: true };
|
|
@@ -120,6 +122,10 @@ function rowToSource(row) {
|
|
|
120
122
|
}
|
|
121
123
|
export class KnowledgeSourceStore {
|
|
122
124
|
db;
|
|
125
|
+
/** listVisible 批处理期的 source 行同步缓存(filter 无 await,无并发窗口;平时为 null) */
|
|
126
|
+
rowCache = null;
|
|
127
|
+
/** 项目行批处理缓存(评审 m6:防逐源 N+1;与 rowCache 同模式,批次内同步读) */
|
|
128
|
+
projectRowCache = null;
|
|
123
129
|
constructor(db) {
|
|
124
130
|
this.db = db;
|
|
125
131
|
}
|
|
@@ -136,7 +142,7 @@ export class KnowledgeSourceStore {
|
|
|
136
142
|
/**
|
|
137
143
|
* 注册知识源
|
|
138
144
|
*/
|
|
139
|
-
register(input) {
|
|
145
|
+
register(input, opts) {
|
|
140
146
|
const now = Date.now();
|
|
141
147
|
const id = input.id ?? `ks_${randomUUID().slice(0, 12)}`;
|
|
142
148
|
const displayName = input.displayName?.trim() || deriveDisplayName(input.location);
|
|
@@ -161,7 +167,12 @@ export class KnowledgeSourceStore {
|
|
|
161
167
|
updatedAt: now,
|
|
162
168
|
};
|
|
163
169
|
if (scopeRef.level === 'project') {
|
|
164
|
-
|
|
170
|
+
// 项目行必须与源同域登记(归属打标在写路径;同 key 可跨域各一行)。
|
|
171
|
+
// platform 域隐式登记 = 隐式共享库(评审 M2):显式 createProject 才能造 private 平台项目
|
|
172
|
+
this.createProject(scopeRef.key, undefined, {
|
|
173
|
+
...opts,
|
|
174
|
+
visibility: opts?.tenantId === PLATFORM_TENANT_ID ? 'public' : opts?.visibility,
|
|
175
|
+
});
|
|
165
176
|
}
|
|
166
177
|
this.upsertRow(source);
|
|
167
178
|
return source;
|
|
@@ -213,8 +224,16 @@ export class KnowledgeSourceStore {
|
|
|
213
224
|
? {
|
|
214
225
|
scopeRef: (() => {
|
|
215
226
|
const scope = normalizeScope(patch.scopeRef);
|
|
216
|
-
if (scope.level === 'project')
|
|
217
|
-
|
|
227
|
+
if (scope.level === 'project') {
|
|
228
|
+
// 项目行与源同域登记(评审 m2:否则固定落 default 域成跨域幻影行);
|
|
229
|
+
// platform 域隐式登记 = 隐式共享库(评审 M2),显式 createProject 才能造 private 平台项目
|
|
230
|
+
const row = this.sourceRow(id);
|
|
231
|
+
this.createProject(scope.key, undefined, {
|
|
232
|
+
tenantId: row?.tenant_id,
|
|
233
|
+
registeredBy: row?.registered_by,
|
|
234
|
+
visibility: row?.tenant_id === PLATFORM_TENANT_ID ? 'public' : undefined,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
218
237
|
return scope;
|
|
219
238
|
})(),
|
|
220
239
|
}
|
|
@@ -239,7 +258,7 @@ export class KnowledgeSourceStore {
|
|
|
239
258
|
*/
|
|
240
259
|
remove(id) {
|
|
241
260
|
const res = this.db.raw.prepare('DELETE FROM knowledge_sources WHERE id = ?').run(id);
|
|
242
|
-
this.db.raw.prepare('DELETE FROM
|
|
261
|
+
this.db.raw.prepare('DELETE FROM knowledge_agent_visibility WHERE source_id = ?').run(id);
|
|
243
262
|
this.db.raw
|
|
244
263
|
.prepare("DELETE FROM knowledge_session_visibility WHERE target_type = 'source' AND target_id = ?")
|
|
245
264
|
.run(id);
|
|
@@ -294,11 +313,12 @@ export class KnowledgeSourceStore {
|
|
|
294
313
|
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', projectKey, agentId);
|
|
295
314
|
}
|
|
296
315
|
listProjectAgents(projectKey, opts) {
|
|
316
|
+
const gate = tenantOwnGateSql('tenant_id', opts ?? {});
|
|
297
317
|
const rows = this.db.raw
|
|
298
318
|
.prepare(`SELECT local_agent_id AS agent_id FROM knowledge_project_agents
|
|
299
|
-
WHERE
|
|
319
|
+
WHERE ${gate.sql} AND gateway_id = ? AND project_key = ?
|
|
300
320
|
ORDER BY local_agent_id`)
|
|
301
|
-
.all(
|
|
321
|
+
.all(...gate.params, opts?.gatewayId ?? 'default', projectKey);
|
|
302
322
|
return rows.map((r) => r.agent_id);
|
|
303
323
|
}
|
|
304
324
|
/**
|
|
@@ -309,28 +329,69 @@ export class KnowledgeSourceStore {
|
|
|
309
329
|
if (source && source.scopeRef.level !== 'global') {
|
|
310
330
|
throw new Error(`hide is only valid for global sources (got scope ${source.scopeRef.level})`);
|
|
311
331
|
}
|
|
332
|
+
// 一表两向(T3b):hide=exclude 覆盖(含翻转既有挂载 include)
|
|
312
333
|
this.db.raw
|
|
313
|
-
.prepare(`INSERT OR
|
|
314
|
-
(tenant_id, gateway_id, local_agent_id, source_id, created_at)
|
|
315
|
-
VALUES (?, ?, ?, ?, ?)`)
|
|
334
|
+
.prepare(`INSERT OR REPLACE INTO knowledge_agent_visibility
|
|
335
|
+
(tenant_id, gateway_id, local_agent_id, source_id, op, created_at)
|
|
336
|
+
VALUES (?, ?, ?, ?, 'exclude', ?)`)
|
|
316
337
|
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId, sourceId, Date.now());
|
|
317
338
|
}
|
|
318
339
|
unhideSource(agentId, sourceId, opts) {
|
|
340
|
+
// 只撤 exclude(挂载 include 行不受影响)
|
|
341
|
+
this.db.raw
|
|
342
|
+
.prepare(`DELETE FROM knowledge_agent_visibility
|
|
343
|
+
WHERE tenant_id = ? AND gateway_id = ? AND local_agent_id = ? AND source_id = ? AND op = 'exclude'`)
|
|
344
|
+
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId, sourceId);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* 挂载(T3b,arch/multi-tenancy.md §6 挂载库):include 覆盖基础可见性(含翻转既有隐藏)。
|
|
348
|
+
* 租户闸不受影响——挂载不出租户(registry 侧另有 guard)。
|
|
349
|
+
*/
|
|
350
|
+
mountSource(agentId, sourceId, opts) {
|
|
351
|
+
this.db.raw
|
|
352
|
+
.prepare(`INSERT OR REPLACE INTO knowledge_agent_visibility
|
|
353
|
+
(tenant_id, gateway_id, local_agent_id, source_id, op, created_at)
|
|
354
|
+
VALUES (?, ?, ?, ?, 'include', ?)`)
|
|
355
|
+
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId, sourceId, Date.now());
|
|
356
|
+
}
|
|
357
|
+
/** 卸载:只撤 include(隐藏 exclude 行不受影响);回基础可见性 */
|
|
358
|
+
unmountSource(agentId, sourceId, opts) {
|
|
319
359
|
this.db.raw
|
|
320
|
-
.prepare(
|
|
360
|
+
.prepare(`DELETE FROM knowledge_agent_visibility
|
|
361
|
+
WHERE tenant_id = ? AND gateway_id = ? AND local_agent_id = ? AND source_id = ? AND op = 'include'`)
|
|
321
362
|
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId, sourceId);
|
|
322
363
|
}
|
|
323
364
|
listHidden(agentId, opts) {
|
|
365
|
+
return this.listAgentOp(agentId, 'exclude', opts);
|
|
366
|
+
}
|
|
367
|
+
/** 已挂载源(include) */
|
|
368
|
+
listMounted(agentId, opts) {
|
|
369
|
+
return this.listAgentOp(agentId, 'include', opts);
|
|
370
|
+
}
|
|
371
|
+
listAgentOp(agentId, op, opts) {
|
|
372
|
+
const gate = tenantOwnGateSql('tenant_id', opts ?? {});
|
|
324
373
|
const rows = this.db.raw
|
|
325
|
-
.prepare(
|
|
326
|
-
|
|
374
|
+
.prepare(`SELECT source_id FROM knowledge_agent_visibility
|
|
375
|
+
WHERE ${gate.sql} AND gateway_id = ? AND local_agent_id = ? AND op = ?
|
|
376
|
+
ORDER BY source_id`)
|
|
377
|
+
.all(...gate.params, opts?.gatewayId ?? 'default', agentId, op);
|
|
327
378
|
return rows.map((r) => r.source_id);
|
|
328
379
|
}
|
|
329
380
|
/** 哪些 agent 对该 global 源做了隐藏 */
|
|
330
381
|
listAgentsHidingSource(sourceId, opts) {
|
|
382
|
+
return this.listAgentsOp(sourceId, 'exclude', opts);
|
|
383
|
+
}
|
|
384
|
+
/** 哪些 agent 挂载了该源 */
|
|
385
|
+
listAgentsMountingSource(sourceId, opts) {
|
|
386
|
+
return this.listAgentsOp(sourceId, 'include', opts);
|
|
387
|
+
}
|
|
388
|
+
listAgentsOp(sourceId, op, opts) {
|
|
389
|
+
const gate = tenantOwnGateSql('tenant_id', opts ?? {});
|
|
331
390
|
const rows = this.db.raw
|
|
332
|
-
.prepare(
|
|
333
|
-
|
|
391
|
+
.prepare(`SELECT local_agent_id FROM knowledge_agent_visibility
|
|
392
|
+
WHERE ${gate.sql} AND gateway_id = ? AND source_id = ? AND op = ?
|
|
393
|
+
ORDER BY local_agent_id`)
|
|
394
|
+
.all(...gate.params, opts?.gatewayId ?? 'default', sourceId, op);
|
|
334
395
|
return rows.map((r) => r.local_agent_id);
|
|
335
396
|
}
|
|
336
397
|
/**
|
|
@@ -359,15 +420,18 @@ export class KnowledgeSourceStore {
|
|
|
359
420
|
for (const item of items)
|
|
360
421
|
this.setSessionVisibility(sessionId, item, identity);
|
|
361
422
|
}
|
|
362
|
-
clearSessionVisibility(sessionId, target, identity) {
|
|
423
|
+
clearSessionVisibility(sessionId, target, identity, op) {
|
|
363
424
|
const tenant = identity?.tenantId ?? 'default';
|
|
364
425
|
const gateway = identity?.gatewayId ?? 'default';
|
|
365
426
|
if (target) {
|
|
427
|
+
// op 过滤(T3b unmount 对称):只撤指定向的 override,不误删用户反向显式设置
|
|
366
428
|
this.db.raw
|
|
367
429
|
.prepare(`DELETE FROM knowledge_session_visibility
|
|
368
430
|
WHERE tenant_id = ? AND gateway_id = ? AND local_session_id = ?
|
|
369
|
-
AND target_type = ? AND target_id =
|
|
370
|
-
.run(
|
|
431
|
+
AND target_type = ? AND target_id = ?${op ? ' AND op = ?' : ''}`)
|
|
432
|
+
.run(...(op
|
|
433
|
+
? [tenant, gateway, sessionId, target.targetType, target.targetId, op]
|
|
434
|
+
: [tenant, gateway, sessionId, target.targetType, target.targetId]));
|
|
371
435
|
return;
|
|
372
436
|
}
|
|
373
437
|
this.db.raw
|
|
@@ -376,12 +440,13 @@ export class KnowledgeSourceStore {
|
|
|
376
440
|
.run(tenant, gateway, sessionId);
|
|
377
441
|
}
|
|
378
442
|
listSessionVisibility(sessionId, identity) {
|
|
443
|
+
const gate = tenantOwnGateSql('tenant_id', identity ?? {});
|
|
379
444
|
const rows = this.db.raw
|
|
380
445
|
.prepare(`SELECT local_session_id AS session_id, target_type, target_id, op, created_at
|
|
381
446
|
FROM knowledge_session_visibility
|
|
382
|
-
WHERE
|
|
447
|
+
WHERE ${gate.sql} AND gateway_id = ? AND local_session_id = ?
|
|
383
448
|
ORDER BY created_at`)
|
|
384
|
-
.all(
|
|
449
|
+
.all(...gate.params, identity?.gatewayId ?? 'default', sessionId);
|
|
385
450
|
return rows.map((r) => ({
|
|
386
451
|
sessionId: r.session_id,
|
|
387
452
|
targetType: r.target_type,
|
|
@@ -392,38 +457,62 @@ export class KnowledgeSourceStore {
|
|
|
392
457
|
}
|
|
393
458
|
/** 项目登记视图(管理面:先建项目再挂源;**不**用孤儿源计数复活已删项目) */
|
|
394
459
|
listProjects(opts) {
|
|
395
|
-
|
|
460
|
+
// 读身份:无 opts / 字段缺省均按 DEFAULT 域(MAJ-2 fail-closed);平台域=全量视图
|
|
461
|
+
const identity = opts ?? {};
|
|
462
|
+
const projectGate = projectGateSql('tenant_id', 'visibility', identity);
|
|
463
|
+
const tenantGate = tenantGateSql('tenant_id', identity);
|
|
464
|
+
const mountGate = tenantOwnGateSql('tenant_id', identity);
|
|
396
465
|
const gateway = opts?.gatewayId ?? 'default';
|
|
466
|
+
const ownTenant = opts?.tenantId ?? 'default';
|
|
397
467
|
const registered = this.db.raw
|
|
398
|
-
.prepare(`SELECT project_key, display_name FROM knowledge_projects
|
|
399
|
-
WHERE
|
|
400
|
-
.all(
|
|
468
|
+
.prepare(`SELECT tenant_id, project_key, display_name FROM knowledge_projects
|
|
469
|
+
WHERE ${projectGate.sql} ORDER BY project_key`)
|
|
470
|
+
.all(...projectGate.params);
|
|
471
|
+
// 同 key 可跨域各登记一行(评审 M3):按域优先级择一输出(本域 > platform > 其它,
|
|
472
|
+
// 同级按域名字典序),禁止重复 key 进 UI、禁止跨域混计
|
|
473
|
+
const rank = (t) => (t === ownTenant ? 0 : t === PLATFORM_TENANT_ID ? 1 : 2);
|
|
474
|
+
const chosen = new Map();
|
|
475
|
+
for (const row of registered) {
|
|
476
|
+
const prev = chosen.get(row.project_key);
|
|
477
|
+
if (!prev ||
|
|
478
|
+
rank(row.tenant_id) < rank(prev.tenant_id) ||
|
|
479
|
+
(rank(row.tenant_id) === rank(prev.tenant_id) && row.tenant_id < prev.tenant_id)) {
|
|
480
|
+
chosen.set(row.project_key, row);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
401
483
|
const counts = this.db.raw
|
|
402
|
-
.prepare(`SELECT scope_key AS project_key, COUNT(*) AS source_count
|
|
484
|
+
.prepare(`SELECT tenant_id, scope_key AS project_key, COUNT(*) AS source_count
|
|
403
485
|
FROM knowledge_sources
|
|
404
|
-
WHERE scope_level = 'project' AND status != 'removed' AND
|
|
405
|
-
GROUP BY scope_key`)
|
|
406
|
-
.all(
|
|
407
|
-
const countMap = new Map(counts.map((c) => [c.project_key
|
|
408
|
-
// 一次拉齐 project→agents,禁止每项目一条 N+1
|
|
486
|
+
WHERE scope_level = 'project' AND status != 'removed' AND ${tenantGate.sql}
|
|
487
|
+
GROUP BY tenant_id, scope_key`)
|
|
488
|
+
.all(...tenantGate.params);
|
|
489
|
+
const countMap = new Map(counts.map((c) => [`${c.tenant_id}\u0000${c.project_key}`, c.source_count]));
|
|
490
|
+
// 一次拉齐 project→agents,禁止每项目一条 N+1;挂载行=写入物,按本域闸不跨域扩散
|
|
409
491
|
const agentRows = this.db.raw
|
|
410
|
-
.prepare(`SELECT project_key, local_agent_id FROM knowledge_project_agents
|
|
411
|
-
WHERE
|
|
492
|
+
.prepare(`SELECT tenant_id, project_key, local_agent_id FROM knowledge_project_agents
|
|
493
|
+
WHERE ${mountGate.sql} AND gateway_id = ?
|
|
412
494
|
ORDER BY project_key, local_agent_id`)
|
|
413
|
-
.all(
|
|
495
|
+
.all(...mountGate.params, gateway);
|
|
414
496
|
const agentsByProject = new Map();
|
|
415
497
|
for (const row of agentRows) {
|
|
498
|
+
// 挂载行 = 读方域的写入物(本域闸已过滤):回答「我的哪些 agent 可见」,按 key 聚合
|
|
416
499
|
const list = agentsByProject.get(row.project_key) ?? [];
|
|
417
500
|
list.push(row.local_agent_id);
|
|
418
501
|
agentsByProject.set(row.project_key, list);
|
|
419
502
|
}
|
|
420
503
|
// 只列已登记项目;孤儿源(项目已删)不再“复活”项目行
|
|
421
|
-
return
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
504
|
+
return [...chosen.values()]
|
|
505
|
+
.map((r) => {
|
|
506
|
+
const domainKey = `${r.tenant_id}\u0000${r.project_key}`;
|
|
507
|
+
return {
|
|
508
|
+
tenantId: r.tenant_id,
|
|
509
|
+
projectKey: r.project_key,
|
|
510
|
+
displayName: r.display_name ?? undefined,
|
|
511
|
+
sourceCount: countMap.get(domainKey) ?? 0,
|
|
512
|
+
assignedAgentIds: agentsByProject.get(r.project_key) ?? [],
|
|
513
|
+
};
|
|
514
|
+
})
|
|
515
|
+
.sort((a, b) => (a.projectKey < b.projectKey ? -1 : a.projectKey > b.projectKey ? 1 : 0));
|
|
427
516
|
}
|
|
428
517
|
/** 登记空项目(先建项目再挂源) */
|
|
429
518
|
createProject(projectKey, displayName, opts) {
|
|
@@ -435,11 +524,11 @@ export class KnowledgeSourceStore {
|
|
|
435
524
|
this.db.raw
|
|
436
525
|
.prepare(`INSERT INTO knowledge_projects
|
|
437
526
|
(tenant_id, project_key, display_name, registered_by, visibility, created_at, updated_at)
|
|
438
|
-
VALUES (?, ?, ?, ?,
|
|
527
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
439
528
|
ON CONFLICT(tenant_id, project_key) DO UPDATE SET
|
|
440
529
|
display_name = COALESCE(excluded.display_name, knowledge_projects.display_name),
|
|
441
530
|
updated_at = excluded.updated_at`)
|
|
442
|
-
.run(tenant, key, displayName ?? null, opts?.registeredBy ?? 'default', now, now);
|
|
531
|
+
.run(tenant, key, displayName ?? null, opts?.registeredBy ?? 'default', opts?.visibility ?? 'private', now, now);
|
|
443
532
|
}
|
|
444
533
|
/**
|
|
445
534
|
* 删除项目登记。非空项目拒绝(先卸载/删除项目下源),避免孤儿源与静默收养。
|
|
@@ -453,50 +542,67 @@ export class KnowledgeSourceStore {
|
|
|
453
542
|
if (sourceCount > 0) {
|
|
454
543
|
throw new Error(`project "${projectKey}" still has ${sourceCount} source(s); remove or reassign them first`);
|
|
455
544
|
}
|
|
545
|
+
// 同 key 可跨域各登记一行(评审 B1):删除只作用于调用方域内的行——跨域删除既是越权
|
|
546
|
+
// 破坏,又会把「无登记行=孤儿不设闸」变成跨域读旁路
|
|
456
547
|
const res = this.db.raw
|
|
457
|
-
.prepare('DELETE FROM knowledge_projects WHERE project_key = ?')
|
|
458
|
-
.run(projectKey);
|
|
548
|
+
.prepare('DELETE FROM knowledge_projects WHERE project_key = ? AND tenant_id = ?')
|
|
549
|
+
.run(projectKey, tenant);
|
|
459
550
|
this.db.raw
|
|
460
|
-
.prepare('DELETE FROM knowledge_project_agents WHERE project_key = ?')
|
|
461
|
-
.run(projectKey);
|
|
551
|
+
.prepare('DELETE FROM knowledge_project_agents WHERE project_key = ? AND tenant_id = ?')
|
|
552
|
+
.run(projectKey, tenant);
|
|
462
553
|
this.db.raw
|
|
463
|
-
.prepare("DELETE FROM knowledge_session_visibility WHERE target_type = 'project' AND target_id = ?")
|
|
464
|
-
.run(projectKey);
|
|
554
|
+
.prepare("DELETE FROM knowledge_session_visibility WHERE target_type = 'project' AND target_id = ? AND tenant_id = ?")
|
|
555
|
+
.run(projectKey, tenant);
|
|
465
556
|
return Number(res.changes ?? 0) > 0;
|
|
466
557
|
}
|
|
467
558
|
/**
|
|
468
|
-
* 源是否对 (agent, session)
|
|
559
|
+
* 源是否对 (agent, session) 可见(租户闸 ⊕ 网关漏斗 ⊕ base ⊕ session overlay)
|
|
469
560
|
*/
|
|
470
561
|
isVisible(source, agentId, sessionId, identity) {
|
|
471
562
|
if (source.status === 'removed' || source.status === 'disabled')
|
|
472
563
|
return false;
|
|
564
|
+
if (!this.isTenantVisible(source, identity))
|
|
565
|
+
return false;
|
|
473
566
|
if (!this.isGatewayVisible(source, identity))
|
|
474
567
|
return false;
|
|
568
|
+
// 项目跨域闸(收紧):project 域源还须其项目行对本身份可见(base 与 session overlay
|
|
569
|
+
// 两路 grant 都不得绕过;写侧另有 fail-loud 防线)
|
|
570
|
+
if (source.scopeRef.level === 'project' &&
|
|
571
|
+
!this.isProjectVisible(source, source.scopeRef.key, identity)) {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
475
574
|
return this.applySessionOverlay(source, this.isBaseVisible(source, agentId, sessionId, identity), sessionId, identity);
|
|
476
575
|
}
|
|
477
|
-
/**
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
576
|
+
/**
|
|
577
|
+
* 租户闸(arch/multi-tenancy.md §4/§6):有归属租户即生效(不依赖 gatewayId)。
|
|
578
|
+
* platform 域(PLATFORM_TENANT_ID)= broker/运维面,全量可见;其余只见本域 ∪ platform;
|
|
579
|
+
* 无归属 = DEFAULT 域(fail-closed,评审 MAJ-2:不做全量放行——存量语义即 default 域)。
|
|
580
|
+
*/
|
|
581
|
+
isTenantVisible(source, identity) {
|
|
582
|
+
const row = this.sourceRow(source.id);
|
|
583
|
+
if (!row)
|
|
481
584
|
return true;
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
585
|
+
return tenantGateAllows(row.tenant_id, identity ?? {});
|
|
586
|
+
}
|
|
587
|
+
/** 网关漏斗:registered_by=己方 ∪ public(租户闸已由 isTenantVisible 独立收口) */
|
|
588
|
+
isGatewayVisible(source, identity) {
|
|
589
|
+
const row = this.sourceRow(source.id);
|
|
485
590
|
if (!row)
|
|
486
591
|
return true;
|
|
487
|
-
|
|
488
|
-
if (row.tenant_id && row.tenant_id !== tenant)
|
|
489
|
-
return false;
|
|
490
|
-
if (row.registered_by && row.registered_by !== gw && row.visibility !== 'public') {
|
|
491
|
-
return false;
|
|
492
|
-
}
|
|
493
|
-
return true;
|
|
592
|
+
return ownerFunnelAllows(row.registered_by, row.visibility, identity ?? {});
|
|
494
593
|
}
|
|
495
594
|
/** Agent 级 base 可见(不含会话 overlay;排查用) */
|
|
496
595
|
isBaseVisible(source, agentId, sessionId, identity) {
|
|
497
596
|
if (source.status === 'removed' || source.status === 'disabled')
|
|
498
597
|
return false;
|
|
499
598
|
const { level, key } = source.scopeRef;
|
|
599
|
+
// 项目跨域闸(收紧):project 域源的任何 grant(挂载库/项目分配/session overlay)
|
|
600
|
+
// 都不得绕过项目行可见性
|
|
601
|
+
if (level === 'project' && !this.isProjectVisible(source, key, identity))
|
|
602
|
+
return false;
|
|
603
|
+
// 挂载库(T3b,§6 并集):显式挂载覆盖基础可见性(租户闸仍在其上)
|
|
604
|
+
if (this.isMounted(agentId, source.id, identity))
|
|
605
|
+
return true;
|
|
500
606
|
if (level === 'global') {
|
|
501
607
|
return !this.isHidden(agentId, source.id, identity);
|
|
502
608
|
}
|
|
@@ -523,23 +629,39 @@ export class KnowledgeSourceStore {
|
|
|
523
629
|
return base;
|
|
524
630
|
}
|
|
525
631
|
sessionOp(sessionId, targetType, targetId, identity) {
|
|
632
|
+
const gate = tenantOwnGateSql('tenant_id', identity ?? {});
|
|
633
|
+
const order = tenantPrecedenceOrder('tenant_id', identity ?? {});
|
|
526
634
|
const row = this.db.raw
|
|
527
635
|
.prepare(`SELECT op FROM knowledge_session_visibility
|
|
528
|
-
WHERE
|
|
529
|
-
AND target_type = ? AND target_id =
|
|
530
|
-
|
|
636
|
+
WHERE ${gate.sql} AND gateway_id = ? AND local_session_id = ?
|
|
637
|
+
AND target_type = ? AND target_id = ?
|
|
638
|
+
ORDER BY ${order.sql} LIMIT 1`)
|
|
639
|
+
.get(...gate.params, identity?.gatewayId ?? 'default', sessionId, targetType, targetId, ...order.params);
|
|
531
640
|
return row?.op ?? null;
|
|
532
641
|
}
|
|
533
642
|
isHidden(agentId, sourceId, identity) {
|
|
643
|
+
return this.agentOp(agentId, sourceId, identity) === 'exclude';
|
|
644
|
+
}
|
|
645
|
+
/** 挂载(T3b):include 覆盖 = 挂载库(§6 命中=挂载库∪本租户公共∪platform 公共) */
|
|
646
|
+
isMounted(agentId, sourceId, identity) {
|
|
647
|
+
return this.agentOp(agentId, sourceId, identity) === 'include';
|
|
648
|
+
}
|
|
649
|
+
/** (agent, source) 的可见性 override(单 op:include=挂载 / exclude=隐藏;无行=基础语义) */
|
|
650
|
+
agentOp(agentId, sourceId, identity) {
|
|
651
|
+
const gate = tenantOwnGateSql('tenant_id', identity ?? {});
|
|
652
|
+
const order = tenantPrecedenceOrder('tenant_id', identity ?? {});
|
|
534
653
|
const row = this.db.raw
|
|
535
|
-
.prepare(
|
|
536
|
-
|
|
537
|
-
|
|
654
|
+
.prepare(`SELECT op FROM knowledge_agent_visibility
|
|
655
|
+
WHERE ${gate.sql} AND gateway_id = ? AND local_agent_id = ? AND source_id = ?
|
|
656
|
+
ORDER BY ${order.sql} LIMIT 1`)
|
|
657
|
+
.get(...gate.params, identity?.gatewayId ?? 'default', agentId, sourceId, ...order.params);
|
|
658
|
+
return row?.op ?? null;
|
|
538
659
|
}
|
|
539
660
|
isProjectAssigned(projectKey, agentId, identity) {
|
|
661
|
+
const gate = tenantOwnGateSql('tenant_id', identity ?? {});
|
|
540
662
|
const row = this.db.raw
|
|
541
|
-
.prepare(
|
|
542
|
-
.get(
|
|
663
|
+
.prepare(`SELECT 1 AS ok FROM knowledge_project_agents WHERE ${gate.sql} AND gateway_id = ? AND project_key = ? AND local_agent_id = ?`)
|
|
664
|
+
.get(...gate.params, identity?.gatewayId ?? 'default', projectKey, agentId);
|
|
543
665
|
return Boolean(row);
|
|
544
666
|
}
|
|
545
667
|
// ── Catalog ──
|
|
@@ -627,7 +749,111 @@ export class KnowledgeSourceStore {
|
|
|
627
749
|
* 可见源列表(effective = base ⊕ session overlay;含 hiddenFromCatalog)
|
|
628
750
|
*/
|
|
629
751
|
listVisible(agentId, sessionId, identity) {
|
|
630
|
-
|
|
752
|
+
// 2N 优化(T3b):一次批量装载 source 行进同步缓存(filter 全程无 await,无并发窗口),
|
|
753
|
+
// isTenantVisible/isGatewayVisible 走缓存,不再每源一查
|
|
754
|
+
const rows = this.db.raw
|
|
755
|
+
.prepare('SELECT id, tenant_id, registered_by, visibility FROM knowledge_sources')
|
|
756
|
+
.all();
|
|
757
|
+
this.rowCache = new Map(rows.map((r) => [r.id, r]));
|
|
758
|
+
try {
|
|
759
|
+
return this.withProjectRowCache(() => this.list().filter((s) => this.isVisible(s, agentId, sessionId, identity)));
|
|
760
|
+
}
|
|
761
|
+
finally {
|
|
762
|
+
this.rowCache = null;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* 批量判定期的项目行缓存(评审 m6):一次装载全部项目行,批次内逐源判定走缓存;
|
|
767
|
+
* 可嵌套(外层已开则复用),finally 必须还原(防缓存外泄到批次外的读路径)。
|
|
768
|
+
*/
|
|
769
|
+
withProjectRowCache(fn) {
|
|
770
|
+
if (this.projectRowCache)
|
|
771
|
+
return fn();
|
|
772
|
+
const rows = this.db.raw
|
|
773
|
+
.prepare('SELECT tenant_id, project_key, display_name, visibility FROM knowledge_projects')
|
|
774
|
+
.all();
|
|
775
|
+
const map = new Map();
|
|
776
|
+
for (const r of rows) {
|
|
777
|
+
const list = map.get(r.project_key) ?? [];
|
|
778
|
+
list.push({ tenant_id: r.tenant_id, display_name: r.display_name, visibility: r.visibility });
|
|
779
|
+
map.set(r.project_key, list);
|
|
780
|
+
}
|
|
781
|
+
this.projectRowCache = map;
|
|
782
|
+
try {
|
|
783
|
+
return fn();
|
|
784
|
+
}
|
|
785
|
+
finally {
|
|
786
|
+
this.projectRowCache = null;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
/** source 行读取(listVisible 批处理期走同步缓存) */
|
|
790
|
+
sourceRow(id) {
|
|
791
|
+
if (this.rowCache)
|
|
792
|
+
return this.rowCache.get(id);
|
|
793
|
+
return this.db.raw
|
|
794
|
+
.prepare('SELECT tenant_id, registered_by, visibility FROM knowledge_sources WHERE id = ?')
|
|
795
|
+
.get(id);
|
|
796
|
+
}
|
|
797
|
+
/** 项目行按 key 批量缓存读取(缓存期一次装载;否则按 key 单查——idx_knowledge_projects_key) */
|
|
798
|
+
projectRowsByKey(projectKey) {
|
|
799
|
+
if (this.projectRowCache)
|
|
800
|
+
return this.projectRowCache.get(projectKey) ?? [];
|
|
801
|
+
return this.db.raw
|
|
802
|
+
.prepare('SELECT tenant_id, display_name, visibility FROM knowledge_projects WHERE project_key = ?')
|
|
803
|
+
.all(projectKey);
|
|
804
|
+
}
|
|
805
|
+
/** 项目行按源归属域解析(同一 project_key 可在多个租户域各登记一行,必须按域取行) */
|
|
806
|
+
projectRow(projectKey, sourceTenant) {
|
|
807
|
+
const norm = (t) => (t ? t : null);
|
|
808
|
+
return this.projectRowsByKey(projectKey).find((r) => norm(r.tenant_id) === norm(sourceTenant ?? null));
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* 项目跨域闸(arch/multi-tenancy.md §6):本域项目可见;platform 项目仅
|
|
812
|
+
* `visibility='public'` 才对租户可见(跨租户共享唯一合法形态 = platform 公共);
|
|
813
|
+
* 项目行按**源自身归属域**解析;无登记行(孤儿挂载)= 无归属,不设闸。
|
|
814
|
+
*/
|
|
815
|
+
isProjectVisible(source, projectKey, identity) {
|
|
816
|
+
const row = this.projectRow(projectKey, this.sourceRow(source.id)?.tenant_id);
|
|
817
|
+
if (!row)
|
|
818
|
+
return true;
|
|
819
|
+
return projectGateAllows(row.tenant_id, row.visibility, identity ?? {});
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* 项目跨域闸(写侧防绕过用):key 必须存在**对写者可见**的登记行(任一域命中即放行);
|
|
823
|
+
* 完全无登记行 = 孤儿挂载,不设闸(既有语义)。
|
|
824
|
+
*/
|
|
825
|
+
isProjectVisibleTo(projectKey, identity) {
|
|
826
|
+
const count = this.db.raw
|
|
827
|
+
.prepare('SELECT COUNT(*) AS n FROM knowledge_projects WHERE project_key = ?')
|
|
828
|
+
.get(projectKey).n;
|
|
829
|
+
// 完全无登记行 = 孤儿挂载,不设闸(既有语义)
|
|
830
|
+
if (count === 0)
|
|
831
|
+
return true;
|
|
832
|
+
return this.resolveProjectRow(projectKey, identity) !== undefined;
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* 项目行解析(管理面域继承用):返回对 identity 可见的项目行(同 key 跨域多行时
|
|
836
|
+
* 本域优先,其次 platform,再按域名字典序);无可见行 = null。
|
|
837
|
+
*/
|
|
838
|
+
getProject(projectKey, identity) {
|
|
839
|
+
const row = this.resolveProjectRow(projectKey, identity);
|
|
840
|
+
if (!row)
|
|
841
|
+
return null;
|
|
842
|
+
return {
|
|
843
|
+
tenantId: row.tenant_id ?? 'default',
|
|
844
|
+
projectKey,
|
|
845
|
+
displayName: row.display_name ?? undefined,
|
|
846
|
+
visibility: row.visibility === 'public' ? 'public' : 'private',
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
resolveProjectRow(projectKey, identity) {
|
|
850
|
+
const rows = this.projectRowsByKey(projectKey);
|
|
851
|
+
const visible = rows.filter((r) => projectGateAllows(r.tenant_id, r.visibility, identity ?? {}));
|
|
852
|
+
if (visible.length === 0)
|
|
853
|
+
return undefined;
|
|
854
|
+
const own = identity?.tenantId ?? 'default';
|
|
855
|
+
const rank = (t) => t === own ? 0 : t === PLATFORM_TENANT_ID ? 1 : 2;
|
|
856
|
+
return [...visible].sort((a, b) => rank(a.tenant_id) - rank(b.tenant_id) || String(a.tenant_id).localeCompare(String(b.tenant_id)))[0];
|
|
631
857
|
}
|
|
632
858
|
/** Agent 级 base 可见源(不含会话 overlay) */
|
|
633
859
|
listBaseVisible(agentId, sessionId, identity) {
|
|
@@ -4,12 +4,11 @@
|
|
|
4
4
|
* 为什么是进程而不是 Worker:知识索引重且稳定不可控,不得与 Gateway 共命运。
|
|
5
5
|
* Worker 仍共享堆/OOM 与进程生命周期,只是把 event loop 挪开,不是故障隔离。
|
|
6
6
|
*
|
|
7
|
-
* fork
|
|
8
|
-
*
|
|
9
|
-
* -
|
|
10
|
-
*
|
|
11
|
-
* -
|
|
12
|
-
* 换成捆绑 node.exe 当 execPath 会切断 Windows 上的 fork IPC。
|
|
7
|
+
* fork 根因约束见 `forkChildNodeOptions`(`execArgv: []` / 剥离 `NODE_CHANNEL_*` /
|
|
8
|
+
* `windowsHide` / Electron 宿主)。本处只保留两点知识专属取舍:
|
|
9
|
+
* - `stdio` 全 `ignore` 而非 `inherit`:Windows 管道句柄继承会干扰 IPC
|
|
10
|
+
* (run 实例相反——它的日志要靠继承的 fd 落 `logs/gateway.log`)。
|
|
11
|
+
* - **保持** `process.execPath`:Electron 下换成捆绑 node.exe 会切断 Windows 上的 fork IPC。
|
|
13
12
|
*/
|
|
14
13
|
import { type ChildProcess } from 'node:child_process';
|
|
15
14
|
import type { KnowledgeServiceToken } from './http-app.js';
|
|
@@ -28,7 +27,12 @@ export interface KnowledgeServiceProcessOptions {
|
|
|
28
27
|
retriever?: KnowledgeBootRetriever;
|
|
29
28
|
/** false = 索引稳定后不自动生成 generatedDescription(knowledge.catalog.autoDescribe) */
|
|
30
29
|
autoDescribe?: boolean;
|
|
31
|
-
|
|
30
|
+
/** 跨租户共享闸(B-5):允许创建 visibility='public' 项目;缺省 false(fail-closed) */
|
|
31
|
+
allowPublic?: boolean;
|
|
32
|
+
sqliteVec?: boolean | {
|
|
33
|
+
extensionPath?: string;
|
|
34
|
+
require?: boolean;
|
|
35
|
+
};
|
|
32
36
|
embeddingModels?: {
|
|
33
37
|
providers?: Record<string, unknown>;
|
|
34
38
|
embedding?: unknown;
|
|
@@ -38,7 +42,12 @@ export interface KnowledgeServiceProcessOptions {
|
|
|
38
42
|
export interface KnowledgeServiceProcessHandle {
|
|
39
43
|
child: ChildProcess;
|
|
40
44
|
port: number;
|
|
41
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* 停止子进程并**等待其退出**。connected 时先发 close 让其优雅收摊,3s 未退则
|
|
47
|
+
* SIGTERM,再 2s 未退则 SIGKILL;通道已断(宿主硬死)时直接走信号升级。
|
|
48
|
+
* SIGKILL 之后仍等 1s 观察 exit,超时则放行(避免不可观测的僵尸让调用方永久挂起,
|
|
49
|
+
* 代价是极端情形下进程可能尚未完全退出)。
|
|
50
|
+
*/
|
|
42
51
|
stop(): Promise<void>;
|
|
43
52
|
}
|
|
44
53
|
/**
|
|
@@ -55,3 +64,13 @@ export declare function buildKnowledgeServiceBootFile(opts: KnowledgeServiceProc
|
|
|
55
64
|
* @returns child + 端口 + stop
|
|
56
65
|
*/
|
|
57
66
|
export declare function startKnowledgeServiceProcess(opts: KnowledgeServiceProcessOptions): Promise<KnowledgeServiceProcessHandle>;
|
|
67
|
+
/**
|
|
68
|
+
* 停止 knowledge 子进程并等待它退出(转调共享实现,预算为本服务的历史取值)。
|
|
69
|
+
*
|
|
70
|
+
* 语义与告警行为见 `stopForkedChild`:3s 优雅宽限 → SIGTERM → 2s → SIGKILL →
|
|
71
|
+
* 1s 兜底放行。
|
|
72
|
+
*
|
|
73
|
+
* @param child - knowledge 子进程
|
|
74
|
+
* @returns 子进程已退出(或兜底放行)时 resolve
|
|
75
|
+
*/
|
|
76
|
+
export declare function stopChild(child: ChildProcess): Promise<void>;
|