@octopi-agent/engine 0.102.10 → 0.121.10
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/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 +6 -2
- package/dist/config-admin/index.js +3 -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 +71 -0
- package/dist/config-admin/onboarding-apply.js +235 -0
- package/dist/config-admin/onboarding.d.ts +154 -0
- package/dist/config-admin/onboarding.js +270 -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
|
@@ -4,34 +4,19 @@
|
|
|
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 { fork } from 'node:child_process';
|
|
15
14
|
import { fileURLToPath } from 'node:url';
|
|
16
|
-
import {
|
|
17
|
-
import { tmpdir } from 'node:os';
|
|
15
|
+
import { rm } from 'node:fs/promises';
|
|
18
16
|
import { join } from 'node:path';
|
|
17
|
+
import { forkChildNodeOptions, stopForkedChild } from '../../fork-child.js';
|
|
18
|
+
import { createPrivateTempDir, writeSecretFileSync } from '../../file-mode.js';
|
|
19
19
|
import { resolveWorkerUrl } from '../../worker-path.js';
|
|
20
|
-
function isElectronHost() {
|
|
21
|
-
return (Boolean(process.versions.electron) ||
|
|
22
|
-
/electron|MiMo\.exe/i.test(process.execPath));
|
|
23
|
-
}
|
|
24
|
-
function childNodeEnv() {
|
|
25
|
-
const env = { ...process.env };
|
|
26
|
-
// 父进程可能是 IPC 子进程(vitest worker / octopi serve daemon):不得把通道句柄带进孙进程
|
|
27
|
-
delete env.NODE_CHANNEL_FD;
|
|
28
|
-
delete env.NODE_CHANNEL_SERIALIZATION;
|
|
29
|
-
delete env.NODE_UNIQUE_ID;
|
|
30
|
-
if (isElectronHost()) {
|
|
31
|
-
env.ELECTRON_RUN_AS_NODE = '1';
|
|
32
|
-
}
|
|
33
|
-
return env;
|
|
34
|
-
}
|
|
35
20
|
/**
|
|
36
21
|
* 组装 boot 文件内容 — Gateway 调参面 → Service 子进程的唯一交接面(纯函数,可单测锁 hop)。
|
|
37
22
|
*
|
|
@@ -49,7 +34,8 @@ export function buildKnowledgeServiceBootFile(opts) {
|
|
|
49
34
|
ingest: opts.ingest,
|
|
50
35
|
retriever: opts.retriever,
|
|
51
36
|
autoDescribe: opts.autoDescribe,
|
|
52
|
-
|
|
37
|
+
allowPublic: opts.allowPublic,
|
|
38
|
+
sqliteVec: opts.sqliteVec,
|
|
53
39
|
embeddingModels: opts.embeddingModels ?? null,
|
|
54
40
|
};
|
|
55
41
|
}
|
|
@@ -61,18 +47,30 @@ export function buildKnowledgeServiceBootFile(opts) {
|
|
|
61
47
|
*/
|
|
62
48
|
export async function startKnowledgeServiceProcess(opts) {
|
|
63
49
|
const entry = fileURLToPath(resolveWorkerUrl('./knowledge-serve-child.js', import.meta.url));
|
|
64
|
-
|
|
50
|
+
// 私有目录 + 真实权限收紧:boot 含 loopback token,Windows 上 `mode` 不生效。
|
|
51
|
+
// 建目录之后的每一步都可能抛(写 secret、fork),而 `finally` 在 try 之外不生效——
|
|
52
|
+
// 所以把它们全部纳入 try,失败时连目录(含 token)一起回收。
|
|
53
|
+
const bootDir = createPrivateTempDir('octopi-kn-boot-');
|
|
65
54
|
const bootPath = join(bootDir, 'boot.json');
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
55
|
+
let child;
|
|
56
|
+
try {
|
|
57
|
+
writeSecretFileSync(bootPath, JSON.stringify(buildKnowledgeServiceBootFile(opts)));
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
await rm(bootDir, { recursive: true, force: true }).catch(() => undefined);
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
// execPath 保持 process.execPath:Electron 下换捆绑 node 会切断 IPC
|
|
65
|
+
child = fork(entry, [bootPath], forkChildNodeOptions({
|
|
66
|
+
stdio: ['ignore', 'ignore', 'ignore'],
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
// fork 同步抛:boot 已写而子进程没机会读删——父进程兜底清 secret
|
|
71
|
+
await rm(bootDir, { recursive: true, force: true }).catch(() => undefined);
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
76
74
|
const startTimeoutMs = opts.startTimeoutMs ?? 30_000;
|
|
77
75
|
try {
|
|
78
76
|
const port = await new Promise((resolve, reject) => {
|
|
@@ -110,61 +108,22 @@ export async function startKnowledgeServiceProcess(opts) {
|
|
|
110
108
|
};
|
|
111
109
|
}
|
|
112
110
|
finally {
|
|
113
|
-
|
|
111
|
+
// 清理失败不改变启动结果,但必须留痕:这类 EPERM 正是"句柄未释放就删目录"的症状,
|
|
112
|
+
// 静默吞掉会让下一轮排障从零开始。
|
|
113
|
+
await rm(bootDir, { recursive: true, force: true }).catch((err) => {
|
|
114
|
+
console.warn(`[knowledge] boot dir cleanup failed (${String(err)}): ${bootDir}`);
|
|
115
|
+
});
|
|
114
116
|
}
|
|
115
117
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const hard = setTimeout(() => {
|
|
128
|
-
try {
|
|
129
|
-
child.kill('SIGKILL');
|
|
130
|
-
}
|
|
131
|
-
catch {
|
|
132
|
-
/* ignore */
|
|
133
|
-
}
|
|
134
|
-
resolve();
|
|
135
|
-
}, 2_000);
|
|
136
|
-
hard.unref?.();
|
|
137
|
-
}, 3_000);
|
|
138
|
-
child.once('exit', () => {
|
|
139
|
-
clearTimeout(timer);
|
|
140
|
-
resolve();
|
|
141
|
-
});
|
|
142
|
-
try {
|
|
143
|
-
if (child.connected) {
|
|
144
|
-
child.send({ type: 'close' }, () => {
|
|
145
|
-
/* send callback only observes channel errors; exit path resolves */
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
clearTimeout(timer);
|
|
150
|
-
try {
|
|
151
|
-
child.kill('SIGTERM');
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
154
|
-
/* ignore */
|
|
155
|
-
}
|
|
156
|
-
resolve();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
catch {
|
|
160
|
-
clearTimeout(timer);
|
|
161
|
-
try {
|
|
162
|
-
child.kill('SIGTERM');
|
|
163
|
-
}
|
|
164
|
-
catch {
|
|
165
|
-
/* ignore */
|
|
166
|
-
}
|
|
167
|
-
resolve();
|
|
168
|
-
}
|
|
169
|
-
});
|
|
118
|
+
/**
|
|
119
|
+
* 停止 knowledge 子进程并等待它退出(转调共享实现,预算为本服务的历史取值)。
|
|
120
|
+
*
|
|
121
|
+
* 语义与告警行为见 `stopForkedChild`:3s 优雅宽限 → SIGTERM → 2s → SIGKILL →
|
|
122
|
+
* 1s 兜底放行。
|
|
123
|
+
*
|
|
124
|
+
* @param child - knowledge 子进程
|
|
125
|
+
* @returns 子进程已退出(或兜底放行)时 resolve
|
|
126
|
+
*/
|
|
127
|
+
export function stopChild(child) {
|
|
128
|
+
return stopForkedChild(child, { graceMs: 3_000, termMs: 2_000, reapFallbackMs: 1_000, label: 'knowledge' });
|
|
170
129
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** 闸身份:缺省字段按 DEFAULT 域(fail-closed,评审 MAJ-2)解析。 */
|
|
2
|
+
export interface GateIdentity {
|
|
3
|
+
tenantId?: string;
|
|
4
|
+
gatewayId?: string;
|
|
5
|
+
}
|
|
6
|
+
/** SQL 谓词片段:`sql` 直接拼接进 WHERE,`params` 按序绑定。 */
|
|
7
|
+
export interface GateSql {
|
|
8
|
+
sql: string;
|
|
9
|
+
params: string[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 租户域闸(内存形态)。
|
|
13
|
+
*
|
|
14
|
+
* @param rowTenant 行归属租户(来源行的 tenant_id);空值视为无归属(可见)
|
|
15
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
16
|
+
* @returns 行是否落在该身份的可见域内
|
|
17
|
+
*/
|
|
18
|
+
export declare function tenantGateAllows(rowTenant: string | undefined | null, identity?: GateIdentity): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* 租户域闸(SQL 形态;与 {@link tenantGateAllows} 逐格等价)。
|
|
21
|
+
*
|
|
22
|
+
* @param column tenant_id 列名(可带表别名前缀)
|
|
23
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
24
|
+
* @returns 可拼接的 WHERE 谓词与绑定参数
|
|
25
|
+
*/
|
|
26
|
+
export declare function tenantGateSql(column?: string, identity?: GateIdentity): GateSql;
|
|
27
|
+
/**
|
|
28
|
+
* 本域闸(内存形态)——用于**写入物**(agent/session 覆盖行、挂载行、主体行):
|
|
29
|
+
* 这类行按域归属隔离,platform 行**不**自动扩散到租户(与资产闸 {@link tenantGateAllows}
|
|
30
|
+
* 的「platform=公共」语义有意不同:覆盖行扩散=平台行悄悄操纵他租户 agent 可见性);
|
|
31
|
+
* 仅 platform 读身份(broker 运维面)= 全量。
|
|
32
|
+
*
|
|
33
|
+
* @param rowTenant 行归属租户;空值视为无归属(可见)
|
|
34
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
35
|
+
*/
|
|
36
|
+
export declare function tenantOwnGateAllows(rowTenant: string | undefined | null, identity?: GateIdentity): boolean;
|
|
37
|
+
/** 本域闸(SQL 形态;与 {@link tenantOwnGateAllows} 逐格等价) */
|
|
38
|
+
export declare function tenantOwnGateSql(column?: string, identity?: GateIdentity): GateSql;
|
|
39
|
+
/**
|
|
40
|
+
* 单行读取的确定性排序(同 (gateway, agent/session, target) 可在多域各有一行,
|
|
41
|
+
* `.get()` 无序返回任意行会让可见性判定漂移):本域行优先,其次 platform 行,再按域名字典序。
|
|
42
|
+
*/
|
|
43
|
+
export declare function tenantPrecedenceOrder(column?: string, identity?: GateIdentity): GateSql;
|
|
44
|
+
/**
|
|
45
|
+
* 归属漏斗(内存形态;只用于源)。
|
|
46
|
+
*
|
|
47
|
+
* @param rowRegisteredBy 行登记网关(registered_by)
|
|
48
|
+
* @param rowVisibility 行可见性('public' | 'private')
|
|
49
|
+
* @param identity 读身份;undefined = 宿主面(不设闸);无 gatewayId = 不设漏斗
|
|
50
|
+
* @returns 行是否通过归属漏斗
|
|
51
|
+
*/
|
|
52
|
+
export declare function ownerFunnelAllows(rowRegisteredBy: string | undefined | null, rowVisibility: string | undefined | null, identity?: GateIdentity): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 归属漏斗(SQL 形态;与 {@link ownerFunnelAllows} 逐格等价)。
|
|
55
|
+
*
|
|
56
|
+
* @param registeredByColumn registered_by 列名(可带表别名前缀)
|
|
57
|
+
* @param visibilityColumn visibility 列名(可带表别名前缀)
|
|
58
|
+
* @param identity 读身份;undefined = 宿主面(不设闸);无 gatewayId = 不设漏斗
|
|
59
|
+
* @returns 可拼接的 WHERE 谓词与绑定参数
|
|
60
|
+
*/
|
|
61
|
+
export declare function ownerFunnelSql(registeredByColumn?: string, visibilityColumn?: string, identity?: GateIdentity): GateSql;
|
|
62
|
+
/**
|
|
63
|
+
* 项目跨域闸(内存形态):本域项目(含无归属)始终可见;platform 项目仅
|
|
64
|
+
* `visibility='public'` 才对租户可见(跨租户共享唯一合法形态 = platform 公共,
|
|
65
|
+
* arch/multi-tenancy.md §6);platform 身份 = 全量视图。
|
|
66
|
+
*
|
|
67
|
+
* @param rowTenant 项目行归属租户
|
|
68
|
+
* @param rowVisibility 项目行可见性('public' | 'private')
|
|
69
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
70
|
+
* @returns 项目行是否对该身份可见
|
|
71
|
+
*/
|
|
72
|
+
export declare function projectGateAllows(rowTenant: string | undefined | null, rowVisibility: string | undefined | null, identity?: GateIdentity): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* 项目跨域闸(SQL 形态;与 {@link projectGateAllows} 逐格等价)。
|
|
75
|
+
*
|
|
76
|
+
* @param tenantColumn tenant_id 列名(可带表别名前缀)
|
|
77
|
+
* @param visibilityColumn visibility 列名(可带表别名前缀)
|
|
78
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
79
|
+
* @returns 可拼接的 WHERE 谓词与绑定参数
|
|
80
|
+
*/
|
|
81
|
+
export declare function projectGateSql(tenantColumn?: string, visibilityColumn?: string, identity?: GateIdentity): GateSql;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 租户闸(arch/multi-tenancy.md §6:命中 = 挂载库 ∪ 本租户公共 ∪ platform 公共)。
|
|
3
|
+
*
|
|
4
|
+
* 单源两形态:本模块同时导出内存判定与 SQL 谓词,二者必须逐格等价
|
|
5
|
+
* (tests/harness/knowledge-tenant-gate.test.ts 固化)。所有读路径改用本模块,
|
|
6
|
+
* 禁止再手写 `tenant_id = ?` 硬等值——platform 域(broker 全量视图)会被硬等值清空
|
|
7
|
+
* (MAJ-4 遗留:管理面读口租户闸)。
|
|
8
|
+
*
|
|
9
|
+
* 语义(三闸各司其职,勿混用):
|
|
10
|
+
* - `tenantGate*` 租户域闸:platform=全量;行无归属=可见;platform 行=对全租户可见;其余=本域。
|
|
11
|
+
* - `ownerFunnel*` 归属漏斗(**只属于源**):registered_by=己方 ∪ visibility='public'。
|
|
12
|
+
* 项目**不**走漏斗:同租户跨网关共享项目是既定设计
|
|
13
|
+
* (tests/harness/knowledge-cross-gateway.test.ts「catalog/挂载同口径」)。
|
|
14
|
+
* - `projectGate*` 项目跨域闸:本域项目可见;platform 项目仅 visibility='public' 才跨域可见。
|
|
15
|
+
*
|
|
16
|
+
* 无身份(identity === undefined)= 宿主面 = 不设闸(与 listSources/getSource 既有语义一致)。
|
|
17
|
+
*/
|
|
18
|
+
import { DEFAULT_TENANT_ID, PLATFORM_TENANT_ID } from '@octopi-agent/core/types.js';
|
|
19
|
+
/** 解析读身份的租户域;无身份返回 null(宿主面,不设闸)。 */
|
|
20
|
+
function tenantOf(identity) {
|
|
21
|
+
if (identity === undefined)
|
|
22
|
+
return null;
|
|
23
|
+
return identity.tenantId ?? DEFAULT_TENANT_ID;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 租户域闸(内存形态)。
|
|
27
|
+
*
|
|
28
|
+
* @param rowTenant 行归属租户(来源行的 tenant_id);空值视为无归属(可见)
|
|
29
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
30
|
+
* @returns 行是否落在该身份的可见域内
|
|
31
|
+
*/
|
|
32
|
+
export function tenantGateAllows(rowTenant, identity) {
|
|
33
|
+
const tenant = tenantOf(identity);
|
|
34
|
+
if (tenant === null || tenant === PLATFORM_TENANT_ID)
|
|
35
|
+
return true;
|
|
36
|
+
if (!rowTenant)
|
|
37
|
+
return true;
|
|
38
|
+
if (rowTenant === PLATFORM_TENANT_ID)
|
|
39
|
+
return true;
|
|
40
|
+
return rowTenant === tenant;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 租户域闸(SQL 形态;与 {@link tenantGateAllows} 逐格等价)。
|
|
44
|
+
*
|
|
45
|
+
* @param column tenant_id 列名(可带表别名前缀)
|
|
46
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
47
|
+
* @returns 可拼接的 WHERE 谓词与绑定参数
|
|
48
|
+
*/
|
|
49
|
+
export function tenantGateSql(column = 'tenant_id', identity) {
|
|
50
|
+
const tenant = tenantOf(identity);
|
|
51
|
+
if (tenant === null || tenant === PLATFORM_TENANT_ID)
|
|
52
|
+
return { sql: '1 = 1', params: [] };
|
|
53
|
+
return {
|
|
54
|
+
sql: `(IFNULL(${column}, '') = '' OR ${column} = ? OR ${column} = ?)`,
|
|
55
|
+
params: [tenant, PLATFORM_TENANT_ID],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 本域闸(内存形态)——用于**写入物**(agent/session 覆盖行、挂载行、主体行):
|
|
60
|
+
* 这类行按域归属隔离,platform 行**不**自动扩散到租户(与资产闸 {@link tenantGateAllows}
|
|
61
|
+
* 的「platform=公共」语义有意不同:覆盖行扩散=平台行悄悄操纵他租户 agent 可见性);
|
|
62
|
+
* 仅 platform 读身份(broker 运维面)= 全量。
|
|
63
|
+
*
|
|
64
|
+
* @param rowTenant 行归属租户;空值视为无归属(可见)
|
|
65
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
66
|
+
*/
|
|
67
|
+
export function tenantOwnGateAllows(rowTenant, identity) {
|
|
68
|
+
const tenant = tenantOf(identity);
|
|
69
|
+
if (tenant === null || tenant === PLATFORM_TENANT_ID)
|
|
70
|
+
return true;
|
|
71
|
+
if (!rowTenant)
|
|
72
|
+
return true;
|
|
73
|
+
return rowTenant === tenant;
|
|
74
|
+
}
|
|
75
|
+
/** 本域闸(SQL 形态;与 {@link tenantOwnGateAllows} 逐格等价) */
|
|
76
|
+
export function tenantOwnGateSql(column = 'tenant_id', identity) {
|
|
77
|
+
const tenant = tenantOf(identity);
|
|
78
|
+
if (tenant === null || tenant === PLATFORM_TENANT_ID)
|
|
79
|
+
return { sql: '1 = 1', params: [] };
|
|
80
|
+
return {
|
|
81
|
+
sql: `(IFNULL(${column}, '') = '' OR ${column} = ?)`,
|
|
82
|
+
params: [tenant],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 单行读取的确定性排序(同 (gateway, agent/session, target) 可在多域各有一行,
|
|
87
|
+
* `.get()` 无序返回任意行会让可见性判定漂移):本域行优先,其次 platform 行,再按域名字典序。
|
|
88
|
+
*/
|
|
89
|
+
export function tenantPrecedenceOrder(column = 'tenant_id', identity) {
|
|
90
|
+
const tenant = tenantOf(identity) ?? '';
|
|
91
|
+
return {
|
|
92
|
+
sql: `CASE WHEN IFNULL(${column}, '') = ? THEN 0 WHEN ${column} = ? THEN 1 ELSE 2 END, ${column}`,
|
|
93
|
+
params: [tenant, PLATFORM_TENANT_ID],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 归属漏斗(内存形态;只用于源)。
|
|
98
|
+
*
|
|
99
|
+
* @param rowRegisteredBy 行登记网关(registered_by)
|
|
100
|
+
* @param rowVisibility 行可见性('public' | 'private')
|
|
101
|
+
* @param identity 读身份;undefined = 宿主面(不设闸);无 gatewayId = 不设漏斗
|
|
102
|
+
* @returns 行是否通过归属漏斗
|
|
103
|
+
*/
|
|
104
|
+
export function ownerFunnelAllows(rowRegisteredBy, rowVisibility, identity) {
|
|
105
|
+
if (identity === undefined)
|
|
106
|
+
return true;
|
|
107
|
+
const gw = identity.gatewayId;
|
|
108
|
+
if (!gw)
|
|
109
|
+
return true;
|
|
110
|
+
if (!rowRegisteredBy || rowRegisteredBy === gw)
|
|
111
|
+
return true;
|
|
112
|
+
return rowVisibility === 'public';
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 归属漏斗(SQL 形态;与 {@link ownerFunnelAllows} 逐格等价)。
|
|
116
|
+
*
|
|
117
|
+
* @param registeredByColumn registered_by 列名(可带表别名前缀)
|
|
118
|
+
* @param visibilityColumn visibility 列名(可带表别名前缀)
|
|
119
|
+
* @param identity 读身份;undefined = 宿主面(不设闸);无 gatewayId = 不设漏斗
|
|
120
|
+
* @returns 可拼接的 WHERE 谓词与绑定参数
|
|
121
|
+
*/
|
|
122
|
+
export function ownerFunnelSql(registeredByColumn = 'registered_by', visibilityColumn = 'visibility', identity) {
|
|
123
|
+
if (identity === undefined || !identity.gatewayId)
|
|
124
|
+
return { sql: '1 = 1', params: [] };
|
|
125
|
+
return {
|
|
126
|
+
sql: `(IFNULL(${registeredByColumn}, '') = '' OR ${registeredByColumn} = ? OR ${visibilityColumn} = 'public')`,
|
|
127
|
+
params: [identity.gatewayId],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 项目跨域闸(内存形态):本域项目(含无归属)始终可见;platform 项目仅
|
|
132
|
+
* `visibility='public'` 才对租户可见(跨租户共享唯一合法形态 = platform 公共,
|
|
133
|
+
* arch/multi-tenancy.md §6);platform 身份 = 全量视图。
|
|
134
|
+
*
|
|
135
|
+
* @param rowTenant 项目行归属租户
|
|
136
|
+
* @param rowVisibility 项目行可见性('public' | 'private')
|
|
137
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
138
|
+
* @returns 项目行是否对该身份可见
|
|
139
|
+
*/
|
|
140
|
+
export function projectGateAllows(rowTenant, rowVisibility, identity) {
|
|
141
|
+
const tenant = tenantOf(identity);
|
|
142
|
+
if (tenant === null || tenant === PLATFORM_TENANT_ID)
|
|
143
|
+
return true;
|
|
144
|
+
if (!rowTenant)
|
|
145
|
+
return true;
|
|
146
|
+
if (rowTenant === PLATFORM_TENANT_ID)
|
|
147
|
+
return rowVisibility === 'public';
|
|
148
|
+
return rowTenant === tenant;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 项目跨域闸(SQL 形态;与 {@link projectGateAllows} 逐格等价)。
|
|
152
|
+
*
|
|
153
|
+
* @param tenantColumn tenant_id 列名(可带表别名前缀)
|
|
154
|
+
* @param visibilityColumn visibility 列名(可带表别名前缀)
|
|
155
|
+
* @param identity 读身份;undefined = 宿主面(不设闸)
|
|
156
|
+
* @returns 可拼接的 WHERE 谓词与绑定参数
|
|
157
|
+
*/
|
|
158
|
+
export function projectGateSql(tenantColumn = 'tenant_id', visibilityColumn = 'visibility', identity) {
|
|
159
|
+
const tenant = tenantOf(identity);
|
|
160
|
+
if (tenant === null || tenant === PLATFORM_TENANT_ID)
|
|
161
|
+
return { sql: '1 = 1', params: [] };
|
|
162
|
+
return {
|
|
163
|
+
sql: `(IFNULL(${tenantColumn}, '') = '' OR ${tenantColumn} = ?` +
|
|
164
|
+
` OR (${tenantColumn} = ? AND ${visibilityColumn} = 'public'))`,
|
|
165
|
+
params: [tenant, PLATFORM_TENANT_ID],
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -17,6 +17,8 @@ import type { EmbeddingProvider } from '../memory/sqlite/embedding.js';
|
|
|
17
17
|
export interface WriteIdentity {
|
|
18
18
|
tenantId: string;
|
|
19
19
|
gatewayId: string;
|
|
20
|
+
/** 是否携带显式租户断言(x-octopi-tenant);归属打标区分「显式」与「token 域回落」 */
|
|
21
|
+
claimed?: boolean;
|
|
20
22
|
}
|
|
21
23
|
export interface WriteAbortStats {
|
|
22
24
|
cancelledQueued: number;
|
|
@@ -110,6 +112,10 @@ export interface KnowledgeWriteService {
|
|
|
110
112
|
unassignProject(identity: WriteIdentity, agentId: string, projectKey: string): Promise<void>;
|
|
111
113
|
hideSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
112
114
|
unhideSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
115
|
+
/** 挂载(T3b):agent 级 include 覆盖(§6 挂载库);翻转既有隐藏 */
|
|
116
|
+
mountSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
117
|
+
/** 卸载:撤 include,回基础可见性 */
|
|
118
|
+
unmountSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
113
119
|
replaceSessionVisibility(identity: WriteIdentity, agentId: string, sessionId: string, items: Array<{
|
|
114
120
|
targetType: string;
|
|
115
121
|
targetId: string;
|
|
@@ -123,7 +129,7 @@ export interface KnowledgeWriteService {
|
|
|
123
129
|
clearSessionVisibility(identity: WriteIdentity, sessionId: string, target?: {
|
|
124
130
|
targetType: 'project' | 'source';
|
|
125
131
|
targetId: string;
|
|
126
|
-
}): Promise<void>;
|
|
132
|
+
}, op?: 'include' | 'exclude'): Promise<void>;
|
|
127
133
|
isSourceOwner(sourceId: string, gatewayId: string): Promise<boolean>;
|
|
128
134
|
sourceVisibleToGateway(sourceId: string, gatewayId: string): Promise<boolean>;
|
|
129
135
|
startIngestRuntime(): Promise<void>;
|
|
@@ -141,6 +147,8 @@ export interface LocalWriteDeps {
|
|
|
141
147
|
ingest?: KnowledgeBootIngest | null;
|
|
142
148
|
/** false = 索引稳定后不自动生成 generatedDescription(knowledge.catalog.autoDescribe) */
|
|
143
149
|
autoDescribe?: boolean;
|
|
150
|
+
/** 跨租户共享闸(B-5):允许创建 visibility='public' 项目;缺省 false(fail-closed) */
|
|
151
|
+
allowPublic?: boolean;
|
|
144
152
|
}
|
|
145
153
|
/** 进程内唯一写者(测试 / :memory:)。 */
|
|
146
154
|
export declare class LocalKnowledgeWriteService implements KnowledgeWriteService {
|
|
@@ -183,6 +191,12 @@ export declare class LocalKnowledgeWriteService implements KnowledgeWriteService
|
|
|
183
191
|
listDescribePending(identity: WriteIdentity): Promise<string[]>;
|
|
184
192
|
/** 启发式/封锁描述生成(无 describePort:Service 侧永不含 LLM 出网,B 方案拍板) */
|
|
185
193
|
private describeWithSample;
|
|
194
|
+
/**
|
|
195
|
+
* 源写权限(arch/multi-tenancy.md §6,评审 MAJ-5):
|
|
196
|
+
* 网关漏斗(registered_by)⊕ 租户域(tenant_id)——platform 身份 = 运维豁免;
|
|
197
|
+
* 租户身份只可写本域源。缺行按无约束放行(老库/投影缺失),由调用方的 not-found 路径兜底。
|
|
198
|
+
*/
|
|
199
|
+
private assertSourceWritable;
|
|
186
200
|
private assertSourceOwner;
|
|
187
201
|
reindexSource(sourceId: string): Promise<void>;
|
|
188
202
|
reprocessFiles(sourceId: string, paths: string[]): Promise<WriteReprocessResult>;
|
|
@@ -200,6 +214,8 @@ export declare class LocalKnowledgeWriteService implements KnowledgeWriteService
|
|
|
200
214
|
unassignProject(identity: WriteIdentity, agentId: string, projectKey: string): Promise<void>;
|
|
201
215
|
hideSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
202
216
|
unhideSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
217
|
+
mountSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
218
|
+
unmountSource(identity: WriteIdentity, agentId: string, sourceId: string): Promise<void>;
|
|
203
219
|
replaceSessionVisibility(identity: WriteIdentity, _agentId: string, sessionId: string, items: Array<{
|
|
204
220
|
targetType: string;
|
|
205
221
|
targetId: string;
|
|
@@ -213,7 +229,7 @@ export declare class LocalKnowledgeWriteService implements KnowledgeWriteService
|
|
|
213
229
|
clearSessionVisibility(identity: WriteIdentity, sessionId: string, target?: {
|
|
214
230
|
targetType: 'project' | 'source';
|
|
215
231
|
targetId: string;
|
|
216
|
-
}): Promise<void>;
|
|
232
|
+
}, op?: 'include' | 'exclude'): Promise<void>;
|
|
217
233
|
isSourceOwner(sourceId: string, gatewayId: string): Promise<boolean>;
|
|
218
234
|
sourceVisibleToGateway(sourceId: string, gatewayId: string): Promise<boolean>;
|
|
219
235
|
startIngestRuntime(): Promise<void>;
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* 生产:Writer Worker 持有 knowledge.db + ingest;API 经 RPC 调用。
|
|
5
5
|
* 测试 / :memory::LocalKnowledgeWriteService 同进程实现(同一接口)。
|
|
6
6
|
*/
|
|
7
|
+
import { DEFAULT_TENANT_ID, PLATFORM_TENANT_ID } from '@octopi-agent/core/types.js';
|
|
7
8
|
import { KnowledgeSourceStore } from './source-store.js';
|
|
8
9
|
import { KnowledgeIndexStore } from './index-store.js';
|
|
9
10
|
import { MembershipStore } from './membership-store.js';
|
|
@@ -101,6 +102,15 @@ export class LocalKnowledgeWriteService {
|
|
|
101
102
|
code: 'not_resource_owner',
|
|
102
103
|
});
|
|
103
104
|
}
|
|
105
|
+
// 评审 m3:visibility='public' 是跨租户共享面动作,只接受显式 claim 'platform' 的写——
|
|
106
|
+
// 租户/无断言写造出的 'public' 不产生共享语义,如实拒绝而非假成功
|
|
107
|
+
if (input.visibility === 'public' && this.deps.allowPublic !== true) {
|
|
108
|
+
throw Object.assign(new Error("visibility 'public' is disabled (knowledge.projects.allowPublic=false) — public projects are cross-tenant shared assets"), { code: 'public_projects_disabled' });
|
|
109
|
+
}
|
|
110
|
+
if (input.visibility === 'public' &&
|
|
111
|
+
!(identity.claimed && identity.tenantId === PLATFORM_TENANT_ID)) {
|
|
112
|
+
throw Object.assign(new Error("visibility 'public' requires an explicit platform claim"), { code: 'visibility_requires_platform_claim' });
|
|
113
|
+
}
|
|
104
114
|
this.sources.createProject(input.projectKey, input.displayName, {
|
|
105
115
|
tenantId: identity.tenantId,
|
|
106
116
|
registeredBy: identity.gatewayId,
|
|
@@ -144,11 +154,24 @@ export class LocalKnowledgeWriteService {
|
|
|
144
154
|
delete input.registered_by;
|
|
145
155
|
delete input.tenantId;
|
|
146
156
|
delete input.tenant_id;
|
|
147
|
-
|
|
157
|
+
// 归属打标(arch/multi-tenancy.md §6,评审 MAJ-1 修配套):
|
|
158
|
+
// - claim 在场 = 显式归属(含显式 claim 'platform' = 公共库)
|
|
159
|
+
// - platform token 无 claim = 受信 broker 的未声明建档 → 降级 default 域(存量语义,
|
|
160
|
+
// 永不放大成 platform 共享)并告警
|
|
161
|
+
// - 租户 token 无 claim = token 域(旧语义不变)
|
|
162
|
+
let ownerTenant = identity.tenantId;
|
|
163
|
+
if (!identity.claimed && identity.tenantId === PLATFORM_TENANT_ID) {
|
|
164
|
+
ownerTenant = DEFAULT_TENANT_ID;
|
|
165
|
+
console.warn(`[Knowledge] registerSource without ownership claim stamped to "${DEFAULT_TENANT_ID}" domain; assert x-octopi-tenant to declare ownership`);
|
|
166
|
+
}
|
|
167
|
+
const src = this.sources.register(input, {
|
|
168
|
+
tenantId: ownerTenant,
|
|
169
|
+
registeredBy: identity.gatewayId,
|
|
170
|
+
});
|
|
148
171
|
this.db.raw
|
|
149
172
|
.prepare(`UPDATE knowledge_sources SET registered_by = ?, visibility = ?, tenant_id = ?
|
|
150
173
|
WHERE id = ?`)
|
|
151
|
-
.run(identity.gatewayId, typeof input.visibility === 'string' ? input.visibility : 'private',
|
|
174
|
+
.run(identity.gatewayId, typeof input.visibility === 'string' ? input.visibility : 'private', ownerTenant, src.id);
|
|
152
175
|
void this.ingest.ingestSource(src.id, { full: true }).catch((e) => {
|
|
153
176
|
console.warn(`[Knowledge] auto-ingest after register failed (${src.id}): ${e instanceof Error ? e.message : String(e)}`);
|
|
154
177
|
});
|
|
@@ -156,11 +179,9 @@ export class LocalKnowledgeWriteService {
|
|
|
156
179
|
}
|
|
157
180
|
async updateSource(identity, sourceId, patch) {
|
|
158
181
|
const row = this.db.raw
|
|
159
|
-
.prepare('SELECT registered_by FROM knowledge_sources WHERE id = ?')
|
|
182
|
+
.prepare('SELECT registered_by, tenant_id FROM knowledge_sources WHERE id = ?')
|
|
160
183
|
.get(sourceId);
|
|
161
|
-
|
|
162
|
-
throw Object.assign(new Error('not source owner'), { code: 'not_resource_owner' });
|
|
163
|
-
}
|
|
184
|
+
this.assertSourceWritable(identity, row, sourceId);
|
|
164
185
|
const updated = this.sources.update(sourceId, patch);
|
|
165
186
|
if (!updated)
|
|
166
187
|
return null;
|
|
@@ -177,11 +198,9 @@ export class LocalKnowledgeWriteService {
|
|
|
177
198
|
throw Object.assign(new Error('source not found'), { code: 'source_not_found' });
|
|
178
199
|
}
|
|
179
200
|
const row = this.db.raw
|
|
180
|
-
.prepare('SELECT registered_by FROM knowledge_sources WHERE id = ?')
|
|
201
|
+
.prepare('SELECT registered_by, tenant_id FROM knowledge_sources WHERE id = ?')
|
|
181
202
|
.get(sourceId);
|
|
182
|
-
|
|
183
|
-
throw Object.assign(new Error('not source owner'), { code: 'not_resource_owner' });
|
|
184
|
-
}
|
|
203
|
+
this.assertSourceWritable(identity, row, sourceId);
|
|
185
204
|
// 先停在途 parse/embed:否则 purge 让出窗口内 upsert 会把 File/Chunk 再写回来
|
|
186
205
|
this.ingest.abortJobs({ sourceId });
|
|
187
206
|
const purged = await this.memberships.unclaimAllForSourceAsync(sourceId, (fileId) => this.index.purgeFileAsync(fileId));
|
|
@@ -250,16 +269,31 @@ export class LocalKnowledgeWriteService {
|
|
|
250
269
|
sampleChars: DESCRIBE_SAMPLE_MAX_CHARS,
|
|
251
270
|
});
|
|
252
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* 源写权限(arch/multi-tenancy.md §6,评审 MAJ-5):
|
|
274
|
+
* 网关漏斗(registered_by)⊕ 租户域(tenant_id)——platform 身份 = 运维豁免;
|
|
275
|
+
* 租户身份只可写本域源。缺行按无约束放行(老库/投影缺失),由调用方的 not-found 路径兜底。
|
|
276
|
+
*/
|
|
277
|
+
assertSourceWritable(identity, row, sourceId) {
|
|
278
|
+
if (row?.registered_by && row.registered_by !== identity.gatewayId) {
|
|
279
|
+
throw Object.assign(new Error('not source owner'), { code: 'not_resource_owner' });
|
|
280
|
+
}
|
|
281
|
+
if (row?.tenant_id &&
|
|
282
|
+
identity.tenantId !== PLATFORM_TENANT_ID &&
|
|
283
|
+
row.tenant_id !== identity.tenantId) {
|
|
284
|
+
throw Object.assign(new Error(`source ${sourceId} belongs to another tenant domain`), {
|
|
285
|
+
code: 'not_resource_owner',
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
253
289
|
async assertSourceOwner(identity, sourceId) {
|
|
254
290
|
const row = this.db.raw
|
|
255
|
-
.prepare('SELECT registered_by FROM knowledge_sources WHERE id = ?')
|
|
291
|
+
.prepare('SELECT registered_by, tenant_id FROM knowledge_sources WHERE id = ?')
|
|
256
292
|
.get(sourceId);
|
|
257
293
|
if (!row) {
|
|
258
294
|
throw Object.assign(new Error('source not found'), { code: 'source_not_found' });
|
|
259
295
|
}
|
|
260
|
-
|
|
261
|
-
throw Object.assign(new Error('not source owner'), { code: 'not_resource_owner' });
|
|
262
|
-
}
|
|
296
|
+
this.assertSourceWritable(identity, row, sourceId);
|
|
263
297
|
}
|
|
264
298
|
async reindexSource(sourceId) {
|
|
265
299
|
void this.ingest.ingestSource(sourceId, { full: true }).catch((e) => {
|
|
@@ -315,6 +349,12 @@ export class LocalKnowledgeWriteService {
|
|
|
315
349
|
return true;
|
|
316
350
|
}
|
|
317
351
|
async assignProject(identity, agentId, projectKey) {
|
|
352
|
+
// 写侧防绕过(收紧配套):不可见项目的 key 不得经分配 grant 进入 agent 视图
|
|
353
|
+
if (!this.sources.isProjectVisibleTo(projectKey, identity)) {
|
|
354
|
+
throw Object.assign(new Error(`project not visible to writer: ${projectKey}`), {
|
|
355
|
+
code: 'project_not_visible',
|
|
356
|
+
});
|
|
357
|
+
}
|
|
318
358
|
this.sources.assignProject(projectKey, agentId, identity);
|
|
319
359
|
}
|
|
320
360
|
async unassignProject(identity, agentId, projectKey) {
|
|
@@ -326,6 +366,30 @@ export class LocalKnowledgeWriteService {
|
|
|
326
366
|
async unhideSource(identity, agentId, sourceId) {
|
|
327
367
|
this.sources.unhideSource(agentId, sourceId, identity);
|
|
328
368
|
}
|
|
369
|
+
async mountSource(identity, agentId, sourceId) {
|
|
370
|
+
// 写侧防绕过(收紧配套):不可见源不得经挂载 grant 进入 agent 视图
|
|
371
|
+
// (与读侧同口径:租户闸 ⊕ 网关漏斗 ⊕ 项目跨域闸,fail-loud 而非写成无效挂载)
|
|
372
|
+
const src = this.sources.get(sourceId);
|
|
373
|
+
if (!src) {
|
|
374
|
+
// 评审 m4:未知源 fail-loud,不静默写 include 行
|
|
375
|
+
throw Object.assign(new Error(`source not found: ${sourceId}`), {
|
|
376
|
+
code: 'source_not_found',
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
const visible = this.sources.isTenantVisible(src, identity) &&
|
|
380
|
+
this.sources.isGatewayVisible(src, identity) &&
|
|
381
|
+
(src.scopeRef.level !== 'project' ||
|
|
382
|
+
this.sources.isProjectVisible(src, src.scopeRef.key, identity));
|
|
383
|
+
if (!visible) {
|
|
384
|
+
throw Object.assign(new Error(`source not visible to writer: ${sourceId}`), {
|
|
385
|
+
code: 'source_not_visible',
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
this.sources.mountSource(agentId, sourceId, identity);
|
|
389
|
+
}
|
|
390
|
+
async unmountSource(identity, agentId, sourceId) {
|
|
391
|
+
this.sources.unmountSource(agentId, sourceId, identity);
|
|
392
|
+
}
|
|
329
393
|
async replaceSessionVisibility(identity, _agentId, sessionId, items) {
|
|
330
394
|
const typed = items.map((raw) => {
|
|
331
395
|
const targetType = raw.targetType;
|
|
@@ -344,8 +408,8 @@ export class LocalKnowledgeWriteService {
|
|
|
344
408
|
async setSessionVisibilityItem(identity, sessionId, item) {
|
|
345
409
|
this.sources.setSessionVisibility(sessionId, item, identity);
|
|
346
410
|
}
|
|
347
|
-
async clearSessionVisibility(identity, sessionId, target) {
|
|
348
|
-
this.sources.clearSessionVisibility(sessionId, target, identity);
|
|
411
|
+
async clearSessionVisibility(identity, sessionId, target, op) {
|
|
412
|
+
this.sources.clearSessionVisibility(sessionId, target, identity, op);
|
|
349
413
|
}
|
|
350
414
|
async isSourceOwner(sourceId, gatewayId) {
|
|
351
415
|
return this.isOwnerRow(sourceId, gatewayId);
|